1 // natDeflater.cc - Implementation of Deflater native methods.
3 /* Copyright (C) 1999, 2002 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
11 // Written by Tom Tromey <tromey@cygnus.com>
21 #include <java/util/zip/Deflater.h>
22 #include <java/util/zip/DataFormatException.h>
24 #include <java/lang/InternalError.h>
25 #include <java/lang/NullPointerException.h>
26 #include <java/lang/ArrayIndexOutOfBoundsException.h>
28 extern void *_Jv_ZMalloc (void *, uInt nitems
, uInt size
);
29 extern void _Jv_ZFree (void *, void *addr
);
34 java::util::zip::Deflater::deflate (jbyteArray buf
, jint off
, jint len
)
36 JvSynchronize
sync (this);
37 z_streamp s
= (z_streamp
) zstream
;
40 throw new java::lang::NullPointerException
;
41 if (off
< 0 || len
< 0 || off
+ len
> buf
->length
)
42 throw new java::lang::ArrayIndexOutOfBoundsException
;
47 s
->next_out
= (Bytef
*) (elements (buf
) + off
);
50 switch (::deflate (s
, flush_flag
))
54 if (s
->avail_out
== (unsigned int) len
)
61 throw new java::lang::InternalError
;
68 return len
- s
->avail_out
;
72 java::util::zip::Deflater::end ()
74 JvSynchronize
sync (this);
75 // Just ignore errors.
76 deflateEnd ((z_streamp
) zstream
);
82 java::util::zip::Deflater::finish ()
84 JvSynchronize
sync (this);
85 flush_flag
= Z_FINISH
;
89 java::util::zip::Deflater::getAdler ()
91 JvSynchronize
sync (this);
92 z_streamp s
= (z_streamp
) zstream
;
97 java::util::zip::Deflater::getTotalIn ()
99 JvSynchronize
sync (this);
100 z_streamp s
= (z_streamp
) zstream
;
105 java::util::zip::Deflater::getTotalOut ()
107 JvSynchronize
sync (this);
108 z_streamp s
= (z_streamp
) zstream
;
113 java::util::zip::Deflater::needsInput ()
115 JvSynchronize
sync (this);
116 z_streamp s
= (z_streamp
) zstream
;
117 return s
->avail_in
== 0;
121 java::util::zip::Deflater::reset ()
123 JvSynchronize
sync (this);
124 z_streamp s
= (z_streamp
) zstream
;
125 // Just ignore errors.
133 java::util::zip::Deflater::setDictionary (jbyteArray buf
, jint off
, jint len
)
135 JvSynchronize
sync (this);
136 z_streamp s
= (z_streamp
) zstream
;
139 throw new java::lang::NullPointerException
;
140 if (off
< 0 || len
< 0 || off
+ len
> buf
->length
)
141 throw new java::lang::ArrayIndexOutOfBoundsException
;
144 deflateSetDictionary (s
, (Bytef
*) (elements (buf
) + off
), len
);
148 java::util::zip::Deflater::setInput (jbyteArray buf
, jint off
, jint len
)
150 JvSynchronize
sync (this);
151 z_streamp s
= (z_streamp
) zstream
;
154 throw new java::lang::NullPointerException
;
155 if (off
< 0 || len
< 0 || off
+ len
> buf
->length
)
156 throw new java::lang::ArrayIndexOutOfBoundsException
;
158 s
->next_in
= (Bytef
*) (elements (buf
) + off
);
163 java::util::zip::Deflater::update ()
165 JvSynchronize
sync (this);
166 z_streamp s
= (z_streamp
) zstream
;
168 int strat
= Z_DEFAULT_STRATEGY
;
171 case DEFAULT_STRATEGY
:
172 strat
= Z_DEFAULT_STRATEGY
;
178 strat
= Z_HUFFMAN_ONLY
;
181 JvFail ("unexpected strategy");
185 deflateParams (s
, level
, strat
);
189 java::util::zip::Deflater::init (jint level
, jboolean no_header
)
191 z_stream_s
*stream
= (z_stream_s
*) _Jv_Malloc (sizeof (z_stream_s
));
192 stream
->next_in
= Z_NULL
;
193 stream
->avail_in
= 0;
194 stream
->zalloc
= _Jv_ZMalloc
;
195 stream
->zfree
= _Jv_ZFree
;
196 stream
->opaque
= NULL
;
198 // Handle NO_HEADER using undocumented zlib feature.
199 int wbits
= MAX_WBITS
;
203 #define DEFAULT_MEM_LEVEL 8
204 if (deflateInit2 (stream
, level
, Z_DEFLATED
, wbits
,
205 DEFAULT_MEM_LEVEL
, Z_DEFAULT_STRATEGY
) != Z_OK
)
208 if (stream
->msg
!= NULL
)
209 msg
= JvNewStringLatin1 (stream
->msg
);
210 throw new java::lang::InternalError (msg
);
213 zstream
= reinterpret_cast<gnu::gcj::RawData
*> (stream
);