📄 inflater.java
字号:
* In the later case, getAdler() can be used to get the Adler-32 * value of the dictionary required. * @param b the buffer for the uncompressed data * @return the actual number of uncompressed bytes * @exception DataFormatException if the compressed data format is invalid * @see Inflater#needsInput * @see Inflater#needsDictionary */ public int inflate(byte[] b) throws DataFormatException, IOException { return inflate(b, 0, b.length); } /** * Returns the ADLER-32 value of the uncompressed data. * @return the ADLER-32 value of the uncompressed data */ public synchronized int getAdler() { if (d_stream == null) { throw new NullPointerException(); } return (int) d_stream.adler; } /** * Returns the total number of bytes input so far. * @return the total number of bytes input so far */ public synchronized int getTotalIn() { if (d_stream == null) { throw new NullPointerException(); } return (int) d_stream.total_in; } /** * Returns the total number of bytes output so far. * @return the total number of bytes output so far */ public synchronized int getTotalOut() { if (d_stream == null) { throw new NullPointerException(); } return (int) d_stream.total_out; } /** * Resets inflater so that a new set of input data can be processed. */ public synchronized void reset() throws IOException { if (d_stream == null) { throw new NullPointerException(); }//if d_stream = null; init(nowrap); finished = false; needDict = false; //off = len = 0; d_stream.next_in_index = d_stream.avail_in = 0; } /** * Closes the decompressor and discards any unprocessed input. * This method should be called when the decompressor is no longer * being used, but will also be called automatically by the finalize() * method. Once this method is called, the behavior of the Inflater * object is undefined. */ public synchronized void end() { if (d_stream != null) { d_stream.free(); d_stream = null; } } // /** // * Closes the decompressor when garbage is collected. // */ // protected void finalize() { // end(); // } private final void init(boolean nowrap) throws IOException { this.nowrap = nowrap; d_stream = new ZStream(); int err; if (nowrap) { err = d_stream.inflateInit(-15); //sliding window for PKZIP is 32K } else { err = d_stream.inflateInit(); }//if if (err == JZlib.Z_STREAM_END) { finished = true; } if (err != JZlib.Z_OK) { System.out.println(ErrorMsg(d_stream, err)); throw new IOException(ErrorMsg(d_stream, err)); }//if } private int inflateBytes(byte[] b, int off, int len) throws DataFormatException, IOException { final long last_total_out = d_stream.total_out; d_stream.next_out = b; d_stream.next_out_index = off; d_stream.avail_out = len; int err = d_stream.inflate(JZlib.Z_NO_FLUSH); if (err == JZlib.Z_STREAM_END) { finished = true; } if (err != JZlib.Z_OK && err != JZlib.Z_STREAM_END) { System.out.println(ErrorMsg(d_stream, err)); throw new IOException(ErrorMsg(d_stream, err)); }//if return (int) (d_stream.total_out - last_total_out); } private static final void CheckError(ZStream z, int err) throws IOException { if (err != JZlib.Z_OK) { String err_s = "zip error: " + err; if (z.msg != null) { err_s = err_s + " (" + z.msg + ")"; } throw new IOException(err_s); }//if } private static final String ErrorMsg(ZStream z, int err) throws IOException { String ret = null; if (err != JZlib.Z_OK) { String err_s = "zip error: " + err; if (z.msg != null) { err_s = err_s + " (" + z.msg + ")"; } ret = err_s; }//if return ret; } //================================================================== public static void main(String[] args) throws Exception { //compress data System.out.println("compress data"); final int hlen = 0; final byte[] buff = "qwertyuiopasdfghjklzxcvbnm".getBytes(); int l_in = buff.length; final byte[] out_buf = new byte[buff.length + 30]; Deflater compressor = new Deflater(9); System.out.println("#1"); compressor.reset(); System.out.println("#2"); compressor.setInput(buff, 0, l_in); System.out.println("#3"); while (!compressor.needsInput()) { System.out.println("#7"); compressor.deflate(out_buf, hlen + compressor.getTotalOut(), out_buf.length - (hlen + compressor.getTotalOut())); System.out.println("#7.2"); } //while System.out.println("#8"); compressor.finish(); System.out.println("#9"); compressor.deflate(out_buf, hlen + compressor.getTotalOut(), out_buf.length - (hlen + compressor.getTotalOut())); System.out.println("#10"); int l_out = compressor.getTotalOut(); System.out.println("data compressed in=" + compressor.getTotalIn() + " out=" + compressor.getTotalOut()); //put output of compressor in to decompressor input final byte[] data = out_buf;//压缩过的数据 l_in = l_out; final byte[] udata = new byte[buff.length + 20]; //decompress data System.out.println("decompress data"); System.out.println("#11"); Inflater decompressor = new Inflater(); System.out.println("#12"); decompressor.reset(); System.out.println("#13"); decompressor.setInput(data, hlen, l_in); System.out.println("#14"); int offs = 0; int n = 0; while (!decompressor.needsInput() && !decompressor.finished()) { System.out.println("#15"); n = decompressor.inflate(udata, offs, udata.length - offs); offs = offs + n; System.out.println("#16"); }//while System.out.println("#33"); System.out.println(new String(udata)); System.out.println("data decompressed in=" + decompressor.getTotalIn() + " out=" + decompressor.getTotalOut()); //---------------------------------- /* try { decompressor.setInput(data, hlen, l_in);//������ ������������� //�� ������� �� ����� ��������� offs = 0; n = 0; while (offs < max_udatalen - 1 && !decompressor.needsInput() && !decompressor.finished()) { n = decompressor.inflate(udata, offs, udata.length - offs); offs = offs + n; if (udata.length <= offs) { //�������� ������ ������� int q = Math.max(udata.length * 6 / 5, 2 * data.length); byte[] old_udataref = udata; udata = new byte[Math.min(q, max_udatalen)]; System.arraycopy(old_udataref, 0, udata, 0, old_udataref.length); System.out.println("Increased buffer for uncompressed DataPacket to " + udata.length + " bytes."); }//if(udata.ref.length<offs+1) }//while } catch (Exception e) {} */ }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -