add primitive array compressor/decompressor

This commit is contained in:
Taro L. Saito 2011-04-06 17:45:43 +09:00
parent 8a27c47e85
commit 119a119b30
2 changed files with 38 additions and 10 deletions

View File

@ -63,12 +63,16 @@ public class Snappy
return compressBytes(input, input.length);
}
public static byte[] compress(short[] input) {
return compressBytes(input, input.length * 2); // short use 2 bytes
}
public static byte[] compress(char[] input) {
return compressBytes(input, input.length * 2); // short use 2 bytes
}
public static byte[] compress(short[] input) {
return compressBytes(input, input.length * 2); // short use 2 bytes
public static byte[] compress(int[] input) {
return compressBytes(input, input.length * 4); // int use 4 bytes
}
public static byte[] compress(float[] input) {
@ -107,18 +111,18 @@ public class Snappy
return result;
}
public static char[] uncompressChar(byte[] input) throws SnappyException {
public static short[] uncompressShort(byte[] input) throws SnappyException {
int uncompressedLength = Snappy.uncompressedLength(input, 0, input.length);
char[] result = new char[uncompressedLength / 2];
short[] result = new short[uncompressedLength / 2];
int byteSize = SnappyNative.rawUncompress(input, 0, input.length, result, 0);
if (byteSize != uncompressedLength)
throw new SnappyException(SnappyErrorCode.INVALID_DECOMPRESSION);
return result;
}
public static short[] uncompressShort(byte[] input) throws SnappyException {
public static char[] uncompressChar(byte[] input) throws SnappyException {
int uncompressedLength = Snappy.uncompressedLength(input, 0, input.length);
short[] result = new short[uncompressedLength / 2];
char[] result = new char[uncompressedLength / 2];
int byteSize = SnappyNative.rawUncompress(input, 0, input.length, result, 0);
if (byteSize != uncompressedLength)
throw new SnappyException(SnappyErrorCode.INVALID_DECOMPRESSION);
@ -143,18 +147,18 @@ public class Snappy
return result;
}
public static double[] uncompressDouble(byte[] input) throws SnappyException {
public static long[] uncompressLong(byte[] input) throws SnappyException {
int uncompressedLength = Snappy.uncompressedLength(input, 0, input.length);
double[] result = new double[uncompressedLength / 8];
long[] result = new long[uncompressedLength / 8];
int byteSize = SnappyNative.rawUncompress(input, 0, input.length, result, 0);
if (byteSize != uncompressedLength)
throw new SnappyException(SnappyErrorCode.INVALID_DECOMPRESSION);
return result;
}
public static long[] uncompressLong(byte[] input) throws SnappyException {
public static double[] uncompressDouble(byte[] input) throws SnappyException {
int uncompressedLength = Snappy.uncompressedLength(input, 0, input.length);
long[] result = new long[uncompressedLength / 8];
double[] result = new double[uncompressedLength / 8];
int byteSize = SnappyNative.rawUncompress(input, 0, input.length, result, 0);
if (byteSize != uncompressedLength)
throw new SnappyException(SnappyErrorCode.INVALID_DECOMPRESSION);

View File

@ -238,4 +238,28 @@ public class SnappyTest
assertArrayEquals(data, result);
}
@Test
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.uncompressShort(compressed);
assertArrayEquals(data, result);
}
@Test
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.uncompressInt(compressed);
assertArrayEquals(data, result);
}
@Test
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.uncompressChar(compressed);
assertArrayEquals(data, result);
}
}