posts - 51, comments - 17, trackbacks - 0, articles - 9
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          java deflater and inflater

          Posted on 2007-04-10 21:29 chenweicai 閱讀(593) 評論(0)  編輯  收藏

          類Deflater 和 類Inflater:此類使用流行的 ZLIB 壓縮程序庫為通用壓縮提供支持。ZLIB 壓縮程序庫最初是作為 PNG 圖形標準的一部分開發的,不受專利的保護。

            //encoding a string into bytes
            String inputString = "chenweicai";
            byte[] input = inputString.getBytes("UTF-8");
            System.out.println("before compressed, the bytes length is :" + input.length);
            
            //compress the bytes
            byte[] output = new byte[100];
            Deflater compresser = new Deflater();
            compresser.setInput(input);
            compresser.finish();
            int compressedDataLength = compresser.deflate(output);
            System.out.println("compressed bytes length is :" + compressedDataLength);
            
            //decompress the bytes
            Inflater decompresser = new Inflater();
            decompresser.setInput(output, 0, compressedDataLength);
            byte[] result = new byte[100];
            int resultLength = decompresser.inflate(result);
            decompresser.end();
            System.out.println("after decompressed, the bytes length is :" + resultLength);
            
            //decode the bytes into a string
            String outputString = new String(result, 0, resultLength, "UTF-8");
            System.out.println("after compress and decompress the string is :" + outputString);


          2.


          import java.io.Serializable;

          public class Employee implements Serializable {

           private String name;
           
           private double salary;

           public Employee(String name, double salary) {
            super();
            // TODO Auto-generated constructor stub
            this.name = name;
            this.salary = salary;
           }
           
           public void raiseSalary(double byPercent){
            double temp = salary * byPercent / 100;
            salary += temp;
           }

           public String toString() {
            // TODO Auto-generated method stub
            return getClass().getName() +
             "[ Name = " + name + ", salary = " + salary +"]";
           }
           
          }





          public class Test2 {

           protected byte[] deflate(Object object) throws IOException{
            
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            Deflater def = new Deflater (Deflater.BEST_COMPRESSION);
            DeflaterOutputStream dos = new DeflaterOutputStream(baos, def);
            
            ObjectOutputStream out = new ObjectOutputStream(dos);
            out.writeObject(object);
            out.close();
            dos.finish();
            dos.close();
            
            return baos.toByteArray();
           }
           
           protected Object inflate(byte[] compressedContent) throws IOException{
            if(compressedContent == null)
             return null;
            
            try{
             ObjectInputStream in = new ObjectInputStream(
               new InflaterInputStream(new ByteArrayInputStream(compressedContent)));
             Object object = in.readObject();
             in.close();
             return object;
            }catch(Exception e){
             throw new IOException(e.toString());
            }
           }
           
           public static void main(String[] args) throws IOException{
            Employee employee = new Employee("LiLei", 1000);
            Test2 test2 = new Test2();
            byte[] bytes = new byte[1000];
            
            bytes = test2.deflate(employee);
            System.out.println(bytes);
            
            System.out.println(test2.inflate(bytes));
            
           }
          }


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 海林市| 军事| 东海县| 丰原市| 金沙县| 米脂县| 冷水江市| 慈溪市| 汉中市| 横峰县| 永平县| 滨海县| 南涧| 丹阳市| 天等县| 宿迁市| 织金县| 岫岩| 西乌珠穆沁旗| 邢台县| 宝应县| 定陶县| 抚顺市| 海城市| 河北区| 建平县| 承德县| 鄯善县| 吴忠市| 潢川县| 巢湖市| 赤城县| 噶尔县| 沂南县| 华池县| 德格县| 东乌珠穆沁旗| 桦川县| 台山市| 阜康市| 太仆寺旗|