<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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

    java deflater and inflater

    Posted on 2007-04-10 21:29 chenweicai 閱讀(592) 評論(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));
      
     }
    }


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


    網站導航:
     
    主站蜘蛛池模板: 一本久久免费视频| 日韩少妇内射免费播放| 久久不见久久见免费视频7| 国产精品亚洲а∨无码播放| 国产福利在线观看永久免费| 国产精品亚洲玖玖玖在线观看| 青草青草视频2免费观看| 免费a级毛片网站| 亚洲精品视频免费观看| 亚洲啪啪AV无码片| 三年片在线观看免费观看大全一 | 亚洲视频小说图片| 2022久久国产精品免费热麻豆| 亚洲人成在线影院| 7723日本高清完整版免费| 亚洲中文字幕无码久久| 免费观看国产小粉嫩喷水| eeuss草民免费| 亚洲日本中文字幕| 成全高清视频免费观看| 免费又黄又爽又猛大片午夜| 亚洲午夜无码片在线观看影院猛 | 2020久久精品亚洲热综合一本| 麻豆成人精品国产免费| 成人在线免费视频| 午夜影视日本亚洲欧洲精品一区| 97视频免费观看2区| 亚洲国产日韩a在线播放| 亚洲色偷偷狠狠综合网| 久久国产乱子伦精品免费看| 国产成人精品日本亚洲11| 免费很黄很色裸乳在线观看| 久久青草免费91线频观看站街| 国产.亚洲.欧洲在线| 老司机亚洲精品影视www| 免费在线观看视频网站| 免费的黄色的网站| 久久亚洲AV无码精品色午夜| 国产精品国产免费无码专区不卡| 国产在线精品观看免费观看| 亚洲国产综合在线|