-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathCompressionUtils.java
More file actions
85 lines (73 loc) · 2.23 KB
/
CompressionUtils.java
File metadata and controls
85 lines (73 loc) · 2.23 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
package net.spy.memcached.transcoders;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import net.spy.memcached.compat.CloseUtil;
import net.spy.memcached.compat.SpyObject;
/**
* Utility class for compression and decompression operations.
*/
@Deprecated
public class CompressionUtils extends SpyObject {
public static final int DEFAULT_COMPRESSION_THRESHOLD = 16384;
private int compressionThreshold;
public CompressionUtils() {
this(DEFAULT_COMPRESSION_THRESHOLD);
}
public CompressionUtils(int compressionThreshold) {
this.compressionThreshold = compressionThreshold;
}
public void setCompressionThreshold(int compressionThreshold) {
this.compressionThreshold = compressionThreshold;
}
public boolean isCompressionCandidate(byte[] data) {
return data != null && data.length > compressionThreshold;
}
/**
* Compress the given array of bytes.
*/
public byte[] compress(byte[] in) {
if (in == null) {
throw new NullPointerException("Can't compress null");
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
GZIPOutputStream gz = null;
try {
gz = new GZIPOutputStream(bos);
gz.write(in);
} catch (IOException e) {
throw new RuntimeException("IO exception compressing data", e);
} finally {
CloseUtil.close(gz);
CloseUtil.close(bos);
}
byte[] rv = bos.toByteArray();
getLogger().debug("Compressed %d bytes to %d", in.length, rv.length);
return rv;
}
/**
* Decompress the given array of bytes.
*/
public byte[] decompress(byte[] in) {
ByteArrayOutputStream bos = null;
if (in != null) {
ByteArrayInputStream bis = new ByteArrayInputStream(in);
bos = new ByteArrayOutputStream();
GZIPInputStream gis;
try {
gis = new GZIPInputStream(bis);
byte[] buf = new byte[8192];
int r;
while ((r = gis.read(buf)) > 0) {
bos.write(buf, 0, r);
}
} catch (IOException e) {
getLogger().warn("Failed to decompress data", e);
bos = null;
}
}
return bos == null ? null : bos.toByteArray();
}
}