⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 deflater.java

📁 zlib 算法在j2me 中的应用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	    this.level = level;	    setParams = true;	}    }    /**     * Returns true if the input data buffer is empty and setInput()     * should be called in order to provide more input.     * @return true if the input data buffer is empty and setInput()     * should be called in order to provide more input     */    public boolean needsInput() {	return c_stream.avail_in <= 0;    }    /**     * When called, indicates that compression should end with the current     * contents of the input buffer.     */    public synchronized void finish() throws IOException{     finish = true;     int err=c_stream.deflate(JZlib.Z_FINISH);     if(err==JZlib.Z_STREAM_END) finished=true;     //if(err!=JZlib.Z_OK)System.out.println(ErrorMsg(c_stream,err));     //err=c_stream.deflateEnd();     if(err!=JZlib.Z_OK && err!=JZlib.Z_STREAM_END){      System.out.println(ErrorMsg(c_stream,err));      throw new IOException(ErrorMsg(c_stream,err));     }//if          }    /**     * Returns true if the end of the compressed data output stream has     * been reached.     * @return true if the end of the compressed data output stream has     * been reached     */    public synchronized boolean finished() {	return finished;    }    /**     * Fills specified buffer with compressed data. Returns actual number     * of bytes of compressed data. A return value of 0 indicates that     * needsInput() should be called in order to determine if more input     * data is required.     * @param b the buffer for the compressed data     * @param off the start offset of the data     * @param len the maximum number of bytes of compressed data     * @return the actual number of bytes of compressed data     */    public synchronized int deflate(byte[] b, int off, int len) throws IOException{	if (b == null) {	    throw new NullPointerException();	}	if (off < 0 || len < 0 || off + len > b.length) {	    throw new ArrayIndexOutOfBoundsException();	}	return deflateBytes(b, off, len);    }    /**     * Fills specified buffer with compressed data. Returns actual number     * of bytes of compressed data. A return value of 0 indicates that     * needsInput() should be called in order to determine if more input     * data is required.     * @param b the buffer for the compressed data     * @return the actual number of bytes of compressed data     */    public int deflate(byte[] b)  throws IOException{	return deflate(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 (c_stream==null) {      throw new NullPointerException();     }     return (int)c_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 (c_stream==null) {      throw new NullPointerException();     }     return (int)c_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 (c_stream==null) {      throw new NullPointerException();     }     return (int)c_stream.total_out;    }    /**     * Resets deflater so that a new set of input data can be processed.     * Keeps current compression level and strategy settings.     */    public synchronized void reset()  throws IOException{     if (c_stream==null) {      throw new NullPointerException();     }//if     c_stream=null;     init(level, DEFAULT_STRATEGY, nowrap);     finish = false;     finished = false;     //off = len = 0;          c_stream.next_in_index=c_stream.avail_in=0;    }        /**     * Closes the compressor and discards any unprocessed input.     * This method should be called when the compressor is no longer     * being used, but will also be called automatically by the     * finalize() method. Once this method is called, the behavior     * of the Deflater object is undefined.     */    public synchronized void end() {     if (c_stream!=null) {      c_stream.free();      c_stream=null;     }    }    //    /**    //     * Closes the compressor when garbage is collected.    //     */    //    protected void finalize() {    //     end();    //    }        private void init(int level, int strategy, boolean nowrap) throws IOException{     this.level=level;     this.strategy=strategy;     c_stream = new ZStream();          //??? may be correct ZIP data is needed here     int err;     if(nowrap){      //err=c_stream.deflateInit(JZlib.Z_DEFAULT_COMPRESSION,-1);      err=c_stream.deflateInit(level,-1);           }else{      //err=c_stream.deflateInit(JZlib.Z_DEFAULT_COMPRESSION);      err=c_stream.deflateInit(level);     }//if     if(err==JZlib.Z_STREAM_END) finished=true;     if(err!=JZlib.Z_OK){      System.out.println(ErrorMsg(c_stream,err));       throw new IOException(ErrorMsg(c_stream,err));     }//if          }        private  int deflateBytes(byte[] b, int off, int len) throws IOException{     //System.out.println("b.length="+b.length+" off="+off+" len="+len);     final long last_total_out=c_stream.total_out;	     c_stream.next_out=b;     c_stream.next_out_index=off;     c_stream.avail_out = len;     final int err;     if(finish==true){      err=c_stream.deflate(JZlib.Z_FINISH);     }else{      err=c_stream.deflate(JZlib.Z_NO_FLUSH);//TODO adjust FLUSH method     }//if      if(err==JZlib.Z_STREAM_END) finished=true;     if(err!=JZlib.Z_OK&&err!=JZlib.Z_STREAM_END){      System.out.println(ErrorMsg(c_stream,err));      throw new IOException(ErrorMsg(c_stream,err));     }//if           if(c_stream.total_out>Integer.MAX_VALUE){      throw new IOException("Internal error: JZlib deflated data is too long");     }//if     return (int)(c_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{  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");      }//whileSystem.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(); }*/    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -