Package org.xerial.snappy
Class Snappy
- java.lang.Object
-
- org.xerial.snappy.Snappy
-
public class Snappy extends java.lang.ObjectSnappy API for data compression/decompression Note: if the native libraries cannot be loaded, an ExceptionInInitializerError will be thrown at first use of this class.
-
-
Constructor Summary
Constructors Constructor Description Snappy()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidarrayCopy(java.lang.Object src, int offset, int byteLength, java.lang.Object dest, int dest_offset)Copy bytes from source to destinationstatic voidcleanUp()Clean up a temporary file (native lib) generated by snappy-java.static byte[]compress(byte[] input)High-level API for compressing the input byte array.static intcompress(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset)Compress the input buffer content in [inputOffset, ...inputOffset+inputLength) then output to the specified output buffer.static byte[]compress(char[] input)Compress the input char arraystatic byte[]compress(double[] input)Compress the input double arraystatic byte[]compress(float[] input)Compress the input float arraystatic byte[]compress(int[] input)Compress the input int arraystatic byte[]compress(long[] input)Compress the input long arraystatic byte[]compress(short[] input)Compress the input short arraystatic byte[]compress(java.lang.String s)Compress the input Stringstatic byte[]compress(java.lang.String s, java.lang.String encoding)Compress the input string using the given encodingstatic byte[]compress(java.lang.String s, java.nio.charset.Charset encoding)Compress the input string using the given encodingstatic intcompress(java.nio.ByteBuffer uncompressed, java.nio.ByteBuffer compressed)Compress the content in the given input buffer.static java.lang.StringgetNativeLibraryVersion()Get the native library version of the snappystatic booleanisValidCompressedBuffer(byte[] input)Returns true iff the contents of compressed buffer [offset, offset+length) can be uncompressed successfully.static booleanisValidCompressedBuffer(byte[] input, int offset, int length)Returns true iff the contents of compressed buffer [offset, offset+length) can be uncompressed successfully.static booleanisValidCompressedBuffer(long inputAddr, long offset, long length)Returns true iff the contents of compressed buffer [offset, offset+length) can be uncompressed successfully.static booleanisValidCompressedBuffer(java.nio.ByteBuffer compressed)Returns true iff the contents of compressed buffer [pos() ...static intmaxCompressedLength(int byteSize)Get the maximum byte size needed for compressing data of the given byte size.static longrawCompress(long inputAddr, long inputSize, long destAddr)Zero-copy compress using memory addresses.static byte[]rawCompress(java.lang.Object data, int byteSize)Compress the input data and produce a byte array of the uncompressed datastatic intrawCompress(java.lang.Object input, int inputOffset, int inputLength, byte[] output, int outputOffset)Compress the input buffer [offset,...static intrawUncompress(byte[] input, int inputOffset, int inputLength, java.lang.Object output, int outputOffset)Uncompress the content in the input buffer.static longrawUncompress(long inputAddr, long inputSize, long destAddr)Zero-copy decompress using memory addresses.static byte[]uncompress(byte[] input)High-level API for uncompressing the input byte array.static intuncompress(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset)Uncompress the content in the input buffer.static intuncompress(java.nio.ByteBuffer compressed, java.nio.ByteBuffer uncompressed)Uncompress the content in the input buffer.static char[]uncompressCharArray(byte[] input)Uncompress the input data as char arraystatic char[]uncompressCharArray(byte[] input, int offset, int length)Uncompress the input[offset, .., offset+length) as a char arraystatic double[]uncompressDoubleArray(byte[] input)Uncompress the input as a double arraystatic double[]uncompressDoubleArray(byte[] input, int offset, int length)Uncompress the input as a double arraystatic intuncompressedLength(byte[] input)Get the uncompressed byte size of the given compressed input.static intuncompressedLength(byte[] input, int offset, int length)Get the uncompressed byte size of the given compressed input.static longuncompressedLength(long inputAddr, long len)Get the uncompressed byte size of the given compressed input.static intuncompressedLength(java.nio.ByteBuffer compressed)Get the uncompressed byte size of the given compressed input.static float[]uncompressFloatArray(byte[] input)Uncompress the input as a float arraystatic float[]uncompressFloatArray(byte[] input, int offset, int length)Uncompress the input[offset, offset+length) as a float arraystatic int[]uncompressIntArray(byte[] input)Uncompress the input data as an int arraystatic int[]uncompressIntArray(byte[] input, int offset, int length)Uncompress the input[offset, offset+length) as an int arraystatic long[]uncompressLongArray(byte[] input)Uncompress the input data as a long arraystatic long[]uncompressLongArray(byte[] input, int offset, int length)Uncompress the input[offset, offset+length) as a long arraystatic short[]uncompressShortArray(byte[] input)Uncompress the input as a short arraystatic short[]uncompressShortArray(byte[] input, int offset, int length)Uncompress the input[offset, offset+length) as a short arraystatic java.lang.StringuncompressString(byte[] input)Uncompress the input as a Stringstatic java.lang.StringuncompressString(byte[] input, int offset, int length)Uncompress the input[offset, offset+length) as a Stringstatic java.lang.StringuncompressString(byte[] input, int offset, int length, java.lang.String encoding)Uncompress the input[offset, offset+length) as a String of the given encodingstatic java.lang.StringuncompressString(byte[] input, int offset, int length, java.nio.charset.Charset encoding)Uncompress the input[offset, offset+length) as a String of the given encodingstatic java.lang.StringuncompressString(byte[] input, java.lang.String encoding)Uncompress the input as a String of the given encodingstatic java.lang.StringuncompressString(byte[] input, java.nio.charset.Charset encoding)Uncompress the input as a String of the given encoding
-
-
-
Method Detail
-
cleanUp
public static void cleanUp()
Clean up a temporary file (native lib) generated by snappy-java. General users do not need to call this method, since the native library extracted in snappy-java is deleted upon JVM termination (vie deleteOnExit()). This method is useful when using a J2EE container, which will restart servlet containers multiple times without restarting JVM.
-
arrayCopy
public static void arrayCopy(java.lang.Object src, int offset, int byteLength, java.lang.Object dest, int dest_offset) throws java.io.IOExceptionCopy bytes from source to destination- Parameters:
src- pointer to the source arrayoffset- byte offset in the source arraybyteLength- the number of bytes to copydest- pointer to the destination arraydest_offset- byte offset in the destination array- Throws:
java.io.IOException
-
compress
public static byte[] compress(byte[] input) throws java.io.IOExceptionHigh-level API for compressing the input byte array. This method performs array copy to generate the result. If you want to reduce the memory copy cost, usecompress(byte[], int, int, byte[], int)orcompress(ByteBuffer, ByteBuffer).- Parameters:
input- the input data- Returns:
- the compressed byte array
- Throws:
java.io.IOException
-
compress
public static int compress(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset) throws java.io.IOExceptionCompress the input buffer content in [inputOffset, ...inputOffset+inputLength) then output to the specified output buffer.- Parameters:
input-inputOffset-inputLength-output-outputOffset-- Returns:
- byte size of the compressed data
- Throws:
java.io.IOException- when failed to access the input/output buffer
-
compress
public static int compress(java.nio.ByteBuffer uncompressed, java.nio.ByteBuffer compressed) throws java.io.IOExceptionCompress the content in the given input buffer. After the compression, you can retrieve the compressed data from the output buffer [pos() ... limit()) (compressed data size = limit() - pos() = remaining())- Parameters:
uncompressed- buffer[pos() ... limit()) containing the input datacompressed- output of the compressed data. Uses range [pos()..].- Returns:
- byte size of the compressed data.
- Throws:
SnappyError- when the input is not a direct bufferjava.io.IOException
-
compress
public static byte[] compress(char[] input) throws java.io.IOExceptionCompress the input char array- Parameters:
input-- Returns:
- the compressed data
- Throws:
java.io.IOException
-
compress
public static byte[] compress(double[] input) throws java.io.IOExceptionCompress the input double array- Parameters:
input-- Returns:
- the compressed data
- Throws:
java.io.IOException
-
compress
public static byte[] compress(float[] input) throws java.io.IOExceptionCompress the input float array- Parameters:
input-- Returns:
- the compressed data
- Throws:
java.io.IOException
-
compress
public static byte[] compress(int[] input) throws java.io.IOExceptionCompress the input int array- Parameters:
input-- Returns:
- the compressed data
- Throws:
java.io.IOException
-
compress
public static byte[] compress(long[] input) throws java.io.IOExceptionCompress the input long array- Parameters:
input-- Returns:
- the compressed data
- Throws:
java.io.IOException
-
compress
public static byte[] compress(short[] input) throws java.io.IOExceptionCompress the input short array- Parameters:
input-- Returns:
- the compressed data
- Throws:
java.io.IOException
-
compress
public static byte[] compress(java.lang.String s) throws java.io.IOExceptionCompress the input String- Parameters:
s-- Returns:
- the compressed data
- Throws:
java.io.IOException
-
compress
public static byte[] compress(java.lang.String s, java.lang.String encoding) throws java.io.UnsupportedEncodingException, java.io.IOExceptionCompress the input string using the given encoding- Parameters:
s-encoding-- Returns:
- the compressed data
- Throws:
java.io.UnsupportedEncodingExceptionjava.io.IOException
-
compress
public static byte[] compress(java.lang.String s, java.nio.charset.Charset encoding) throws java.io.IOExceptionCompress the input string using the given encoding- Parameters:
s-encoding-- Returns:
- the compressed data
- Throws:
java.io.UnsupportedEncodingExceptionjava.io.IOException
-
getNativeLibraryVersion
public static java.lang.String getNativeLibraryVersion()
Get the native library version of the snappy- Returns:
- native library version
-
isValidCompressedBuffer
public static boolean isValidCompressedBuffer(byte[] input, int offset, int length) throws java.io.IOExceptionReturns true iff the contents of compressed buffer [offset, offset+length) can be uncompressed successfully. Does not return the uncompressed data. Takes time proportional to the input length, but is usually at least a factor of four faster than actual decompression.- Throws:
java.io.IOException
-
isValidCompressedBuffer
public static boolean isValidCompressedBuffer(byte[] input) throws java.io.IOExceptionReturns true iff the contents of compressed buffer [offset, offset+length) can be uncompressed successfully. Does not return the uncompressed data. Takes time proportional to the input length, but is usually at least a factor of four faster than actual decompression.- Throws:
java.io.IOException
-
isValidCompressedBuffer
public static boolean isValidCompressedBuffer(java.nio.ByteBuffer compressed) throws java.io.IOExceptionReturns true iff the contents of compressed buffer [pos() ... limit()) can be uncompressed successfully. Does not return the uncompressed data. Takes time proportional to the input length, but is usually at least a factor of four faster than actual decompression.- Throws:
java.io.IOException
-
isValidCompressedBuffer
public static boolean isValidCompressedBuffer(long inputAddr, long offset, long length) throws java.io.IOExceptionReturns true iff the contents of compressed buffer [offset, offset+length) can be uncompressed successfully. Does not return the uncompressed data. Takes time proportional to the input length, but is usually at least a factor of four faster than actual decompression.- Throws:
java.io.IOException
-
maxCompressedLength
public static int maxCompressedLength(int byteSize)
Get the maximum byte size needed for compressing data of the given byte size.- Parameters:
byteSize- byte size of the data to compress- Returns:
- maximum byte size of the compressed data
-
rawCompress
public static long rawCompress(long inputAddr, long inputSize, long destAddr) throws java.io.IOExceptionZero-copy compress using memory addresses.- Parameters:
inputAddr- input memory addressinputSize- input byte sizedestAddr- destination address of the compressed data- Returns:
- the compressed data size
- Throws:
java.io.IOException
-
rawUncompress
public static long rawUncompress(long inputAddr, long inputSize, long destAddr) throws java.io.IOExceptionZero-copy decompress using memory addresses.- Parameters:
inputAddr- input memory addressinputSize- input byte sizedestAddr- destination address of the uncompressed data- Returns:
- the uncompressed data size
- Throws:
java.io.IOException
-
rawCompress
public static byte[] rawCompress(java.lang.Object data, int byteSize) throws java.io.IOExceptionCompress the input data and produce a byte array of the uncompressed data- Parameters:
data- input array. The input MUST be an array typebyteSize- the input byte size- Returns:
- compressed data
- Throws:
java.io.IOException
-
rawCompress
public static int rawCompress(java.lang.Object input, int inputOffset, int inputLength, byte[] output, int outputOffset) throws java.io.IOExceptionCompress the input buffer [offset,... ,offset+length) contents, then write the compressed data to the output buffer[offset, ...)- Parameters:
input- input array. This MUST be a primitive array typeinputOffset- byte offset at the output arrayinputLength- byte length of the input dataoutput- output array. This MUST be a primitive array typeoutputOffset- byte offset at the output array- Returns:
- byte size of the compressed data
- Throws:
java.io.IOException
-
rawUncompress
public static int rawUncompress(byte[] input, int inputOffset, int inputLength, java.lang.Object output, int outputOffset) throws java.io.IOExceptionUncompress the content in the input buffer. The uncompressed data is written to the output buffer. Note that if you pass the wrong data or the range [inputOffset, inputOffset + inputLength) that cannot be uncompressed, your JVM might crash due to the access violation exception issued in the native code written in C++. To avoid this type of crash, useisValidCompressedBuffer(byte[], int, int)first.- Parameters:
input- input byte arrayinputOffset- byte offset in the input byte arrayinputLength- byte length of the input dataoutput- output buffer, MUST be a primitive type arrayoutputOffset- byte offset in the output buffer- Returns:
- the byte size of the uncompressed data
- Throws:
java.io.IOException- when failed to uncompress the input data
-
uncompress
public static byte[] uncompress(byte[] input) throws java.io.IOExceptionHigh-level API for uncompressing the input byte array.- Parameters:
input-- Returns:
- the uncompressed byte array
- Throws:
java.io.IOException
-
uncompress
public static int uncompress(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset) throws java.io.IOExceptionUncompress the content in the input buffer. The uncompressed data is written to the output buffer. Note that if you pass the wrong data or the range [inputOffset, inputOffset + inputLength) that cannot be uncompressed, your JVM might crash due to the access violation exception issued in the native code written in C++. To avoid this type of crash, useisValidCompressedBuffer(byte[], int, int)first.- Parameters:
input-inputOffset-inputLength-output-outputOffset-- Returns:
- the byte size of the uncompressed data
- Throws:
java.io.IOException
-
uncompress
public static int uncompress(java.nio.ByteBuffer compressed, java.nio.ByteBuffer uncompressed) throws java.io.IOExceptionUncompress the content in the input buffer. The result is dumped to the specified output buffer. Note that if you pass the wrong data or the range [pos(), limit()) that cannot be uncompressed, your JVM might crash due to the access violation exception issued in the native code written in C++. To avoid this type of crash, useisValidCompressedBuffer(ByteBuffer)first.- Parameters:
compressed- buffer[pos() ... limit()) containing the input datauncompressed- output of the the uncompressed data. It uses buffer[pos()..]- Returns:
- uncompressed data size
- Throws:
java.io.IOException- when failed to uncompress the given inputSnappyError- when the input is not a direct buffer
-
uncompressCharArray
public static char[] uncompressCharArray(byte[] input) throws java.io.IOExceptionUncompress the input data as char array- Parameters:
input-- Returns:
- the uncompressed data
- Throws:
java.io.IOException
-
uncompressCharArray
public static char[] uncompressCharArray(byte[] input, int offset, int length) throws java.io.IOExceptionUncompress the input[offset, .., offset+length) as a char array- Parameters:
input-offset-length-- Returns:
- the uncompressed data
- Throws:
java.io.IOException
-
uncompressDoubleArray
public static double[] uncompressDoubleArray(byte[] input) throws java.io.IOExceptionUncompress the input as a double array- Parameters:
input-- Returns:
- the uncompressed data
- Throws:
java.io.IOException
-
uncompressDoubleArray
public static double[] uncompressDoubleArray(byte[] input, int offset, int length) throws java.io.IOExceptionUncompress the input as a double array- Parameters:
input-offset-length-- Returns:
- the uncompressed data
- Throws:
java.io.IOException
-
uncompressedLength
public static int uncompressedLength(byte[] input) throws java.io.IOExceptionGet the uncompressed byte size of the given compressed input. This operation takes O(1) time.- Parameters:
input-- Returns:
- uncompressed byte size of the the given input data
- Throws:
java.io.IOException- when failed to uncompress the given input. The error code isSnappyErrorCode.PARSING_ERROR
-
uncompressedLength
public static int uncompressedLength(byte[] input, int offset, int length) throws java.io.IOExceptionGet the uncompressed byte size of the given compressed input. This operation takes O(1) time.- Parameters:
input-offset-length-- Returns:
- uncompressed byte size of the the given input data
- Throws:
java.io.IOException- when failed to uncompress the given input. The error code isSnappyErrorCode.PARSING_ERROR
-
uncompressedLength
public static int uncompressedLength(java.nio.ByteBuffer compressed) throws java.io.IOExceptionGet the uncompressed byte size of the given compressed input. This operation takes O(1) time.- Parameters:
compressed- input data [pos() ... limit())- Returns:
- uncompressed byte length of the given input
- Throws:
java.io.IOException- when failed to uncompress the given input. The error code isSnappyErrorCode.PARSING_ERRORSnappyError- when the input is not a direct buffer
-
uncompressedLength
public static long uncompressedLength(long inputAddr, long len) throws java.io.IOExceptionGet the uncompressed byte size of the given compressed input. This operation takes O(1) time.- Parameters:
inputAddr- compressed data addresslen- byte length of the input- Returns:
- uncompressed byte length of the given input
- Throws:
java.io.IOException- when failed to uncompress the given input. The error code isSnappyErrorCode.PARSING_ERROR
-
uncompressFloatArray
public static float[] uncompressFloatArray(byte[] input) throws java.io.IOExceptionUncompress the input as a float array- Parameters:
input-- Returns:
- the uncompressed data
- Throws:
java.io.IOException
-
uncompressFloatArray
public static float[] uncompressFloatArray(byte[] input, int offset, int length) throws java.io.IOExceptionUncompress the input[offset, offset+length) as a float array- Parameters:
input-offset-length-- Returns:
- the uncompressed data
- Throws:
java.io.IOException
-
uncompressIntArray
public static int[] uncompressIntArray(byte[] input) throws java.io.IOExceptionUncompress the input data as an int array- Parameters:
input-- Returns:
- the uncompressed data
- Throws:
java.io.IOException
-
uncompressIntArray
public static int[] uncompressIntArray(byte[] input, int offset, int length) throws java.io.IOExceptionUncompress the input[offset, offset+length) as an int array- Parameters:
input-offset-length-- Returns:
- the uncompressed data
- Throws:
java.io.IOException
-
uncompressLongArray
public static long[] uncompressLongArray(byte[] input) throws java.io.IOExceptionUncompress the input data as a long array- Parameters:
input-- Returns:
- the uncompressed data
- Throws:
java.io.IOException
-
uncompressLongArray
public static long[] uncompressLongArray(byte[] input, int offset, int length) throws java.io.IOExceptionUncompress the input[offset, offset+length) as a long array- Parameters:
input-offset-length-- Returns:
- the uncompressed data
- Throws:
java.io.IOException
-
uncompressShortArray
public static short[] uncompressShortArray(byte[] input) throws java.io.IOExceptionUncompress the input as a short array- Parameters:
input-- Returns:
- the uncompressed data
- Throws:
java.io.IOException
-
uncompressShortArray
public static short[] uncompressShortArray(byte[] input, int offset, int length) throws java.io.IOExceptionUncompress the input[offset, offset+length) as a short array- Parameters:
input-offset-length-- Returns:
- the uncompressed data
- Throws:
java.io.IOException
-
uncompressString
public static java.lang.String uncompressString(byte[] input) throws java.io.IOExceptionUncompress the input as a String- Parameters:
input-- Returns:
- the uncompressed dasta
- Throws:
java.io.IOException
-
uncompressString
public static java.lang.String uncompressString(byte[] input, int offset, int length) throws java.io.IOExceptionUncompress the input[offset, offset+length) as a String- Parameters:
input-offset-length-- Returns:
- the uncompressed data
- Throws:
java.io.IOException
-
uncompressString
public static java.lang.String uncompressString(byte[] input, int offset, int length, java.lang.String encoding) throws java.io.IOException, java.io.UnsupportedEncodingExceptionUncompress the input[offset, offset+length) as a String of the given encoding- Parameters:
input-offset-length-encoding-- Returns:
- the uncompressed data
- Throws:
java.io.IOExceptionjava.io.UnsupportedEncodingException
-
uncompressString
public static java.lang.String uncompressString(byte[] input, int offset, int length, java.nio.charset.Charset encoding) throws java.io.IOException, java.io.UnsupportedEncodingExceptionUncompress the input[offset, offset+length) as a String of the given encoding- Parameters:
input-offset-length-encoding-- Returns:
- the uncompressed data
- Throws:
java.io.IOExceptionjava.io.UnsupportedEncodingException
-
uncompressString
public static java.lang.String uncompressString(byte[] input, java.lang.String encoding) throws java.io.IOException, java.io.UnsupportedEncodingExceptionUncompress the input as a String of the given encoding- Parameters:
input-encoding-- Returns:
- the uncompressed data
- Throws:
java.io.IOExceptionjava.io.UnsupportedEncodingException
-
uncompressString
public static java.lang.String uncompressString(byte[] input, java.nio.charset.Charset encoding) throws java.io.IOException, java.io.UnsupportedEncodingExceptionUncompress the input as a String of the given encoding- Parameters:
input-encoding-- Returns:
- the uncompressed data
- Throws:
java.io.IOExceptionjava.io.UnsupportedEncodingException
-
-