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

📄 deflater.java

📁 zlib 算法在j2me 中的应用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*Copyright (c) 2003 Asoft ltd. All rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions are met:  1. Redistributions of source code must retain the above copyright notice,     this list of conditions and the following disclaimer.  2. Redistributions in binary form must reproduce the above copyright     notice, this list of conditions and the following disclaimer in     the documentation and/or other materials provided with the distribution.  3. The names of the authors may not be used to endorse or promote products     derived from this software without specific prior written permission.THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY ANDFITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ASOFTLTD. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOTLIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDINGNEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*//*                                                                             *Copyright (c) 2003 Asoft ltd. All rights reserved.                            *www.asoft.ru                                                                  *Authors: Alexandre Rusev, Alexey Soloviev                                     */package com.asoft.midp.zip;import java.io.*;import com.asoft.midp.jzlib.*;/** * This class provides support for general purpose compression using the * popular JZLIB compression library. JZLIB is a port of popular ZLIB library. *(see http://www.jcraft.com/jzlib/ for further JZLIB information) *The ZLIB compression library was * initially developed as part of the PNG graphics standard and is not * protected by patents. *  * @see		Inflater *///!!!  TODO exclude casting of "long" to  "int" in case if "long" is excluded from JZLIBpublic class Deflater {    private int level, strategy;    private boolean setParams;    private boolean finish, finished;    private ZStream c_stream;    private boolean nowrap;//GZIP compatability (not supported)    /**     * Compression method for the deflate algorithm (the only one currently     * supported).     */    public static final int DEFLATED = 8;    /**     * Compression level for no compression.     */    public static final int NO_COMPRESSION = JZlib.Z_NO_COMPRESSION;    /**     * Compression level for fastest compression.     */    public static final int BEST_SPEED = JZlib.Z_BEST_SPEED;    /**     * Compression level for best compression.     */    public static final int BEST_COMPRESSION = JZlib.Z_BEST_COMPRESSION;    /**     * Default compression level.     */    public static final int DEFAULT_COMPRESSION = JZlib.Z_DEFAULT_COMPRESSION;    /**     * Compression strategy best used for data consisting mostly of small     * values with a somewhat random distribution. Forces more Huffman coding     * and less string matching.     */    public static final int FILTERED = JZlib.Z_FILTERED;    /**     * Compression strategy for Huffman coding only.     */    public static final int HUFFMAN_ONLY = JZlib.Z_HUFFMAN_ONLY;    /**     * Default compression strategy.     */    public static final int DEFAULT_STRATEGY = JZlib.Z_DEFAULT_STRATEGY;        /**      * Creates a new compressor using the specified compression level.      * If 'nowrap' is true then the ZLIB header and checksum fields will      * not be used in order to support the compression format used in      * both GZIP and PKZIP.      * @param level the compression level (0-9)      * @param nowrap if true then use GZIP compatible compression NOT TESTED      */    private Deflater(int level, boolean nowrap)  throws IOException{     this.level = level;     this.strategy = DEFAULT_STRATEGY;     this.nowrap=nowrap;     init(level, DEFAULT_STRATEGY, nowrap);    }    /**      * Creates a new compressor using the specified compression level.     * Compressed data will be generated in ZLIB format.     * @param level the compression level (0-9)     */    public Deflater(int level)  throws IOException{	this(level, false);    }        /**     * Creates a new compressor with the default compression level.     * Compressed data will be generated in ZLIB format.     */    public Deflater()  throws IOException{	this(DEFAULT_COMPRESSION, false);    }    /**     * Sets input data for compression. This should be called whenever     * needsInput() returns true indicating that more input data is required.     * @param b the input data bytes     * @param off the start offset of the data     * @param len the length of the data     * @see Deflater#needsInput     */    public synchronized void setInput(byte[] b, int off, int len) {	if (b== null) {	    throw new NullPointerException();	}	if (off < 0 || len < 0 || off + len > b.length) {	    throw new ArrayIndexOutOfBoundsException();	}	//this.buf = b;	//this.off = off;	//this.len = len;	c_stream.next_in=b;	c_stream.next_in_index=off;	c_stream.avail_in=len;    }    /**     * Sets input data for compression. This should be called whenever     * needsInput() returns true indicating that more input data is required.     * @param b the input data bytes     * @see Deflater#needsInput     */    public void setInput(byte[] b) {	setInput(b, 0, b.length);    }    /**     * Sets preset dictionary for compression. A preset dictionary is used     * when the history buffer can be predetermined. When the data is later     * uncompressed with Inflater.inflate(), Inflater.getAdler() can be called     * in order to get the Adler-32 value of the dictionary required for     * decompression.     * @param b the dictionary data bytes     * @param off the start offset of the data     * @param len the length of the data     * @see Inflater#inflate     * @see Inflater#getAdler     */    public synchronized void setDictionary(byte[] b, int off, int len) throws IOException{     if (c_stream==null || b == null) {      throw new NullPointerException();     }     if (off < 0 || len < 0 || off + len > b.length) {      throw new ArrayIndexOutOfBoundsException();     }     byte[] dictionary = new byte[len];     System.arraycopy(b,off,dictionary,0,len);     int err=c_stream.deflateSetDictionary(dictionary, dictionary.length);     if(err!=JZlib.Z_OK){	        System.out.println(ErrorMsg(c_stream,err));       throw new IOException(ErrorMsg(c_stream,err));     }//if    }    /**     * Sets preset dictionary for compression. A preset dictionary is used     * when the history buffer can be predetermined. When the data is later     * uncompressed with Inflater.inflate(), Inflater.getAdler() can be called     * in order to get the Adler-32 value of the dictionary required for     * decompression.     * @param b the dictionary data bytes     * @see Inflater#inflate     * @see Inflater#getAdler     */    public void setDictionary(byte[] b)  throws IOException{	setDictionary(b, 0, b.length);    }    /**     * Sets the compression strategy to the specified value.     * @param strategy the new compression strategy     * @exception IllegalArgumentException if the compression strategy is     *				           invalid     */    public synchronized void setStrategy(int strategy) {	switch (strategy) {	  case DEFAULT_STRATEGY:	  case FILTERED:	  case HUFFMAN_ONLY:	    break;	  default:	    throw new IllegalArgumentException();	}	if (this.strategy != strategy) {	    this.strategy = strategy;	    setParams = true;	}    }    /**     * Sets the current compression level to the specified value.     * @param level the new compression level (0-9)     * @exception IllegalArgumentException if the compression level is invalid     *<p>NOT PROPERLY IMPLEMENTED     *<p>TODO: make it change compression level on fly (not only on reset)     */    public synchronized void setLevel(int level) {	if ((level < 0 || level > 9) && level != DEFAULT_COMPRESSION) {	    throw new IllegalArgumentException("invalid compression level");	}	if (this.level != level) {

⌨️ 快捷键说明

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