add encode(String), uncompressString(byte[])
This commit is contained in:
parent
508b24805a
commit
fd5cec5441
|
@ -24,6 +24,7 @@
|
||||||
//--------------------------------------
|
//--------------------------------------
|
||||||
package org.xerial.snappy;
|
package org.xerial.snappy;
|
||||||
|
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -87,6 +88,20 @@ public class Snappy
|
||||||
return rawCompress(input, input.length * 8); // double use 8 bytes
|
return rawCompress(input, input.length * 8); // double use 8 bytes
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static byte[] compress(String s, String encoding) throws UnsupportedEncodingException, SnappyException {
|
||||||
|
byte[] data = s.getBytes(encoding);
|
||||||
|
return compress(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] compress(String s) throws SnappyException {
|
||||||
|
try {
|
||||||
|
return compress(s, "UTF-8");
|
||||||
|
}
|
||||||
|
catch (UnsupportedEncodingException e) {
|
||||||
|
throw new IllegalStateException("UTF-8 encoder is not found");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compress the input data and produce an output array
|
* Compress the input data and produce an output array
|
||||||
*
|
*
|
||||||
|
@ -120,7 +135,7 @@ public class Snappy
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static short[] uncompressShort(byte[] input) throws SnappyException {
|
public static short[] uncompressShortArray(byte[] input) throws SnappyException {
|
||||||
int uncompressedLength = Snappy.uncompressedLength(input, 0, input.length);
|
int uncompressedLength = Snappy.uncompressedLength(input, 0, input.length);
|
||||||
short[] result = new short[uncompressedLength / 2];
|
short[] result = new short[uncompressedLength / 2];
|
||||||
int byteSize = SnappyNative.rawUncompress(input, 0, input.length, result, 0);
|
int byteSize = SnappyNative.rawUncompress(input, 0, input.length, result, 0);
|
||||||
|
@ -129,7 +144,7 @@ public class Snappy
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static char[] uncompressChar(byte[] input) throws SnappyException {
|
public static char[] uncompressCharArray(byte[] input) throws SnappyException {
|
||||||
int uncompressedLength = Snappy.uncompressedLength(input, 0, input.length);
|
int uncompressedLength = Snappy.uncompressedLength(input, 0, input.length);
|
||||||
char[] result = new char[uncompressedLength / 2];
|
char[] result = new char[uncompressedLength / 2];
|
||||||
int byteSize = SnappyNative.rawUncompress(input, 0, input.length, result, 0);
|
int byteSize = SnappyNative.rawUncompress(input, 0, input.length, result, 0);
|
||||||
|
@ -138,7 +153,7 @@ public class Snappy
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int[] uncompressInt(byte[] input) throws SnappyException {
|
public static int[] uncompressIntArray(byte[] input) throws SnappyException {
|
||||||
int uncompressedLength = Snappy.uncompressedLength(input, 0, input.length);
|
int uncompressedLength = Snappy.uncompressedLength(input, 0, input.length);
|
||||||
int[] result = new int[uncompressedLength / 4];
|
int[] result = new int[uncompressedLength / 4];
|
||||||
int byteSize = SnappyNative.rawUncompress(input, 0, input.length, result, 0);
|
int byteSize = SnappyNative.rawUncompress(input, 0, input.length, result, 0);
|
||||||
|
@ -147,7 +162,7 @@ public class Snappy
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static float[] uncompressFloat(byte[] input) throws SnappyException {
|
public static float[] uncompressFloatArray(byte[] input) throws SnappyException {
|
||||||
int uncompressedLength = Snappy.uncompressedLength(input, 0, input.length);
|
int uncompressedLength = Snappy.uncompressedLength(input, 0, input.length);
|
||||||
float[] result = new float[uncompressedLength / 4];
|
float[] result = new float[uncompressedLength / 4];
|
||||||
int byteSize = SnappyNative.rawUncompress(input, 0, input.length, result, 0);
|
int byteSize = SnappyNative.rawUncompress(input, 0, input.length, result, 0);
|
||||||
|
@ -156,7 +171,7 @@ public class Snappy
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static long[] uncompressLong(byte[] input) throws SnappyException {
|
public static long[] uncompressLongArray(byte[] input) throws SnappyException {
|
||||||
int uncompressedLength = Snappy.uncompressedLength(input, 0, input.length);
|
int uncompressedLength = Snappy.uncompressedLength(input, 0, input.length);
|
||||||
long[] result = new long[uncompressedLength / 8];
|
long[] result = new long[uncompressedLength / 8];
|
||||||
int byteSize = SnappyNative.rawUncompress(input, 0, input.length, result, 0);
|
int byteSize = SnappyNative.rawUncompress(input, 0, input.length, result, 0);
|
||||||
|
@ -165,7 +180,7 @@ public class Snappy
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static double[] uncompressDouble(byte[] input) throws SnappyException {
|
public static double[] uncompressDoubleArray(byte[] input) throws SnappyException {
|
||||||
int uncompressedLength = Snappy.uncompressedLength(input, 0, input.length);
|
int uncompressedLength = Snappy.uncompressedLength(input, 0, input.length);
|
||||||
double[] result = new double[uncompressedLength / 8];
|
double[] result = new double[uncompressedLength / 8];
|
||||||
int byteSize = SnappyNative.rawUncompress(input, 0, input.length, result, 0);
|
int byteSize = SnappyNative.rawUncompress(input, 0, input.length, result, 0);
|
||||||
|
@ -174,6 +189,21 @@ public class Snappy
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String uncompressString(byte[] input, String encoding) throws SnappyException,
|
||||||
|
UnsupportedEncodingException {
|
||||||
|
byte[] uncompressed = uncompress(input);
|
||||||
|
return new String(uncompressed, encoding);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String uncompressString(byte[] input) throws SnappyException {
|
||||||
|
try {
|
||||||
|
return uncompressString(input, "UTF-8");
|
||||||
|
}
|
||||||
|
catch (UnsupportedEncodingException e) {
|
||||||
|
throw new IllegalStateException("UTF-8 decoder is not found");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compress the content in the given input buffer. After the compression,
|
* Compress the content in the given input buffer. After the compression,
|
||||||
* you can retrieve the compressed data from the output buffer [pos() ...
|
* you can retrieve the compressed data from the output buffer [pos() ...
|
||||||
|
|
|
@ -230,7 +230,7 @@ public class SnappyTest
|
||||||
public void floatArray() throws Exception {
|
public void floatArray() throws Exception {
|
||||||
float[] data = new float[] { 1.0f, -0.3f, 1.3f, 234.4f, 34 };
|
float[] data = new float[] { 1.0f, -0.3f, 1.3f, 234.4f, 34 };
|
||||||
byte[] compressed = Snappy.compress(data);
|
byte[] compressed = Snappy.compress(data);
|
||||||
float[] result = Snappy.uncompressFloat(compressed);
|
float[] result = Snappy.uncompressFloatArray(compressed);
|
||||||
assertArrayEquals(data, result, 0.0f);
|
assertArrayEquals(data, result, 0.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -238,7 +238,7 @@ public class SnappyTest
|
||||||
public void doubleArray() throws Exception {
|
public void doubleArray() throws Exception {
|
||||||
double[] data = new double[] { 1.0, -0.3, 1.3, 234.4, 34 };
|
double[] data = new double[] { 1.0, -0.3, 1.3, 234.4, 34 };
|
||||||
byte[] compressed = Snappy.compress(data);
|
byte[] compressed = Snappy.compress(data);
|
||||||
double[] result = Snappy.uncompressDouble(compressed);
|
double[] result = Snappy.uncompressDoubleArray(compressed);
|
||||||
assertArrayEquals(data, result, 0.0f);
|
assertArrayEquals(data, result, 0.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -246,7 +246,7 @@ public class SnappyTest
|
||||||
public void longArray() throws Exception {
|
public void longArray() throws Exception {
|
||||||
long[] data = new long[] { 2, 3, 15, 4234, 43251531412342342L, 23423422342L };
|
long[] data = new long[] { 2, 3, 15, 4234, 43251531412342342L, 23423422342L };
|
||||||
byte[] compressed = Snappy.compress(data);
|
byte[] compressed = Snappy.compress(data);
|
||||||
long[] result = Snappy.uncompressLong(compressed);
|
long[] result = Snappy.uncompressLongArray(compressed);
|
||||||
assertArrayEquals(data, result);
|
assertArrayEquals(data, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -254,7 +254,7 @@ public class SnappyTest
|
||||||
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 };
|
short[] data = new short[] { 432, -32267, 1, 3, 34, 43, 34, Short.MAX_VALUE, -1 };
|
||||||
byte[] compressed = Snappy.compress(data);
|
byte[] compressed = Snappy.compress(data);
|
||||||
short[] result = Snappy.uncompressShort(compressed);
|
short[] result = Snappy.uncompressShortArray(compressed);
|
||||||
assertArrayEquals(data, result);
|
assertArrayEquals(data, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -262,7 +262,7 @@ public class SnappyTest
|
||||||
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 };
|
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);
|
byte[] compressed = Snappy.compress(data);
|
||||||
int[] result = Snappy.uncompressInt(compressed);
|
int[] result = Snappy.uncompressIntArray(compressed);
|
||||||
assertArrayEquals(data, result);
|
assertArrayEquals(data, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -270,8 +270,16 @@ public class SnappyTest
|
||||||
public void charArray() throws Exception {
|
public void charArray() throws Exception {
|
||||||
char[] data = new char[] { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!' };
|
char[] data = new char[] { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!' };
|
||||||
byte[] compressed = Snappy.compress(data);
|
byte[] compressed = Snappy.compress(data);
|
||||||
char[] result = Snappy.uncompressChar(compressed);
|
char[] result = Snappy.uncompressCharArray(compressed);
|
||||||
assertArrayEquals(data, result);
|
assertArrayEquals(data, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void string() throws Exception {
|
||||||
|
String s = "Hello Snappy! Snappy! Snappy!";
|
||||||
|
byte[] compressed = Snappy.compress(s);
|
||||||
|
String uncompressedString = Snappy.uncompressString(compressed);
|
||||||
|
assertEquals(s, uncompressedString);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue