public override void write(byte[] b, int off, int len)// throws IOException { { if (len == 0) { return; } int err; z.next_in = b; z.next_in_index = off; z.avail_in = len; do { z.next_out = buf; z.next_out_index = 0; z.avail_out = bufsize; if (compress) { err = z.deflate(flushJ); } else { err = z.inflate(flushJ); } if (err != JZlib.Z_OK) { throw new ZStreamException((compress?"de":"in") + "flating: " + z.msg); } outJ.write(buf, 0, bufsize - z.avail_out); }while(z.avail_in > 0 || z.avail_out == 0); }
public override int read(byte[] b, int off, int len)//throws IOException { { if (len == 0) { return(0); } int err; z.next_out = b; z.next_out_index = off; z.avail_out = len; do { if ((z.avail_in == 0) && (!nomoreinput)) // if buffer is empty and more input is avaiable, refill it { z.next_in_index = 0; z.avail_in = inJ.read(buf, 0, bufsize);//(bufsize<z.avail_out ? bufsize : z.avail_out)); if (z.avail_in == -1) { z.avail_in = 0; nomoreinput = true; } } if (compress) { err = z.deflate(flush); } else { err = z.inflate(flush); } if (nomoreinput && (err == JZlib.Z_BUF_ERROR)) { return(-1); } if (err != JZlib.Z_OK && err != JZlib.Z_STREAM_END) { throw new ZStreamException((compress ? "de" : "in") + "flating: " + z.msg); } if ((nomoreinput || err == JZlib.Z_STREAM_END) && (z.avail_out == len)) { return(-1); } }while(z.avail_out == len && err == JZlib.Z_OK); //System.err.print("("+(len-z.avail_out)+")"); return(len - z.avail_out); }
public static void main(String[] arg) { int err; int comprLen = 40000; int uncomprLen = comprLen; byte[] compr = new byte[comprLen]; byte[] uncompr = new byte[uncomprLen]; int len = hello.Length; ZStream c_stream = new ZStream(); err = c_stream.deflateInit(JZlib.Z_DEFAULT_COMPRESSION); CHECK_ERR(c_stream, err, "deflate"); c_stream.next_in = hello; c_stream.next_in_index = 0; c_stream.next_out = compr; c_stream.next_out_index = 0; c_stream.avail_in = 3; c_stream.avail_out = comprLen; err = c_stream.deflate(JZlib.Z_FULL_FLUSH); CHECK_ERR(c_stream, err, "deflate"); compr[3]++; // force an error in first compressed block c_stream.avail_in = len - 3; err = c_stream.deflate(JZlib.Z_FINISH); if (err != JZlib.Z_STREAM_END) { CHECK_ERR(c_stream, err, "deflate"); } err = c_stream.deflateEnd(); CHECK_ERR(c_stream, err, "deflateEnd"); comprLen = (int)(c_stream.total_out); ZStream d_stream = new ZStream(); d_stream.next_in = compr; d_stream.next_in_index = 0; d_stream.avail_in = 2; err = d_stream.inflateInit(); CHECK_ERR(d_stream, err, "inflateInit"); d_stream.next_out = uncompr; d_stream.next_out_index = 0; d_stream.avail_out = uncomprLen; err = d_stream.inflate(JZlib.Z_NO_FLUSH); CHECK_ERR(d_stream, err, "inflate"); d_stream.avail_in = comprLen - 2; err = d_stream.inflateSync(); CHECK_ERR(d_stream, err, "inflateSync"); err = d_stream.inflate(JZlib.Z_FINISH); if (err != JZlib.Z_DATA_ERROR) { java.lang.SystemJ.outJ.println("inflate should report DATA_ERROR"); /* Because of incorrect adler32 */ java.lang.SystemJ.exit(1); } err = d_stream.inflateEnd(); CHECK_ERR(d_stream, err, "inflateEnd"); int j = 0; for (; j < uncompr.Length; j++) if (uncompr[j] == 0) break; java.lang.SystemJ.outJ.println("after inflateSync(): hel" + new java.lang.StringJ(uncompr, 0, j)); }
public static void main(String[] arg) { int err; int comprLen = 40000; int uncomprLen = comprLen; byte[] compr = new byte[comprLen]; byte[] uncompr = new byte[uncomprLen]; ZStream c_stream = new ZStream(); err = c_stream.deflateInit(JZlib.Z_DEFAULT_COMPRESSION); CHECK_ERR(c_stream, err, "deflateInit"); c_stream.next_in = hello; c_stream.next_in_index = 0; c_stream.next_out = compr; c_stream.next_out_index = 0; while (c_stream.total_in != hello.Length && c_stream.total_out < comprLen) { c_stream.avail_in = c_stream.avail_out = 1; // force small buffers err = c_stream.deflate(JZlib.Z_NO_FLUSH); CHECK_ERR(c_stream, err, "deflate"); } while (true) { c_stream.avail_out = 1; err = c_stream.deflate(JZlib.Z_FINISH); if (err == JZlib.Z_STREAM_END) break; CHECK_ERR(c_stream, err, "deflate"); } err = c_stream.deflateEnd(); CHECK_ERR(c_stream, err, "deflateEnd"); ZStream d_stream = new ZStream(); d_stream.next_in = compr; d_stream.next_in_index = 0; d_stream.next_out = uncompr; d_stream.next_out_index = 0; err = d_stream.inflateInit(); CHECK_ERR(d_stream, err, "inflateInit"); while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) { d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */ err = d_stream.inflate(JZlib.Z_NO_FLUSH); if (err == JZlib.Z_STREAM_END) break; CHECK_ERR(d_stream, err, "inflate"); } err = d_stream.inflateEnd(); CHECK_ERR(d_stream, err, "inflateEnd"); int i = 0; for (; i < hello.Length; i++) if (hello[i] == 0) break; int j = 0; for (; j < uncompr.Length; j++) if (uncompr[j] == 0) break; if (i == j) { for (i = 0; i < j; i++) if (hello[i] != uncompr[i]) break; if (i == j) { java.lang.SystemJ.outJ.println("inflate(): " + new java.lang.StringJ(uncompr, 0, j).ToString()); return; } } else { java.lang.SystemJ.outJ.println("bad inflate"); } }
public static void main(String[] arg) { int err; int comprLen = 40000; int uncomprLen = comprLen; byte[] uncompr = new byte[uncomprLen]; byte[] compr = new byte[comprLen]; long dictId; ZStream c_stream = new ZStream(); err = c_stream.deflateInit(JZlib.Z_BEST_COMPRESSION); CHECK_ERR(c_stream, err, "deflateInit"); err = c_stream.deflateSetDictionary(dictionary, dictionary.Length); CHECK_ERR(c_stream, err, "deflateSetDictionary"); dictId = c_stream.adler; c_stream.next_out = compr; c_stream.next_out_index = 0; c_stream.avail_out = comprLen; c_stream.next_in = hello; c_stream.next_in_index = 0; c_stream.avail_in = hello.Length; err = c_stream.deflate(JZlib.Z_FINISH); if (err != JZlib.Z_STREAM_END) { java.lang.SystemJ.outJ.println("deflate should report Z_STREAM_END"); java.lang.SystemJ.exit(1); } err = c_stream.deflateEnd(); CHECK_ERR(c_stream, err, "deflateEnd"); ZStream d_stream = new ZStream(); d_stream.next_in = compr; d_stream.next_in_index = 0; d_stream.avail_in = comprLen; err = d_stream.inflateInit(); CHECK_ERR(d_stream, err, "inflateInit"); d_stream.next_out = uncompr; d_stream.next_out_index = 0; d_stream.avail_out = uncomprLen; while (true) { err = d_stream.inflate(JZlib.Z_NO_FLUSH); if (err == JZlib.Z_STREAM_END) { break; } if (err == JZlib.Z_NEED_DICT) { if ((int)d_stream.adler != (int)dictId) { java.lang.SystemJ.outJ.println("unexpected dictionary"); java.lang.SystemJ.exit(1); } err = d_stream.inflateSetDictionary(dictionary, dictionary.Length); } CHECK_ERR(d_stream, err, "inflate with dict"); } err = d_stream.inflateEnd(); CHECK_ERR(d_stream, err, "inflateEnd"); int j = 0; for (; j < uncompr.Length; j++) if (uncompr[j] == 0) break; java.lang.SystemJ.outJ.println("after inflateSync(): hel" + new java.lang.StringJ(uncompr, 0, j)); }