-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathCompression.java
More file actions
232 lines (226 loc) · 6.84 KB
/
Copy pathCompression.java
File metadata and controls
232 lines (226 loc) · 6.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package javaforce;
import java.io.*;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
/** Compression and Serialization.
*
* Provides methods to serialize/compress and decompress/deserialize Objects.
*
* Objects must implement java.io.Serializable and should include a serialVersionUID.
* public static final long serialVersionUID = 1L;
*
* @author pquiring
*/
public class Compression {
public static class UnitTest implements Serializable {
private static final long serialVersionUID = 1L;
public String name;
public int iValue;
public float fValue;
public String sValue;
}
public static void main(String[] args) {
if (args.length == 0) {
JFLog.log("Usage:compression {compress, decompress, serialize, deserialize}");
return;
}
switch (args[0]) {
case "compress": compressionTest(); break;
case "decompress": decompressionTest(); break;
case "serialize": serializeTest(); break;
case "deserialize": deserializeTest(); break;
}
}
private static void compressionTest() {
//test compress/decompress
try {
File input = new File("javaforce.exe");
File output = new File("compress.dat");
FileInputStream fis = new FileInputStream(input);
FileOutputStream fos = new FileOutputStream(output);
long uncompressed = input.length();
System.out.println(" org size=" + uncompressed);
long compressed = compress(fis, fos, uncompressed);
System.out.println(" compressed=" + compressed);
fis.close();
fos.close();
System.out.println("done");
} catch (Exception e) {
JFLog.log(e);
return;
}
}
private static void decompressionTest() {
try {
File input = new File("compress.dat");
File output = new File("uncompress.dat");
long compressed = input.length();
FileInputStream fis = new FileInputStream(input);
FileOutputStream fos = new FileOutputStream(output);
long uncompressed = decompress(fis, fos, compressed);
System.out.println("uncompressed=" + uncompressed);
fis.close();
fos.close();
System.out.println("done");
} catch (Exception e) {
JFLog.log(e);
return;
}
}
private static void serializeTest() {
try {
serialize("serialized.dat", new UnitTest());
System.out.println("done");
} catch (Exception e) {
JFLog.log(e);
}
}
private static void deserializeTest() {
try {
UnitTest unit = (UnitTest)deserialize("serialized.dat");
System.out.println("done");
} catch (Exception e) {
JFLog.log(e);
}
}
private final static int blocksize = (64 * 1024);
public static long compress(InputStream is, OutputStream os, long uncompressed) throws Exception {
long left = uncompressed;
long wrote = 0;
byte[] in = new byte[blocksize];
byte[] out = new byte[blocksize];
Deflater compress = new Deflater();
while (left > 0) {
if (compress.needsInput()) {
int toread = left > blocksize ? blocksize : (int)left;
int read = is.read(in, 0, toread);
if (read > 0) {
compress.setInput(in, 0, read);
left -= read;
}
}
int compressed = compress.deflate(out);
if (compressed > 0) {
os.write(out, 0, compressed);
wrote += compressed;
}
}
compress.finish();
while (!compress.finished()) {
int compressed = compress.deflate(out);
if (compressed > 0) {
os.write(out, 0, compressed);
wrote += compressed;
}
}
compress.end();
return wrote;
}
public static long decompress(InputStream is, OutputStream os, long compressed) throws Exception {
long left = compressed;
long wrote = 0;
byte[] in = new byte[blocksize];
byte[] out = new byte[blocksize];
Inflater decompress = new Inflater();
while (left > 0) {
if (decompress.needsInput()) {
int toread = left > blocksize ? blocksize : (int)left;
int read = is.read(in, 0, toread);
if (read > 0) {
decompress.setInput(in, 0, read);
left -= read;
}
}
int uncompressed = decompress.inflate(out);
if (uncompressed > 0) {
os.write(out, 0, uncompressed);
wrote += uncompressed;
}
}
while (!decompress.finished()) {
int uncompressed = decompress.inflate(out);
if (uncompressed > 0) {
os.write(out, 0, uncompressed);
wrote += uncompressed;
}
}
decompress.end();
return wrote;
}
/** Serialize and compress an object to a file. */
public static boolean serialize(String file, Object obj) {
try {
FileOutputStream fos = new FileOutputStream(file);
boolean res = serialize(fos, obj);
fos.close();
return res;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
/** Serialize and compress an object to a file. */
public static boolean serialize(File file, Object obj) {
try {
FileOutputStream fos = new FileOutputStream(file);
boolean res = serialize(fos, obj);
fos.close();
return res;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
/** Serialize and compress an object to an OutputStream. */
public static boolean serialize(OutputStream os, Object obj) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
ByteArrayInputStream biis = new ByteArrayInputStream(baos.toByteArray());
Compression.compress(biis, os, baos.size());
return true;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
/** Decompress and deserialize a file to an Object. */
public static Object deserialize(String file) {
try {
FileInputStream fis = new FileInputStream(file);
Object obj = deserialize(fis, new File(file).length());
fis.close();
return obj;
} catch (Exception e) {
JFLog.log(e);
return null;
}
}
/** Decompress and deserialize a file to an Object. */
public static Object deserialize(File file) {
try {
FileInputStream fis = new FileInputStream(file);
Object obj = deserialize(fis, file.length());
fis.close();
return obj;
} catch (Exception e) {
JFLog.log(e);
return null;
}
}
/** Decompress and deserialize an InputStream to an Object. */
public static Object deserialize(InputStream is, long length) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Compression.decompress(is, baos, length);
ByteArrayInputStream biis = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(biis);
Object obj = ois.readObject();
return obj;
} catch (Exception e) {
JFLog.log(e);
return null;
}
}
}