Skip to content

Commit 593c519

Browse files
committed
Support for standard zip Deflater/Inflater
needs testing
1 parent bfd3979 commit 593c519

File tree

11 files changed

+983
-545
lines changed

11 files changed

+983
-545
lines changed

sources/net.sf.j2s.java.core/src/java/util/zip/Deflater.java

Lines changed: 473 additions & 13 deletions
Large diffs are not rendered by default.

sources/net.sf.j2s.java.core/src/java/util/zip/ZipInputStream.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,8 @@ private void readEnd(ZipEntry e) throws IOException {
431431
}
432432
if ((flag & 8) == 8) {
433433
/* "Data Descriptor" present */
434-
if (inf.getTotalOut() > ZipConstants64.ZIP64_MAGICVAL
435-
|| inf.getTotalIn() > ZipConstants64.ZIP64_MAGICVAL) {
434+
if (inf.getTotalOutL() > ZipConstants64.ZIP64_MAGICVAL
435+
|| inf.getTotalInL() > ZipConstants64.ZIP64_MAGICVAL) {
436436
// ZIP64 format
437437
readFully(tmpbuf, 0, ZipConstants64.ZIP64_EXTHDR);
438438
long sig = get32(tmpbuf, 0);
@@ -465,13 +465,13 @@ private void readEnd(ZipEntry e) throws IOException {
465465
}
466466
}
467467
}
468-
if (e.size != inf.getTotalOut()) {
468+
if (e.size != inf.getTotalOutL()) {
469469
throw new ZipException("invalid entry size (expected " + e.size
470-
+ " but got " + inf.getTotalOut() + " bytes)");
470+
+ " but got " + inf.getTotalOutL() + " bytes)");
471471
}
472-
if (e.csize != inf.getTotalIn()) {
472+
if (e.csize != inf.getTotalInL()) {
473473
throw new ZipException("invalid entry compressed size (expected "
474-
+ e.csize + " but got " + inf.getTotalIn() + " bytes)");
474+
+ e.csize + " but got " + inf.getTotalInL() + " bytes)");
475475
}
476476
if (e.crc != crc.getValue()) {
477477
throw new ZipException("invalid entry CRC (expected 0x"

sources/net.sf.j2s.java.core/src/swingjs/jzlib/Deflate.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,20 +1492,20 @@ int deflateParams(int _level, int _strategy) {
14921492
return err;
14931493
}
14941494

1495-
int deflateSetDictionary(byte[] dictionary, int dictLength) {
1496-
int length = dictLength;
1497-
int index = 0;
1495+
int deflateSetDictionary(byte[] dictionary, int off, int len) {
1496+
int length = len;
1497+
int index = off;
14981498

14991499
if (dictionary == null || status != INIT_STATE)
15001500
return Z_STREAM_ERROR;
15011501

1502-
strm.checksum.update(dictionary, 0, dictLength);
1502+
strm.checksum.update(dictionary, off, len);
15031503

15041504
if (length < MIN_MATCH)
15051505
return Z_OK;
15061506
if (length > w_size - MIN_LOOKAHEAD) {
15071507
length = w_size - MIN_LOOKAHEAD;
1508-
index = dictLength - length; // use the tail of the dictionary
1508+
index = len - length; // use the tail of the dictionary
15091509
}
15101510
System.arraycopy(dictionary, index, window, 0, length);
15111511
strstart = length;
@@ -1534,7 +1534,7 @@ int deflate(int flush) {
15341534
return Z_STREAM_ERROR;
15351535
}
15361536

1537-
if (strm.next_out == null || (strm.next_in == null && strm.avail_in != 0)
1537+
if (strm.next_out == null || (strm.in == null && strm.avail_in != 0)
15381538
|| (status == FINISH_STATE && flush != Z_FINISH)) {
15391539
strm.msg = z_errmsg[Z_NEED_DICT - (Z_STREAM_ERROR)];
15401540
return Z_STREAM_ERROR;
@@ -1794,4 +1794,5 @@ public long getBytesRead() {
17941794
public long getBytesWritten() {
17951795
return strm.total_out;
17961796
}
1797+
17971798
}

sources/net.sf.j2s.java.core/src/swingjs/jzlib/Deflater.java

Lines changed: 137 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -36,137 +36,141 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3636

3737
public class Deflater extends ZStream {
3838

39-
static final private int MAX_WBITS = 15; // 32K LZ77 window
40-
41-
//static final private int DEF_WBITS=MAX_WBITS;
42-
43-
// static final private int Z_NO_FLUSH=0;
44-
// static final private int Z_PARTIAL_FLUSH=1;
45-
// static final private int Z_SYNC_FLUSH=2;
46-
// static final private int Z_FULL_FLUSH=3;
47-
// static final private int Z_FINISH=4;
48-
//
49-
// static final private int MAX_MEM_LEVEL=9;
50-
51-
//static final private int Z_OK = 0;
52-
static final private int Z_STREAM_END = 1;
53-
// static final private int Z_NEED_DICT=2;
54-
// static final private int Z_ERRNO=-1;
55-
static final private int Z_STREAM_ERROR = -2;
56-
// static final private int Z_DATA_ERROR=-3;
57-
// static final private int Z_MEM_ERROR=-4;
58-
// static final private int Z_BUF_ERROR=-5;
59-
// static final private int Z_VERSION_ERROR=-6;
60-
61-
private boolean finished = false;
62-
63-
/*
64-
65-
public Deflater(int level) {
66-
this(level, 0, false);
67-
}
68-
69-
public Deflater(int level, boolean nowrap) {
70-
this(level, 0, nowrap);
71-
}
72-
73-
public Deflater(int level, int bits) {
74-
this(level, bits, false);
75-
}
76-
*/
77-
78-
/*
79-
public Deflater(int level, int bits, int memlevel) {
80-
super();
81-
init3(level, bits, memlevel);
82-
//if (ret != Z_OK)
83-
//throw new GZIPException(ret + ": " + msg);
84-
}
85-
public int init(int level) {
86-
return init2(level, MAX_WBITS);
87-
}
88-
89-
public int init2(int level, int bits) {
90-
return init3b(level, bits, false);
91-
}
92-
93-
94-
public int init2b(int level, boolean nowrap) {
95-
return init3b(level, MAX_WBITS, nowrap);
96-
}
97-
98-
public int init3(int level, int bits, int memlevel) {
99-
finished = false;
100-
dstate = new Deflate(this);
101-
return dstate.deflateInit3(level, bits, memlevel);
102-
}
103-
104-
105-
*/
106-
public Deflater init(int level, int bits, boolean nowrap) {
107-
if (bits == 0)
108-
bits = MAX_WBITS;
109-
finished = false;
110-
setAdler32();
111-
dstate = new Deflate(this);
112-
dstate.deflateInit2(level, nowrap ? -bits : bits);
113-
return this;
114-
}
115-
116-
@Override
117-
public int deflate(int flush) {
118-
if (dstate == null) {
119-
return Z_STREAM_ERROR;
120-
}
121-
int ret = dstate.deflate(flush);
122-
if (ret == Z_STREAM_END)
123-
finished = true;
124-
return ret;
125-
}
126-
127-
@Override
128-
public int end() {
129-
finished = true;
130-
if (dstate == null)
131-
return Z_STREAM_ERROR;
132-
int ret = dstate.deflateEnd();
133-
dstate = null;
134-
free();
135-
return ret;
136-
}
137-
138-
public int params(int level, int strategy) {
139-
if (dstate == null)
140-
return Z_STREAM_ERROR;
141-
return dstate.deflateParams(level, strategy);
142-
}
143-
144-
public int setDictionary(byte[] dictionary, int dictLength) {
145-
if (dstate == null)
146-
return Z_STREAM_ERROR;
147-
return dstate.deflateSetDictionary(dictionary, dictLength);
148-
}
149-
150-
@Override
151-
public boolean finished() {
152-
return finished;
153-
}
154-
155-
public void finish() {
156-
// native use only?
157-
158-
}
159-
160-
public long getBytesRead() {
161-
return dstate.getBytesRead();
162-
}
163-
164-
public long getBytesWritten() {
165-
return dstate.getBytesWritten();
166-
}
167-
168-
// public int copy(Deflater src){
169-
// this.finished = src.finished;
170-
// return Deflate.deflateCopy(this, src);
171-
// }
39+
static final private int MAX_WBITS = 15; // 32K LZ77 window
40+
41+
// static final private int DEF_WBITS=MAX_WBITS;
42+
43+
// static final private int Z_NO_FLUSH=0;
44+
// static final private int Z_PARTIAL_FLUSH=1;
45+
// static final private int Z_SYNC_FLUSH=2;
46+
// static final private int Z_FULL_FLUSH=3;
47+
// static final private int Z_FINISH=4;
48+
//
49+
// static final private int MAX_MEM_LEVEL=9;
50+
51+
// static final private int Z_OK = 0;
52+
static final private int Z_STREAM_END = 1;
53+
// static final private int Z_NEED_DICT=2;
54+
// static final private int Z_ERRNO=-1;
55+
static final private int Z_STREAM_ERROR = -2;
56+
// static final private int Z_DATA_ERROR=-3;
57+
// static final private int Z_MEM_ERROR=-4;
58+
// static final private int Z_BUF_ERROR=-5;
59+
// static final private int Z_VERSION_ERROR=-6;
60+
61+
private boolean finished = false;
62+
63+
/*
64+
*
65+
* public Deflater(int level) { this(level, 0, false); }
66+
*
67+
* public Deflater(int level, boolean nowrap) { this(level, 0, nowrap); }
68+
*
69+
* public Deflater(int level, int bits) { this(level, bits, false); }
70+
*/
71+
72+
/*
73+
* public Deflater(int level, int bits, int memlevel) { super(); init3(level,
74+
* bits, memlevel); //if (ret != Z_OK) //throw new GZIPException(ret + ": " +
75+
* msg); } public int init(int level) { return init2(level, MAX_WBITS); }
76+
*
77+
* public int init2(int level, int bits) { return init3b(level, bits, false); }
78+
*
79+
*
80+
* public int init2b(int level, boolean nowrap) { return init3b(level,
81+
* MAX_WBITS, nowrap); }
82+
*
83+
* public int init3(int level, int bits, int memlevel) { finished = false;
84+
* dstate = new Deflate(this); return dstate.deflateInit3(level, bits,
85+
* memlevel); }
86+
*
87+
*
88+
*/
89+
public Deflater init(int level, int bits, boolean nowrap) {
90+
if (bits == 0)
91+
bits = MAX_WBITS;
92+
finished = false;
93+
setAdler32();
94+
dstate = new Deflate(this);
95+
dstate.deflateInit2(level, nowrap ? -bits : bits);
96+
return this;
97+
}
98+
99+
public int deflate(byte[] buf, int off, int len, int flush) {
100+
super.setOutput(buf, off, len);
101+
long thisLen = getBytesWritten();
102+
deflate(flush);
103+
long newLen = getBytesWritten();
104+
return (int) (newLen - thisLen);
105+
}
106+
107+
@Override
108+
public int deflate(int flush) {
109+
if (dstate == null) {
110+
return Z_STREAM_ERROR;
111+
}
112+
int ret = dstate.deflate(flush);
113+
if (ret == Z_STREAM_END)
114+
finished = true;
115+
return ret;
116+
}
117+
118+
// public boolean needsInput() {
119+
// return avail_out == 0;
120+
// }
121+
//
122+
@Override
123+
public void end() {
124+
finished = true;
125+
if (dstate == null)
126+
return;
127+
// return Z_STREAM_ERROR;
128+
int ret = dstate.deflateEnd();
129+
dstate = null;
130+
free();
131+
// return ret;
132+
}
133+
134+
public int params(int level, int strategy) {
135+
if (dstate == null)
136+
return Z_STREAM_ERROR;
137+
return dstate.deflateParams(level, strategy);
138+
}
139+
140+
public int setDictionaryRet(byte[] dictionary, int off, int len) {
141+
if (dstate == null)
142+
return Z_STREAM_ERROR;
143+
return dstate.deflateSetDictionary(dictionary, off, len);
144+
}
145+
146+
@Override
147+
public boolean finished() {
148+
return finished;
149+
}
150+
151+
public void finish() {
152+
// native use only?
153+
154+
}
155+
156+
public long getBytesRead() {
157+
return dstate.getBytesRead();
158+
}
159+
160+
public long getBytesWritten() {
161+
return dstate.getBytesWritten();
162+
}
163+
164+
public void reset() {
165+
dstate.deflateReset();
166+
}
167+
168+
public boolean needsInput() {
169+
return avail_in <= 0;
170+
}
171+
172+
// public int copy(Deflater src){
173+
// this.finished = src.finished;
174+
// return Deflate.deflateCopy(this, src);
175+
// }
172176
}

sources/net.sf.j2s.java.core/src/swingjs/jzlib/DeflaterOutputStream.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ public void close() throws IOException {
131131
closed = true;
132132
}
133133
}
134+
135+
public boolean isClosed() {
136+
return closed;
137+
}
134138

135139
protected int deflate(int flush) throws IOException {
136140
deflater.setOutput(buffer, 0, buffer.length);
@@ -170,11 +174,11 @@ public void flush() throws IOException {
170174
}
171175

172176
public long getTotalIn() {
173-
return deflater.getTotalIn();
177+
return deflater.getTotalInL();
174178
}
175179

176180
public long getTotalOut() {
177-
return deflater.getTotalOut();
181+
return deflater.getTotalOutL();
178182
}
179183

180184
public void setSyncFlush(boolean syncFlush) {

0 commit comments

Comments
 (0)