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 <tromey@cygnus.com>
|
---|
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 |
|
---|
159 | s->next_in = (Bytef *) (elements (buf) + off);
|
---|
160 | s->avail_in = len;
|
---|
161 | }
|
---|
162 |
|
---|
163 | void
|
---|
164 | java::util::zip::Deflater::update ()
|
---|
165 | {
|
---|
166 | JvSynchronize sync (this);
|
---|
167 | z_streamp s = (z_streamp) zstream;
|
---|
168 |
|
---|
169 | int strat = Z_DEFAULT_STRATEGY;
|
---|
170 | switch (strategy)
|
---|
171 | {
|
---|
172 | case DEFAULT_STRATEGY:
|
---|
173 | strat = Z_DEFAULT_STRATEGY;
|
---|
174 | break;
|
---|
175 | case FILTERED:
|
---|
176 | strat = Z_FILTERED;
|
---|
177 | break;
|
---|
178 | case HUFFMAN_ONLY:
|
---|
179 | strat = Z_HUFFMAN_ONLY;
|
---|
180 | break;
|
---|
181 | default:
|
---|
182 | JvFail ("unexpected strategy");
|
---|
183 | }
|
---|
184 |
|
---|
185 | // Ignore errors.
|
---|
186 | deflateParams (s, level, strat);
|
---|
187 | }
|
---|
188 |
|
---|
189 | void
|
---|
190 | java::util::zip::Deflater::init (jint level, jboolean no_header)
|
---|
191 | {
|
---|
192 | z_stream_s *stream = (z_stream_s *) _Jv_Malloc (sizeof (z_stream_s));
|
---|
193 | stream->next_in = Z_NULL;
|
---|
194 | stream->avail_in = 0;
|
---|
195 | stream->zalloc = _Jv_ZMalloc;
|
---|
196 | stream->zfree = _Jv_ZFree;
|
---|
197 | stream->opaque = NULL;
|
---|
198 |
|
---|
199 | // Handle NO_HEADER using undocumented zlib feature.
|
---|
200 | int wbits = MAX_WBITS;
|
---|
201 | if (no_header)
|
---|
202 | wbits = - wbits;
|
---|
203 |
|
---|
204 | #define DEFAULT_MEM_LEVEL 8
|
---|
205 | if (deflateInit2 (stream, level, Z_DEFLATED, wbits,
|
---|
206 | DEFAULT_MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK)
|
---|
207 | {
|
---|
208 | jstring msg = NULL;
|
---|
209 | if (stream->msg != NULL)
|
---|
210 | msg = JvNewStringLatin1 (stream->msg);
|
---|
211 | throw new java::lang::InternalError (msg);
|
---|
212 | }
|
---|
213 |
|
---|
214 | zstream = reinterpret_cast<gnu::gcj::RawData *> (stream);
|
---|
215 | is_finished = false;
|
---|
216 | flush_flag = 0;
|
---|
217 | }
|
---|