nativepackedoutputstream.java

来自「java调用ie浏览器demo源码,可以用在windows或者linux」· Java 代码 · 共 438 行 · 第 1/2 页

JAVA
438
字号
/*

 * Copyright (C) 2008 Sun Microsystems, Inc. All rights reserved. Use is

 * subject to license terms.

 *

 * This program is free software; you can redistribute it and/or modify

 * it under the terms of the Lesser GNU General Public License as

 * published by the Free Software Foundation; either version 2 of the

 * License, or (at your option) any later version.

 *

 * This program is distributed in the hope that it will be useful, but

 * WITHOUT ANY WARRANTY; without even the implied warranty of

 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU

 * General Public License for more details.

 *

 * You should have received a copy of the GNU General Public License

 * along with this program; if not, write to the Free Software

 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307

 * USA.

 */



package org.jdic.arc;



import java.io.*;

import java.util.zip.ZipOutputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipException;

import java.util.zip.CRC32;

import java.util.HashMap;

import java.util.Formatter;

import org.jdic.NativeLoadMgr;



/**

 * The class is responsible for CAB pack operatrions. 

 * @author uta

 */



public class NativePackedOutputStream extends ZipOutputStream {

        static {

            NativeLoadMgr.loadLibrary("jdicArc");

            initIDs();

        }

        private static native void initIDs();



        /*

         * fields

         */

        protected OutputStream os; //destination stream

        protected long osNative; //native handler for packer

        protected ZipEntry entry;

        protected HashMap<String, ZipEntry> names = new HashMap<String, ZipEntry>();

        protected int method = METHOD_LZX; //default compression method

        protected int level = 9; //default compression strength [0..9]

        protected CRC32 crc = new CRC32(); //CRC calculator

        protected long actualUncomressedSize = 0;





        /**

        * Format constant for constructor

        * Primitive stream copier. Non-functionable.

        * Test purpose only

        */

        public static int FORMAT_COPY = 0;



        /**

        * Format constant for constructor

        * CAB stream unpacker. Applies for CAB stream unpacking

        */

        public static int FORMAT_CAB  = 1;



        /**

         * Hint constant for constructor

         * Hold all temporary buffers in memory

         */

        public static final long HINT_IN_MEMORY = 0;



        /**

         * Hint constant for constructor

         * Hold all temporary buffers in temp folder as files

         */

        public static final long HINT_ON_DISK = 1;





        /**

         * Hint constant for constructor

         * Hold CRC32 and Compressed Size in entry name like

         * name{FAFAFAFA:FAFAFAFA}.ext  

         */

        public static final long HINT_SORE_EINFO = 2;



        /**

         * Mask for compression type: No compression. The same as STORED

         */

        public static final int METHOD_NONE    = 0x0000;//STORED



        /**

         * Mask for compression type: MSZIP.

         */

        public static final int METHOD_MSZIP   = 0x0001;



        /**

         * Mask for compression type: Quantum (not yet supported by MS)

         */

        public static final int METHOD_QUANTUM = 0x0002;



        /**

         * Mask for compression type: LZX

         */

        public static final int METHOD_LZX     = 0x0003;



        /**

         * Set <code>method</code> field of <code>ze</code> to value METHOD_MSZIP, 

         * METHOD_QUANTUM, METHOD_LZX.

         * @param ze the entry for modify

         * @param method the desired compression method

         */

         public void setMethod(ZipEntry ze, int method) {

            switch(method){

            case METHOD_MSZIP:

            case METHOD_QUANTUM:

            case METHOD_LZX:

                ZipEntryAccessor.setMethod(ze, method);

                return;

            }

            ze.setMethod(method);

        }





        /*

        * Internal state validator.

        */

        void checkValid() throws IOException {

            if(null == os || 0 == osNative) {

                throw new IOException("Empty or corrupted output stream");

            }

            //checkNativeValid(osNative);

        }

        //private static native void checkNativeValid(long osNative) throws IOException;

        

        /**

         * Constructs a wrapper for native compressor.

         * @param _os the destination stream

         * @param iFormat a FORMAT_XXXX constant 

         * @param hint a combination of HINT_XXXX constants

         * @throws java.io.IOException

         */

        public NativePackedOutputStream(

                OutputStream _os, 

                int iFormat, 

                long hint) throws IOException 

        {

            super(_os);

            os = _os;

            if(null == os) {

                throw new IOException("No base output stream");

            }

            osNative = createNativeStream(os, iFormat, hint);

        }

        

        /**

         * Constructs a wrapper for native compressor. All buffers will be opened 

         * on a disk, CRC will be calculated. 

         * @param _os the destination stream

         * @param iFormat a FORMAT_XXXX constant 

         * @throws java.io.IOException

         */

        public NativePackedOutputStream(

                OutputStream _os, 

                int iFormat) throws IOException 

        {

            this(_os, iFormat, HINT_ON_DISK | HINT_SORE_EINFO);

        }

        

        /**

         * Creates the native encoder.

         * @param os the destination stream

         * @param iFormat a FORMAT_XXXX constant 

         * @param hint a combination of HINT_XXXX constants

         * @return the native encoder handle

         * @throws java.io.IOException

         */

        private native long createNativeStream(

                OutputStream os, 

                int iFormat, 

                long hint) throws IOException;





        /**

         * Sets the CAB file comment. Not supported.

         * @param comment the comment string

         * @exception IllegalArgumentException if the length of the specified

         *		  ZIP file comment os greater than 0xFFFF bytes

         */

        @Override

        public void setComment(String comment) {

            if ( null != comment) {

                throw new IllegalArgumentException("CAB does not support.");

            }

        }



        /**

         * Sets the default compression method for subsequent entries. This

         * default will be used whenever the compression method is not specified

         * for an individual CAB file entry, and is initially set to DEFLATED.

         * @param method the default compression method

         * @exception IllegalArgumentException if the specified compression method

         *		  is invalid

         */

        @Override

        public void setMethod(int method) {

            this.method = method;

        }



        /**

         * Sets the compression level for subsequent entries which are DEFLATED.

         * The default setting is DEFAULT_COMPRESSION.

         * @param level the compression level (0-9)

         * @exception IllegalArgumentException if the compression level is invalid

         */        

        @Override

⌨️ 快捷键说明

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