00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 package org.objectweb.cjdbc.common.stream.encoding;
00026
00027 import java.io.ByteArrayInputStream;
00028 import java.io.ByteArrayOutputStream;
00029 import java.io.IOException;
00030 import java.util.zip.Deflater;
00031 import java.util.zip.DeflaterOutputStream;
00032 import java.util.zip.Inflater;
00033 import java.util.zip.InflaterInputStream;
00034
00035
00036
00037
00038
00039
00040
00041 public class ZipEncoding
00042 {
00043
00044
00045
00046
00047
00048
00049
00050 public static final byte[] encode(byte[] data) throws IOException
00051 {
00052 ByteArrayInputStream bais = new ByteArrayInputStream(data);
00053 ByteArrayOutputStream baos = new ByteArrayOutputStream();
00054
00055 DeflaterOutputStream zipOutputStream = new DeflaterOutputStream(baos,
00056 new Deflater(Deflater.BEST_COMPRESSION, true));
00057
00058
00059 byte[] bdata = new byte[1024];
00060 int byteCount;
00061 while ((byteCount = bais.read(bdata, 0, 1024)) > -1)
00062 {
00063 zipOutputStream.write(bdata, 0, byteCount);
00064 }
00065 zipOutputStream.flush();
00066 zipOutputStream.finish();
00067 zipOutputStream.close();
00068 return baos.toByteArray();
00069 }
00070
00071
00072
00073
00074
00075
00076
00077
00078 public static final byte[] decode(byte[] data) throws IOException
00079 {
00080 InflaterInputStream input = new InflaterInputStream(
00081 new ByteArrayInputStream(data), new Inflater(true));
00082 ByteArrayOutputStream baos = new ByteArrayOutputStream();
00083
00084 byte[] bdata = new byte[1024];
00085 int byteCount;
00086 while ((byteCount = input.read(bdata, 0, 1024)) > -1)
00087 baos.write(bdata, 0, byteCount);
00088 baos.flush();
00089 baos.close();
00090
00091 return baos.toByteArray();
00092 }
00093 }