| 1 | // natDeflater.cc - Implementation of Deflater native methods.
|
|---|
| 2 |
|
|---|
| 3 | /* Copyright (C) 1999, 2002 Free Software Foundation
|
|---|
| 4 |
|
|---|
| 5 | This file is part of libgcj.
|
|---|
| 6 |
|
|---|
| 7 | This software is copyrighted work licensed under the terms of the
|
|---|
| 8 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
|---|
| 9 | details. */
|
|---|
| 10 |
|
|---|
| 11 | // Written by Tom Tromey <[email protected]>
|
|---|
| 12 |
|
|---|
| 13 | #include <config.h>
|
|---|
| 14 |
|
|---|
| 15 | #include <zlib.h>
|
|---|
| 16 | #include <stdlib.h>
|
|---|
| 17 |
|
|---|
| 18 | #include <gcj/cni.h>
|
|---|
| 19 | #include <jvm.h>
|
|---|
| 20 |
|
|---|
| 21 | #include <java/util/zip/Deflater.h>
|
|---|
| 22 | #include <java/util/zip/DataFormatException.h>
|
|---|
| 23 |
|
|---|
| 24 | #include <java/lang/InternalError.h>
|
|---|
| 25 | #include <java/lang/NullPointerException.h>
|
|---|
| 26 | #include <java/lang/ArrayIndexOutOfBoundsException.h>
|
|---|
| 27 |
|
|---|
| 28 | extern void *_Jv_ZMalloc (void *, uInt nitems, uInt size);
|
|---|
| 29 | extern void _Jv_ZFree (void *, void *addr);
|
|---|
| 30 |
|
|---|
| 31 | |
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 | jint
|
|---|
| 35 | java::util::zip::Deflater::deflate (jbyteArray buf, jint off, jint len)
|
|---|
| 36 | {
|
|---|
| 37 | JvSynchronize sync (this);
|
|---|
| 38 | z_streamp s = (z_streamp) zstream;
|
|---|
| 39 |
|
|---|
| 40 | if (! buf)
|
|---|
| 41 | throw new java::lang::NullPointerException;
|
|---|
| 42 | if (off < 0 || len < 0 || off + len > buf->length)
|
|---|
| 43 | throw new java::lang::ArrayIndexOutOfBoundsException;
|
|---|
| 44 |
|
|---|
| 45 | if (len == 0)
|
|---|
| 46 | return 0;
|
|---|
| 47 |
|
|---|
| 48 | s->next_out = (Bytef *) (elements (buf) + off);
|
|---|
| 49 | s->avail_out = len;
|
|---|
| 50 |
|
|---|
| 51 | switch (::deflate (s, flush_flag))
|
|---|
| 52 | {
|
|---|
| 53 | case Z_STREAM_END:
|
|---|
| 54 | is_finished = true;
|
|---|
| 55 | if (s->avail_out == (unsigned int) len)
|
|---|
| 56 | return -1;
|
|---|
| 57 | break;
|
|---|
| 58 |
|
|---|
| 59 | case Z_STREAM_ERROR:
|
|---|
| 60 | case Z_BUF_ERROR:
|
|---|
| 61 | // FIXME?
|
|---|
| 62 | throw new java::lang::InternalError;
|
|---|
| 63 | break;
|
|---|
| 64 |
|
|---|
| 65 | case Z_OK:
|
|---|
| 66 | break;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | return len - s->avail_out;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | void
|
|---|
| 73 | java::util::zip::Deflater::end ()
|
|---|
| 74 | {
|
|---|
| 75 | JvSynchronize sync (this);
|
|---|
| 76 | // Just ignore errors.
|
|---|
| 77 | deflateEnd ((z_streamp) zstream);
|
|---|
| 78 | _Jv_Free (zstream);
|
|---|
| 79 | zstream = NULL;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | void
|
|---|
| 83 | java::util::zip::Deflater::finish ()
|
|---|
| 84 | {
|
|---|
| 85 | JvSynchronize sync (this);
|
|---|
| 86 | flush_flag = Z_FINISH;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | jint
|
|---|
| 90 | java::util::zip::Deflater::getAdler ()
|
|---|
| 91 | {
|
|---|
| 92 | JvSynchronize sync (this);
|
|---|
| 93 | z_streamp s = (z_streamp) zstream;
|
|---|
| 94 | return s->adler;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | jint
|
|---|
| 98 | java::util::zip::Deflater::getTotalIn ()
|
|---|
| 99 | {
|
|---|
| 100 | JvSynchronize sync (this);
|
|---|
| 101 | z_streamp s = (z_streamp) zstream;
|
|---|
| 102 | return s->total_in;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | jint
|
|---|
| 106 | java::util::zip::Deflater::getTotalOut ()
|
|---|
| 107 | {
|
|---|
| 108 | JvSynchronize sync (this);
|
|---|
| 109 | z_streamp s = (z_streamp) zstream;
|
|---|
| 110 | return s->total_out;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | jboolean
|
|---|
| 114 | java::util::zip::Deflater::needsInput ()
|
|---|
| 115 | {
|
|---|
| 116 | JvSynchronize sync (this);
|
|---|
| 117 | z_streamp s = (z_streamp) zstream;
|
|---|
| 118 | return s->avail_in == 0;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | void
|
|---|
| 122 | java::util::zip::Deflater::reset ()
|
|---|
| 123 | {
|
|---|
| 124 | JvSynchronize sync (this);
|
|---|
| 125 | z_streamp s = (z_streamp) zstream;
|
|---|
| 126 | // Just ignore errors.
|
|---|
| 127 | deflateReset (s);
|
|---|
| 128 | s->avail_in = 0;
|
|---|
| 129 | flush_flag = 0;
|
|---|
| 130 | is_finished = false;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | void
|
|---|
| 134 | java::util::zip::Deflater::setDictionary (jbyteArray buf, jint off, jint len)
|
|---|
| 135 | {
|
|---|
| 136 | JvSynchronize sync (this);
|
|---|
| 137 | z_streamp s = (z_streamp) zstream;
|
|---|
| 138 |
|
|---|
| 139 | if (! buf)
|
|---|
| 140 | throw new java::lang::NullPointerException;
|
|---|
| 141 | if (off < 0 || len < 0 || off + len > buf->length)
|
|---|
| 142 | throw new java::lang::ArrayIndexOutOfBoundsException;
|
|---|
| 143 |
|
|---|
| 144 | // Ignore errors.
|
|---|
| 145 | deflateSetDictionary (s, (Bytef *) (elements (buf) + off), len);
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | void
|
|---|
| 149 | java::util::zip::Deflater::setInput (jbyteArray buf, jint off, jint len)
|
|---|
| 150 | {
|
|---|
| 151 | JvSynchronize sync (this);
|
|---|
| 152 | z_streamp s = (z_streamp) zstream;
|
|---|
| 153 |
|
|---|
| 154 | if (! buf)
|
|---|
| 155 | throw new java::lang::NullPointerException;
|
|---|
| 156 | if (off < 0 || len < 0 || off + len > buf->length)
|
|---|
| 157 | throw new java::lang::ArrayIndexOutOfBoundsException;
|
|---|
| 158 |
|
|---|
|
|---|