add high-level API through byte[] for compression/decompression
This commit is contained in:
parent
c0b8b0be9c
commit
58eb961a93
|
@ -44,6 +44,41 @@ public class Snappy
|
|||
return SnappyNative.nativeLibraryVersion();
|
||||
}
|
||||
|
||||
/**
|
||||
* High-level API for compressing the input byte array. This method performs
|
||||
* array copy to generate the result. If you want to save this cost, use
|
||||
* {@link #compress(byte[], int, int, byte[], int)} or
|
||||
* {@link #compress(ByteBuffer, ByteBuffer)}.
|
||||
*
|
||||
* @param input
|
||||
* the input data
|
||||
* @return the compressed byte array
|
||||
* @throws SnappyException
|
||||
*/
|
||||
public static byte[] compress(byte[] input) throws SnappyException {
|
||||
byte[] buf = new byte[Snappy.maxCompressedLength(input.length)];
|
||||
int compressedByteSize = Snappy.compress(input, 0, input.length, buf, 0);
|
||||
byte[] result = new byte[compressedByteSize];
|
||||
System.arraycopy(buf, 0, result, 0, compressedByteSize);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* High-level API for uncompressing the input byte array.
|
||||
*
|
||||
* @param input
|
||||
* @return
|
||||
* @throws SnappyException
|
||||
*/
|
||||
public static byte[] uncompress(byte[] input) throws SnappyException {
|
||||
int uncompressedLength = Snappy.uncompressedLength(input, 0, input.length);
|
||||
byte[] result = new byte[uncompressedLength];
|
||||
int byteSize = Snappy.uncompress(input, 0, input.length, result, 0);
|
||||
if (byteSize != uncompressedLength)
|
||||
throw new SnappyException(SnappyErrorCode.INVALID_DECOMPRESSION);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compress the content in the given input buffer. After the compression,
|
||||
* you can retrieve the compressed data from the output buffer [pos() ...
|
||||
|
@ -167,13 +202,15 @@ public class Snappy
|
|||
}
|
||||
|
||||
/**
|
||||
* Get the uncompressed byte size of the given compressed input.
|
||||
* Get the uncompressed byte size of the given compressed input. This
|
||||
* operation taks O(1) time.
|
||||
*
|
||||
* @param compressed
|
||||
* input data [pos() ... limit())
|
||||
* @return uncompressed byte length of the given input
|
||||
* @throws SnappyException
|
||||
* when failed to uncompress the given input
|
||||
* 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
|
||||
*/
|
||||
|
@ -185,13 +222,16 @@ public class Snappy
|
|||
}
|
||||
|
||||
/**
|
||||
* Get the uncompressed byte size of the given compressed input
|
||||
* Get the uncompressed byte size of the given compressed input. This
|
||||
* operation takes O(1) time.
|
||||
*
|
||||
* @param input
|
||||
* @param offset
|
||||
* @param length
|
||||
* @return umcompressed byte size of the the given input data
|
||||
* @throws SnappyException
|
||||
* 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 SnappyException {
|
||||
if (input == null)
|
||||
|
|
|
@ -32,7 +32,12 @@ package org.xerial.snappy;
|
|||
*/
|
||||
public enum SnappyErrorCode {
|
||||
|
||||
UNKNOWN(0), FAILED_TO_LOAD_NATIVE_LIBRARY(1), PARSING_ERROR(2), NOT_A_DIRECT_BUFFER(3), OUT_OF_MEMORY(4);
|
||||
UNKNOWN(0),
|
||||
FAILED_TO_LOAD_NATIVE_LIBRARY(1),
|
||||
PARSING_ERROR(2),
|
||||
NOT_A_DIRECT_BUFFER(3),
|
||||
OUT_OF_MEMORY(4),
|
||||
INVALID_DECOMPRESSION(5);
|
||||
|
||||
public final int id;
|
||||
|
||||
|
|
|
@ -191,4 +191,16 @@ public class SnappyTest
|
|||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void highLevelAPI() throws Exception {
|
||||
|
||||
String m = "Hello! 01234 ACGDSFSDFJ World. FDSDF02394234 fdsfda03924";
|
||||
byte[] input = m.getBytes();
|
||||
byte[] output = Snappy.compress(input);
|
||||
|
||||
byte[] uncompressed = Snappy.uncompress(output);
|
||||
String m2 = new String(uncompressed);
|
||||
assertEquals(m, m2);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue