-
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
7 changed files
with
165 additions
and
121 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
24 changes: 24 additions & 0 deletions
24
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,24 @@ | ||
package net.spy.memcached.transcoders; | ||
|
||
public interface Compressor { | ||
/** | ||
* Default compression threshold value. | ||
*/ | ||
int DEFAULT_COMPRESSION_THRESHOLD = 16384; | ||
|
||
/** | ||
* Compress the given array of bytes. | ||
*/ | ||
public byte[] compress(byte[] in); | ||
|
||
/** | ||
* Decompress the given array of bytes. | ||
* | ||
* @return null if the bytes cannot be decompressed | ||
*/ | ||
public byte[] decompress(byte[] in); | ||
|
||
public int getCompressionThreshold(); | ||
|
||
public void setCompressionThreshold(int to); | ||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/net/spy/memcached/transcoders/GzipCompressor.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,66 @@ | ||
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 GzipCompressor extends SpyObject implements Compressor { | ||
private int compressionThreshold = DEFAULT_COMPRESSION_THRESHOLD; | ||
|
||
@Override | ||
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); | ||
} | ||
return bos.toByteArray(); | ||
} | ||
|
||
@Override | ||
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(); | ||
} | ||
|
||
@Override | ||
public int getCompressionThreshold() { | ||
return compressionThreshold; | ||
} | ||
|
||
@Override | ||
public void setCompressionThreshold(int compressionThreshold) { | ||
this.compressionThreshold = compressionThreshold; | ||
} | ||
|
||
} |
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
Oops, something went wrong.