| |
"Zip Objects, Zap Wait Time"
Vol. 8, Issue 10, p. 28
Listing 1
1 public class cZipObject implements java.io.Serializable
2 {
3 private int iOriginalSize;
4 private byte[] compressData;
5
6 public cZipObject()
7 {
8 iOriginalSize = 0;
9 }
10
11 public synchronized void setData(byte[] inData, int
iOrigSize)
12 {
13 compressData = inData;
14 iOriginalSize = iOrigSize;
15 }
16
17 public synchronized byte[] getData()
18 {
19 return compressData;
20 }
21
22 public synchronized int getOriginalSize()
23 {
24 return iOriginalSize;
25 }
26 }
Listing 2
1 public byte[] CompressBytes(byte[]DataArray)
2 {
3 byte[] testBytes = new byte[DataArray.length];
4
5 cDeflate = new Deflater(iCompressLevel);
6 cDeflate.setInput(DataArray);
7 cDeflate.finish();
8 cDeflate.deflate(testBytes);
9
10 int iRetLen = cDeflate.getTotalOut();
11 byte[] retArray = new byte[iRetLen];
12 System.arraycopy(testBytes, 0, retArray, 0,
iRetLen);
13 return retArray;
14 }
Listing 3
1 import java.io.ByteArrayOutputStream;
2 import java.io.ByteArrayInputStream;
3 import java.io.ObjectInputStream;
4 import java.io.ObjectOutputStream;
5 import java.io.Serializable;
6 import java.util.zip.Deflater;
7 import java.util.zip.Inflater;
8
9 public class cZipFactory
10 {
11 private int iCompressLevel = Deflater.BEST_COMPRESSION;
12 private Deflater cDeflate;
13 private Inflater cInflate;
14
15 public cZipFactory(int iCompLevel)
16 {
17 iCompressLevel = iCompLevel;
18 }
19
20 public cZipObject Compress(Serializable inObj)
21 {
22 cZipObject cZip = new cZipObject();
23
24 try {
25 ByteArrayOutputStream byteOut = new
ByteArrayOutputStream();
26 ObjectOutputStream objOut = new
ObjectOutputStream(byteOut);
27 objOut.writeObject(inObj);
28
29 byte[] DataArray = byteOut.toByteArray();
30 int iOrigSize = DataArray.length;
31 cZip.setData(CompressBytes(DataArray),
iOrigSize);
32 } catch (Exception e) {
33 System.out.println(e.getMessage());
34 }
35 return cZip;
36 }
37
38 public byte[] CompressBytes(byte[] array)
39 {
40 byte[] testBytes = new byte[array.length];
41
42 cDeflate = new Deflater(iCompressLevel);
43 cDeflate.setInput(array);
44 cDeflate.finish();
45 cDeflate.deflate(testBytes);
46
47 int iRetLen = cDeflate.getTotalOut();
48 byte[] retArray = new byte[iRetLen];
49 System.arraycopy(testBytes, 0, retArray, 0,
iRetLen);
50 return retArray;
51 }
52
53 public Object Decompress(cZipObject cZip)
54 {
55 byte[] unzipped = DecompressBytes(cZip.get
Data(), cZip.getOriginalSize());
56 return ConvertByteToObject(unzipped);
57 }
58
59 public byte[] DecompressBytes(byte[] array, int iLen)
60 {
61 byte[] retBytes = new byte[iLen];
62
63 try {
64 Inflater decompresser = new Inflater();
65 decompresser.setInput(array);
66 decompresser.inflate(retBytes);
67 decompresser.end();
68 } catch (Exception e) {
69 System.out.println(e.getMessage());
70 }
71 return retBytes;
72 }
73
74 public Object ConvertByteToObject(byte[] data)
75 {
76 Object objCache = null;
77
78 try {
79 ByteArrayInputStream fIn = new
java.io.ByteArrayInputStream(data);
80 ObjectInputStream objInput = new
ObjectInputStream(fIn);
81 objCache = objInput.readObject();
82 fIn.close();
83 } catch (Exception e) {
84 System.out.println(e.getMessage());
85 }
86 return objCache;
87 }
Listing 4
1 public byte[] DecompressBytes(byte[] array, int iLen)
2 {
3 byte[] retBytes = new byte[iLen];
4
5 try {
6 Inflater decompresser = new Inflater();
7 decompresser.setInput(array);
8 decompresser.inflate(retBytes);
9 decompresser.end();
10 } catch (Exception e) {
11 System.out.println(e.getMessage());
12 }
13 return retBytes;
14 }
Listing 5
1 public Object ConvertByteToObject(byte[] data)
2 {
3 Object objCache = null;
4
5 try {
6 ByteArrayInputStream fIn = new
java.io.ByteArrayInputStream(data);
7 ObjectInputStream objInput = new
ObjectInputStream(fIn);
8 objCache = objInput.readObject();
9 fIn.close();
10 } catch (Exception e) {
11 System.out.println(e.getMessage());
12 }
13 return objCache;
14 }
Listing 6
1 import java.util.Vector;
2
3 public class cZipExamples
4 {
5 cZipFactory ZipFactory;
6
7 static public void main(String[] args)
8 {
9 cZipExamples newZipExam = new cZipExamples();
10
11 newZipExam.ClientExample(10);
12 newZipExam.ClientExample(100);
13 newZipExam.ClientExample(1000);
14 newZipExam.ClientExample(10000);
15 newZipExam.ClientExample(100000);
16 newZipExam.ClientExample(1000000);
17 }
18
19 public cZipExamples()
20 {
21 ZipFactory = new cZipFactory(java.util.zip.Deflater.BEST_COMPRESSION);
22 }
23
24 public void ClientExample(int iSize)
25 {
26 Vector inVector = new Vector(iSize);
27 for (int i = 0; i < iSize; i++)
28 inVector.add("Client #" + i);
29
30 long lStart = System.currentTimeMillis();
31 cZipObject newZObject =
ZipFactory.Compress(inVector);
32 long lTime = System.currentTimeMillis() - lStart;
33
34 int iOrig = newZObject.getOriginalSize();
35 int iNew = newZObject.getData().length;
36
37 System.out.println("Zipped Vector object containing "
+ iSize + " from " + iOrig + " to "
+ iNew + " in time: " + lTime);
38
39 lStart = System.currentTimeMillis();
40 Vector vClients = (Vector)ZipFactory.Decompress(newZObject);
41 lTime = System.currentTimeMillis() - lStart;
42
43 System.out.println("Returned Vector object in
time: " + lTime);
44 }
45 }
|
|