public class CompressibleByteArrayOutputStream extends OutputStream
An OutputStream that compresses the data once the size crosses a certain threshold. The setCompressionEnabled can be used to turn off compressions for sections that are not part of the data and should not be compressed. When compressionEnabled is flipped from false to true a new compression session is started and the data size is counted from that point and when it exceeds the threshold the stream is reset to that point, a GZIPOutputStream is started and the data in session is written to the GZIPOutputStream. compressionEnabled is false for commands and keys. It is turned on by the DataHandler if it supports compression.
When gzip compression is on the stream does not store the original data. To avoid compressing data that is already compressed by gzip or other means a compression ratio check is made after every 1024 bytes. If the compression ratio is above the abandonCompressionRatio then the compression is abandoned. This is done by closing the GZIPOutputStream resetting the stream back to the start of the compression session, writing back the original data by uncompressing the data written and continuing with future writes without compression.
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_COMPRESSION_THRESHOLD
Default Compression Threshold.
|
Constructor and Description |
---|
CompressibleByteArrayOutputStream()
Creates a new CompressibleByteArrayOutputStream with the default compression threshold of 1024 bytes.
|
CompressibleByteArrayOutputStream(int pcompressionThreshold)
Creates a new CompressibleByteArrayOutputStream.
|
CompressibleByteArrayOutputStream(int size,
int pcompressionThreshold)
Creates a new CompressibleByteArrayOutputStream.
|
Modifier and Type | Method and Description |
---|---|
boolean |
clearCompressionInfo() |
void |
close() |
double |
getAbandonCompressionRatio()
Gets the abandon compression ratio.
|
byte[] |
getBuf()
Gets the underlying buffer.
|
String |
getCompressionInfo()
Returns the current compression info as a String.
|
int |
getCompressionThreshold()
Gets the compression threshold.
|
int |
getCount()
Low level method to get the count of the data in the buffer.
|
int |
getCountAtSessionStart()
Low level method to get the count at the start of the current compression session.
|
PrintStream |
getPrintStream(String charSet)
Gets a printStream that writes to this Stream corresponding to a given Charset.
|
boolean |
isAbandonCompressionEnabled()
Checks if abandon compression is enabled.
|
boolean |
isCompressed()
Checks if compression is currently in effect.
|
boolean |
isCompressionEnabled()
Checks if compression is enabled.
|
void |
reset()
Resets the stream by clearing any existing data.
|
void |
setAbandonCompressionEnabled(boolean pabandonCompressionEnabled)
Sets if abandon compression should be enabled.
|
void |
setAbandonCompressionRatio(double ratio)
Sets the abandon compression ratio.
|
void |
setCompressionEnabled(boolean pcompressionEnabled)
Sets if compression is enabled.
|
void |
setCompressionThreshold(int pcompressionThreshold)
Sets the compression threshold.
|
void |
setCount(int pcount)
Low level method to set the count in the buffer.
|
void |
write(byte[] b,
int off,
int len) |
void |
write(int b) |
void |
writeTo(OutputStream op)
Writes the data in the Stream to an OutputStream
|
flush, write
public static final int DEFAULT_COMPRESSION_THRESHOLD
public CompressibleByteArrayOutputStream(int pcompressionThreshold)
pcompressionThreshold
- Compression Threshold in bytespublic CompressibleByteArrayOutputStream(int size, int pcompressionThreshold)
size
- Initial buffer capacitypcompressionThreshold
- Compression Threshold in bytespublic CompressibleByteArrayOutputStream()
public void reset()
public PrintStream getPrintStream(String charSet)
charSet
- CharSet to usepublic void write(byte[] b, int off, int len)
write
in class OutputStream
public void write(int b)
write
in class OutputStream
public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
close
in class OutputStream
IOException
public void writeTo(OutputStream op) throws IOException
op
- OutputStream to write the data toIOException
- In case of an error during writeIllegalStateException
- if compression is currently in effect on the streampublic boolean isCompressed()
public int getCount()
public void setCount(int pcount)
pcount
- New countIllegalStateException
- if compression is currently in effect on the streampublic byte[] getBuf()
public int getCompressionThreshold()
public void setCompressionThreshold(int pcompressionThreshold)
pcompressionThreshold
- New Compression Thresholdpublic boolean isCompressionEnabled()
public String getCompressionInfo()
public boolean clearCompressionInfo()
public void setCompressionEnabled(boolean pcompressionEnabled)
pcompressionEnabled
- whether compression should be enabledpublic boolean isAbandonCompressionEnabled()
public void setAbandonCompressionEnabled(boolean pabandonCompressionEnabled)
pabandonCompressionEnabled
- true if abandon compression should be enabled, false otherwise.public int getCountAtSessionStart()
public double getAbandonCompressionRatio()
public void setAbandonCompressionRatio(double ratio)
ratio
- New Abandon Compression ratioCopyright © 2013- Suresh Mahalingam. All Rights Reserved.