This commit is contained in:
Taro L. Saito 2015-05-18 16:51:53 +09:00
parent 02c14db79e
commit 66115a49ad
29 changed files with 1926 additions and 1491 deletions

View File

@ -32,7 +32,6 @@ import java.util.Locale;
* Provides OS name and architecture name.
*
* @author leo
*
*/
public class OSInfo
{
@ -83,8 +82,8 @@ public class OSInfo
archMapping.put("power_rs64", PPC64);
}
public static void main(String[] args) {
public static void main(String[] args)
{
if (args.length >= 1) {
if ("--os".equals(args[0])) {
System.out.print(getOSName());
@ -99,15 +98,18 @@ public class OSInfo
System.out.print(getNativeLibFolderPathForCurrentOS());
}
public static String getNativeLibFolderPathForCurrentOS() {
public static String getNativeLibFolderPathForCurrentOS()
{
return getOSName() + "/" + getArchName();
}
public static String getOSName() {
public static String getOSName()
{
return translateOSNameToFolderName(System.getProperty("os.name"));
}
public static String getArchName() {
public static String getArchName()
{
// if running Linux on ARM, need to determine ABI of JVM
String osArch = System.getProperty("os.arch");
if (osArch.startsWith("arm") && System.getProperty("os.name").contains("Linux")) {
@ -118,9 +120,10 @@ public class OSInfo
"' -name 'libjvm.so' | head -1 | xargs readelf -A | " +
"grep 'Tag_ABI_VFP_args: VFP registers'"};
int exitCode = Runtime.getRuntime().exec(cmdarray).waitFor();
if (exitCode == 0)
if (exitCode == 0) {
return "armhf";
}
}
catch (IOException e) {
// ignored: fall back to "arm" arch (soft-float ABI)
}
@ -130,13 +133,15 @@ public class OSInfo
}
else {
String lc = osArch.toLowerCase(Locale.US);
if(archMapping.containsKey(lc))
if (archMapping.containsKey(lc)) {
return archMapping.get(lc);
}
}
return translateArchNameToFolderName(osArch);
}
static String translateOSNameToFolderName(String osName) {
static String translateOSNameToFolderName(String osName)
{
if (osName.contains("Windows")) {
return "Windows";
}
@ -155,7 +160,8 @@ public class OSInfo
}
}
static String translateArchNameToFolderName(String archName) {
static String translateArchNameToFolderName(String archName)
{
return archName.replaceAll("\\W", "");
}
}

View File

@ -6,15 +6,15 @@
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* <p/>
* Some portions of this file Copyright (c) 2004-2006 Intel Corportation
* and licensed under the BSD license.
*/
@ -27,33 +27,40 @@ import java.util.zip.Checksum;
* the CRC32-C polynomial, the same polynomial used by iSCSI
* and implemented on many Intel chipsets supporting SSE4.2.
*/
public class PureJavaCrc32C implements Checksum {
public class PureJavaCrc32C
implements Checksum
{
/** the current CRC value, bit-flipped */
private int crc;
/** Create a new PureJavaCrc32 object. */
public PureJavaCrc32C() {
public PureJavaCrc32C()
{
reset();
}
public int getIntegerValue() {
public int getIntegerValue()
{
return ~crc;
}
/** {@inheritDoc} */
public long getValue() {
public long getValue()
{
long ret = crc;
return (~ret) & 0xffffffffL;
}
/** {@inheritDoc} */
public void reset() {
public void reset()
{
crc = 0xffffffff;
}
/** {@inheritDoc} */
public void update(byte[] b, int off, int len) {
public void update(byte[] b, int off, int len)
{
int localCrc = crc;
while (len > 7) {
int c0 = b[off++] ^ localCrc;
@ -78,7 +85,8 @@ public class PureJavaCrc32C implements Checksum {
}
/** {@inheritDoc} */
final public void update(int b) {
final public void update(int b)
{
crc = (crc >>> 8) ^ T8_0[(crc ^ b) & 0xff];
}

View File

@ -33,12 +33,11 @@ import java.util.Properties;
/**
* Snappy API for data compression/decompression
*
* <p/>
* Note: if the native libraries cannot be loaded, an ExceptionInInitializerError
* will be thrown at first use of this class.
*
* @author Taro L. Saito
*
*/
public class Snappy
{
@ -56,7 +55,6 @@ public class Snappy
*/
private static SnappyNative impl;
/**
* 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
@ -64,29 +62,25 @@ public class Snappy
* This method is useful when using a J2EE container, which will restart servlet containers multiple times without
* restarting JVM.
*/
public static void cleanUp() {
public static void cleanUp()
{
SnappyLoader.cleanUpExtractedNativeLib();
SnappyLoader.setApi(null);
}
/**
* Copy bytes from source to destination
*
* @param src
* pointer to the source array
* @param offset
* byte offset in the source array
* @param byteLength
* the number of bytes to copy
* @param dest
* pointer to the destination array
* @param dest_offset
* byte offset in the destination array
* @param src pointer to the source array
* @param offset byte offset in the source array
* @param byteLength the number of bytes to copy
* @param dest pointer to the destination array
* @param dest_offset byte offset in the destination array
* @throws IOException
*/
public static void arrayCopy(Object src, int offset, int byteLength, Object dest, int dest_offset)
throws IOException {
throws IOException
{
impl.arrayCopy(src, offset, byteLength, dest, dest_offset);
}
@ -96,12 +90,13 @@ public class Snappy
* cost, use {@link #compress(byte[], int, int, byte[], int)} or
* {@link #compress(ByteBuffer, ByteBuffer)}.
*
* @param input
* the input data
* @param input the input data
* @return the compressed byte array
* @throws IOException
*/
public static byte[] compress(byte[] input) throws IOException {
public static byte[] compress(byte[] input)
throws IOException
{
return rawCompress(input, input.length);
}
@ -115,11 +110,11 @@ public class Snappy
* @param output
* @param outputOffset
* @return byte size of the compressed data
* @throws IOException
* when failed to access the input/output buffer
* @throws IOException when failed to access the input/output buffer
*/
public static int compress(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset)
throws IOException {
throws IOException
{
return rawCompress(input, inputOffset, inputLength, output, outputOffset);
}
@ -128,21 +123,21 @@ public class Snappy
* you can retrieve the compressed data from the output buffer [pos() ...
* limit()) (compressed data size = limit() - pos() = remaining())
*
* @param uncompressed
* buffer[pos() ... limit()) containing the input data
* @param compressed
* output of the compressed data. Uses range [pos()..].
* @param uncompressed buffer[pos() ... limit()) containing the input data
* @param compressed output of the compressed data. Uses range [pos()..].
* @return byte size of the compressed data.
*
* @throws SnappyError
* when the input is not a direct buffer
* @throws SnappyError when the input is not a direct buffer
*/
public static int compress(ByteBuffer uncompressed, ByteBuffer compressed) throws IOException {
public static int compress(ByteBuffer uncompressed, ByteBuffer compressed)
throws IOException
{
if (!uncompressed.isDirect())
if (!uncompressed.isDirect()) {
throw new SnappyError(SnappyErrorCode.NOT_A_DIRECT_BUFFER, "input is not a direct buffer");
if (!compressed.isDirect())
}
if (!compressed.isDirect()) {
throw new SnappyError(SnappyErrorCode.NOT_A_DIRECT_BUFFER, "destination is not a direct buffer");
}
// input: uncompressed[pos(), limit())
// output: compressed
@ -164,7 +159,9 @@ public class Snappy
* @param input
* @return the compressed data
*/
public static byte[] compress(char[] input) throws IOException {
public static byte[] compress(char[] input)
throws IOException
{
return rawCompress(input, input.length * 2); // char uses 2 bytes
}
@ -174,7 +171,9 @@ public class Snappy
* @param input
* @return the compressed data
*/
public static byte[] compress(double[] input) throws IOException {
public static byte[] compress(double[] input)
throws IOException
{
return rawCompress(input, input.length * 8); // double uses 8 bytes
}
@ -184,7 +183,9 @@ public class Snappy
* @param input
* @return the compressed data
*/
public static byte[] compress(float[] input) throws IOException {
public static byte[] compress(float[] input)
throws IOException
{
return rawCompress(input, input.length * 4); // float uses 4 bytes
}
@ -194,7 +195,9 @@ public class Snappy
* @param input
* @return the compressed data
*/
public static byte[] compress(int[] input) throws IOException {
public static byte[] compress(int[] input)
throws IOException
{
return rawCompress(input, input.length * 4); // int uses 4 bytes
}
@ -204,7 +207,9 @@ public class Snappy
* @param input
* @return the compressed data
*/
public static byte[] compress(long[] input) throws IOException {
public static byte[] compress(long[] input)
throws IOException
{
return rawCompress(input, input.length * 8); // long uses 8 bytes
}
@ -214,7 +219,9 @@ public class Snappy
* @param input
* @return the compressed data
*/
public static byte[] compress(short[] input) throws IOException {
public static byte[] compress(short[] input)
throws IOException
{
return rawCompress(input, input.length * 2); // short uses 2 bytes
}
@ -225,7 +232,9 @@ public class Snappy
* @return the compressed data
* @throws IOException
*/
public static byte[] compress(String s) throws IOException {
public static byte[] compress(String s)
throws IOException
{
try {
return compress(s, "UTF-8");
}
@ -243,7 +252,9 @@ public class Snappy
* @throws UnsupportedEncodingException
* @throws IOException
*/
public static byte[] compress(String s, String encoding) throws UnsupportedEncodingException, IOException {
public static byte[] compress(String s, String encoding)
throws UnsupportedEncodingException, IOException
{
byte[] data = s.getBytes(encoding);
return compress(data);
}
@ -257,7 +268,9 @@ public class Snappy
* @throws UnsupportedEncodingException
* @throws IOException
*/
public static byte[] compress(String s, Charset encoding) throws IOException {
public static byte[] compress(String s, Charset encoding)
throws IOException
{
byte[] data = s.getBytes(encoding);
return compress(data);
}
@ -267,7 +280,8 @@ public class Snappy
*
* @return native library version
*/
public static String getNativeLibraryVersion() {
public static String getNativeLibraryVersion()
{
URL versionFile = SnappyLoader.class.getResource("/org/xerial/snappy/VERSION");
@ -277,8 +291,9 @@ public class Snappy
Properties versionData = new Properties();
versionData.load(versionFile.openStream());
version = versionData.getProperty("version", version);
if (version.equals("unknown"))
if (version.equals("unknown")) {
version = versionData.getProperty("VERSION", version);
}
version = version.trim().replaceAll("[^0-9\\.]", "");
}
}
@ -294,9 +309,12 @@ public class Snappy
* uncompressed data. Takes time proportional to the input length, but is
* usually at least a factor of four faster than actual decompression.
*/
public static boolean isValidCompressedBuffer(byte[] input, int offset, int length) throws IOException {
if (input == null)
public static boolean isValidCompressedBuffer(byte[] input, int offset, int length)
throws IOException
{
if (input == null) {
throw new NullPointerException("input is null");
}
return impl.isValidCompressedBuffer(input, offset, length);
}
@ -306,7 +324,9 @@ public class Snappy
* uncompressed data. Takes time proportional to the input length, but is
* usually at least a factor of four faster than actual decompression.
*/
public static boolean isValidCompressedBuffer(byte[] input) throws IOException {
public static boolean isValidCompressedBuffer(byte[] input)
throws IOException
{
return isValidCompressedBuffer(input, 0, input.length);
}
@ -316,7 +336,9 @@ public class Snappy
* Takes time proportional to the input length, but is usually at least a
* factor of four faster than actual decompression.
*/
public static boolean isValidCompressedBuffer(ByteBuffer compressed) throws IOException {
public static boolean isValidCompressedBuffer(ByteBuffer compressed)
throws IOException
{
return impl.isValidCompressedBuffer(compressed, compressed.position(),
compressed.remaining());
}
@ -327,58 +349,64 @@ public class Snappy
* uncompressed data. Takes time proportional to the input length, but is
* usually at least a factor of four faster than actual decompression.
*/
public static boolean isValidCompressedBuffer(long inputAddr, long offset, long length) throws IOException {
public static boolean isValidCompressedBuffer(long inputAddr, long offset, long length)
throws IOException
{
return impl.isValidCompressedBuffer(inputAddr, offset, length);
}
/**
* Get the maximum byte size needed for compressing data of the given byte
* size.
*
* @param byteSize
* byte size of the data to compress
* @param byteSize byte size of the data to compress
* @return maximum byte size of the compressed data
*/
public static int maxCompressedLength(int byteSize) {
public static int maxCompressedLength(int byteSize)
{
return impl.maxCompressedLength(byteSize);
}
/**
* Zero-copy compress using memory addresses.
*
* @param inputAddr input memory address
* @param inputSize input byte size
* @param destAddr destination address of the compressed data
* @return the compressed data size
* @throws IOException
*/
public static long rawCompress(long inputAddr, long inputSize, long destAddr) throws IOException {
public static long rawCompress(long inputAddr, long inputSize, long destAddr)
throws IOException
{
return impl.rawCompress(inputAddr, inputSize, destAddr);
}
/**
* Zero-copy decompress using memory addresses.
*
* @param inputAddr input memory address
* @param inputSize input byte size
* @param destAddr destination address of the uncompressed data
* @return the uncompressed data size
* @throws IOException
*/
public static long rawUncompress(long inputAddr, long inputSize, long destAddr) throws IOException {
public static long rawUncompress(long inputAddr, long inputSize, long destAddr)
throws IOException
{
return impl.rawUncompress(inputAddr, inputSize, destAddr);
}
/**
* Compress the input data and produce a byte array of the uncompressed data
*
* @param data
* input array. The input MUST be an array type
* @param byteSize
* the input byte size
* @param data input array. The input MUST be an array type
* @param byteSize the input byte size
* @return compressed data
*/
public static byte[] rawCompress(Object data, int byteSize) throws IOException {
public static byte[] rawCompress(Object data, int byteSize)
throws IOException
{
byte[] buf = new byte[Snappy.maxCompressedLength(byteSize)];
int compressedByteSize = impl.rawCompress(data, 0, byteSize, buf, 0);
byte[] result = new byte[compressedByteSize];
@ -390,23 +418,20 @@ public class Snappy
* Compress the input buffer [offset,... ,offset+length) contents, then
* write the compressed data to the output buffer[offset, ...)
*
* @param input
* input array. This MUST be a primitive array type
* @param inputOffset
* byte offset at the output array
* @param inputLength
* byte length of the input data
* @param output
* output array. This MUST be a primitive array type
* @param outputOffset
* byte offset at the output array
* @param input input array. This MUST be a primitive array type
* @param inputOffset byte offset at the output array
* @param inputLength byte length of the input data
* @param output output array. This MUST be a primitive array type
* @param outputOffset byte offset at the output array
* @return byte size of the compressed data
* @throws IOException
*/
public static int rawCompress(Object input, int inputOffset, int inputLength, byte[] output, int outputOffset)
throws IOException {
if (input == null || output == null)
throws IOException
{
if (input == null || output == null) {
throw new NullPointerException("input or output is null");
}
int compressedSize = impl
.rawCompress(input, inputOffset, inputLength, output, outputOffset);
@ -416,31 +441,27 @@ public class Snappy
/**
* Uncompress the content in the input buffer. The uncompressed data is
* written to the output buffer.
*
* <p/>
* 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
* {@link #isValidCompressedBuffer(byte[], int, int)} first.
*
* @param input
* input byte array
* @param inputOffset
* byte offset in the input byte array
* @param inputLength
* byte length of the input data
* @param output
* output buffer, MUST be a primitive type array
* @param outputOffset
* byte offset in the output buffer
* @param input input byte array
* @param inputOffset byte offset in the input byte array
* @param inputLength byte length of the input data
* @param output output buffer, MUST be a primitive type array
* @param outputOffset byte offset in the output buffer
* @return the byte size of the uncompressed data
* @throws IOException
* when failed to uncompress the input data
* @throws IOException when failed to uncompress the input data
*/
public static int rawUncompress(byte[] input, int inputOffset, int inputLength, Object output, int outputOffset)
throws IOException {
if (input == null || output == null)
throws IOException
{
if (input == null || output == null) {
throw new NullPointerException("input or output is null");
}
return impl.rawUncompress(input, inputOffset, inputLength, output, outputOffset);
}
@ -451,7 +472,9 @@ public class Snappy
* @return the uncompressed byte array
* @throws IOException
*/
public static byte[] uncompress(byte[] input) throws IOException {
public static byte[] uncompress(byte[] input)
throws IOException
{
byte[] result = new byte[Snappy.uncompressedLength(input)];
int byteSize = Snappy.uncompress(input, 0, input.length, result, 0);
return result;
@ -460,7 +483,7 @@ public class Snappy
/**
* Uncompress the content in the input buffer. The uncompressed data is
* written to the output buffer.
*
* <p/>
* 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
@ -476,37 +499,36 @@ public class Snappy
* @throws IOException
*/
public static int uncompress(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset)
throws IOException {
throws IOException
{
return rawUncompress(input, inputOffset, inputLength, output, outputOffset);
}
/**
* Uncompress the content in the input buffer. The result is dumped to the
* specified output buffer.
*
* <p/>
* 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 {@link #isValidCompressedBuffer(ByteBuffer)} first.
*
*
* @param compressed
* buffer[pos() ... limit()) containing the input data
* @param uncompressed
* output of the the uncompressed data. It uses buffer[pos()..]
* @param compressed buffer[pos() ... limit()) containing the input data
* @param uncompressed output of the the uncompressed data. It uses buffer[pos()..]
* @return uncompressed data size
*
* @throws IOException
* when failed to uncompress the given input
* @throws SnappyError
* when the input is not a direct buffer
* @throws IOException when failed to uncompress the given input
* @throws SnappyError when the input is not a direct buffer
*/
public static int uncompress(ByteBuffer compressed, ByteBuffer uncompressed) throws IOException {
public static int uncompress(ByteBuffer compressed, ByteBuffer uncompressed)
throws IOException
{
if (!compressed.isDirect())
if (!compressed.isDirect()) {
throw new SnappyError(SnappyErrorCode.NOT_A_DIRECT_BUFFER, "input is not a direct buffer");
if (!uncompressed.isDirect())
}
if (!uncompressed.isDirect()) {
throw new SnappyError(SnappyErrorCode.NOT_A_DIRECT_BUFFER, "destination is not a direct buffer");
}
int cPos = compressed.position();
int cLen = compressed.remaining();
@ -527,7 +549,9 @@ public class Snappy
* @return the uncompressed data
* @throws IOException
*/
public static char[] uncompressCharArray(byte[] input) throws IOException {
public static char[] uncompressCharArray(byte[] input)
throws IOException
{
return uncompressCharArray(input, 0, input.length);
}
@ -540,7 +564,9 @@ public class Snappy
* @return the uncompressed data
* @throws IOException
*/
public static char[] uncompressCharArray(byte[] input, int offset, int length) throws IOException {
public static char[] uncompressCharArray(byte[] input, int offset, int length)
throws IOException
{
int uncompressedLength = Snappy.uncompressedLength(input, offset, length);
char[] result = new char[uncompressedLength / 2];
int byteSize = impl.rawUncompress(input, offset, length, result, 0);
@ -554,7 +580,9 @@ public class Snappy
* @return the uncompressed data
* @throws IOException
*/
public static double[] uncompressDoubleArray(byte[] input) throws IOException {
public static double[] uncompressDoubleArray(byte[] input)
throws IOException
{
int uncompressedLength = Snappy.uncompressedLength(input, 0, input.length);
double[] result = new double[uncompressedLength / 8];
int byteSize = impl.rawUncompress(input, 0, input.length, result, 0);
@ -567,11 +595,12 @@ public class Snappy
*
* @param input
* @return uncompressed byte size of the the given input data
* @throws IOException
* when failed to uncompress the given input. The error code is
* @throws IOException when failed to uncompress the given input. The error code is
* {@link SnappyErrorCode#PARSING_ERROR}
*/
public static int uncompressedLength(byte[] input) throws IOException {
public static int uncompressedLength(byte[] input)
throws IOException
{
return impl.uncompressedLength(input, 0, input.length);
}
@ -583,13 +612,15 @@ public class Snappy
* @param offset
* @param length
* @return uncompressed byte size of the the given input data
* @throws IOException
* when failed to uncompress the given input. The error code is
* @throws IOException when failed to uncompress the given input. The error code is
* {@link SnappyErrorCode#PARSING_ERROR}
*/
public static int uncompressedLength(byte[] input, int offset, int length) throws IOException {
if (input == null)
public static int uncompressedLength(byte[] input, int offset, int length)
throws IOException
{
if (input == null) {
throw new NullPointerException("input is null");
}
return impl.uncompressedLength(input, offset, length);
}
@ -598,31 +629,34 @@ public class Snappy
* Get the uncompressed byte size of the given compressed input. This
* operation takes O(1) time.
*
* @param compressed
* input data [pos() ... limit())
* @param compressed input data [pos() ... limit())
* @return uncompressed byte length of the given input
* @throws IOException
* when failed to uncompress the given input. The error code is
* @throws IOException when failed to uncompress the given input. The error code is
* {@link SnappyErrorCode#PARSING_ERROR}
* @throws SnappyError
* when the input is not a direct buffer
* @throws SnappyError when the input is not a direct buffer
*/
public static int uncompressedLength(ByteBuffer compressed) throws IOException {
if (!compressed.isDirect())
public static int uncompressedLength(ByteBuffer compressed)
throws IOException
{
if (!compressed.isDirect()) {
throw new SnappyError(SnappyErrorCode.NOT_A_DIRECT_BUFFER, "input is not a direct buffer");
}
return impl.uncompressedLength(compressed, compressed.position(), compressed.remaining());
}
/**
* Get the uncompressed byte size of the given compressed input. This operation takes O(1) time.
*
* @param inputAddr compressed data address
* @param len byte length of the input
* @return uncompressed byte length of the given input
* @throws IOException when failed to uncompress the given input. The error code is
* {@link SnappyErrorCode#PARSING_ERROR}
*/
public static long uncompressedLength(long inputAddr, long len) throws IOException {
public static long uncompressedLength(long inputAddr, long len)
throws IOException
{
return impl.uncompressedLength(inputAddr, len);
}
@ -633,7 +667,9 @@ public class Snappy
* @return the uncompressed data
* @throws IOException
*/
public static float[] uncompressFloatArray(byte[] input) throws IOException {
public static float[] uncompressFloatArray(byte[] input)
throws IOException
{
return uncompressFloatArray(input, 0, input.length);
}
@ -646,7 +682,9 @@ public class Snappy
* @return the uncompressed data
* @throws IOException
*/
public static float[] uncompressFloatArray(byte[] input, int offset, int length) throws IOException {
public static float[] uncompressFloatArray(byte[] input, int offset, int length)
throws IOException
{
int uncompressedLength = Snappy.uncompressedLength(input, offset, length);
float[] result = new float[uncompressedLength / 4];
int byteSize = impl.rawUncompress(input, offset, length, result, 0);
@ -660,7 +698,9 @@ public class Snappy
* @return the uncompressed data
* @throws IOException
*/
public static int[] uncompressIntArray(byte[] input) throws IOException {
public static int[] uncompressIntArray(byte[] input)
throws IOException
{
return uncompressIntArray(input, 0, input.length);
}
@ -673,7 +713,9 @@ public class Snappy
* @return the uncompressed data
* @throws IOException
*/
public static int[] uncompressIntArray(byte[] input, int offset, int length) throws IOException {
public static int[] uncompressIntArray(byte[] input, int offset, int length)
throws IOException
{
int uncompressedLength = Snappy.uncompressedLength(input, offset, length);
int[] result = new int[uncompressedLength / 4];
int byteSize = impl.rawUncompress(input, offset, length, result, 0);
@ -687,7 +729,9 @@ public class Snappy
* @return the uncompressed data
* @throws IOException
*/
public static long[] uncompressLongArray(byte[] input) throws IOException {
public static long[] uncompressLongArray(byte[] input)
throws IOException
{
return uncompressLongArray(input, 0, input.length);
}
@ -700,7 +744,9 @@ public class Snappy
* @return the uncompressed data
* @throws IOException
*/
public static long[] uncompressLongArray(byte[] input, int offset, int length) throws IOException {
public static long[] uncompressLongArray(byte[] input, int offset, int length)
throws IOException
{
int uncompressedLength = Snappy.uncompressedLength(input, offset, length);
long[] result = new long[uncompressedLength / 8];
int byteSize = impl.rawUncompress(input, offset, length, result, 0);
@ -714,7 +760,9 @@ public class Snappy
* @return the uncompressed data
* @throws IOException
*/
public static short[] uncompressShortArray(byte[] input) throws IOException {
public static short[] uncompressShortArray(byte[] input)
throws IOException
{
return uncompressShortArray(input, 0, input.length);
}
@ -727,7 +775,9 @@ public class Snappy
* @return the uncompressed data
* @throws IOException
*/
public static short[] uncompressShortArray(byte[] input, int offset, int length) throws IOException {
public static short[] uncompressShortArray(byte[] input, int offset, int length)
throws IOException
{
int uncompressedLength = Snappy.uncompressedLength(input, offset, length);
short[] result = new short[uncompressedLength / 2];
int byteSize = impl.rawUncompress(input, offset, length, result, 0);
@ -741,7 +791,9 @@ public class Snappy
* @return the uncompressed dasta
* @throws IOException
*/
public static String uncompressString(byte[] input) throws IOException {
public static String uncompressString(byte[] input)
throws IOException
{
try {
return uncompressString(input, "UTF-8");
}
@ -759,7 +811,9 @@ public class Snappy
* @return the uncompressed data
* @throws IOException
*/
public static String uncompressString(byte[] input, int offset, int length) throws IOException {
public static String uncompressString(byte[] input, int offset, int length)
throws IOException
{
try {
return uncompressString(input, offset, length, "UTF-8");
}
@ -779,8 +833,10 @@ public class Snappy
* @return the uncompressed data
* @throws IOException
*/
public static String uncompressString(byte[] input, int offset, int length, String encoding) throws IOException,
UnsupportedEncodingException {
public static String uncompressString(byte[] input, int offset, int length, String encoding)
throws IOException,
UnsupportedEncodingException
{
byte[] uncompressed = new byte[uncompressedLength(input, offset, length)];
int compressedSize = uncompress(input, offset, length, uncompressed, 0);
return new String(uncompressed, encoding);
@ -797,8 +853,10 @@ public class Snappy
* @return the uncompressed data
* @throws IOException
*/
public static String uncompressString(byte[] input, int offset, int length, Charset encoding) throws IOException,
UnsupportedEncodingException {
public static String uncompressString(byte[] input, int offset, int length, Charset encoding)
throws IOException,
UnsupportedEncodingException
{
byte[] uncompressed = new byte[uncompressedLength(input, offset, length)];
int compressedSize = uncompress(input, offset, length, uncompressed, 0);
return new String(uncompressed, encoding);
@ -813,8 +871,10 @@ public class Snappy
* @throws IOException
* @throws UnsupportedEncodingException
*/
public static String uncompressString(byte[] input, String encoding) throws IOException,
UnsupportedEncodingException {
public static String uncompressString(byte[] input, String encoding)
throws IOException,
UnsupportedEncodingException
{
byte[] uncompressed = uncompress(input);
return new String(uncompressed, encoding);
}
@ -827,8 +887,10 @@ public class Snappy
* @return the uncompressed data
* @throws IOException
*/
public static String uncompressString(byte[] input, Charset encoding) throws IOException,
UnsupportedEncodingException {
public static String uncompressString(byte[] input, Charset encoding)
throws IOException,
UnsupportedEncodingException
{
byte[] uncompressed = uncompress(input);
return new String(uncompressed, encoding);
}

View File

@ -34,9 +34,9 @@ import java.util.jar.Manifest;
* OSGi bundle entry point
*
* @author leo
*
*/
public class SnappyBundleActivator implements BundleActivator
public class SnappyBundleActivator
implements BundleActivator
{
/**
* Name of the Snappy native library
@ -47,11 +47,11 @@ public class SnappyBundleActivator implements BundleActivator
* Make a call to {@link System#loadLibrary(String)} to load the native library which assumes
* that the library is available on the path based on this {@link Bundle}'s {@link Manifest}.
*/
public void start(BundleContext context) throws Exception
public void start(BundleContext context)
throws Exception
{
String library = System.mapLibraryName(LIBRARY_NAME);
if (library.toLowerCase().endsWith(".dylib"))
{
if (library.toLowerCase().endsWith(".dylib")) {
// some MacOS JDK7+ vendors map to dylib instead of jnilib
library = library.replace(".dylib", ".jnilib");
}
@ -59,7 +59,8 @@ public class SnappyBundleActivator implements BundleActivator
SnappyLoader.setApi(new SnappyNative());
}
public void stop(BundleContext context) throws Exception
public void stop(BundleContext context)
throws Exception
{
SnappyLoader.setApi(null);
SnappyLoader.cleanUpExtractedNativeLib();

View File

@ -34,18 +34,17 @@ import java.util.Arrays;
/**
* Preamble header for {@link SnappyOutputStream}.
*
* <p/>
* <p>
* The magic header is the following 8 bytes data:
*
* <p/>
* <pre>
* -126, 'S', 'N', 'A', 'P', 'P', 'Y', 0
* </pre>
*
* <p/>
* </p>
*
* @author leo
*
*/
public class SnappyCodec
{
@ -59,7 +58,6 @@ public class SnappyCodec
assert (MAGIC_HEADER_HEAD < 0);
}
public static final int DEFAULT_VERSION = 1;
public static final int MINIMUM_COMPATIBLE_VERSION = 1;
@ -68,7 +66,8 @@ public class SnappyCodec
public final int compatibleVersion;
private final byte[] headerArray;
private SnappyCodec(byte[] magic, int version, int compatibleVersion) {
private SnappyCodec(byte[] magic, int version, int compatibleVersion)
{
this.magic = magic;
this.version = version;
this.compatibleVersion = compatibleVersion;
@ -88,29 +87,37 @@ public class SnappyCodec
}
@Override
public String toString() {
public String toString()
{
return String.format("version:%d, compatible version:%d", version, compatibleVersion);
}
public static int headerSize() {
public static int headerSize()
{
return HEADER_SIZE;
}
public int writeHeader(byte[] dst, int dstOffset) {
public int writeHeader(byte[] dst, int dstOffset)
{
System.arraycopy(headerArray, 0, dst, dstOffset, headerArray.length);
return headerArray.length;
}
public int writeHeader(OutputStream out) throws IOException {
public int writeHeader(OutputStream out)
throws IOException
{
out.write(headerArray, 0, headerArray.length);
return headerArray.length;
}
public boolean isValidMagicHeader() {
public boolean isValidMagicHeader()
{
return Arrays.equals(MAGIC_HEADER, magic);
}
public static SnappyCodec readHeader(InputStream in) throws IOException {
public static SnappyCodec readHeader(InputStream in)
throws IOException
{
DataInputStream d = new DataInputStream(in);
byte[] magic = new byte[MAGIC_LEN];
d.readFully(magic, 0, MAGIC_LEN);
@ -120,5 +127,4 @@ public class SnappyCodec
}
public static SnappyCodec currentHeader = new SnappyCodec(MAGIC_HEADER, DEFAULT_VERSION, MINIMUM_COMPATIBLE_VERSION);
}

View File

@ -28,9 +28,9 @@ package org.xerial.snappy;
* Used when serious errors (unchecked exception) are observed.
*
* @author leo
*
*/
public class SnappyError extends Error
public class SnappyError
extends Error
{
/**
*
@ -39,24 +39,27 @@ public class SnappyError extends Error
public final SnappyErrorCode errorCode;
public SnappyError(SnappyErrorCode code) {
public SnappyError(SnappyErrorCode code)
{
super();
this.errorCode = code;
}
public SnappyError(SnappyErrorCode code, Error e) {
public SnappyError(SnappyErrorCode code, Error e)
{
super(e);
this.errorCode = code;
}
public SnappyError(SnappyErrorCode code, String message) {
public SnappyError(SnappyErrorCode code, String message)
{
super(message);
this.errorCode = code;
}
@Override
public String getMessage() {
public String getMessage()
{
return String.format("[%s] %s", errorCode.name(), super.getMessage());
}
}

View File

@ -28,9 +28,9 @@ package org.xerial.snappy;
* Error codes of snappy-java
*
* @author leo
*
*/
public enum SnappyErrorCode {
public enum SnappyErrorCode
{
// DO NOT change these error code IDs because these numbers are used inside SnappyNative.cpp
UNKNOWN(0),
@ -41,24 +41,27 @@ public enum SnappyErrorCode {
FAILED_TO_UNCOMPRESS(5),
EMPTY_INPUT(6),
INCOMPATIBLE_VERSION(7),
INVALID_CHUNK_SIZE(8)
;
INVALID_CHUNK_SIZE(8);
public final int id;
private SnappyErrorCode(int id) {
private SnappyErrorCode(int id)
{
this.id = id;
}
public static SnappyErrorCode getErrorCode(int id) {
public static SnappyErrorCode getErrorCode(int id)
{
for (SnappyErrorCode code : SnappyErrorCode.values()) {
if (code.id == id)
if (code.id == id) {
return code;
}
}
return UNKNOWN;
}
public static String getErrorMessage(int id) {
public static String getErrorMessage(int id)
{
return getErrorCode(id).name();
}
}

View File

@ -29,46 +29,54 @@ import java.io.IOException;
/**
* Exception in snappy-java
*
* @deprecated Snappy-java now uses {@link IOException}
* @author leo
*
* @deprecated Snappy-java now uses {@link IOException}
*/
@Deprecated
public class SnappyException extends Exception
public class SnappyException
extends Exception
{
private static final long serialVersionUID = 1L;
public final SnappyErrorCode errorCode;
public SnappyException(int code) {
public SnappyException(int code)
{
this(SnappyErrorCode.getErrorCode(code));
}
public SnappyException(SnappyErrorCode errorCode) {
public SnappyException(SnappyErrorCode errorCode)
{
super();
this.errorCode = errorCode;
}
public SnappyException(SnappyErrorCode errorCode, Exception e) {
public SnappyException(SnappyErrorCode errorCode, Exception e)
{
super(e);
this.errorCode = errorCode;
}
public SnappyException(SnappyErrorCode errorCode, String message) {
public SnappyException(SnappyErrorCode errorCode, String message)
{
super(message);
this.errorCode = errorCode;
}
public SnappyErrorCode getErrorCode() {
public SnappyErrorCode getErrorCode()
{
return errorCode;
}
public static void throwException(int errorCode) throws SnappyException {
public static void throwException(int errorCode)
throws SnappyException
{
throw new SnappyException(errorCode);
}
@Override
public String getMessage() {
public String getMessage()
{
return String.format("[%s] %s", errorCode.name(), super.getMessage());
}
}

View File

@ -16,7 +16,8 @@ import java.util.logging.Logger;
* @author Brett Okken
* @since 1.1.0
*/
final class SnappyFramed {
final class SnappyFramed
{
public static final int COMPRESSED_DATA_FLAG = 0x00;
public static final int UNCOMPRESSED_DATA_FLAG = 0x01;
@ -33,8 +34,7 @@ final class SnappyFramed {
private static final Method SUN_BUFFER_CLEANER;
private static final Method SUN_CLEANER_CLEAN;
static
{
static {
Method bufferCleaner = null;
Method cleanerClean = null;
try {
@ -45,7 +45,8 @@ final class SnappyFramed {
Class<?> cleanClazz = lookupClassQuietly("sun.misc.Cleaner");
cleanerClean = cleanClazz.getMethod("clean", (Class[]) null);
}
} catch(Throwable t) {
}
catch (Throwable t) {
Logger.getLogger(SnappyFramed.class.getName()).log(Level.FINE, "Exception occurred attempting to lookup Sun specific DirectByteBuffer cleaner classes.", t);
}
SUN_BUFFER_CLEANER = bufferCleaner;
@ -78,7 +79,7 @@ final class SnappyFramed {
* in Apache Hadoop: Rotate the checksum by 15 bits, then add the constant
* 0xa282ead8 (using wraparound as normal for unsigned integers). This is
* equivalent to the following C code:
*
* <p/>
* <pre>
* uint32_t mask_checksum(uint32_t x) {
* return ((x >> 15) | (x << 17)) + 0xa282ead8;
@ -91,8 +92,8 @@ final class SnappyFramed {
return ((crc >>> 15) | (crc << 17)) + MASK_DELTA;
}
static final int readBytes(ReadableByteChannel source, ByteBuffer dest) throws IOException
static final int readBytes(ReadableByteChannel source, ByteBuffer dest)
throws IOException
{
// tells how many bytes to read.
final int expectedLength = dest.remaining();
@ -105,34 +106,30 @@ final class SnappyFramed {
totalRead = lastRead;
// if we did not read as many bytes as we had hoped, try reading again.
if (lastRead < expectedLength)
{
if (lastRead < expectedLength) {
// as long the buffer is not full (remaining() == 0) and we have not reached EOF (lastRead == -1) keep reading.
while (dest.remaining() != 0 && lastRead != -1)
{
while (dest.remaining() != 0 && lastRead != -1) {
lastRead = source.read(dest);
// if we got EOF, do not add to total read.
if (lastRead != -1)
{
if (lastRead != -1) {
totalRead += lastRead;
}
}
}
if (totalRead > 0)
{
if (totalRead > 0) {
dest.limit(dest.position());
}
else
{
else {
dest.position(dest.limit());
}
return totalRead;
}
static int skip(final ReadableByteChannel source, final int skip, final ByteBuffer buffer) throws IOException
static int skip(final ReadableByteChannel source, final int skip, final ByteBuffer buffer)
throws IOException
{
if (skip <= 0) {
return 0;
@ -156,10 +153,12 @@ final class SnappyFramed {
return skip - toSkip;
}
private static Class<?> lookupClassQuietly(String name) {
private static Class<?> lookupClassQuietly(String name)
{
try {
return SnappyFramed.class.getClassLoader().loadClass(name);
} catch (Throwable t) {
}
catch (Throwable t) {
Logger.getLogger(SnappyFramed.class.getName()).log(Level.FINE, "Did not find requested class: " + name, t);
}
@ -168,6 +167,7 @@ final class SnappyFramed {
/**
* Provides jvm implementation specific operation to aggressively release resources associated with <i>buffer</i>.
*
* @param buffer The {@code ByteBuffer} to release. Must not be {@code null}. Must be {@link ByteBuffer#isDirect() direct}.
*/
static void releaseDirectByteBuffer(ByteBuffer buffer)
@ -178,7 +178,8 @@ final class SnappyFramed {
try {
Object cleaner = SUN_BUFFER_CLEANER.invoke(buffer, (Object[]) null);
SUN_CLEANER_CLEAN.invoke(cleaner, (Object[]) null);
} catch (Throwable t) {
}
catch (Throwable t) {
Logger.getLogger(SnappyFramed.class.getName()).log(Level.FINE, "Exception occurred attempting to clean up Sun specific DirectByteBuffer.", t);
}
}

View File

@ -32,8 +32,11 @@ import java.util.Arrays;
* @author Brett Okken
* @since 1.1.0
*/
public final class SnappyFramedInputStream extends InputStream implements
ReadableByteChannel {
public final class SnappyFramedInputStream
extends InputStream
implements
ReadableByteChannel
{
private final ReadableByteChannel rbc;
private final ByteBuffer frameHeader;
@ -78,10 +81,11 @@ public final class SnappyFramedInputStream extends InputStream implements
* Creates a Snappy input stream to read data from the specified underlying
* input stream.
*
* @param in
* the underlying input stream. Must not be {@code null}.
* @param in the underlying input stream. Must not be {@code null}.
*/
public SnappyFramedInputStream(InputStream in) throws IOException {
public SnappyFramedInputStream(InputStream in)
throws IOException
{
this(in, true);
}
@ -89,13 +93,12 @@ public final class SnappyFramedInputStream extends InputStream implements
* Creates a Snappy input stream to read data from the specified underlying
* input stream.
*
* @param in
* the underlying input stream. Must not be {@code null}.
* @param verifyChecksums
* if true, checksums in input stream will be verified
* @param in the underlying input stream. Must not be {@code null}.
* @param verifyChecksums if true, checksums in input stream will be verified
*/
public SnappyFramedInputStream(InputStream in, boolean verifyChecksums)
throws IOException {
throws IOException
{
this(Channels.newChannel(in), verifyChecksums);
}
@ -103,11 +106,11 @@ public final class SnappyFramedInputStream extends InputStream implements
* Creates a Snappy input stream to read data from the specified underlying
* channel.
*
* @param in
* the underlying readable channel. Must not be {@code null}.
* @param in the underlying readable channel. Must not be {@code null}.
*/
public SnappyFramedInputStream(ReadableByteChannel in)
throws IOException {
throws IOException
{
this(in, true);
}
@ -115,13 +118,13 @@ public final class SnappyFramedInputStream extends InputStream implements
* Creates a Snappy input stream to read data from the specified underlying
* channel.
*
* @param in
* the underlying readable channel. Must not be {@code null}.
* @param verifyChecksums
* if true, checksums in input stream will be verified
* @param in the underlying readable channel. Must not be {@code null}.
* @param verifyChecksums if true, checksums in input stream will be verified
*/
public SnappyFramedInputStream(ReadableByteChannel in,
boolean verifyChecksums) throws IOException {
boolean verifyChecksums)
throws IOException
{
if (in == null) {
throw new NullPointerException("in is null");
}
@ -150,7 +153,8 @@ public final class SnappyFramedInputStream extends InputStream implements
/**
* @param size
*/
private void allocateBuffersBasedOnSize(int size) {
private void allocateBuffersBasedOnSize(int size)
{
if (input != null) {
releaseDirectByteBuffer(input);
@ -167,7 +171,9 @@ public final class SnappyFramedInputStream extends InputStream implements
}
@Override
public int read() throws IOException {
public int read()
throws IOException
{
if (closed) {
return -1;
}
@ -178,7 +184,9 @@ public final class SnappyFramedInputStream extends InputStream implements
}
@Override
public int read(byte[] output, int offset, int length) throws IOException {
public int read(byte[] output, int offset, int length)
throws IOException
{
if (output == null) {
throw new IllegalArgumentException("output is null");
@ -207,7 +215,9 @@ public final class SnappyFramedInputStream extends InputStream implements
}
@Override
public int available() throws IOException {
public int available()
throws IOException
{
if (closed) {
return 0;
}
@ -218,7 +228,8 @@ public final class SnappyFramedInputStream extends InputStream implements
* {@inheritDoc}
*/
@Override
public boolean isOpen() {
public boolean isOpen()
{
return !closed;
}
@ -226,7 +237,9 @@ public final class SnappyFramedInputStream extends InputStream implements
* {@inheritDoc}
*/
@Override
public int read(ByteBuffer dst) throws IOException {
public int read(ByteBuffer dst)
throws IOException
{
if (dst == null) {
throw new IllegalArgumentException("dst is null");
@ -260,13 +273,14 @@ public final class SnappyFramedInputStream extends InputStream implements
* value of {@code 0}.
* </p>
*
* @param os
* The destination to write decompressed content to.
* @param os The destination to write decompressed content to.
* @return The number of bytes transferred.
* @throws IOException
* @since 1.1.1
*/
public long transferTo(OutputStream os) throws IOException {
public long transferTo(OutputStream os)
throws IOException
{
if (os == null) {
throw new IllegalArgumentException("os is null");
}
@ -291,7 +305,7 @@ public final class SnappyFramedInputStream extends InputStream implements
* Transfers the entire content of this {@link ReadableByteChannel} to
* <i>wbc</i>. This potentially limits the amount of buffering required to
* decompress content.
*
* <p/>
* <p>
* Unlike {@link #read(ByteBuffer)}, this method does not need to be called
* multiple times. A single call will transfer all available content. Any
@ -299,13 +313,14 @@ public final class SnappyFramedInputStream extends InputStream implements
* of {@code 0}.
* </p>
*
* @param wbc
* The destination to write decompressed content to.
* @param wbc The destination to write decompressed content to.
* @return The number of bytes transferred.
* @throws IOException
* @since 1.1.1
*/
public long transferTo(WritableByteChannel wbc) throws IOException {
public long transferTo(WritableByteChannel wbc)
throws IOException
{
if (wbc == null) {
throw new IllegalArgumentException("wbc is null");
}
@ -335,10 +350,13 @@ public final class SnappyFramedInputStream extends InputStream implements
}
@Override
public void close() throws IOException {
public void close()
throws IOException
{
try {
rbc.close();
} finally {
}
finally {
if (!closed) {
closed = true;
}
@ -353,11 +371,13 @@ public final class SnappyFramedInputStream extends InputStream implements
}
}
static enum FrameAction {
static enum FrameAction
{
RAW, SKIP, UNCOMPRESS;
}
public static final class FrameMetaData {
public static final class FrameMetaData
{
final int length;
final FrameAction frameAction;
@ -365,14 +385,16 @@ public final class SnappyFramedInputStream extends InputStream implements
* @param frameAction
* @param length
*/
public FrameMetaData(FrameAction frameAction, int length) {
public FrameMetaData(FrameAction frameAction, int length)
{
super();
this.frameAction = frameAction;
this.length = length;
}
}
public static final class FrameData {
public static final class FrameData
{
final int checkSum;
final int offset;
@ -380,14 +402,17 @@ public final class SnappyFramedInputStream extends InputStream implements
* @param checkSum
* @param offset
*/
public FrameData(int checkSum, int offset) {
public FrameData(int checkSum, int offset)
{
super();
this.checkSum = checkSum;
this.offset = offset;
}
}
private boolean ensureBuffer() throws IOException {
private boolean ensureBuffer()
throws IOException
{
if (available() > 0) {
return true;
}
@ -442,7 +467,8 @@ public final class SnappyFramedInputStream extends InputStream implements
uncompressedDirect.get(buffer, 0, valid);
this.position = 0;
} else {
}
else {
// we need to start reading at the offset
input.position(frameData.offset);
this.position = 0;
@ -461,7 +487,9 @@ public final class SnappyFramedInputStream extends InputStream implements
return true;
}
private boolean readBlockHeader() throws IOException {
private boolean readBlockHeader()
throws IOException
{
frameHeader.clear();
int read = readBytes(rbc, frameHeader);
@ -478,13 +506,13 @@ public final class SnappyFramedInputStream extends InputStream implements
}
/**
*
* @param frameHeader
* @return
* @throws IOException
*/
private FrameMetaData getFrameMetaData(ByteBuffer frameHeader)
throws IOException {
throws IOException
{
assert frameHeader.hasArray();
@ -537,16 +565,18 @@ public final class SnappyFramedInputStream extends InputStream implements
}
/**
*
* @param content
* @return
* @throws IOException
*/
private FrameData getFrameData(ByteBuffer content) throws IOException {
private FrameData getFrameData(ByteBuffer content)
throws IOException
{
return new FrameData(getCrc32c(content), 4);
}
private int getCrc32c(ByteBuffer content) {
private int getCrc32c(ByteBuffer content)
{
final int position = content.position();

View File

@ -28,8 +28,11 @@ import java.nio.channels.WritableByteChannel;
* @author Brett Okken
* @since 1.1.0
*/
public final class SnappyFramedOutputStream extends OutputStream implements
WritableByteChannel {
public final class SnappyFramedOutputStream
extends OutputStream
implements
WritableByteChannel
{
/**
* The x-snappy-framed specification allows for a chunk size up to
@ -69,32 +72,33 @@ public final class SnappyFramedOutputStream extends OutputStream implements
/**
* Creates a new {@link SnappyFramedOutputStream} using the {@link #DEFAULT_BLOCK_SIZE}
* and {@link #DEFAULT_MIN_COMPRESSION_RATIO}.
* @param out
* The underlying {@link OutputStream} to write to. Must not be
*
* @param out The underlying {@link OutputStream} to write to. Must not be
* {@code null}.
* @throws IOException
*/
public SnappyFramedOutputStream(OutputStream out) throws IOException {
public SnappyFramedOutputStream(OutputStream out)
throws IOException
{
this(out, DEFAULT_BLOCK_SIZE, DEFAULT_MIN_COMPRESSION_RATIO);
}
/**
* Creates a new {@link SnappyFramedOutputStream} instance.
*
* @param out
* The underlying {@link OutputStream} to write to. Must not be
* @param out The underlying {@link OutputStream} to write to. Must not be
* {@code null}.
* @param blockSize
* The block size (of raw data) to compress before writing frames
* @param blockSize The block size (of raw data) to compress before writing frames
* to <i>out</i>. Must be in (0, 65536].
* @param minCompressionRatio
* Defines the minimum compression ratio (
* @param minCompressionRatio Defines the minimum compression ratio (
* {@code compressedLength / rawLength}) that must be achieved to
* write the compressed data. This must be in (0, 1.0].
* @throws IOException
*/
public SnappyFramedOutputStream(OutputStream out, int blockSize,
double minCompressionRatio) throws IOException {
double minCompressionRatio)
throws IOException
{
this(Channels.newChannel(out), blockSize, minCompressionRatio);
}
@ -102,34 +106,34 @@ public final class SnappyFramedOutputStream extends OutputStream implements
* Creates a new {@link SnappyFramedOutputStream} using the
* {@link #DEFAULT_BLOCK_SIZE} and {@link #DEFAULT_MIN_COMPRESSION_RATIO}.
*
* @param out
* The underlying {@link WritableByteChannel} to write to. Must
* @param out The underlying {@link WritableByteChannel} to write to. Must
* not be {@code null}.
* @since 1.1.1
* @throws IOException
* @since 1.1.1
*/
public SnappyFramedOutputStream(WritableByteChannel out) throws IOException {
public SnappyFramedOutputStream(WritableByteChannel out)
throws IOException
{
this(out, DEFAULT_BLOCK_SIZE, DEFAULT_MIN_COMPRESSION_RATIO);
}
/**
* Creates a new {@link SnappyFramedOutputStream} instance.
*
* @param out
* The underlying {@link WritableByteChannel} to write to. Must
* @param out The underlying {@link WritableByteChannel} to write to. Must
* not be {@code null}.
* @param blockSize
* The block size (of raw data) to compress before writing frames
* @param blockSize The block size (of raw data) to compress before writing frames
* to <i>out</i>. Must be in (0, 65536].
* @param minCompressionRatio
* Defines the minimum compression ratio (
* @param minCompressionRatio Defines the minimum compression ratio (
* {@code compressedLength / rawLength}) that must be achieved to
* write the compressed data. This must be in (0, 1.0].
* @since 1.1.1
* @throws IOException
* @since 1.1.1
*/
public SnappyFramedOutputStream(WritableByteChannel out, int blockSize,
double minCompressionRatio) throws IOException {
double minCompressionRatio)
throws IOException
{
if (out == null) {
throw new NullPointerException();
}
@ -158,11 +162,12 @@ public final class SnappyFramedOutputStream extends OutputStream implements
* Writes the implementation specific header or "marker bytes" to
* <i>out</i>.
*
* @param out
* The underlying {@link OutputStream}.
* @param out The underlying {@link OutputStream}.
* @throws IOException
*/
private void writeHeader(WritableByteChannel out) throws IOException {
private void writeHeader(WritableByteChannel out)
throws IOException
{
out.write(ByteBuffer.wrap(HEADER_BYTES));
}
@ -170,12 +175,15 @@ public final class SnappyFramedOutputStream extends OutputStream implements
* {@inheritDoc}
*/
@Override
public boolean isOpen() {
public boolean isOpen()
{
return !closed;
}
@Override
public void write(int b) throws IOException {
public void write(int b)
throws IOException
{
if (closed) {
throw new IOException("Stream is closed");
}
@ -186,14 +194,17 @@ public final class SnappyFramedOutputStream extends OutputStream implements
}
@Override
public void write(byte[] input, int offset, int length) throws IOException {
public void write(byte[] input, int offset, int length)
throws IOException
{
if (closed) {
throw new IOException("Stream is closed");
}
if (input == null) {
throw new NullPointerException();
} else if ((offset < 0) || (offset > input.length) || (length < 0)
}
else if ((offset < 0) || (offset > input.length) || (length < 0)
|| ((offset + length) > input.length)
|| ((offset + length) < 0)) {
throw new IndexOutOfBoundsException();
@ -215,7 +226,9 @@ public final class SnappyFramedOutputStream extends OutputStream implements
* {@inheritDoc}
*/
@Override
public int write(ByteBuffer src) throws IOException {
public int write(ByteBuffer src)
throws IOException
{
if (closed) {
throw new ClosedChannelException();
}
@ -256,13 +269,14 @@ public final class SnappyFramedOutputStream extends OutputStream implements
* This potentially limits the amount of buffering required to compress
* content.
*
* @param is
* The source of data to compress.
* @param is The source of data to compress.
* @return The number of bytes read from <i>is</i>.
* @throws IOException
* @since 1.1.1
*/
public long transferFrom(InputStream is) throws IOException {
public long transferFrom(InputStream is)
throws IOException
{
if (closed) {
throw new ClosedChannelException();
}
@ -300,13 +314,14 @@ public final class SnappyFramedOutputStream extends OutputStream implements
* {@link WritableByteChannel}. This potentially limits the amount of
* buffering required to compress content.
*
* @param rbc
* The source of data to compress.
* @param rbc The source of data to compress.
* @return The number of bytes read from <i>rbc</i>.
* @throws IOException
* @since 1.1.1
*/
public long transferFrom(ReadableByteChannel rbc) throws IOException {
public long transferFrom(ReadableByteChannel rbc)
throws IOException
{
if (closed) {
throw new ClosedChannelException();
}
@ -333,7 +348,9 @@ public final class SnappyFramedOutputStream extends OutputStream implements
}
@Override
public final void flush() throws IOException {
public final void flush()
throws IOException
{
if (closed) {
throw new IOException("Stream is closed");
}
@ -341,14 +358,17 @@ public final class SnappyFramedOutputStream extends OutputStream implements
}
@Override
public final void close() throws IOException {
public final void close()
throws IOException
{
if (closed) {
return;
}
try {
flush();
out.close();
} finally {
}
finally {
closed = true;
releaseDirectByteBuffer(directInputBuffer);
@ -362,7 +382,9 @@ public final class SnappyFramedOutputStream extends OutputStream implements
*
* @throws IOException
*/
private void flushBuffer() throws IOException {
private void flushBuffer()
throws IOException
{
if (buffer.position() > 0) {
buffer.flip();
writeCompressed(buffer);
@ -377,10 +399,11 @@ public final class SnappyFramedOutputStream extends OutputStream implements
* actually write the frame.
*
* @param buffer
* @throws IOException
*/
private void writeCompressed(ByteBuffer buffer) throws IOException {
private void writeCompressed(ByteBuffer buffer)
throws IOException
{
final byte[] input = buffer.array();
final int length = buffer.remaining();
@ -401,7 +424,8 @@ public final class SnappyFramedOutputStream extends OutputStream implements
// minCompressonRatio
if (((double) compressedLength / (double) length) <= minCompressionRatio) {
writeBlock(out, outputBuffer, true, crc32c);
} else {
}
else {
// otherwise use the uncompressed data.
buffer.flip();
writeBlock(out, buffer, false, crc32c);
@ -411,20 +435,18 @@ public final class SnappyFramedOutputStream extends OutputStream implements
/**
* Write a frame (block) to <i>out</i>.
*
* @param out
* The {@link OutputStream} to write to.
* @param data
* The data to write.
* @param compressed
* Indicates if <i>data</i> is the compressed or raw content.
* @param out The {@link OutputStream} to write to.
* @param data The data to write.
* @param compressed Indicates if <i>data</i> is the compressed or raw content.
* This is based on whether the compression ratio desired is
* reached.
* @param crc32c
* The calculated checksum.
* @param crc32c The calculated checksum.
* @throws IOException
*/
private void writeBlock(final WritableByteChannel out, ByteBuffer data,
boolean compressed, int crc32c) throws IOException {
boolean compressed, int crc32c)
throws IOException
{
headerBuffer.clear();
headerBuffer.put((byte) (compressed ? COMPRESSED_DATA_FLAG

View File

@ -5,19 +5,22 @@ import java.io.IOException;
/**
* Enhanced IOException with SnappyErrorCode
*/
public class SnappyIOException extends IOException {
public class SnappyIOException
extends IOException
{
private final SnappyErrorCode errorCode;
public SnappyIOException(SnappyErrorCode errorCode, String message) {
public SnappyIOException(SnappyErrorCode errorCode, String message)
{
super(message);
this.errorCode = errorCode;
}
@Override
public String getMessage() {
public String getMessage()
{
return String.format("[%s] %s", errorCode.name(), super.getMessage());
}
public SnappyErrorCode getErrorCode() { return errorCode; }
}

View File

@ -31,11 +31,10 @@ import java.io.InputStream;
/**
* A stream filter for reading data compressed by {@link SnappyOutputStream}.
*
*
* @author leo
*
*/
public class SnappyInputStream extends InputStream
public class SnappyInputStream
extends InputStream
{
private boolean finishedReading = false;
protected final InputStream in;
@ -53,7 +52,9 @@ public class SnappyInputStream extends InputStream
* @param input
* @throws IOException
*/
public SnappyInputStream(InputStream input) throws IOException {
public SnappyInputStream(InputStream input)
throws IOException
{
this.in = input;
readHeader();
}
@ -65,19 +66,25 @@ public class SnappyInputStream extends InputStream
* @see java.io.InputStream#close()
*/
@Override
public void close() throws IOException {
public void close()
throws IOException
{
compressed = null;
uncompressed = null;
if (in != null)
if (in != null) {
in.close();
}
}
protected void readHeader() throws IOException {
protected void readHeader()
throws IOException
{
int readBytes = 0;
while (readBytes < header.length) {
int ret = in.read(header, readBytes, header.length - readBytes);
if (ret == -1)
if (ret == -1) {
break;
}
readBytes += ret;
}
@ -99,7 +106,9 @@ public class SnappyInputStream extends InputStream
}
}
private static boolean isValidHeader(byte[] header) throws IOException {
private static boolean isValidHeader(byte[] header)
throws IOException
{
SnappyCodec codec = SnappyCodec.readHeader(new ByteArrayInputStream(header));
if (codec.isValidMagicHeader()) {
// The input data is compressed by SnappyOutputStream
@ -110,11 +119,14 @@ public class SnappyInputStream extends InputStream
}
return true;
}
else
else {
return false;
}
}
protected void readFully(byte[] fragment, int fragmentLength) throws IOException {
protected void readFully(byte[] fragment, int fragmentLength)
throws IOException
{
if (fragmentLength == 0) {
finishedReading = true;
return;
@ -140,7 +152,6 @@ public class SnappyInputStream extends InputStream
Snappy.uncompress(compressed, 0, cursor, uncompressed, 0);
this.uncompressedCursor = 0;
this.uncompressedLimit = uncompressedLength;
}
/**
@ -151,7 +162,9 @@ public class SnappyInputStream extends InputStream
* @see java.io.InputStream#read(byte[], int, int)
*/
@Override
public int read(byte[] b, int off, int len) throws IOException {
public int read(byte[] b, int off, int len)
throws IOException
{
return rawRead(b, off, len);
}
@ -164,13 +177,16 @@ public class SnappyInputStream extends InputStream
* @return written bytes
* @throws IOException
*/
public int rawRead(Object array, int byteOffset, int byteLength) throws IOException {
public int rawRead(Object array, int byteOffset, int byteLength)
throws IOException
{
int writtenBytes = 0;
for (; writtenBytes < byteLength; ) {
if (uncompressedCursor >= uncompressedLimit) {
if (hasNextChunk())
if (hasNextChunk()) {
continue;
}
else {
return writtenBytes == 0 ? -1 : writtenBytes;
}
@ -187,17 +203,16 @@ public class SnappyInputStream extends InputStream
/**
* Read long array from the stream
*
* @param d
* input
* @param off
* offset
* @param len
* the number of long elements to read
* @param d input
* @param off offset
* @param len the number of long elements to read
* @return the total number of bytes read into the buffer, or -1 if there is
* no more data because the end of the stream has been reached.
* @throws IOException
*/
public int read(long[] d, int off, int len) throws IOException {
public int read(long[] d, int off, int len)
throws IOException
{
return rawRead(d, off * 8, len * 8);
}
@ -209,24 +224,25 @@ public class SnappyInputStream extends InputStream
* no more data because the end of the stream has been reached.
* @throws IOException
*/
public int read(long[] d) throws IOException {
public int read(long[] d)
throws IOException
{
return read(d, 0, d.length);
}
/**
* Read double array from the stream
*
* @param d
* input
* @param off
* offset
* @param len
* the number of double elements to read
* @param d input
* @param off offset
* @param len the number of double elements to read
* @return the total number of bytes read into the buffer, or -1 if there is
* no more data because the end of the stream has been reached.
* @throws IOException
*/
public int read(double[] d, int off, int len) throws IOException {
public int read(double[] d, int off, int len)
throws IOException
{
return rawRead(d, off * 8, len * 8);
}
@ -238,7 +254,9 @@ public class SnappyInputStream extends InputStream
* no more data because the end of the stream has been reached.
* @throws IOException
*/
public int read(double[] d) throws IOException {
public int read(double[] d)
throws IOException
{
return read(d, 0, d.length);
}
@ -250,41 +268,41 @@ public class SnappyInputStream extends InputStream
* no more data because the end of the stream has been reached.
* @throws IOException
*/
public int read(int[] d) throws IOException {
public int read(int[] d)
throws IOException
{
return read(d, 0, d.length);
}
/**
* Read int array from the stream
*
* @param d
* input
* @param off
* offset
* @param len
* the number of int elements to read
* @param d input
* @param off offset
* @param len the number of int elements to read
* @return the total number of bytes read into the buffer, or -1 if there is
* no more data because the end of the stream has been reached.
* @throws IOException
*/
public int read(int[] d, int off, int len) throws IOException {
public int read(int[] d, int off, int len)
throws IOException
{
return rawRead(d, off * 4, len * 4);
}
/**
* Read float array from the stream
*
* @param d
* input
* @param off
* offset
* @param len
* the number of float elements to read
* @param d input
* @param off offset
* @param len the number of float elements to read
* @return the total number of bytes read into the buffer, or -1 if there is
* no more data because the end of the stream has been reached.
* @throws IOException
*/
public int read(float[] d, int off, int len) throws IOException {
public int read(float[] d, int off, int len)
throws IOException
{
return rawRead(d, off * 4, len * 4);
}
@ -296,24 +314,25 @@ public class SnappyInputStream extends InputStream
* no more data because the end of the stream has been reached.
* @throws IOException
*/
public int read(float[] d) throws IOException {
public int read(float[] d)
throws IOException
{
return read(d, 0, d.length);
}
/**
* Read short array from the stream
*
* @param d
* input
* @param off
* offset
* @param len
* the number of short elements to read
* @param d input
* @param off offset
* @param len the number of short elements to read
* @return the total number of bytes read into the buffer, or -1 if there is
* no more data because the end of the stream has been reached.
* @throws IOException
*/
public int read(short[] d, int off, int len) throws IOException {
public int read(short[] d, int off, int len)
throws IOException
{
return rawRead(d, off * 2, len * 2);
}
@ -325,18 +344,23 @@ public class SnappyInputStream extends InputStream
* no more data because the end of the stream has been reached.
* @throws IOException
*/
public int read(short[] d) throws IOException {
public int read(short[] d)
throws IOException
{
return read(d, 0, d.length);
}
/**
* Read next len bytes
*
* @param dest
* @param offset
* @param len
* @return read bytes
*/
private int readNext(byte[] dest, int offset, int len) throws IOException {
private int readNext(byte[] dest, int offset, int len)
throws IOException
{
int readBytes = 0;
while (readBytes < len) {
int ret = in.read(dest, readBytes + offset, len - readBytes);
@ -349,30 +373,37 @@ public class SnappyInputStream extends InputStream
return readBytes;
}
protected boolean hasNextChunk() throws IOException {
if (finishedReading)
protected boolean hasNextChunk()
throws IOException
{
if (finishedReading) {
return false;
}
uncompressedCursor = 0;
uncompressedLimit = 0;
int readBytes = readNext(header, 0, 4);
if(readBytes < 4)
if (readBytes < 4) {
return false;
}
int chunkSize = SnappyOutputStream.readInt(header, 0);
if (chunkSize == SnappyCodec.MAGIC_HEADER_HEAD) {
// Concatenated data
int remainingHeaderSize = SnappyCodec.headerSize() - 4;
readBytes = readNext(header, 4, remainingHeaderSize);
if(readBytes < remainingHeaderSize)
if (readBytes < remainingHeaderSize) {
return false;
}
if(isValidHeader(header))
if (isValidHeader(header)) {
return hasNextChunk();
else
}
else {
return false;
}
}
// extend the compressed data buffer size
if (compressed == null || chunkSize > compressed.length) {
@ -381,8 +412,9 @@ public class SnappyInputStream extends InputStream
readBytes = 0;
while (readBytes < chunkSize) {
int ret = in.read(compressed, readBytes, chunkSize - readBytes);
if (ret == -1)
if (ret == -1) {
break;
}
readBytes += ret;
}
if (readBytes < chunkSize) {
@ -412,23 +444,29 @@ public class SnappyInputStream extends InputStream
* @see java.io.InputStream#read()
*/
@Override
public int read() throws IOException {
public int read()
throws IOException
{
if (uncompressedCursor < uncompressedLimit) {
return uncompressed[uncompressedCursor++] & 0xFF;
}
else {
if (hasNextChunk())
if (hasNextChunk()) {
return read();
else
}
else {
return -1;
}
}
}
/* (non-Javadoc)
* @see java.io.InputStream#available()
*/
@Override
public int available() throws IOException {
public int available()
throws IOException
{
if (uncompressedCursor < uncompressedLimit) {
return uncompressedLimit - uncompressedCursor;
}

View File

@ -36,10 +36,10 @@ import java.util.UUID;
* user platform (<i>os.name</i> and <i>os.arch</i>). The natively compiled
* libraries bundled to snappy-java contain the codes of the original snappy and
* JNI programs to access Snappy.
*
* <p/>
* In default, no configuration is required to use snappy-java, but you can load
* your own native library created by 'make native' command.
*
* <p/>
* This SnappyLoader searches for native libraries (snappyjava.dll,
* libsnappy.so, etc.) in the following order:
* <ol>
@ -53,23 +53,22 @@ import java.util.UUID;
* <i>org.xerial.snappy.tempdir</i> is set, use this folder instead of
* <i>java.io.tempdir</i>.
* </ol>
*
* <p/>
* <p>
* If you do not want to use folder <i>java.io.tempdir</i>, set the System
* property <i>org.xerial.snappy.tempdir</i>. For example, to use
* <i>/tmp/leo</i> as a temporary folder to copy native libraries, use -D option
* of JVM:
*
* <p/>
* <pre>
* <code>
* java -Dorg.xerial.snappy.tempdir="/tmp/leo" ...
* </code>
* </pre>
*
* <p/>
* </p>
*
* @author leo
*
*/
public class SnappyLoader
{
@ -85,10 +84,12 @@ public class SnappyLoader
private static File nativeLibFile = null;
static void cleanUpExtractedNativeLib() {
if(nativeLibFile != null && nativeLibFile.exists())
static void cleanUpExtractedNativeLib()
{
if (nativeLibFile != null && nativeLibFile.exists()) {
nativeLibFile.delete();
}
}
/**
* Set the api instance.
@ -104,13 +105,15 @@ public class SnappyLoader
* load system properties when configuration file of the name
* {@link #SNAPPY_SYSTEM_PROPERTIES_FILE} is found
*/
private static void loadSnappySystemProperties() {
private static void loadSnappySystemProperties()
{
try {
InputStream is = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(SNAPPY_SYSTEM_PROPERTIES_FILE);
if (is == null)
if (is == null) {
return; // no configuration file is found
}
// Load property file
Properties props = new Properties();
@ -138,8 +141,9 @@ public class SnappyLoader
static synchronized SnappyNative load()
{
if (api != null)
if (api != null) {
return api;
}
try {
loadNativeLibrary();
@ -158,7 +162,8 @@ public class SnappyLoader
/**
* Load a native library of snappy-java
*/
private static void loadNativeLibrary() {
private static void loadNativeLibrary()
{
nativeLibFile = findNativeLibrary();
if (nativeLibFile != null) {
@ -171,8 +176,9 @@ public class SnappyLoader
}
}
private static boolean contentsEquals(InputStream in1, InputStream in2) throws IOException {
private static boolean contentsEquals(InputStream in1, InputStream in2)
throws IOException
{
if (!(in1 instanceof BufferedInputStream)) {
in1 = new BufferedInputStream(in1);
}
@ -183,13 +189,15 @@ public class SnappyLoader
int ch = in1.read();
while (ch != -1) {
int ch2 = in2.read();
if(ch != ch2)
if (ch != ch2) {
return false;
}
ch = in1.read();
}
int ch2 = in2.read();
return ch2 == -1;
}
/**
* Extract the specified library file to the target folder
*
@ -198,7 +206,8 @@ public class SnappyLoader
* @param targetFolder
* @return
*/
private static File extractLibraryFile(String libFolderForCurrentOS, String libraryFileName, String targetFolder) {
private static File extractLibraryFile(String libFolderForCurrentOS, String libraryFileName, String targetFolder)
{
String nativeLibraryFilePath = libFolderForCurrentOS + "/" + libraryFileName;
// Attach UUID to the native library file to ensure multiple class loaders can read the libsnappy-java multiple times.
@ -221,33 +230,37 @@ public class SnappyLoader
// Delete the extracted lib file on JVM exit.
extractedLibFile.deleteOnExit();
if(writer != null)
if (writer != null) {
writer.close();
if(reader != null)
}
if (reader != null) {
reader.close();
}
}
// Set executable (x) flag to enable Java to load the native library
extractedLibFile.setReadable(true);
extractedLibFile.setWritable(true, true);
extractedLibFile.setExecutable(true);
// Check whether the contents are properly copied from the resource folder
{
InputStream nativeIn = SnappyLoader.class.getResourceAsStream(nativeLibraryFilePath);
InputStream extractedLibIn = new FileInputStream(extractedLibFile);
try {
if(!contentsEquals(nativeIn, extractedLibIn))
if (!contentsEquals(nativeIn, extractedLibIn)) {
throw new SnappyError(SnappyErrorCode.FAILED_TO_LOAD_NATIVE_LIBRARY, String.format("Failed to write a native library file at %s", extractedLibFile));
}
}
finally {
if(nativeIn != null)
if (nativeIn != null) {
nativeIn.close();
if(extractedLibIn != null)
}
if (extractedLibIn != null) {
extractedLibIn.close();
}
}
}
return new File(targetFolder, extractedLibFileName);
}
@ -257,28 +270,31 @@ public class SnappyLoader
}
}
static File findNativeLibrary() {
static File findNativeLibrary()
{
boolean useSystemLib = Boolean.parseBoolean(System.getProperty(KEY_SNAPPY_USE_SYSTEMLIB, "false"));
boolean disabledBundledLibs = Boolean
.parseBoolean(System.getProperty(KEY_SNAPPY_DISABLE_BUNDLED_LIBS, "false"));
if (useSystemLib || disabledBundledLibs)
if (useSystemLib || disabledBundledLibs) {
return null; // Use a pre-installed libsnappyjava
}
// Try to load the library in org.xerial.snappy.lib.path */
String snappyNativeLibraryPath = System.getProperty(KEY_SNAPPY_LIB_PATH);
String snappyNativeLibraryName = System.getProperty(KEY_SNAPPY_LIB_NAME);
// Resolve the library file name with a suffix (e.g., dll, .so, etc.)
if (snappyNativeLibraryName == null)
if (snappyNativeLibraryName == null) {
snappyNativeLibraryName = System.mapLibraryName("snappyjava");
}
if (snappyNativeLibraryPath != null) {
File nativeLib = new File(snappyNativeLibraryPath, snappyNativeLibraryName);
if (nativeLib.exists())
if (nativeLib.exists()) {
return nativeLib;
}
}
// Load an OS-dependent native library inside a jar file
snappyNativeLibraryPath = "/org/xerial/snappy/native/" + OSInfo.getNativeLibFolderPathForCurrentOS();
@ -309,13 +325,11 @@ public class SnappyLoader
return extractLibraryFile(snappyNativeLibraryPath, snappyNativeLibraryName, tempFolder.getAbsolutePath());
}
private static boolean hasResource(String path) {
private static boolean hasResource(String path)
{
return SnappyLoader.class.getResource(path) != null;
}
/**
* Get the snappy-java version by reading pom.properties embedded in jar.
* This version data is used as a suffix of a dll file extracted from the
@ -323,12 +337,14 @@ public class SnappyLoader
*
* @return the version string
*/
public static String getVersion() {
public static String getVersion()
{
URL versionFile = SnappyLoader.class
.getResource("/META-INF/maven/org.xerial.snappy/snappy-java/pom.properties");
if (versionFile == null)
if (versionFile == null) {
versionFile = SnappyLoader.class.getResource("/org/xerial/snappy/VERSION");
}
String version = "unknown";
try {
@ -336,8 +352,9 @@ public class SnappyLoader
Properties versionData = new Properties();
versionData.load(versionFile.openStream());
version = versionData.getProperty("version", version);
if (version.equals("unknown"))
if (version.equals("unknown")) {
version = versionData.getProperty("VERSION", version);
}
version = version.trim().replaceAll("[^0-9M\\.]", "");
}
}
@ -346,5 +363,4 @@ public class SnappyLoader
}
return version;
}
}

View File

@ -30,14 +30,13 @@ import java.nio.ByteBuffer;
/**
* JNI interface of the {@link Snappy} implementation. The native method in this class is
* defined in SnappyNative.h (genereted by javah) and SnappyNative.cpp
*
* <p/>
* <p>
* <b> DO NOT USE THIS CLASS since the direct use of this class might break the
* native library code loading in {@link SnappyLoader}. </b>
* </p>
*
* @author leo
*
*/
public class SnappyNative
{
@ -47,16 +46,22 @@ public class SnappyNative
// ------------------------------------------------------------------------
// Generic compression/decompression routines.
// ------------------------------------------------------------------------
public native long rawCompress(long inputAddr, long inputSize, long destAddr) throws IOException;
public native long rawUncompress(long inputAddr, long inputSize, long destAddr) throws IOException;
public native long rawCompress(long inputAddr, long inputSize, long destAddr)
throws IOException;
public native long rawUncompress(long inputAddr, long inputSize, long destAddr)
throws IOException;
public native int rawCompress(ByteBuffer input, int inputOffset, int inputLength, ByteBuffer compressed,
int outputOffset) throws IOException;
int outputOffset)
throws IOException;
public native int rawCompress(Object input, int inputOffset, int inputByteLength, Object output, int outputOffset) throws IOException;
public native int rawCompress(Object input, int inputOffset, int inputByteLength, Object output, int outputOffset)
throws IOException;
public native int rawUncompress(ByteBuffer compressed, int inputOffset, int inputLength, ByteBuffer uncompressed,
int outputOffset) throws IOException;
int outputOffset)
throws IOException;
public native int rawUncompress(Object input, int inputOffset, int inputLength, Object output, int outputOffset)
throws IOException;
@ -66,22 +71,30 @@ public class SnappyNative
public native int maxCompressedLength(int source_bytes);
// This operation takes O(1) time.
public native int uncompressedLength(ByteBuffer compressed, int offset, int len) throws IOException;
public native int uncompressedLength(ByteBuffer compressed, int offset, int len)
throws IOException;
public native int uncompressedLength(Object input, int offset, int len) throws IOException;
public native int uncompressedLength(Object input, int offset, int len)
throws IOException;
public native long uncompressedLength(long inputAddr, long len) throws IOException;
public native long uncompressedLength(long inputAddr, long len)
throws IOException;
public native boolean isValidCompressedBuffer(ByteBuffer compressed, int offset, int len) throws IOException;
public native boolean isValidCompressedBuffer(ByteBuffer compressed, int offset, int len)
throws IOException;
public native boolean isValidCompressedBuffer(Object input, int offset, int len) throws IOException;
public native boolean isValidCompressedBuffer(Object input, int offset, int len)
throws IOException;
public native boolean isValidCompressedBuffer(long inputAddr, long offset, long len) throws IOException;
public native boolean isValidCompressedBuffer(long inputAddr, long offset, long len)
throws IOException;
public native void arrayCopy(Object src, int offset, int byteLength, Object dest, int dOffset) throws IOException;
public native void arrayCopy(Object src, int offset, int byteLength, Object dest, int dOffset)
throws IOException;
public void throw_error(int errorCode) throws IOException {
public void throw_error(int errorCode)
throws IOException
{
throw new IOException(String.format("%s(%d)", SnappyErrorCode.getErrorMessage(errorCode), errorCode));
}
}

View File

@ -55,7 +55,9 @@ import java.io.OutputStream;
*
* @author leo
*/
public class SnappyOutputStream extends OutputStream {
public class SnappyOutputStream
extends OutputStream
{
static final int MIN_BLOCK_SIZE = 1 * 1024;
static final int DEFAULT_BLOCK_SIZE = 32 * 1024; // Use 32kb for the default block size
@ -72,7 +74,8 @@ public class SnappyOutputStream extends OutputStream {
private int outputCursor = 0;
private boolean closed;
public SnappyOutputStream(OutputStream out) {
public SnappyOutputStream(OutputStream out)
{
this(out, DEFAULT_BLOCK_SIZE);
}
@ -81,11 +84,13 @@ public class SnappyOutputStream extends OutputStream {
* @param blockSize byte size of the internal buffer size
* @throws IOException
*/
public SnappyOutputStream(OutputStream out, int blockSize) {
public SnappyOutputStream(OutputStream out, int blockSize)
{
this(out, blockSize, CachedBufferAllocator.factory);
}
public SnappyOutputStream(OutputStream out, int blockSize, BufferAllocatorFactory bufferAllocatorFactory) {
public SnappyOutputStream(OutputStream out, int blockSize, BufferAllocatorFactory bufferAllocatorFactory)
{
this.out = out;
this.blockSize = Math.max(MIN_BLOCK_SIZE, blockSize);
int inputSize = blockSize;
@ -100,12 +105,13 @@ public class SnappyOutputStream extends OutputStream {
outputCursor = SnappyCodec.currentHeader.writeHeader(outputBuffer, 0);
}
/* (non-Javadoc)
* @see java.io.OutputStream#write(byte[], int, int)
*/
@Override
public void write(byte[] b, int off, int len) throws IOException {
public void write(byte[] b, int off, int len)
throws IOException
{
rawWrite(b, off, len);
}
@ -117,7 +123,9 @@ public class SnappyOutputStream extends OutputStream {
* @param len the number of elements in the array to copy
* @throws IOException
*/
public void write(long[] d, int off, int len) throws IOException {
public void write(long[] d, int off, int len)
throws IOException
{
rawWrite(d, off * 8, len * 8);
}
@ -129,7 +137,9 @@ public class SnappyOutputStream extends OutputStream {
* @param len the number of elements in the array to copy
* @throws IOException
*/
public void write(double[] f, int off, int len) throws IOException {
public void write(double[] f, int off, int len)
throws IOException
{
rawWrite(f, off * 8, len * 8);
}
@ -141,7 +151,9 @@ public class SnappyOutputStream extends OutputStream {
* @param len the number of elements in the array to copy
* @throws IOException
*/
public void write(float[] f, int off, int len) throws IOException {
public void write(float[] f, int off, int len)
throws IOException
{
rawWrite(f, off * 4, len * 4);
}
@ -153,7 +165,9 @@ public class SnappyOutputStream extends OutputStream {
* @param len the number of elements in the array to copy
* @throws IOException
*/
public void write(int[] f, int off, int len) throws IOException {
public void write(int[] f, int off, int len)
throws IOException
{
rawWrite(f, off * 4, len * 4);
}
@ -165,7 +179,9 @@ public class SnappyOutputStream extends OutputStream {
* @param len the number of elements in the array to copy
* @throws IOException
*/
public void write(short[] f, int off, int len) throws IOException {
public void write(short[] f, int off, int len)
throws IOException
{
rawWrite(f, off * 2, len * 2);
}
@ -175,7 +191,9 @@ public class SnappyOutputStream extends OutputStream {
* @param d
* @throws IOException
*/
public void write(long[] d) throws IOException {
public void write(long[] d)
throws IOException
{
write(d, 0, d.length);
}
@ -185,7 +203,9 @@ public class SnappyOutputStream extends OutputStream {
* @param f
* @throws IOException
*/
public void write(double[] f) throws IOException {
public void write(double[] f)
throws IOException
{
write(f, 0, f.length);
}
@ -195,7 +215,9 @@ public class SnappyOutputStream extends OutputStream {
* @param f
* @throws IOException
*/
public void write(float[] f) throws IOException {
public void write(float[] f)
throws IOException
{
write(f, 0, f.length);
}
@ -205,7 +227,9 @@ public class SnappyOutputStream extends OutputStream {
* @param f
* @throws IOException
*/
public void write(int[] f) throws IOException {
public void write(int[] f)
throws IOException
{
write(f, 0, f.length);
}
@ -215,11 +239,14 @@ public class SnappyOutputStream extends OutputStream {
* @param f
* @throws IOException
*/
public void write(short[] f) throws IOException {
public void write(short[] f)
throws IOException
{
write(f, 0, f.length);
}
private boolean hasSufficientOutputBufferFor(int inputSize) {
private boolean hasSufficientOutputBufferFor(int inputSize)
{
int maxCompressedSize = Snappy.maxCompressedLength(inputSize);
return maxCompressedSize < outputBuffer.length - outputCursor - 4;
}
@ -232,7 +259,9 @@ public class SnappyOutputStream extends OutputStream {
* @param byteLength
* @throws IOException
*/
public void rawWrite(Object array, int byteOffset, int byteLength) throws IOException {
public void rawWrite(Object array, int byteOffset, int byteLength)
throws IOException
{
if (closed) {
throw new IOException("Stream is closed");
}
@ -244,8 +273,9 @@ public class SnappyOutputStream extends OutputStream {
Snappy.arrayCopy(array, byteOffset + cursor, readLen, inputBuffer, inputCursor);
inputCursor += readLen;
}
if(inputCursor < blockSize)
if (inputCursor < blockSize) {
return;
}
compressInput();
cursor += readLen;
@ -262,7 +292,9 @@ public class SnappyOutputStream extends OutputStream {
* @see java.io.OutputStream#write(int)
*/
@Override
public void write(int b) throws IOException {
public void write(int b)
throws IOException
{
if (closed) {
throw new IOException("Stream is closed");
}
@ -276,7 +308,9 @@ public class SnappyOutputStream extends OutputStream {
* @see java.io.OutputStream#flush()
*/
@Override
public void flush() throws IOException {
public void flush()
throws IOException
{
if (closed) {
throw new IOException("Stream is closed");
}
@ -285,14 +319,16 @@ public class SnappyOutputStream extends OutputStream {
out.flush();
}
static void writeInt(byte[] dst, int offset, int v) {
static void writeInt(byte[] dst, int offset, int v)
{
dst[offset] = (byte) ((v >> 24) & 0xFF);
dst[offset + 1] = (byte) ((v >> 16) & 0xFF);
dst[offset + 2] = (byte) ((v >> 8) & 0xFF);
dst[offset + 3] = (byte) ((v >> 0) & 0xFF);
}
static int readInt(byte[] buffer, int pos) {
static int readInt(byte[] buffer, int pos)
{
int b1 = (buffer[pos] & 0xFF) << 24;
int b2 = (buffer[pos + 1] & 0xFF) << 16;
int b3 = (buffer[pos + 2] & 0xFF) << 8;
@ -300,14 +336,18 @@ public class SnappyOutputStream extends OutputStream {
return b1 | b2 | b3 | b4;
}
protected void dumpOutput() throws IOException {
protected void dumpOutput()
throws IOException
{
if (outputCursor > 0) {
out.write(outputBuffer, 0, outputCursor);
outputCursor = 0;
}
}
protected void compressInput() throws IOException {
protected void compressInput()
throws IOException
{
if (inputCursor <= 0) {
return; // no need to dump
}
@ -330,14 +370,17 @@ public class SnappyOutputStream extends OutputStream {
* @see java.io.OutputStream#close()
*/
@Override
public void close() throws IOException {
public void close()
throws IOException
{
if (closed) {
return;
}
try {
flush();
out.close();
} finally {
}
finally {
closed = true;
inputBufferAllocator.release(inputBuffer);
outputBufferAllocator.release(outputBuffer);
@ -345,5 +388,4 @@ public class SnappyOutputStream extends OutputStream {
outputBuffer = null;
}
}
}

View File

@ -3,9 +3,10 @@ package org.xerial.snappy.buffer;
/**
* BufferAllocator interface. The implementation of this interface must be thread-safe
*/
public interface BufferAllocator {
public interface BufferAllocator
{
public byte[] allocate(int size);
public void release(byte[] buffer);
public void release(byte[] buffer);
}

View File

@ -3,7 +3,8 @@ package org.xerial.snappy.buffer;
/**
*
*/
public interface BufferAllocatorFactory {
public interface BufferAllocatorFactory
{
BufferAllocator getBufferAllocator(int minSize);
}

View File

@ -6,11 +6,15 @@ import java.util.*;
/**
* Cached buffer
*/
public class CachedBufferAllocator implements BufferAllocator {
public class CachedBufferAllocator
implements BufferAllocator
{
public static BufferAllocatorFactory factory = new BufferAllocatorFactory() {
public static BufferAllocatorFactory factory = new BufferAllocatorFactory()
{
@Override
public BufferAllocator getBufferAllocator(int bufferSize) {
public BufferAllocator getBufferAllocator(int bufferSize)
{
return CachedBufferAllocator.getAllocator(bufferSize);
}
};
@ -23,12 +27,14 @@ public class CachedBufferAllocator implements BufferAllocator {
private final int bufferSize;
private final Deque<byte[]> bufferQueue;
public CachedBufferAllocator(int bufferSize) {
public CachedBufferAllocator(int bufferSize)
{
this.bufferSize = bufferSize;
this.bufferQueue = new ArrayDeque<byte[]>();
}
public static synchronized CachedBufferAllocator getAllocator(int bufferSize) {
public static synchronized CachedBufferAllocator getAllocator(int bufferSize)
{
CachedBufferAllocator result = null;
if (queueTable.containsKey(bufferSize)) {
@ -42,7 +48,8 @@ public class CachedBufferAllocator implements BufferAllocator {
}
@Override
public byte[] allocate(int size) {
public byte[] allocate(int size)
{
synchronized (this) {
if (bufferQueue.isEmpty()) {
return new byte[size];
@ -52,8 +59,10 @@ public class CachedBufferAllocator implements BufferAllocator {
}
}
}
@Override
public void release(byte[] buffer) {
public void release(byte[] buffer)
{
synchronized (this) {
bufferQueue.addLast(buffer);
}

View File

@ -3,24 +3,30 @@ package org.xerial.snappy.buffer;
/**
* Simple buffer allocator, which does not reuse the allocated buffer
*/
public class DefaultBufferAllocator implements BufferAllocator {
public class DefaultBufferAllocator
implements BufferAllocator
{
public static BufferAllocatorFactory factory = new BufferAllocatorFactory() {
public static BufferAllocatorFactory factory = new BufferAllocatorFactory()
{
public BufferAllocator singleton = new DefaultBufferAllocator();
@Override
public BufferAllocator getBufferAllocator(int bufferSize) {
public BufferAllocator getBufferAllocator(int bufferSize)
{
return singleton;
}
};
@Override
public byte[] allocate(int size) {
public byte[] allocate(int size)
{
return new byte[size];
}
@Override
public void release(byte[] buffer) {
public void release(byte[] buffer)
{
// do nothing
}
}

View File

@ -16,7 +16,7 @@
/**
* Snappy API for compressing/decompressing data.
*
* <p/>
* Usage
* First, import {@link org.xerial.snappy.Snappy} in your Java code:
* <code>
@ -34,9 +34,9 @@
* System.out.println(result);
* </pre>
* </code>
*
* <p/>
* <p>In addition, high-level methods (Snappy.compress(String), Snappy.compress(float[] ..) etc. ) and low-level ones (e.g. Snappy.rawCompress(.. ), Snappy.rawUncompress(..), etc.), which minimize memory copies, can be used. </p>
*
* <p/>
* <h3>Stream-based API</h3>
* Stream-based compressor/decompressor SnappyOutputStream, SnappyInputStream are also available for reading/writing large data sets.
*/

View File

@ -45,7 +45,6 @@ import org.xerial.util.log.Logger;
* Benchmark using Calgary data set
*
* @author leo
*
*/
public class CalgaryTest
{
@ -54,24 +53,30 @@ public class CalgaryTest
@Rule
public final TemporaryFolder tempFolder = new TemporaryFolder();
static byte[] readFile(String file) throws IOException {
static byte[] readFile(String file)
throws IOException
{
InputStream in = FileResource.find(CalgaryTest.class, file).openStream();
if (in == null)
if (in == null) {
throw new IOException("file " + file + " is not found");
}
try {
return SnappyInputStreamTest.readFully(in);
}
finally {
if (in != null)
if (in != null) {
in.close();
}
}
}
public static final String[] files = {"bib", "book1", "book2", "geo", "news", "obj1", "obj2", "paper1", "paper2",
"paper3", "paper4", "paper5", "paper6", "pic", "progc", "progl", "progp", "trans"};
@Test
public void block() throws Exception {
public void block()
throws Exception
{
for (String f : files) {
byte[] orig = readFile("testdata/calgary/" + f);
@ -83,7 +88,9 @@ public class CalgaryTest
}
@Test
public void stream() throws Exception {
public void stream()
throws Exception
{
for (String f : files) {
byte[] orig = readFile("testdata/calgary/" + f);
@ -101,7 +108,9 @@ public class CalgaryTest
}
@Test
public void streamFramed() throws Exception {
public void streamFramed()
throws Exception
{
for (String f : files) {
byte[] orig = readFile("testdata/calgary/" + f);
@ -121,35 +130,33 @@ public class CalgaryTest
}
@Test
public void streamFramedToFile() throws Exception {
public void streamFramedToFile()
throws Exception
{
for (String f : files) {
byte[] orig = readFile("testdata/calgary/" + f);
final File tempFile = tempFolder.newFile(f);
final FileOutputStream compressedFOS = new FileOutputStream(tempFile);
try
{
try {
SnappyFramedOutputStream out = new SnappyFramedOutputStream(compressedFOS);
out.write(orig);
out.close();
}
finally
{
finally {
compressedFOS.close();
}
byte[] uncompressed = new byte[orig.length];
final FileInputStream compressedFIS = new FileInputStream(tempFile);
try
{
try {
SnappyFramedInputStream in = new SnappyFramedInputStream(compressedFIS.getChannel());
int readBytes = readBytes(in, uncompressed, 0, orig.length);
assertEquals(orig.length, readBytes);
}
finally
{
finally {
compressedFIS.close();
}
@ -158,7 +165,9 @@ public class CalgaryTest
}
@Test
public void streamFramedNoCRCVerify() throws Exception {
public void streamFramedNoCRCVerify()
throws Exception
{
for (String f : files) {
byte[] orig = readFile("testdata/calgary/" + f);
@ -178,7 +187,9 @@ public class CalgaryTest
}
@Test
public void byteWiseRead() throws Exception {
public void byteWiseRead()
throws Exception
{
for (String f : files) {
byte[] orig = readFile("testdata/calgary/" + f);
@ -192,8 +203,9 @@ public class CalgaryTest
int cursor = 0;
for (; ; ) {
int b = in.read();
if (b == -1)
if (b == -1) {
break;
}
uncompressed[cursor++] = (byte) b;
}
assertEquals(orig.length, cursor);
@ -201,7 +213,8 @@ public class CalgaryTest
}
}
static final int readBytes(InputStream source, byte[] dest, int offset, int length) throws IOException
static final int readBytes(InputStream source, byte[] dest, int offset, int length)
throws IOException
{
// how many bytes were read.
int lastRead = source.read(dest, offset, length);
@ -209,16 +222,13 @@ public class CalgaryTest
int totalRead = lastRead;
// if we did not read as many bytes as we had hoped, try reading again.
if (lastRead < length)
{
if (lastRead < length) {
// as long the buffer is not full (remaining() == 0) and we have not reached EOF (lastRead == -1) keep reading.
while (totalRead < length && lastRead != -1)
{
while (totalRead < length && lastRead != -1) {
lastRead = source.read(dest, offset + totalRead, length - totalRead);
// if we got EOF, do not add to total read.
if (lastRead != -1)
{
if (lastRead != -1) {
totalRead += lastRead;
}
}

View File

@ -34,7 +34,8 @@ import org.junit.Test;
public class OSInfoTest
{
@Test
public void osName() {
public void osName()
{
assertEquals("Windows", OSInfo.translateOSNameToFolderName("Windows XP"));
assertEquals("Windows", OSInfo.translateOSNameToFolderName("Windows 2000"));
assertEquals("Windows", OSInfo.translateOSNameToFolderName("Windows Vista"));
@ -53,7 +54,8 @@ public class OSInfoTest
}
@Test
public void archName() {
public void archName()
{
assertEquals("i386", OSInfo.translateArchNameToFolderName("i386"));
assertEquals("x86", OSInfo.translateArchNameToFolderName("x86"));
assertEquals("ppc", OSInfo.translateArchNameToFolderName("ppc"));
@ -61,7 +63,8 @@ public class OSInfoTest
}
@Test
public void folderPath() {
public void folderPath()
{
String[] component = OSInfo.getNativeLibFolderPathForCurrentOS().split("/");
assertEquals(2, component.length);
assertEquals(OSInfo.getOSName(), component[0]);
@ -69,7 +72,9 @@ public class OSInfoTest
}
@Test
public void testMainForOSName() throws Exception {
public void testMainForOSName()
throws Exception
{
// preserve the current System.out
PrintStream out = System.out;
@ -85,11 +90,12 @@ public class OSInfoTest
// reset STDOUT
System.setOut(out);
}
}
@Test
public void testMainForArchName() throws Exception {
public void testMainForArchName()
throws Exception
{
// preserver the current System.out
PrintStream out = System.out;
@ -106,5 +112,4 @@ public class OSInfoTest
System.setOut(out);
}
}
}

View File

@ -7,17 +7,19 @@ import java.util.Random;
/**
* Generates random data with specific expected snappy performance characteristics.
*
* <p/>
* <p>
* This has been copied from <a href="https://github.com/dain/snappy"> dain's snappy</a> implementation..
* </p>
*/
public class RandomGenerator {
public class RandomGenerator
{
public final byte[] data;
public int position;
public RandomGenerator(double compressionRatio) {
public RandomGenerator(double compressionRatio)
{
// We use a limited amount of data over and over again and ensure
// that it is larger than the compression window (32KB), and also
// large enough to serve all typical value sizes we want to write.
@ -30,7 +32,8 @@ public class RandomGenerator {
}
}
public int getNextPosition(int length) {
public int getNextPosition(int length)
{
if (position + length > data.length) {
position = 0;
assert (length < data.length);
@ -41,7 +44,8 @@ public class RandomGenerator {
}
private static byte[] compressibleData(Random random,
double compressionRatio, int length) {
double compressionRatio, int length)
{
int raw = (int) (length * compressionRatio);
if (raw < 1) {
raw = 1;
@ -58,7 +62,8 @@ public class RandomGenerator {
return dest;
}
private static byte[] generateRandomData(Random random, int length) {
private static byte[] generateRandomData(Random random, int length)
{
byte[] rawData = new byte[length];
for (int i = 0; i < rawData.length; i++) {
rawData[i] = (byte) random.nextInt(256);

View File

@ -29,13 +29,15 @@ import org.junit.Test;
*
* @author Brett Okken
*/
public class SnappyFramedStreamTest {
public class SnappyFramedStreamTest
{
/**
* @throws IOException
*/
protected OutputStream createOutputStream(OutputStream target)
throws IOException {
throws IOException
{
return new SnappyFramedOutputStream(target);
}
@ -45,16 +47,21 @@ public class SnappyFramedStreamTest {
* @throws IOException
*/
protected InputStream createInputStream(InputStream source,
boolean verifyCheckSums) throws IOException {
boolean verifyCheckSums)
throws IOException
{
return new SnappyFramedInputStream(source, verifyCheckSums);
}
protected byte[] getMarkerFrame() {
protected byte[] getMarkerFrame()
{
return HEADER_BYTES;
}
@Test
public void testSimple() throws Exception {
public void testSimple()
throws Exception
{
byte[] original = "aaaaaaaaaaaabbbbbbbaaaaaa".getBytes("utf-8");
byte[] compressed = compress(original);
@ -83,7 +90,9 @@ public class SnappyFramedStreamTest {
}
@Test
public void testUncompressable() throws Exception {
public void testUncompressable()
throws Exception
{
byte[] random = getRandom(1, 5000);
int crc32c = maskedCrc32c(random);
@ -103,61 +112,78 @@ public class SnappyFramedStreamTest {
}
@Test
public void testEmptyCompression() throws Exception {
public void testEmptyCompression()
throws Exception
{
byte[] empty = new byte[0];
assertArrayEquals(compress(empty), HEADER_BYTES);
assertArrayEquals(uncompress(HEADER_BYTES), empty);
}
@Test(expected = EOFException.class)
public void testShortBlockHeader() throws Exception {
public void testShortBlockHeader()
throws Exception
{
uncompressBlock(new byte[] {0});
}
@Test(expected = EOFException.class)
public void testShortBlockData() throws Exception {
public void testShortBlockData()
throws Exception
{
// flag = 0, size = 8, crc32c = 0, block data= [x, x]
uncompressBlock(new byte[] {1, 8, 0, 0, 0, 0, 0, 0, 'x', 'x'});
}
@Test
public void testUnskippableChunkFlags() throws Exception {
public void testUnskippableChunkFlags()
throws Exception
{
for (int i = 2; i <= 0x7f; i++) {
try {
uncompressBlock(new byte[] {(byte) i, 5, 0, 0, 0, 0, 0, 0, 0});
fail("no exception thrown with flag: " + Integer.toHexString(i));
} catch (IOException e) {
}
catch (IOException e) {
}
}
}
@Test
public void testSkippableChunkFlags() throws Exception {
public void testSkippableChunkFlags()
throws Exception
{
for (int i = 0x80; i <= 0xfe; i++) {
try {
uncompressBlock(new byte[] {(byte) i, 5, 0, 0, 0, 0, 0, 0, 0});
} catch (IOException e) {
}
catch (IOException e) {
fail("exception thrown with flag: " + Integer.toHexString(i));
}
}
}
@Test(expected = IOException.class)
public void testInvalidBlockSizeZero() throws Exception {
public void testInvalidBlockSizeZero()
throws Exception
{
// flag = '0', block size = 4, crc32c = 0
uncompressBlock(new byte[] {1, 4, 0, 0, 0, 0, 0, 0});
}
@Test(expected = IOException.class)
public void testInvalidChecksum() throws Exception {
public void testInvalidChecksum()
throws Exception
{
// flag = 0, size = 5, crc32c = 0, block data = [a]
uncompressBlock(new byte[] {1, 5, 0, 0, 0, 0, 0, 0, 'a'});
}
@Test
public void testInvalidChecksumIgnoredWhenVerificationDisabled()
throws Exception {
throws Exception
{
// flag = 0, size = 4, crc32c = 0, block data = [a]
byte[] block = {1, 5, 0, 0, 0, 0, 0, 0, 'a'};
ByteArrayInputStream inputData = new ByteArrayInputStream(
@ -167,7 +193,9 @@ public class SnappyFramedStreamTest {
}
@Test
public void testTransferFrom_InputStream() throws IOException {
public void testTransferFrom_InputStream()
throws IOException
{
final byte[] random = getRandom(0.5, 100000);
final ByteArrayOutputStream baos = new ByteArrayOutputStream(
@ -184,7 +212,9 @@ public class SnappyFramedStreamTest {
}
@Test
public void testTransferFrom_ReadableByteChannel() throws IOException {
public void testTransferFrom_ReadableByteChannel()
throws IOException
{
final byte[] random = getRandom(0.5, 100000);
final ByteArrayOutputStream baos = new ByteArrayOutputStream(
@ -201,7 +231,9 @@ public class SnappyFramedStreamTest {
}
@Test
public void testTransferTo_OutputStream() throws IOException {
public void testTransferTo_OutputStream()
throws IOException
{
final byte[] random = getRandom(0.5, 100000);
final byte[] compressed = compress(random);
@ -216,7 +248,9 @@ public class SnappyFramedStreamTest {
}
@Test
public void testTransferTo_WritableByteChannel() throws IOException {
public void testTransferTo_WritableByteChannel()
throws IOException
{
final byte[] random = getRandom(0.5, 100000);
final byte[] compressed = compress(random);
@ -233,7 +267,9 @@ public class SnappyFramedStreamTest {
}
@Test
public void testLargerFrames_raw_() throws IOException {
public void testLargerFrames_raw_()
throws IOException
{
final byte[] random = getRandom(0.5, 100000);
final byte[] stream = new byte[HEADER_BYTES.length + 8 + random.length];
@ -260,7 +296,9 @@ public class SnappyFramedStreamTest {
}
@Test
public void testLargerFrames_compressed_() throws IOException {
public void testLargerFrames_compressed_()
throws IOException
{
final byte[] random = getRandom(0.5, 500000);
final byte[] compressed = Snappy.compress(random);
@ -290,7 +328,8 @@ public class SnappyFramedStreamTest {
@Test
public void testLargerFrames_compressed_smaller_raw_larger()
throws IOException {
throws IOException
{
final byte[] random = getRandom(0.5, 100000);
final byte[] compressed = Snappy.compress(random);
@ -319,19 +358,23 @@ public class SnappyFramedStreamTest {
assertArrayEquals(random, uncompressed);
}
private byte[] uncompressBlock(byte[] block) throws IOException {
private byte[] uncompressBlock(byte[] block)
throws IOException
{
return uncompress(blockToStream(block));
}
private static byte[] blockToStream(byte[] block) {
private static byte[] blockToStream(byte[] block)
{
byte[] stream = new byte[HEADER_BYTES.length + block.length];
System.arraycopy(HEADER_BYTES, 0, stream, 0, HEADER_BYTES.length);
System.arraycopy(block, 0, stream, HEADER_BYTES.length, block.length);
return stream;
}
protected byte[] compress(byte[] original) throws IOException {
protected byte[] compress(byte[] original)
throws IOException
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputStream snappyOut = createOutputStream(out);
snappyOut.write(original);
@ -339,12 +382,16 @@ public class SnappyFramedStreamTest {
return out.toByteArray();
}
protected byte[] uncompress(byte[] compressed) throws IOException {
protected byte[] uncompress(byte[] compressed)
throws IOException
{
return toByteArray(createInputStream(new ByteArrayInputStream(
compressed), true));
}
private static byte[] toByteArray(InputStream createInputStream) throws IOException {
private static byte[] toByteArray(InputStream createInputStream)
throws IOException
{
final ByteArrayOutputStream baos = new ByteArrayOutputStream(64 * 1024);
final byte[] buffer = new byte[8 * 1024];
@ -357,11 +404,13 @@ public class SnappyFramedStreamTest {
return baos.toByteArray();
}
static int toInt(byte value) {
static int toInt(byte value)
{
return value & 0xFF;
}
private byte[] getRandom(double compressionRatio, int length) {
private byte[] getRandom(double compressionRatio, int length)
{
RandomGenerator gen = new RandomGenerator(
compressionRatio);
gen.getNextPosition(length);
@ -369,5 +418,4 @@ public class SnappyFramedStreamTest {
assertEquals(random.length, length);
return random;
}
}

View File

@ -41,14 +41,18 @@ public class SnappyInputStreamTest
{
private static Logger _logger = Logger.getLogger(SnappyInputStreamTest.class);
public static byte[] readResourceFile(String fileName) throws IOException {
public static byte[] readResourceFile(String fileName)
throws IOException
{
BufferedInputStream input = new BufferedInputStream(FileResource.find(SnappyOutputStreamTest.class, fileName)
.openStream());
assertNotNull(input);
return readFully(input);
}
public static byte[] readFully(InputStream input) throws IOException {
public static byte[] readFully(InputStream input)
throws IOException
{
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[4096];
@ -63,7 +67,9 @@ public class SnappyInputStreamTest
}
}
public static byte[] byteWiseReadFully(InputStream input) throws IOException {
public static byte[] byteWiseReadFully(InputStream input)
throws IOException
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[4096];
for (int readData = 0; (readData = input.read()) != -1; ) {
@ -74,7 +80,9 @@ public class SnappyInputStreamTest
}
@Test
public void read() throws Exception {
public void read()
throws Exception
{
ByteArrayOutputStream compressedBuf = new ByteArrayOutputStream();
SnappyOutputStream snappyOut = new SnappyOutputStream(compressedBuf);
byte[] orig = readResourceFile("alice29.txt");
@ -88,11 +96,12 @@ public class SnappyInputStreamTest
assertEquals(orig.length, uncompressed.length);
assertArrayEquals(orig, uncompressed);
}
@Test
public void readBlockCompressedData() throws Exception {
public void readBlockCompressedData()
throws Exception
{
byte[] orig = readResourceFile("alice29.txt");
byte[] compressed = Snappy.compress(orig);
@ -104,7 +113,9 @@ public class SnappyInputStreamTest
}
@Test
public void biteWiseRead() throws Exception {
public void biteWiseRead()
throws Exception
{
byte[] orig = readResourceFile("testdata/calgary/paper6");
byte[] compressed = Snappy.compress(orig);
@ -113,11 +124,12 @@ public class SnappyInputStreamTest
assertEquals(orig.length, uncompressed.length);
assertArrayEquals(orig, uncompressed);
}
@Test
public void available() throws Exception {
public void available()
throws Exception
{
byte[] orig = readResourceFile("testdata/calgary/paper6");
byte[] compressed = Snappy.compress(orig);
@ -131,7 +143,9 @@ public class SnappyInputStreamTest
}
@Test
public void emptyStream() throws Exception {
public void emptyStream()
throws Exception
{
try {
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(new byte[0]));
byte[] uncompressed = readFully(in);
@ -143,7 +157,9 @@ public class SnappyInputStreamTest
}
}
public static byte[] compressResource(String resourcePath) throws Exception {
public static byte[] compressResource(String resourcePath)
throws Exception
{
ByteArrayOutputStream compressedBuf = new ByteArrayOutputStream();
SnappyOutputStream snappyOut = new SnappyOutputStream(compressedBuf);
byte[] orig = readResourceFile(resourcePath);
@ -153,7 +169,9 @@ public class SnappyInputStreamTest
}
@Test
public void chunkRead() throws Exception {
public void chunkRead()
throws Exception
{
byte[] chunk1 = compressResource("alice29.txt");
byte[] chunk2 = compressResource("testdata/calgary/paper6");
@ -175,5 +193,4 @@ public class SnappyInputStreamTest
assertArrayEquals(orig1, uncompressed1);
assertArrayEquals(orig2, uncompressed2);
}
}

View File

@ -43,20 +43,25 @@ public class SnappyLoaderTest
private static Logger _logger = Logger.getLogger(SnappyLoaderTest.class);
public static BufferedInputStream openByteStream(Class<?> referenceClass, String resourceFileName)
throws IOException {
throws IOException
{
URL url = FileResource.find(referenceClass, resourceFileName);
if (url != null) {
return new BufferedInputStream(url.openStream());
}
else
else {
return null;
}
}
public static <T> String loadIntoString(Class<T> referenceClass, String path) throws IOException {
public static <T> String loadIntoString(Class<T> referenceClass, String path)
throws IOException
{
BufferedInputStream in = openByteStream(referenceClass, path);
if (in == null)
if (in == null) {
throw new FileNotFoundException(
String.format("reference class:%s, path:%s", referenceClass.getName(), path));
}
ByteArrayOutputStream buf = new ByteArrayOutputStream();
try {
@ -73,7 +78,9 @@ public class SnappyLoaderTest
}
@Test
public void loadSnappyByDiffentClassloadersInTheSameJVM() throws Exception {
public void loadSnappyByDiffentClassloadersInTheSameJVM()
throws Exception
{
// Parent class loader cannot see Snappy.class
ClassLoader parent = this.getClass().getClassLoader().getParent();
@ -107,17 +114,22 @@ public class SnappyLoaderTest
}
@Test
public void load() throws Exception {
public void load()
throws Exception
{
SnappyLoader.load();
_logger.debug(Snappy.maxCompressedLength(1024));
}
@Test
public void autoLoad() throws Exception {
public void autoLoad()
throws Exception
{
_logger.debug(Snappy.maxCompressedLength(1024));
}
public static void main(String[] args) {
public static void main(String[] args)
{
// Test for loading native library specified in -Djava.library.path
System.setProperty(SnappyLoader.KEY_SNAPPY_USE_SYSTEMLIB, "true");
_logger.debug(Snappy.maxCompressedLength(1024));

View File

@ -44,7 +44,9 @@ public class SnappyOutputStreamTest
private static Logger _logger = Logger.getLogger(SnappyOutputStreamTest.class);
@Test
public void test() throws Exception {
public void test()
throws Exception
{
ByteArrayOutputStream buf = new ByteArrayOutputStream();
SnappyOutputStream sout = new SnappyOutputStream(buf);
@ -83,7 +85,9 @@ public class SnappyOutputStreamTest
}
@Test
public void bufferSize() throws Exception {
public void bufferSize()
throws Exception
{
ByteArrayOutputStream b = new ByteArrayOutputStream();
SnappyOutputStream os = new SnappyOutputStream(b, 1500);
final int bytesToWrite = 5000;
@ -96,12 +100,15 @@ public class SnappyOutputStreamTest
os.close();
SnappyInputStream is = new SnappyInputStream(new ByteArrayInputStream(b.toByteArray()));
byte[] buf = new byte[bytesToWrite / 101];
while (is.read(buf) != -1) {}
while (is.read(buf) != -1) {
}
is.close();
}
@Test
public void smallWrites() throws Exception {
public void smallWrites()
throws Exception
{
byte[] orig = CalgaryTest.readFile("alice29.txt");
ByteArrayOutputStream b = new ByteArrayOutputStream();
@ -125,11 +132,14 @@ public class SnappyOutputStreamTest
/**
* Compress the input array by passing it chunk-by-chunk to a SnappyOutputStream.
*
* @param orig the data to compress
* @param maxChunkSize the maximum chunk size, in bytes.
* @return the compressed bytes
*/
private static byte[] compressAsChunks(byte[] orig, int maxChunkSize) throws Exception {
private static byte[] compressAsChunks(byte[] orig, int maxChunkSize)
throws Exception
{
ByteArrayOutputStream b = new ByteArrayOutputStream();
SnappyOutputStream out = new SnappyOutputStream(b);
@ -143,7 +153,9 @@ public class SnappyOutputStreamTest
}
@Test
public void batchingOfWritesShouldNotAffectCompressedDataSize() throws Exception {
public void batchingOfWritesShouldNotAffectCompressedDataSize()
throws Exception
{
// Regression test for issue #100, a bug where the size of compressed data could be affected
// by the batching of writes to the SnappyOutputStream rather than the total amount of data
// written to the stream.
@ -163,7 +175,9 @@ public class SnappyOutputStreamTest
}
@Test
public void closeShouldBeIdempotent() throws Exception {
public void closeShouldBeIdempotent()
throws Exception
{
// Regression test for issue #107, a bug where close() was non-idempotent and would release
// its buffers to the allocator multiple times, which could cause scenarios where two open
// SnappyOutputStreams could share the same buffers, leading to stream corruption issues.
@ -197,38 +211,47 @@ public class SnappyOutputStreamTest
}
@Test
public void writingToClosedStreamShouldThrowIOException() throws IOException {
public void writingToClosedStreamShouldThrowIOException()
throws IOException
{
ByteArrayOutputStream b = new ByteArrayOutputStream();
SnappyOutputStream os = new SnappyOutputStream(b);
os.close();
try {
os.write(4);
fail("Expected write() to throw IOException");
} catch (IOException e) {
}
catch (IOException e) {
// Expected exception
}
try {
os.write(new int[] {1, 2, 3, 4});
fail("Expected write() to throw IOException");
} catch (IOException e) {
}
catch (IOException e) {
// Expected exception
}
}
@Test
public void flushingClosedStreamShouldThrowIOException() throws IOException {
public void flushingClosedStreamShouldThrowIOException()
throws IOException
{
ByteArrayOutputStream b = new ByteArrayOutputStream();
SnappyOutputStream os = new SnappyOutputStream(b);
os.close();
try {
os.flush();
} catch (IOException e) {
}
catch (IOException e) {
// Expected exception
}
}
@Test
public void closingStreamShouldMakeBuffersEligibleForGarbageCollection() throws IOException {
public void closingStreamShouldMakeBuffersEligibleForGarbageCollection()
throws IOException
{
ByteArrayOutputStream b = new ByteArrayOutputStream();
SnappyOutputStream os = new SnappyOutputStream(b, 4095, DefaultBufferAllocator.factory);
WeakReference<byte[]> inputBuffer = new WeakReference<byte[]>(os.inputBuffer);
@ -240,7 +263,9 @@ public class SnappyOutputStreamTest
}
@Test
public void longArrayCompress() throws Exception {
public void longArrayCompress()
throws Exception
{
long[] l = new long[10];
for (int i = 0; i < l.length; ++i) {
l[i] = i % 3 + i * 11;
@ -258,11 +283,12 @@ public class SnappyOutputStreamTest
assertEquals(10 * 8, readBytes);
assertArrayEquals(l, l2);
}
@Test
public void writeDoubleArray() throws Exception {
public void writeDoubleArray()
throws Exception
{
ByteArrayOutputStream b = new ByteArrayOutputStream();
SnappyOutputStream os = new SnappyOutputStream(b);
@ -279,7 +305,9 @@ public class SnappyOutputStreamTest
}
@Test
public void writeFloatArray() throws Exception {
public void writeFloatArray()
throws Exception
{
ByteArrayOutputStream b = new ByteArrayOutputStream();
SnappyOutputStream os = new SnappyOutputStream(b);
@ -296,7 +324,9 @@ public class SnappyOutputStreamTest
}
@Test
public void writeIntArray() throws Exception {
public void writeIntArray()
throws Exception
{
ByteArrayOutputStream b = new ByteArrayOutputStream();
SnappyOutputStream os = new SnappyOutputStream(b);
@ -313,7 +343,9 @@ public class SnappyOutputStreamTest
}
@Test
public void writeShortArray() throws Exception {
public void writeShortArray()
throws Exception
{
ByteArrayOutputStream b = new ByteArrayOutputStream();
SnappyOutputStream os = new SnappyOutputStream(b);
@ -328,5 +360,4 @@ public class SnappyOutputStreamTest
assertArrayEquals(orig, uncompressed);
}
}

View File

@ -38,13 +38,17 @@ public class SnappyTest
private static Logger _logger = Logger.getLogger(SnappyTest.class);
@Test
public void getVersion() throws Exception {
public void getVersion()
throws Exception
{
String version = Snappy.getNativeLibraryVersion();
_logger.debug("version: " + version);
}
@Test
public void directBufferCheck() throws Exception {
public void directBufferCheck()
throws Exception
{
try {
ByteBuffer src = ByteBuffer.allocate(1024);
@ -59,11 +63,12 @@ public class SnappyTest
}
fail("shouldn't reach here");
}
@Test
public void directBuffer() throws Exception {
public void directBuffer()
throws Exception
{
StringBuilder s = new StringBuilder();
for (int i = 0; i < 20; ++i) {
@ -109,7 +114,9 @@ public class SnappyTest
}
@Test
public void bufferOffset() throws Exception {
public void bufferOffset()
throws Exception
{
String m = "ACCAGGGGGGGGGGGGGGGGGGGGATAGATATTTCCCGAGATATTTTATATAAAAAAA";
byte[] orig = m.getBytes();
@ -147,7 +154,9 @@ public class SnappyTest
}
@Test
public void byteArrayCompress() throws Exception {
public void byteArrayCompress()
throws Exception
{
String m = "ACCAGGGGGGGGGGGGGGGGGGGGATAGATATTTCCCGAGATATTTTATATAAAAAAA";
byte[] input = m.getBytes();
@ -159,11 +168,12 @@ public class SnappyTest
int uncompressedSize = Snappy.uncompress(output, 0, compressedSize, uncompressed, 0);
String m2 = new String(uncompressed);
assertEquals(m, m2);
}
@Test
public void rangeCheck() throws Exception {
public void rangeCheck()
throws Exception
{
String m = "ACCAGGGGGGGGGGGGGGGGGGGGATAGATATTTCCCGAGATATTTTATATAAAAAAA";
byte[] input = m.getBytes();
byte[] output = new byte[Snappy.maxCompressedLength(input.length)];
@ -189,11 +199,12 @@ public class SnappyTest
bout.limit(bout.limit() - 1);
bout.position(1);
assertFalse(Snappy.isValidCompressedBuffer(bout));
}
@Test
public void highLevelAPI() throws Exception {
public void highLevelAPI()
throws Exception
{
String m = "Hello! 01234 ACGDSFSDFJ World. FDSDF02394234 fdsfda03924";
byte[] input = m.getBytes();
@ -205,7 +216,9 @@ public class SnappyTest
}
@Test
public void lowLevelAPI() throws Exception {
public void lowLevelAPI()
throws Exception
{
String m = "Hello! 01234 ACGDSFSDFJ World. FDSDF02394234 fdsfda03924";
byte[] input = m.getBytes();
@ -217,7 +230,9 @@ public class SnappyTest
}
@Test
public void simpleUsage() throws Exception {
public void simpleUsage()
throws Exception
{
String input = "Hello snappy-java! Snappy-java is a JNI-based wrapper"
+ " for using Snappy from Google (written in C++), a fast compresser/decompresser.";
@ -225,11 +240,12 @@ public class SnappyTest
byte[] uncompressed = Snappy.uncompress(compressed);
String result = new String(uncompressed, "UTF-8");
_logger.debug(result);
}
@Test
public void floatArray() throws Exception {
public void floatArray()
throws Exception
{
float[] data = new float[] {1.0f, -0.3f, 1.3f, 234.4f, 34};
byte[] compressed = Snappy.compress(data);
float[] result = Snappy.uncompressFloatArray(compressed);
@ -237,7 +253,9 @@ public class SnappyTest
}
@Test
public void doubleArray() throws Exception {
public void doubleArray()
throws Exception
{
double[] data = new double[] {1.0, -0.3, 1.3, 234.4, 34};
byte[] compressed = Snappy.compress(data);
double[] result = Snappy.uncompressDoubleArray(compressed);
@ -245,7 +263,9 @@ public class SnappyTest
}
@Test
public void longArray() throws Exception {
public void longArray()
throws Exception
{
long[] data = new long[] {2, 3, 15, 4234, 43251531412342342L, 23423422342L};
byte[] compressed = Snappy.compress(data);
long[] result = Snappy.uncompressLongArray(compressed);
@ -253,7 +273,9 @@ public class SnappyTest
}
@Test
public void shortArray() throws Exception {
public void shortArray()
throws Exception
{
short[] data = new short[] {432, -32267, 1, 3, 34, 43, 34, Short.MAX_VALUE, -1};
byte[] compressed = Snappy.compress(data);
short[] result = Snappy.uncompressShortArray(compressed);
@ -261,7 +283,9 @@ public class SnappyTest
}
@Test
public void intArray() throws Exception {
public void intArray()
throws Exception
{
int[] data = new int[] {432, -32267, 1, 3, 34, 43, 34, Short.MAX_VALUE, -1, Integer.MAX_VALUE, 3424, 43};
byte[] compressed = Snappy.compress(data);
int[] result = Snappy.uncompressIntArray(compressed);
@ -269,7 +293,9 @@ public class SnappyTest
}
@Test
public void charArray() throws Exception {
public void charArray()
throws Exception
{
char[] data = new char[] {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!'};
byte[] compressed = Snappy.compress(data);
char[] result = Snappy.uncompressCharArray(compressed);
@ -277,7 +303,9 @@ public class SnappyTest
}
@Test
public void string() throws Exception {
public void string()
throws Exception
{
String s = "Hello Snappy! Snappy! Snappy!";
byte[] compressed = Snappy.compress(s);
String uncompressedString = Snappy.uncompressString(compressed);
@ -285,7 +313,9 @@ public class SnappyTest
}
@Test
public void isValidCompressedData() throws Exception {
public void isValidCompressedData()
throws Exception
{
byte[] b = new byte[] {(byte) 91, (byte) 34, (byte) 80, (byte) 73, (byte) 34, (byte) 93};
@ -298,7 +328,5 @@ public class SnappyTest
catch (IOException e) {
_logger.debug(e);
}
}
}