-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
INTERNAL: Refactor transcoder compress logic.
- Loading branch information
Showing
6 changed files
with
107 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/main/java/net/spy/memcached/transcoders/Compressor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
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; | ||
|
||
public class Compressor extends SpyObject { | ||
|
||
/** | ||
* Default compression threshold value. | ||
*/ | ||
public static final int DEFAULT_COMPRESSION_THRESHOLD = 16384; | ||
|
||
private int compressionThreshold = DEFAULT_COMPRESSION_THRESHOLD; | ||
|
||
public int getCompressionThreshold() { | ||
return compressionThreshold; | ||
} | ||
|
||
public void setCompressionThreshold(int compressionThreshold) { | ||
this.compressionThreshold = compressionThreshold; | ||
} | ||
|
||
/** | ||
* Compress the given array of bytes. | ||
*/ | ||
public byte[] compress(byte[] in, String objectType) { | ||
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(); | ||
if (!isProperCompress(in, rv)) { | ||
getLogger().info("Compression increased the size of %s from %d to %d", | ||
objectType, in.length, rv.length); | ||
return in; | ||
} | ||
System.out.println("Compressor.compress"); | ||
getLogger().debug("Compressed %s from %d to %d", objectType, in.length, rv.length); | ||
return rv; | ||
} | ||
|
||
/** | ||
* Compare the length before and after compression. | ||
* */ | ||
private boolean isProperCompress(byte[] before, byte[] after) { | ||
return before.length > after.length; | ||
} | ||
|
||
/** | ||
* Decompress the given array of bytes. | ||
* | ||
* @return null if the bytes cannot be decompressed | ||
*/ | ||
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 = -1; | ||
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters