Update issue 28
A workaround to postpone the initialization of SnappyNativeAPI.
This commit is contained in:
parent
2906673651
commit
f2fe1d3d8b
|
@ -1 +1 @@
|
||||||
acd53350367363d747e66caae0fe53b3d575ba4a wiki
|
0fb78fe3d92a2ff45ab3edb3d821d93619e43663 wiki
|
||||||
|
|
|
@ -36,12 +36,20 @@ import java.nio.ByteBuffer;
|
||||||
*/
|
*/
|
||||||
public class Snappy
|
public class Snappy
|
||||||
{
|
{
|
||||||
private static SnappyNativeAPI impl;
|
|
||||||
|
|
||||||
static {
|
static {
|
||||||
impl = SnappyLoader.load();
|
try {
|
||||||
|
impl = SnappyLoader.load();
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An instance of SnappyNativeAPI
|
||||||
|
*/
|
||||||
|
private static Object impl;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Copy bytes from source to destination
|
* Copy bytes from source to destination
|
||||||
*
|
*
|
||||||
|
@ -59,7 +67,7 @@ public class Snappy
|
||||||
*/
|
*/
|
||||||
public static void arrayCopy(Object src, int offset, int byteLength, Object dest, int dest_offset)
|
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);
|
((SnappyNativeAPI) impl).arrayCopy(src, offset, byteLength, dest, dest_offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -120,7 +128,8 @@ public class Snappy
|
||||||
// output: compressed
|
// output: compressed
|
||||||
int uPos = uncompressed.position();
|
int uPos = uncompressed.position();
|
||||||
int uLen = uncompressed.remaining();
|
int uLen = uncompressed.remaining();
|
||||||
int compressedSize = impl.rawCompress(uncompressed, uPos, uLen, compressed, compressed.position());
|
int compressedSize = ((SnappyNativeAPI) impl).rawCompress(uncompressed, uPos, uLen, compressed,
|
||||||
|
compressed.position());
|
||||||
|
|
||||||
// pos limit
|
// pos limit
|
||||||
// [ ......BBBBBBB.........]
|
// [ ......BBBBBBB.........]
|
||||||
|
@ -173,7 +182,7 @@ public class Snappy
|
||||||
* @return native library version
|
* @return native library version
|
||||||
*/
|
*/
|
||||||
public static String getNativeLibraryVersion() {
|
public static String getNativeLibraryVersion() {
|
||||||
return impl.nativeLibraryVersion();
|
return ((SnappyNativeAPI) impl).nativeLibraryVersion();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -185,7 +194,7 @@ public class Snappy
|
||||||
public static boolean isValidCompressedBuffer(byte[] input, int offset, int length) throws IOException {
|
public static boolean isValidCompressedBuffer(byte[] input, int offset, int length) throws IOException {
|
||||||
if (input == null)
|
if (input == null)
|
||||||
throw new NullPointerException("input is null");
|
throw new NullPointerException("input is null");
|
||||||
return impl.isValidCompressedBuffer(input, offset, length);
|
return ((SnappyNativeAPI) impl).isValidCompressedBuffer(input, offset, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -205,7 +214,8 @@ public class Snappy
|
||||||
* factor of four faster than actual decompression.
|
* 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());
|
return ((SnappyNativeAPI) impl).isValidCompressedBuffer(compressed, compressed.position(),
|
||||||
|
compressed.remaining());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -217,7 +227,7 @@ public class Snappy
|
||||||
* @return maximum byte size of the compressed data
|
* @return maximum byte size of the compressed data
|
||||||
*/
|
*/
|
||||||
public static int maxCompressedLength(int byteSize) {
|
public static int maxCompressedLength(int byteSize) {
|
||||||
return impl.maxCompressedLength(byteSize);
|
return ((SnappyNativeAPI) impl).maxCompressedLength(byteSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -231,7 +241,7 @@ public class Snappy
|
||||||
*/
|
*/
|
||||||
public static byte[] rawCompress(Object data, int byteSize) {
|
public static byte[] rawCompress(Object data, int byteSize) {
|
||||||
byte[] buf = new byte[Snappy.maxCompressedLength(byteSize)];
|
byte[] buf = new byte[Snappy.maxCompressedLength(byteSize)];
|
||||||
int compressedByteSize = impl.rawCompress(data, 0, byteSize, buf, 0);
|
int compressedByteSize = ((SnappyNativeAPI) impl).rawCompress(data, 0, byteSize, buf, 0);
|
||||||
byte[] result = new byte[compressedByteSize];
|
byte[] result = new byte[compressedByteSize];
|
||||||
System.arraycopy(buf, 0, result, 0, compressedByteSize);
|
System.arraycopy(buf, 0, result, 0, compressedByteSize);
|
||||||
return result;
|
return result;
|
||||||
|
@ -259,7 +269,8 @@ public class Snappy
|
||||||
if (input == null || output == null)
|
if (input == null || output == null)
|
||||||
throw new NullPointerException("input or output is null");
|
throw new NullPointerException("input or output is null");
|
||||||
|
|
||||||
int compressedSize = impl.rawCompress(input, inputOffset, inputLength, output, outputOffset);
|
int compressedSize = ((SnappyNativeAPI) impl)
|
||||||
|
.rawCompress(input, inputOffset, inputLength, output, outputOffset);
|
||||||
return compressedSize;
|
return compressedSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -290,7 +301,7 @@ public class Snappy
|
||||||
throws IOException {
|
throws IOException {
|
||||||
if (input == null || output == null)
|
if (input == null || output == null)
|
||||||
throw new NullPointerException("input or output is null");
|
throw new NullPointerException("input or output is null");
|
||||||
return impl.rawUncompress(input, inputOffset, inputLength, output, outputOffset);
|
return ((SnappyNativeAPI) impl).rawUncompress(input, inputOffset, inputLength, output, outputOffset);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -362,7 +373,8 @@ public class Snappy
|
||||||
|
|
||||||
// pos limit
|
// pos limit
|
||||||
// [ ......UUUUUU.........]
|
// [ ......UUUUUU.........]
|
||||||
int decompressedSize = impl.rawUncompress(compressed, cPos, cLen, uncompressed, uncompressed.position());
|
int decompressedSize = ((SnappyNativeAPI) impl).rawUncompress(compressed, cPos, cLen, uncompressed,
|
||||||
|
uncompressed.position());
|
||||||
uncompressed.limit(uncompressed.position() + decompressedSize);
|
uncompressed.limit(uncompressed.position() + decompressedSize);
|
||||||
|
|
||||||
return decompressedSize;
|
return decompressedSize;
|
||||||
|
@ -375,14 +387,14 @@ public class Snappy
|
||||||
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);
|
int uncompressedLength = Snappy.uncompressedLength(input, offset, length);
|
||||||
char[] result = new char[uncompressedLength / 2];
|
char[] result = new char[uncompressedLength / 2];
|
||||||
int byteSize = impl.rawUncompress(input, offset, length, result, 0);
|
int byteSize = ((SnappyNativeAPI) impl).rawUncompress(input, offset, length, result, 0);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static double[] uncompressDoubleArray(byte[] input) throws IOException {
|
public static double[] uncompressDoubleArray(byte[] input) throws IOException {
|
||||||
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 = impl.rawUncompress(input, 0, input.length, result, 0);
|
int byteSize = ((SnappyNativeAPI) impl).rawUncompress(input, 0, input.length, result, 0);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -397,7 +409,7 @@ public class Snappy
|
||||||
* {@link SnappyErrorCode#PARSING_ERROR}
|
* {@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);
|
return ((SnappyNativeAPI) impl).uncompressedLength(input, 0, input.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -416,7 +428,7 @@ public class Snappy
|
||||||
if (input == null)
|
if (input == null)
|
||||||
throw new NullPointerException("input is null");
|
throw new NullPointerException("input is null");
|
||||||
|
|
||||||
return impl.uncompressedLength(input, offset, length);
|
return ((SnappyNativeAPI) impl).uncompressedLength(input, offset, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -436,7 +448,7 @@ public class Snappy
|
||||||
if (!compressed.isDirect())
|
if (!compressed.isDirect())
|
||||||
throw new SnappyError(SnappyErrorCode.NOT_A_DIRECT_BUFFER, "input is not a direct buffer");
|
throw new SnappyError(SnappyErrorCode.NOT_A_DIRECT_BUFFER, "input is not a direct buffer");
|
||||||
|
|
||||||
return impl.uncompressedLength(compressed, compressed.position(), compressed.remaining());
|
return ((SnappyNativeAPI) impl).uncompressedLength(compressed, compressed.position(), compressed.remaining());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static float[] uncompressFloatArray(byte[] input) throws IOException {
|
public static float[] uncompressFloatArray(byte[] input) throws IOException {
|
||||||
|
@ -446,7 +458,7 @@ public class Snappy
|
||||||
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);
|
int uncompressedLength = Snappy.uncompressedLength(input, offset, length);
|
||||||
float[] result = new float[uncompressedLength / 4];
|
float[] result = new float[uncompressedLength / 4];
|
||||||
int byteSize = impl.rawUncompress(input, offset, length, result, 0);
|
int byteSize = ((SnappyNativeAPI) impl).rawUncompress(input, offset, length, result, 0);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -457,7 +469,7 @@ public class Snappy
|
||||||
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 uncompressedLength = Snappy.uncompressedLength(input, offset, length);
|
||||||
int[] result = new int[uncompressedLength / 4];
|
int[] result = new int[uncompressedLength / 4];
|
||||||
int byteSize = impl.rawUncompress(input, offset, length, result, 0);
|
int byteSize = ((SnappyNativeAPI) impl).rawUncompress(input, offset, length, result, 0);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -468,7 +480,7 @@ public class Snappy
|
||||||
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);
|
int uncompressedLength = Snappy.uncompressedLength(input, offset, length);
|
||||||
long[] result = new long[uncompressedLength / 8];
|
long[] result = new long[uncompressedLength / 8];
|
||||||
int byteSize = impl.rawUncompress(input, offset, length, result, 0);
|
int byteSize = ((SnappyNativeAPI) impl).rawUncompress(input, offset, length, result, 0);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -479,7 +491,7 @@ public class Snappy
|
||||||
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);
|
int uncompressedLength = Snappy.uncompressedLength(input, offset, length);
|
||||||
short[] result = new short[uncompressedLength / 2];
|
short[] result = new short[uncompressedLength / 2];
|
||||||
int byteSize = impl.rawUncompress(input, offset, length, result, 0);
|
int byteSize = ((SnappyNativeAPI) impl).rawUncompress(input, offset, length, result, 0);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -85,15 +85,15 @@ import java.util.Properties;
|
||||||
*/
|
*/
|
||||||
public class SnappyLoader
|
public class SnappyLoader
|
||||||
{
|
{
|
||||||
public static final String SNAPPY_SYSTEM_PROPERTIES_FILE = "org-xerial-snappy.properties";
|
public static final String SNAPPY_SYSTEM_PROPERTIES_FILE = "org-xerial-snappy.properties";
|
||||||
public static final String KEY_SNAPPY_LIB_PATH = "org.xerial.snappy.lib.path";
|
public static final String KEY_SNAPPY_LIB_PATH = "org.xerial.snappy.lib.path";
|
||||||
public static final String KEY_SNAPPY_LIB_NAME = "org.xerial.snappy.lib.name";
|
public static final String KEY_SNAPPY_LIB_NAME = "org.xerial.snappy.lib.name";
|
||||||
public static final String KEY_SNAPPY_TEMPDIR = "org.xerial.snappy.tempdir";
|
public static final String KEY_SNAPPY_TEMPDIR = "org.xerial.snappy.tempdir";
|
||||||
public static final String KEY_SNAPPY_USE_SYSTEMLIB = "org.xerial.snappy.use.systemlib";
|
public static final String KEY_SNAPPY_USE_SYSTEMLIB = "org.xerial.snappy.use.systemlib";
|
||||||
public static final String KEY_SNAPPY_DISABLE_BUNDLED_LIBS = "org.xerial.snappy.disable.bundled.libs"; // Depreciated, but preserved for backward compatibility
|
public static final String KEY_SNAPPY_DISABLE_BUNDLED_LIBS = "org.xerial.snappy.disable.bundled.libs"; // Depreciated, but preserved for backward compatibility
|
||||||
|
|
||||||
private static boolean isLoaded = false;
|
private static boolean isLoaded = false;
|
||||||
private static SnappyNativeAPI api = null;
|
private static Object api = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* load system properties when configuration file of the name
|
* load system properties when configuration file of the name
|
||||||
|
@ -205,7 +205,7 @@ public class SnappyLoader
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
static synchronized SnappyNativeAPI load() {
|
static synchronized Object load() {
|
||||||
|
|
||||||
if (api != null)
|
if (api != null)
|
||||||
return api;
|
return api;
|
||||||
|
@ -220,7 +220,8 @@ public class SnappyLoader
|
||||||
|
|
||||||
isLoaded = true;
|
isLoaded = true;
|
||||||
// Look up SnappyNative, injected to the root classloder, using reflection in order to avoid the initialization of SnappyNative class in this context class loader.
|
// Look up SnappyNative, injected to the root classloder, using reflection in order to avoid the initialization of SnappyNative class in this context class loader.
|
||||||
api = (SnappyNativeAPI) Class.forName("org.xerial.snappy.SnappyNative").newInstance();
|
Object nativeCode = Class.forName("org.xerial.snappy.SnappyNative").newInstance();
|
||||||
|
api = nativeCode;
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|
|
@ -33,8 +33,8 @@ import java.nio.ByteBuffer;
|
||||||
* SnappyNative.cpp
|
* SnappyNative.cpp
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>
|
||||||
* <b> DO NOT USE THIS CLASS DIRECTLY since the direct use of this class might
|
* <b> DO NOT USE THIS CLASS since the direct use of this class might break the
|
||||||
* break the native library code loading in {@link SnappyLoader}. </b>
|
* native library code loading in {@link SnappyLoader}. </b>
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @author leo
|
* @author leo
|
||||||
|
|
|
@ -115,6 +115,11 @@ public class SnappyLoaderTest
|
||||||
_logger.debug(Snappy.getNativeLibraryVersion());
|
_logger.debug(Snappy.getNativeLibraryVersion());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void autoLoad() throws Exception {
|
||||||
|
_logger.debug(Snappy.getNativeLibraryVersion());
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// Test for loading native library specified in -Djava.library.path
|
// Test for loading native library specified in -Djava.library.path
|
||||||
System.setProperty(SnappyLoader.KEY_SNAPPY_USE_SYSTEMLIB, "true");
|
System.setProperty(SnappyLoader.KEY_SNAPPY_USE_SYSTEMLIB, "true");
|
||||||
|
|
Loading…
Reference in New Issue