1 24 25 package org.objectweb.cjdbc.common.stream.encoding; 26 27 import java.io.ByteArrayInputStream ; 28 import java.io.ByteArrayOutputStream ; 29 import java.io.IOException ; 30 import java.util.zip.Deflater ; 31 import java.util.zip.DeflaterOutputStream ; 32 import java.util.zip.Inflater ; 33 import java.util.zip.InflaterInputStream ; 34 35 41 public class ZipEncoding 42 { 43 50 public static final byte[] encode(byte[] data) throws IOException 51 { 52 ByteArrayInputStream bais = new ByteArrayInputStream (data); 53 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 54 DeflaterOutputStream zipOutputStream = new DeflaterOutputStream (baos, 56 new Deflater (Deflater.BEST_COMPRESSION, true)); 57 58 byte[] bdata = new byte[1024]; 60 int byteCount; 61 while ((byteCount = bais.read(bdata, 0, 1024)) > -1) 62 { 63 zipOutputStream.write(bdata, 0, byteCount); 64 } 65 zipOutputStream.flush(); 66 zipOutputStream.finish(); 67 zipOutputStream.close(); 68 return baos.toByteArray(); 69 } 70 71 78 public static final byte[] decode(byte[] data) throws IOException 79 { 80 InflaterInputStream input = new InflaterInputStream ( 81 new ByteArrayInputStream (data), new Inflater (true)); 82 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 83 84 byte[] bdata = new byte[1024]; 85 int byteCount; 86 while ((byteCount = input.read(bdata, 0, 1024)) > -1) 87 baos.write(bdata, 0, byteCount); 88 baos.flush(); 89 baos.close(); 90 91 return baos.toByteArray(); 92 } 93 } | Popular Tags |