Top Description Fields Constructors Methods
org.xerial.snappy

public Class Snappy

extends Object
Class Inheritance
Imports
java.io.IOException, .InputStream, .UnsupportedEncodingException, java.net.URL, java.nio.Buffer, .ByteBuffer, java.nio.charset.Charset, java.util.Properties

Snappy API for data compression/decompression

Note

if the native libraries cannot be loaded, an ExceptionInInitializerError will be thrown at first use of this class.

Author
Taro L. Saito

Field Summary

Modifier and TypeField and Description
private static SnappyApi
impl

An instance of SnappyNative

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static void
arrayCopy(Object
pointer to the source array
src
,
int
byte offset in the source array
offset
,
int
the number of bytes to copy
byteLength
,
Object
pointer to the destination array
dest
,
int
byte offset in the destination array
dest_offset
)

Copy bytes from source to destination

public static void
cleanUp()

Clean up a temporary file (native lib) generated by snappy-java.

public static byte[]

Returns:

the compressed byte array
compress
(byte[]
the input data
input
)

High-level API for compressing the input byte array.

public static int

Returns:

byte size of the compressed data
compress
(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.

public static int

Returns:

byte size of the compressed data.
compress
(ByteBuffer
buffer[pos() ... limit()) containing the input data
uncompressed
,
ByteBuffer
output of the compressed data. Uses range [pos()..].
compressed
)

Compress the content in the given input buffer.

public static byte[]

Returns:

the compressed data
compress
(char[] input)

Compress the input char array

public static byte[]

Returns:

the compressed data
compress
(double[] input)

Compress the input double array

public static byte[]

Returns:

the compressed data
compress
(float[] input)

Compress the input float array

public static byte[]

Returns:

the compressed data
compress
(int[] input)

Compress the input int array

public static byte[]

Returns:

the compressed data
compress
(long[] input)

Compress the input long array

public static byte[]

Returns:

the compressed data
compress
(short[] input)

Compress the input short array

public static byte[]

Returns:

the compressed data
compress
(String s)

Compress the input String

public static byte[]

Returns:

the compressed data
compress
(String s, String encoding)

Compress the input string using the given encoding

public static byte[]

Returns:

the compressed data
compress
(String s, Charset encoding)

Compress the input string using the given encoding

public static String

Returns:

native library version
getNativeLibraryVersion
()

Get the native library version of the snappy

pack-priv static void
init()

public static boolean
isValidCompressedBuffer(byte[] input, int offset, int length)

Returns true iff the contents of compressed buffer [offset, offset+length) can be uncompressed successfully.

public static boolean
isValidCompressedBuffer(byte[] input)

Returns true iff the contents of compressed buffer [offset, offset+length) can be uncompressed successfully.

public static boolean
isValidCompressedBuffer(ByteBuffer compressed)

Returns true iff the contents of compressed buffer [pos() ...

public static boolean
isValidCompressedBuffer(long inputAddr, long offset, long length)

Returns true iff the contents of compressed buffer [offset, offset+length) can be uncompressed successfully.

public static int

Returns:

maximum byte size of the compressed data
maxCompressedLength
(int
byte size of the data to compress
byteSize
)

Get the maximum byte size needed for compressing data of the given byte size.

public static long

Returns:

the compressed data size
rawCompress
(long
input memory address
inputAddr
,
long
input byte size
inputSize
,
long
destination address of the compressed data
destAddr
)

Zero-copy compress using memory addresses.

public static byte[]

Returns:

compressed data
rawCompress
(Object
input array. The input MUST be an array type
data
,
int
the input byte size
byteSize
)

Compress the input data and produce a byte array of the uncompressed data

public static int

Returns:

byte size of the compressed data
rawCompress
(Object
input array. This MUST be a primitive array type
input
,
int
byte offset at the output array
inputOffset
,
int
byte length of the input data
inputLength
,
byte[]
output array. This MUST be a primitive array type
output
,
int
byte offset at the output array
outputOffset
)

Compress the input buffer [offset,...

public static long

Returns:

the uncompressed data size
rawUncompress
(long
input memory address
inputAddr
,
long
input byte size
inputSize
,
long
destination address of the uncompressed data
destAddr
)

Zero-copy decompress using memory addresses.

public static int

Returns:

the byte size of the uncompressed data
rawUncompress
(byte[]
input byte array
input
,
int
byte offset in the input byte array
inputOffset
,
int
byte length of the input data
inputLength
,
Object
output buffer, MUST be a primitive type array
output
,
int
byte offset in the output buffer
outputOffset
)

Uncompress the content in the input buffer.

public static byte[]

Returns:

the uncompressed byte array
uncompress
(byte[] input)

High-level API for uncompressing the input byte array.

public static int

Returns:

the byte size of the uncompressed data
uncompress
(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset)

Uncompress the content in the input buffer.

public static int

Returns:

uncompressed data size
uncompress
(ByteBuffer
buffer[pos() ... limit()) containing the input data
compressed
,
ByteBuffer
output of the the uncompressed data. It uses buffer[pos()..]
uncompressed
)

Uncompress the content in the input buffer.

public static char[]

Returns:

the uncompressed data
uncompressCharArray
(byte[] input)

Uncompress the input data as char array

public static char[]

Returns:

the uncompressed data
uncompressCharArray
(byte[] input, int offset, int length)

Uncompress the input[offset, .., offset+length) as a char array

public static double[]

Returns:

the uncompressed data
uncompressDoubleArray
(byte[] input)

Uncompress the input as a double array

public static double[]

Returns:

the uncompressed data
uncompressDoubleArray
(byte[] input, int offset, int length)

Uncompress the input as a double array

public static int

Returns:

uncompressed byte size of the the given input data
uncompressedLength
(byte[] input)

Get the uncompressed byte size of the given compressed input.

public static int

Returns:

uncompressed byte size of the the given input data
uncompressedLength
(byte[] input, int offset, int length)

Get the uncompressed byte size of the given compressed input.

public static int

Returns:

uncompressed byte length of the given input
uncompressedLength
(ByteBuffer
input data [pos() ... limit())
compressed
)

Get the uncompressed byte size of the given compressed input.

public static long

Returns:

uncompressed byte length of the given input
uncompressedLength
(long
compressed data address
inputAddr
,
long
byte length of the input
len
)

Get the uncompressed byte size of the given compressed input.

public static float[]

Returns:

the uncompressed data
uncompressFloatArray
(byte[] input)

Uncompress the input as a float array

public static float[]

Returns:

the uncompressed data
uncompressFloatArray
(byte[] input, int offset, int length)

Uncompress the input[offset, offset+length) as a float array

public static int[]

Returns:

the uncompressed data
uncompressIntArray
(byte[] input)

Uncompress the input data as an int array

public static int[]

Returns:

the uncompressed data
uncompressIntArray
(byte[] input, int offset, int length)

Uncompress the input[offset, offset+length) as an int array

public static long[]

Returns:

the uncompressed data
uncompressLongArray
(byte[] input)

Uncompress the input data as a long array

public static long[]

Returns:

the uncompressed data
uncompressLongArray
(byte[] input, int offset, int length)

Uncompress the input[offset, offset+length) as a long array

public static short[]

Returns:

the uncompressed data
uncompressShortArray
(byte[] input)

Uncompress the input as a short array

public static short[]

Returns:

the uncompressed data
uncompressShortArray
(byte[] input, int offset, int length)

Uncompress the input[offset, offset+length) as a short array

public static String

Returns:

the uncompressed dasta
uncompressString
(byte[] input)

Uncompress the input as a String

public static String

Returns:

the uncompressed data
uncompressString
(byte[] input, int offset, int length)

Uncompress the input[offset, offset+length) as a String

public static String

Returns:

the uncompressed data
uncompressString
(byte[] input, int offset, int length, String encoding)

Uncompress the input[offset, offset+length) as a String of the given encoding

public static String

Returns:

the uncompressed data
uncompressString
(byte[] input, int offset, int length, Charset encoding)

Uncompress the input[offset, offset+length) as a String of the given encoding

public static String

Returns:

the uncompressed data
uncompressString
(byte[] input, String encoding)

Uncompress the input as a String of the given encoding

public static String

Returns:

the uncompressed data
uncompressString
(byte[] input, Charset encoding)

Uncompress the input as a String of the given encoding

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

implback to summary
private static SnappyApi impl

An instance of SnappyNative

Constructor Detail

Snappyback to summary
public Snappy()

Method Detail

arrayCopyback to summary
public static void arrayCopy(Object src, int offset, int byteLength, Object dest, int dest_offset) throws IOException

Copy bytes from source to destination

Parameters
src:Object

pointer to the source array

offset:int

byte offset in the source array

byteLength:int

the number of bytes to copy

dest:Object

pointer to the destination array

dest_offset:int

byte offset in the destination array

cleanUpback to summary
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.

compressback to summary
public static byte[] compress(byte[] input) throws IOException

High-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, use compress(byte[], int, int, byte[], int) or compress(ByteBuffer, ByteBuffer).

Parameters
input:byte[]

the input data

Returns:byte[]

the compressed byte array

compressback to summary
public static int compress(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset) throws IOException

Compress the input buffer content in [inputOffset, ...inputOffset+inputLength) then output to the specified output buffer.

Returns:int

byte size of the compressed data

Exceptions
IOException:
when failed to access the input/output buffer
compressback to summary
public static int compress(ByteBuffer uncompressed, ByteBuffer compressed) throws IOException

Compress 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:ByteBuffer

buffer[pos() ... limit()) containing the input data

compressed:ByteBuffer

output of the compressed data. Uses range [pos()..].

Returns:int

byte size of the compressed data.

Exceptions
SnappyError:
when the input is not a direct buffer
compressback to summary
public static byte[] compress(char[] input) throws IOException

Compress the input char array

Returns:byte[]

the compressed data

compressback to summary
public static byte[] compress(double[] input) throws IOException

Compress the input double array

Returns:byte[]

the compressed data

compressback to summary
public static byte[] compress(float[] input) throws IOException

Compress the input float array

Returns:byte[]

the compressed data

compressback to summary
public static byte[] compress(int[] input) throws IOException

Compress the input int array

Returns:byte[]

the compressed data

compressback to summary
public static byte[] compress(long[] input) throws IOException

Compress the input long array

Returns:byte[]

the compressed data

compressback to summary
public static byte[] compress(short[] input) throws IOException

Compress the input short array

Returns:byte[]

the compressed data

compressback to summary
public static byte[] compress(String s) throws IOException

Compress the input String

Returns:byte[]

the compressed data

compressback to summary
public static byte[] compress(String s, String encoding) throws UnsupportedEncodingException, IOException

Compress the input string using the given encoding

Returns:byte[]

the compressed data

compressback to summary
public static byte[] compress(String s, Charset encoding) throws IOException

Compress the input string using the given encoding

Returns:byte[]

the compressed data

getNativeLibraryVersionback to summary
public static String getNativeLibraryVersion()

Get the native library version of the snappy

Returns:String

native library version

initback to summary
pack-priv static void init()
isValidCompressedBufferback to summary
public static boolean isValidCompressedBuffer(byte[] input, int offset, int length) throws IOException

Returns 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.

isValidCompressedBufferback to summary
public static boolean isValidCompressedBuffer(byte[] input) throws IOException

Returns 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.

isValidCompressedBufferback to summary
public static boolean isValidCompressedBuffer(ByteBuffer compressed) throws IOException

Returns 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.

isValidCompressedBufferback to summary
public static boolean isValidCompressedBuffer(long inputAddr, long offset, long length) throws IOException

Returns 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.

maxCompressedLengthback to summary
public static int maxCompressedLength(int byteSize)

Get the maximum byte size needed for compressing data of the given byte size.

Parameters
byteSize:int

byte size of the data to compress

Returns:int

maximum byte size of the compressed data

rawCompressback to summary
public static long rawCompress(long inputAddr, long inputSize, long destAddr) throws IOException

Zero-copy compress using memory addresses.

Parameters
inputAddr:long

input memory address

inputSize:long

input byte size

destAddr:long

destination address of the compressed data

Returns:long

the compressed data size

rawCompressback to summary
public static byte[] rawCompress(Object data, int byteSize) throws IOException

Compress the input data and produce a byte array of the uncompressed data

Parameters
data:Object

input array. The input MUST be an array type

byteSize:int

the input byte size

Returns:byte[]

compressed data

rawCompressback to summary
public static int rawCompress(Object input, int inputOffset, int inputLength, byte[] output, int outputOffset) throws IOException

Compress the input buffer [offset,... ,offset+length) contents, then write the compressed data to the output buffer[offset, ...)

Parameters
input:Object

input array. This MUST be a primitive array type

inputOffset:int

byte offset at the output array

inputLength:int

byte length of the input data

output:byte[]

output array. This MUST be a primitive array type

outputOffset:int

byte offset at the output array

Returns:int

byte size of the compressed data

rawUncompressback to summary
public static long rawUncompress(long inputAddr, long inputSize, long destAddr) throws IOException

Zero-copy decompress using memory addresses.

Parameters
inputAddr:long

input memory address

inputSize:long

input byte size

destAddr:long

destination address of the uncompressed data

Returns:long

the uncompressed data size

rawUncompressback to summary
public static int rawUncompress(byte[] input, int inputOffset, int inputLength, Object output, int outputOffset) throws IOException

Uncompress 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, use isValidCompressedBuffer(byte[], int, int) first.

Parameters
input:byte[]

input byte array

inputOffset:int

byte offset in the input byte array

inputLength:int

byte length of the input data

output:Object

output buffer, MUST be a primitive type array

outputOffset:int

byte offset in the output buffer

Returns:int

the byte size of the uncompressed data

Exceptions
IOException:
when failed to uncompress the input data
uncompressback to summary
public static byte[] uncompress(byte[] input) throws IOException

High-level API for uncompressing the input byte array.

Returns:byte[]

the uncompressed byte array

uncompressback to summary
public static int uncompress(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset) throws IOException

Uncompress 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, use isValidCompressedBuffer(byte[], int, int) first.

Returns:int

the byte size of the uncompressed data

uncompressback to summary
public static int uncompress(ByteBuffer compressed, ByteBuffer uncompressed) throws IOException

Uncompress 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, use isValidCompressedBuffer(ByteBuffer) first.

Parameters
compressed:ByteBuffer

buffer[pos() ... limit()) containing the input data

uncompressed:ByteBuffer

output of the the uncompressed data. It uses buffer[pos()..]

Returns:int

uncompressed data size

Exceptions
IOException:
when failed to uncompress the given input
SnappyError:
when the input is not a direct buffer
uncompressCharArrayback to summary
public static char[] uncompressCharArray(byte[] input) throws IOException

Uncompress the input data as char array

Returns:char[]

the uncompressed data

uncompressCharArrayback to summary
public static char[] uncompressCharArray(byte[] input, int offset, int length) throws IOException

Uncompress the input[offset, .., offset+length) as a char array

Returns:char[]

the uncompressed data

uncompressDoubleArrayback to summary
public static double[] uncompressDoubleArray(byte[] input) throws IOException

Uncompress the input as a double array

Returns:double[]

the uncompressed data

uncompressDoubleArrayback to summary
public static double[] uncompressDoubleArray(byte[] input, int offset, int length) throws IOException

Uncompress the input as a double array

Returns:double[]

the uncompressed data

uncompressedLengthback to summary
public static int uncompressedLength(byte[] input) throws IOException

Get the uncompressed byte size of the given compressed input. This operation takes O(1) time.

Returns:int

uncompressed byte size of the the given input data

Exceptions
IOException:
when failed to uncompress the given input. The error code is SnappyErrorCode#PARSING_ERROR
uncompressedLengthback to summary
public static int uncompressedLength(byte[] input, int offset, int length) throws IOException

Get the uncompressed byte size of the given compressed input. This operation takes O(1) time.

Returns:int

uncompressed byte size of the the given input data

Exceptions
IOException:
when failed to uncompress the given input. The error code is SnappyErrorCode#PARSING_ERROR
uncompressedLengthback to summary
public static int uncompressedLength(ByteBuffer compressed) throws IOException

Get the uncompressed byte size of the given compressed input. This operation takes O(1) time.

Parameters
compressed:ByteBuffer

input data [pos() ... limit())

Returns:int

uncompressed byte length of the given input

Exceptions
IOException:
when failed to uncompress the given input. The error code is SnappyErrorCode#PARSING_ERROR
SnappyError:
when the input is not a direct buffer
uncompressedLengthback to summary
public static long uncompressedLength(long inputAddr, long len) throws IOException

Get the uncompressed byte size of the given compressed input. This operation takes O(1) time.

Parameters
inputAddr:long

compressed data address

len:long

byte length of the input

Returns:long

uncompressed byte length of the given input

Exceptions
IOException:
when failed to uncompress the given input. The error code is SnappyErrorCode#PARSING_ERROR
uncompressFloatArrayback to summary
public static float[] uncompressFloatArray(byte[] input) throws IOException

Uncompress the input as a float array

Returns:float[]

the uncompressed data

uncompressFloatArrayback to summary
public static float[] uncompressFloatArray(byte[] input, int offset, int length) throws IOException

Uncompress the input[offset, offset+length) as a float array

Returns:float[]

the uncompressed data

uncompressIntArrayback to summary
public static int[] uncompressIntArray(byte[] input) throws IOException

Uncompress the input data as an int array

Returns:int[]

the uncompressed data

uncompressIntArrayback to summary
public static int[] uncompressIntArray(byte[] input, int offset, int length) throws IOException

Uncompress the input[offset, offset+length) as an int array

Returns:int[]

the uncompressed data

uncompressLongArrayback to summary
public static long[] uncompressLongArray(byte[] input) throws IOException

Uncompress the input data as a long array

Returns:long[]

the uncompressed data

uncompressLongArrayback to summary
public static long[] uncompressLongArray(byte[] input, int offset, int length) throws IOException

Uncompress the input[offset, offset+length) as a long array

Returns:long[]

the uncompressed data

uncompressShortArrayback to summary
public static short[] uncompressShortArray(byte[] input) throws IOException

Uncompress the input as a short array

Returns:short[]

the uncompressed data

uncompressShortArrayback to summary
public static short[] uncompressShortArray(byte[] input, int offset, int length) throws IOException

Uncompress the input[offset, offset+length) as a short array

Returns:short[]

the uncompressed data

uncompressStringback to summary
public static String uncompressString(byte[] input) throws IOException

Uncompress the input as a String

Returns:String

the uncompressed dasta

uncompressStringback to summary
public static String uncompressString(byte[] input, int offset, int length) throws IOException

Uncompress the input[offset, offset+length) as a String

Returns:String

the uncompressed data

uncompressStringback to summary
public static String uncompressString(byte[] input, int offset, int length, String encoding) throws IOException, UnsupportedEncodingException

Uncompress the input[offset, offset+length) as a String of the given encoding

Returns:String

the uncompressed data

uncompressStringback to summary
public static String uncompressString(byte[] input, int offset, int length, Charset encoding) throws IOException, UnsupportedEncodingException

Uncompress the input[offset, offset+length) as a String of the given encoding

Returns:String

the uncompressed data

uncompressStringback to summary
public static String uncompressString(byte[] input, String encoding) throws IOException, UnsupportedEncodingException

Uncompress the input as a String of the given encoding

Returns:String

the uncompressed data

uncompressStringback to summary
public static String uncompressString(byte[] input, Charset encoding) throws IOException, UnsupportedEncodingException

Uncompress the input as a String of the given encoding

Returns:String

the uncompressed data