Use SnappyNativeAPI as an interface to the native code

This commit is contained in:
Taro L. Saito 2011-06-27 10:29:58 +09:00
parent 06ba339352
commit 7134608206
12 changed files with 185 additions and 91 deletions

View File

@ -36,15 +36,16 @@ import java.nio.ByteBuffer;
*/ */
public class Snappy public class Snappy
{ {
private static SnappyNativeAPI impl;
static { static {
SnappyLoader.load(); impl = SnappyLoader.load();
} }
/** /**
* High-level API for compressing the input byte array. This method performs * 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 * array copy to generate the result. If you want to reduce the memory copy
* {@link #compress(byte[], int, int, byte[], int)} or * cost, use {@link #compress(byte[], int, int, byte[], int)} or
* {@link #compress(ByteBuffer, ByteBuffer)}. * {@link #compress(ByteBuffer, ByteBuffer)}.
* *
* @param input * @param input
@ -99,7 +100,7 @@ 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 = SnappyNative.rawCompress(uncompressed, uPos, uLen, compressed, compressed.position()); int compressedSize = impl.rawCompress(uncompressed, uPos, uLen, compressed, compressed.position());
// pos limit // pos limit
// [ ......BBBBBBB.........] // [ ......BBBBBBB.........]
@ -152,7 +153,7 @@ public class Snappy
* @return native library version * @return native library version
*/ */
public static String getNativeLibraryVersion() { public static String getNativeLibraryVersion() {
return SnappyNative.nativeLibraryVersion(); return impl.nativeLibraryVersion();
} }
/** /**
@ -164,7 +165,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 SnappyNative.isValidCompressedBuffer(input, offset, length); return impl.isValidCompressedBuffer(input, offset, length);
} }
/** /**
@ -184,7 +185,7 @@ 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 SnappyNative.isValidCompressedBuffer(compressed, compressed.position(), compressed.remaining()); return impl.isValidCompressedBuffer(compressed, compressed.position(), compressed.remaining());
} }
/** /**
@ -196,7 +197,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 SnappyNative.maxCompressedLength(byteSize); return impl.maxCompressedLength(byteSize);
} }
/** /**
@ -210,7 +211,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 = SnappyNative.rawCompress(data, 0, byteSize, buf, 0); int compressedByteSize = 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;
@ -238,7 +239,7 @@ 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 = SnappyNative.rawCompress(input, inputOffset, inputLength, output, outputOffset); int compressedSize = impl.rawCompress(input, inputOffset, inputLength, output, outputOffset);
return compressedSize; return compressedSize;
} }
@ -269,7 +270,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 SnappyNative.rawUncompress(input, inputOffset, inputLength, output, outputOffset); return impl.rawUncompress(input, inputOffset, inputLength, output, outputOffset);
} }
/** /**
@ -341,8 +342,7 @@ public class Snappy
// pos limit // pos limit
// [ ......UUUUUU.........] // [ ......UUUUUU.........]
int decompressedSize = SnappyNative int decompressedSize = impl.rawUncompress(compressed, cPos, cLen, uncompressed, uncompressed.position());
.rawUncompress(compressed, cPos, cLen, uncompressed, uncompressed.position());
uncompressed.limit(uncompressed.position() + decompressedSize); uncompressed.limit(uncompressed.position() + decompressedSize);
return decompressedSize; return decompressedSize;
@ -355,14 +355,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 = SnappyNative.rawUncompress(input, offset, length, result, 0); int byteSize = 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 = SnappyNative.rawUncompress(input, 0, input.length, result, 0); int byteSize = impl.rawUncompress(input, 0, input.length, result, 0);
return result; return result;
} }
@ -377,7 +377,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 SnappyNative.uncompressedLength(input, 0, input.length); return impl.uncompressedLength(input, 0, input.length);
} }
/** /**
@ -396,7 +396,7 @@ public class Snappy
if (input == null) if (input == null)
throw new NullPointerException("input is null"); throw new NullPointerException("input is null");
return SnappyNative.uncompressedLength(input, offset, length); return impl.uncompressedLength(input, offset, length);
} }
public static class CompressedDataLength public static class CompressedDataLength
@ -473,7 +473,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 SnappyNative.uncompressedLength(compressed, compressed.position(), compressed.remaining()); return impl.uncompressedLength(compressed, compressed.position(), compressed.remaining());
} }
public static float[] uncompressFloatArray(byte[] input) throws IOException { public static float[] uncompressFloatArray(byte[] input) throws IOException {
@ -483,7 +483,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 = SnappyNative.rawUncompress(input, offset, length, result, 0); int byteSize = impl.rawUncompress(input, offset, length, result, 0);
return result; return result;
} }
@ -494,7 +494,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 = SnappyNative.rawUncompress(input, offset, length, result, 0); int byteSize = impl.rawUncompress(input, offset, length, result, 0);
return result; return result;
} }
@ -505,7 +505,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 = SnappyNative.rawUncompress(input, offset, length, result, 0); int byteSize = impl.rawUncompress(input, offset, length, result, 0);
return result; return result;
} }
@ -516,7 +516,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 = SnappyNative.rawUncompress(input, offset, length, result, 0); int byteSize = impl.rawUncompress(input, offset, length, result, 0);
return result; return result;
} }

View File

@ -39,6 +39,11 @@ public class SnappyError extends Error
public final SnappyErrorCode errorCode; public final SnappyErrorCode errorCode;
public SnappyError(SnappyErrorCode code) {
super();
this.errorCode = code;
}
public SnappyError(SnappyErrorCode code, Error e) { public SnappyError(SnappyErrorCode code, Error e) {
super(e); super(e);
this.errorCode = code; this.errorCode = code;

View File

@ -32,7 +32,7 @@ package org.xerial.snappy;
*/ */
public enum SnappyErrorCode { public enum SnappyErrorCode {
// DO NOT change the id of the error codes since these IDs are also used in SnappyNative.cpp // DO NOT change these error code IDs because these numbers are used inside SnappyNative.cpp
UNKNOWN(0), UNKNOWN(0),
FAILED_TO_LOAD_NATIVE_LIBRARY(1), FAILED_TO_LOAD_NATIVE_LIBRARY(1),
PARSING_ERROR(2), PARSING_ERROR(2),

View File

@ -34,7 +34,7 @@ import java.io.IOException;
* *
*/ */
@Deprecated @Deprecated
public class SnappyException extends Exception public class SnappyException extends IOException
{ {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@ -45,6 +45,7 @@ public class SnappyException extends Exception
} }
public SnappyException(SnappyErrorCode errorCode) { public SnappyException(SnappyErrorCode errorCode) {
super();
this.errorCode = errorCode; this.errorCode = errorCode;
} }

View File

@ -83,12 +83,13 @@ public class SnappyLoader
{ {
private static boolean isInitialized = false; private static boolean isInitialized = false;
private static boolean isLoaded = false; private static boolean isLoaded = false;
private static SnappyNativeAPI api = null;
private static ClassLoader getAncestorClassLoader() { private static ClassLoader getRootClassLoader() {
ClassLoader cl = SnappyLoader.class.getClassLoader(); ClassLoader cl = SnappyLoader.class.getClassLoader();
// while (cl.getParent() != null) { while (cl.getParent() != null) {
// cl = cl.getParent(); cl = cl.getParent();
// } }
return cl; return cl;
} }
@ -111,21 +112,22 @@ public class SnappyLoader
return isLoaded; return isLoaded;
} }
static void load() { static SnappyNativeAPI load() {
if (isInitialized) if (isInitialized)
return; return api;
isInitialized = true; isInitialized = true;
final String nativeLoaderClassName = "org.xerial.snappy.SnappyNativeLoader"; final String nativeLoaderClassName = "org.xerial.snappy.SnappyNativeLoader";
final String[] classesToPreload = new String[] { "org.xerial.snappy.SnappyNative", final String[] classesToPreload = new String[] { "org.xerial.snappy.SnappyNativeAPI",
"org.xerial.snappy.SnappyErrorCode" }; "org.xerial.snappy.SnappyNative", "org.xerial.snappy.SnappyErrorCode" };
try { try {
Class.forName(nativeLoaderClassName); Class< ? > c = Class.forName(nativeLoaderClassName);
// If this native loader class is already defined, it means that another class loader already loaded the native library of snappy // If this native loader class is already defined, it means that another class loader already loaded the native library of snappy
api = (SnappyNativeAPI) Class.forName("org.xerial.snappy.SnappyNative").newInstance();
isLoaded = true; isLoaded = true;
return; return api;
} }
catch (ClassNotFoundException e) { catch (ClassNotFoundException e) {
try { try {
@ -142,18 +144,19 @@ public class SnappyLoader
Method defineClass = classLoader.getDeclaredMethod("defineClass", new Class[] { String.class, Method defineClass = classLoader.getDeclaredMethod("defineClass", new Class[] { String.class,
byte[].class, int.class, int.class, ProtectionDomain.class }); byte[].class, int.class, int.class, ProtectionDomain.class });
ClassLoader systemClassLoader = getAncestorClassLoader(); // Use parent class loader to load SnappyNative, since Tomcat, which uses different class loaders for each webapps, cannot load JNI interface twice
ClassLoader systemClassLoader = getRootClassLoader();
ProtectionDomain pd = System.class.getProtectionDomain();
// ClassLoader.defineClass is a protected method, so we have to make it accessible // ClassLoader.defineClass is a protected method, so we have to make it accessible
defineClass.setAccessible(true); defineClass.setAccessible(true);
try { try {
// Create a new class using a ClassLoader#defineClass // Create a new class using a ClassLoader#defineClass
defineClass.invoke(systemClassLoader, nativeLoaderClassName, byteCode, 0, byteCode.length, defineClass.invoke(systemClassLoader, nativeLoaderClassName, byteCode, 0, byteCode.length, pd);
System.class.getProtectionDomain());
for (int i = 0; i < classesToPreload.length; ++i) { for (int i = 0; i < classesToPreload.length; ++i) {
byte[] b = preloadClassByteCode.get(i); byte[] b = preloadClassByteCode.get(i);
defineClass.invoke(systemClassLoader, classesToPreload[i], b, 0, b.length, defineClass.invoke(systemClassLoader, classesToPreload[i], b, 0, b.length, pd);
System.class.getProtectionDomain());
} }
} }
finally { finally {
@ -182,13 +185,23 @@ public class SnappyLoader
systemClassLoader.loadClass(each); systemClassLoader.loadClass(each);
} }
isLoaded = true; isLoaded = true;
api = (SnappyNativeAPI) Class.forName("org.xerial.snappy.SnappyNative").newInstance();
return api;
} }
} }
catch (ClassNotFoundException ee) {
throw new SnappyError(SnappyErrorCode.FAILED_TO_LOAD_NATIVE_LIBRARY, ee.getMessage());
}
catch (Exception e2) { catch (Exception e2) {
throw new SnappyError(SnappyErrorCode.FAILED_TO_LOAD_NATIVE_LIBRARY, e.getMessage()); throw new SnappyError(SnappyErrorCode.FAILED_TO_LOAD_NATIVE_LIBRARY, e.getMessage());
} }
} }
catch (Exception e) {
throw new SnappyError(SnappyErrorCode.FAILED_TO_LOAD_NATIVE_LIBRARY, e.getMessage());
}
throw new SnappyError(SnappyErrorCode.FAILED_TO_LOAD_NATIVE_LIBRARY);
} }
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";

View File

@ -17,17 +17,20 @@
#include <snappy.h> #include <snappy.h>
#include "SnappyNative.h" #include "SnappyNative.h"
void throw_exception(JNIEnv *env, jclass self, int errorCode) void throw_exception(JNIEnv *env, jobject self, int errorCode)
{ {
jmethodID mth_throwex = env->GetStaticMethodID(self, "throw_error", "(I)V"); jclass c = env->FindClass("Lorg/xerial/snappy/SnappyNative;");
if(c==0)
return;
jmethodID mth_throwex = env->GetMethodID(c, "throw_error", "(I)V");
if(mth_throwex == 0) if(mth_throwex == 0)
return; return;
env->CallStaticVoidMethod(self, mth_throwex, (jint) errorCode); env->CallVoidMethod(self, mth_throwex, (jint) errorCode);
} }
JNIEXPORT jstring JNICALL Java_org_xerial_snappy_SnappyNative_nativeLibraryVersion JNIEXPORT jstring JNICALL Java_org_xerial_snappy_SnappyNative_nativeLibraryVersion
(JNIEnv * env, jclass self) (JNIEnv * env, jobject self)
{ {
return env->NewStringUTF("1.0.3"); return env->NewStringUTF("1.0.3");
} }
@ -38,7 +41,7 @@ JNIEXPORT jstring JNICALL Java_org_xerial_snappy_SnappyNative_nativeLibraryVersi
* Signature: (Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;)J * Signature: (Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;)J
*/ */
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress__Ljava_nio_ByteBuffer_2IILjava_nio_ByteBuffer_2I JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress__Ljava_nio_ByteBuffer_2IILjava_nio_ByteBuffer_2I
(JNIEnv* env, jclass self, jobject uncompressed, jint upos, jint ulen, jobject compressed, jint cpos) (JNIEnv* env, jobject self, jobject uncompressed, jint upos, jint ulen, jobject compressed, jint cpos)
{ {
char* uncompressedBuffer = (char*) env->GetDirectBufferAddress(uncompressed); char* uncompressedBuffer = (char*) env->GetDirectBufferAddress(uncompressed);
char* compressedBuffer = (char*) env->GetDirectBufferAddress(compressed); char* compressedBuffer = (char*) env->GetDirectBufferAddress(compressed);
@ -54,7 +57,7 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress__Ljava_ni
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress__Ljava_lang_Object_2IILjava_lang_Object_2I JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress__Ljava_lang_Object_2IILjava_lang_Object_2I
(JNIEnv * env, jclass self, jobject input, jint inputOffset, jint inputLen, jobject output, jint outputOffset) (JNIEnv * env, jobject self, jobject input, jint inputOffset, jint inputLen, jobject output, jint outputOffset)
{ {
char* in = (char*) env->GetPrimitiveArrayCritical((jarray) input, 0); char* in = (char*) env->GetPrimitiveArrayCritical((jarray) input, 0);
char* out = (char*) env->GetPrimitiveArrayCritical((jarray) output, 0); char* out = (char*) env->GetPrimitiveArrayCritical((jarray) output, 0);
@ -74,7 +77,7 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress__Ljava_la
} }
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress__Ljava_lang_Object_2IILjava_lang_Object_2I JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress__Ljava_lang_Object_2IILjava_lang_Object_2I
(JNIEnv * env, jclass self, jobject input, jint inputOffset, jint inputLength, jobject output, jint outputOffset) (JNIEnv * env, jobject self, jobject input, jint inputOffset, jint inputLength, jobject output, jint outputOffset)
{ {
char* in = (char*) env->GetPrimitiveArrayCritical((jarray) input, 0); char* in = (char*) env->GetPrimitiveArrayCritical((jarray) input, 0);
char* out = (char*) env->GetPrimitiveArrayCritical((jarray) output, 0); char* out = (char*) env->GetPrimitiveArrayCritical((jarray) output, 0);
@ -106,7 +109,7 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress__Ljava_
* Signature: (Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;)Z * Signature: (Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;)Z
*/ */
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress__Ljava_nio_ByteBuffer_2IILjava_nio_ByteBuffer_2I JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress__Ljava_nio_ByteBuffer_2IILjava_nio_ByteBuffer_2I
(JNIEnv * env, jclass self, jobject compressed, jint cpos, jint clen, jobject decompressed, jint dpos) (JNIEnv * env, jobject self, jobject compressed, jint cpos, jint clen, jobject decompressed, jint dpos)
{ {
char* compressedBuffer = (char*) env->GetDirectBufferAddress(compressed); char* compressedBuffer = (char*) env->GetDirectBufferAddress(compressed);
char* decompressedBuffer = (char*) env->GetDirectBufferAddress(decompressed); char* decompressedBuffer = (char*) env->GetDirectBufferAddress(decompressed);
@ -135,7 +138,7 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress__Ljava_
*/ */
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_maxCompressedLength JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_maxCompressedLength
(JNIEnv *, jclass, jint size) (JNIEnv *, jobject, jint size)
{ {
size_t l = snappy::MaxCompressedLength((size_t) size); size_t l = snappy::MaxCompressedLength((size_t) size);
return (jint) l; return (jint) l;
@ -147,7 +150,7 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_maxCompressedLength
* Signature: (Ljava/nio/ByteBuffer;)J * Signature: (Ljava/nio/ByteBuffer;)J
*/ */
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength__Ljava_nio_ByteBuffer_2II JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength__Ljava_nio_ByteBuffer_2II
(JNIEnv * env, jclass self, jobject compressed, jint cpos, jint clen) (JNIEnv * env, jobject self, jobject compressed, jint cpos, jint clen)
{ {
char* compressedBuffer = (char*) env->GetDirectBufferAddress(compressed); char* compressedBuffer = (char*) env->GetDirectBufferAddress(compressed);
if(compressedBuffer == 0) { if(compressedBuffer == 0) {
@ -165,7 +168,7 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength__L
} }
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength__Ljava_lang_Object_2II JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength__Ljava_lang_Object_2II
(JNIEnv * env, jclass self, jobject input, jint offset, jint length) (JNIEnv * env, jobject self, jobject input, jint offset, jint length)
{ {
char* in = (char*) env->GetPrimitiveArrayCritical((jarray) input, 0); char* in = (char*) env->GetPrimitiveArrayCritical((jarray) input, 0);
if(in == 0) { if(in == 0) {
@ -187,7 +190,7 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength__L
} }
JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_isValidCompressedBuffer__Ljava_nio_ByteBuffer_2II JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_isValidCompressedBuffer__Ljava_nio_ByteBuffer_2II
(JNIEnv * env, jclass self, jobject compressed, jint cpos, jint clen) (JNIEnv * env, jobject self, jobject compressed, jint cpos, jint clen)
{ {
char* compressedBuffer = (char*) env->GetDirectBufferAddress(compressed); char* compressedBuffer = (char*) env->GetDirectBufferAddress(compressed);
if(compressedBuffer == 0) { if(compressedBuffer == 0) {
@ -200,7 +203,7 @@ JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_isValidCompressed
JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_isValidCompressedBuffer__Ljava_lang_Object_2II JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_isValidCompressedBuffer__Ljava_lang_Object_2II
(JNIEnv * env, jclass self, jobject input, jint offset, jint length) (JNIEnv * env, jobject self, jobject input, jint offset, jint length)
{ {
char* in = (char*) env->GetPrimitiveArrayCritical((jarray) input, 0); char* in = (char*) env->GetPrimitiveArrayCritical((jarray) input, 0);
if(in == 0) { if(in == 0) {

View File

@ -13,7 +13,7 @@ extern "C" {
* Signature: ()Ljava/lang/String; * Signature: ()Ljava/lang/String;
*/ */
JNIEXPORT jstring JNICALL Java_org_xerial_snappy_SnappyNative_nativeLibraryVersion JNIEXPORT jstring JNICALL Java_org_xerial_snappy_SnappyNative_nativeLibraryVersion
(JNIEnv *, jclass); (JNIEnv *, jobject);
/* /*
* Class: org_xerial_snappy_SnappyNative * Class: org_xerial_snappy_SnappyNative
@ -21,7 +21,7 @@ JNIEXPORT jstring JNICALL Java_org_xerial_snappy_SnappyNative_nativeLibraryVersi
* Signature: (Ljava/nio/ByteBuffer;IILjava/nio/ByteBuffer;I)I * Signature: (Ljava/nio/ByteBuffer;IILjava/nio/ByteBuffer;I)I
*/ */
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress__Ljava_nio_ByteBuffer_2IILjava_nio_ByteBuffer_2I JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress__Ljava_nio_ByteBuffer_2IILjava_nio_ByteBuffer_2I
(JNIEnv *, jclass, jobject, jint, jint, jobject, jint); (JNIEnv *, jobject, jobject, jint, jint, jobject, jint);
/* /*
* Class: org_xerial_snappy_SnappyNative * Class: org_xerial_snappy_SnappyNative
@ -29,7 +29,7 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress__Ljava_ni
* Signature: (Ljava/lang/Object;IILjava/lang/Object;I)I * Signature: (Ljava/lang/Object;IILjava/lang/Object;I)I
*/ */
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress__Ljava_lang_Object_2IILjava_lang_Object_2I JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress__Ljava_lang_Object_2IILjava_lang_Object_2I
(JNIEnv *, jclass, jobject, jint, jint, jobject, jint); (JNIEnv *, jobject, jobject, jint, jint, jobject, jint);
/* /*
* Class: org_xerial_snappy_SnappyNative * Class: org_xerial_snappy_SnappyNative
@ -37,7 +37,7 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress__Ljava_la
* Signature: (Ljava/nio/ByteBuffer;IILjava/nio/ByteBuffer;I)I * Signature: (Ljava/nio/ByteBuffer;IILjava/nio/ByteBuffer;I)I
*/ */
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress__Ljava_nio_ByteBuffer_2IILjava_nio_ByteBuffer_2I JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress__Ljava_nio_ByteBuffer_2IILjava_nio_ByteBuffer_2I
(JNIEnv *, jclass, jobject, jint, jint, jobject, jint); (JNIEnv *, jobject, jobject, jint, jint, jobject, jint);
/* /*
* Class: org_xerial_snappy_SnappyNative * Class: org_xerial_snappy_SnappyNative
@ -45,7 +45,7 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress__Ljava_
* Signature: (Ljava/lang/Object;IILjava/lang/Object;I)I * Signature: (Ljava/lang/Object;IILjava/lang/Object;I)I
*/ */
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress__Ljava_lang_Object_2IILjava_lang_Object_2I JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress__Ljava_lang_Object_2IILjava_lang_Object_2I
(JNIEnv *, jclass, jobject, jint, jint, jobject, jint); (JNIEnv *, jobject, jobject, jint, jint, jobject, jint);
/* /*
* Class: org_xerial_snappy_SnappyNative * Class: org_xerial_snappy_SnappyNative
@ -53,7 +53,7 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawUncompress__Ljava_
* Signature: (I)I * Signature: (I)I
*/ */
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_maxCompressedLength JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_maxCompressedLength
(JNIEnv *, jclass, jint); (JNIEnv *, jobject, jint);
/* /*
* Class: org_xerial_snappy_SnappyNative * Class: org_xerial_snappy_SnappyNative
@ -61,7 +61,7 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_maxCompressedLength
* Signature: (Ljava/nio/ByteBuffer;II)I * Signature: (Ljava/nio/ByteBuffer;II)I
*/ */
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength__Ljava_nio_ByteBuffer_2II JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength__Ljava_nio_ByteBuffer_2II
(JNIEnv *, jclass, jobject, jint, jint); (JNIEnv *, jobject, jobject, jint, jint);
/* /*
* Class: org_xerial_snappy_SnappyNative * Class: org_xerial_snappy_SnappyNative
@ -69,7 +69,7 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength__L
* Signature: (Ljava/lang/Object;II)I * Signature: (Ljava/lang/Object;II)I
*/ */
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength__Ljava_lang_Object_2II JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength__Ljava_lang_Object_2II
(JNIEnv *, jclass, jobject, jint, jint); (JNIEnv *, jobject, jobject, jint, jint);
/* /*
* Class: org_xerial_snappy_SnappyNative * Class: org_xerial_snappy_SnappyNative
@ -77,7 +77,7 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_uncompressedLength__L
* Signature: (Ljava/nio/ByteBuffer;II)Z * Signature: (Ljava/nio/ByteBuffer;II)Z
*/ */
JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_isValidCompressedBuffer__Ljava_nio_ByteBuffer_2II JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_isValidCompressedBuffer__Ljava_nio_ByteBuffer_2II
(JNIEnv *, jclass, jobject, jint, jint); (JNIEnv *, jobject, jobject, jint, jint);
/* /*
* Class: org_xerial_snappy_SnappyNative * Class: org_xerial_snappy_SnappyNative
@ -85,7 +85,7 @@ JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_isValidCompressed
* Signature: (Ljava/lang/Object;II)Z * Signature: (Ljava/lang/Object;II)Z
*/ */
JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_isValidCompressedBuffer__Ljava_lang_Object_2II JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_isValidCompressedBuffer__Ljava_lang_Object_2II
(JNIEnv *, jclass, jobject, jint, jint); (JNIEnv *, jobject, jobject, jint, jint);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -28,47 +28,47 @@ import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
/** /**
* Interface to access the native code of Snappy. Although the methods in this * JNI interface of the {@link SnappyNativeAPI} implementation. The native
* class are public, do not use them. * method in this class is defined in SnappyNative.h (genereted by javah) and
* SnappyNative.cpp
* *
* *
* @author leo * @author leo
* *
*/ */
public class SnappyNative public class SnappyNative implements SnappyNativeAPI
{ {
public native static String nativeLibraryVersion(); public native String nativeLibraryVersion();
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
// Generic compression/decompression routines. // Generic compression/decompression routines.
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
public native static int rawCompress(ByteBuffer input, int inputOffset, int inputLength, ByteBuffer compressed, public native int rawCompress(ByteBuffer input, int inputOffset, int inputLength, ByteBuffer compressed,
int outputOffset) throws IOException; int outputOffset) throws IOException;
public native static int rawCompress(Object input, int inputOffset, int inputByteLength, Object output, public native int rawCompress(Object input, int inputOffset, int inputByteLength, Object output, int outputOffset);
int outputOffset);
public native static int rawUncompress(ByteBuffer compressed, int inputOffset, int inputLength, public native int rawUncompress(ByteBuffer compressed, int inputOffset, int inputLength, ByteBuffer uncompressed,
ByteBuffer uncompressed, int outputOffset) throws IOException;
public native static int rawUncompress(Object input, int inputOffset, int inputLength, Object output,
int outputOffset) throws IOException; int outputOffset) throws IOException;
public native int rawUncompress(Object input, int inputOffset, int inputLength, Object output, int outputOffset)
throws IOException;
// Returns the maximal size of the compressed representation of // Returns the maximal size of the compressed representation of
// input data that is "source_bytes" bytes in length; // input data that is "source_bytes" bytes in length;
public native static int maxCompressedLength(int source_bytes); public native int maxCompressedLength(int source_bytes);
// This operation takes O(1) time. // This operation takes O(1) time.
public native static int uncompressedLength(ByteBuffer compressed, int offset, int len) throws IOException; public native int uncompressedLength(ByteBuffer compressed, int offset, int len) throws IOException;
public native static int uncompressedLength(Object input, int offset, int len) throws IOException; public native int uncompressedLength(Object input, int offset, int len) throws IOException;
public native static boolean isValidCompressedBuffer(ByteBuffer compressed, int offset, int len) throws IOException; public native boolean isValidCompressedBuffer(ByteBuffer compressed, int offset, int len) throws IOException;
public native static boolean isValidCompressedBuffer(Object input, int offset, int len) throws IOException; public native boolean isValidCompressedBuffer(Object input, int offset, int len) throws IOException;
protected static 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)); throw new IOException(String.format("%s(%d)", SnappyErrorCode.getErrorMessage(errorCode), errorCode));
} }

View File

@ -0,0 +1,71 @@
/*--------------------------------------------------------------------------
* Copyright 2011 Taro L. Saito
*
* Licensed 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*--------------------------------------------------------------------------*/
//--------------------------------------
// snappy-java Project
//
// SnappyNative.java
// Since: 2011/03/30
//
// $URL$
// $Author$
//--------------------------------------
package org.xerial.snappy;
import java.io.IOException;
import java.nio.ByteBuffer;
/**
* Interface to access the native code of Snappy.
*
*
* @author leo
*
*/
public interface SnappyNativeAPI
{
public String nativeLibraryVersion();
// ------------------------------------------------------------------------
// Generic compression/decompression routines.
// ------------------------------------------------------------------------
public int rawCompress(ByteBuffer input, int inputOffset, int inputLength, ByteBuffer compressed, int outputOffset)
throws IOException;
public int rawCompress(Object input, int inputOffset, int inputByteLength, Object output, int outputOffset);
public int rawUncompress(ByteBuffer compressed, int inputOffset, int inputLength, ByteBuffer uncompressed,
int outputOffset) throws IOException;
public int rawUncompress(Object input, int inputOffset, int inputLength, Object output, int outputOffset)
throws IOException;
// Returns the maximal size of the compressed representation of
// input data that is "source_bytes" bytes in length;
public int maxCompressedLength(int source_bytes);
// This operation takes O(1) time.
public int uncompressedLength(ByteBuffer compressed, int offset, int len) throws IOException;
public int uncompressedLength(Object input, int offset, int len) throws IOException;
public boolean isValidCompressedBuffer(ByteBuffer compressed, int offset, int len) throws IOException;
public boolean isValidCompressedBuffer(Object input, int offset, int len) throws IOException;
public void throw_error(int errorCode) throws IOException;
}

View File

@ -42,6 +42,7 @@ import javassist.CtNewMethod;
import org.codehaus.plexus.classworlds.ClassWorld; import org.codehaus.plexus.classworlds.ClassWorld;
import org.codehaus.plexus.classworlds.realm.ClassRealm; import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.xerial.util.FileResource; import org.xerial.util.FileResource;
import org.xerial.util.log.Logger; import org.xerial.util.log.Logger;
@ -80,6 +81,7 @@ public class SnappyLoaderTest
} }
} }
@Ignore
@Test @Test
public void loadFromSytemClassLoader() throws Exception { public void loadFromSytemClassLoader() throws Exception {
@ -109,23 +111,22 @@ public class SnappyLoaderTest
//_logger.info(cl.getName()); //_logger.info(cl.getName());
//Class< ? > loaderClass = cl.toClass(); //Class< ? > loaderClass = cl.toClass();
Class< ? > classLoader = Class.forName("java.lang.ClassLoader"); // Class< ? > classLoader = Class.forName("java.lang.ClassLoader");
java.lang.reflect.Method defineClass = classLoader.getDeclaredMethod("defineClass", new Class[] { String.class, // java.lang.reflect.Method defineClass = classLoader.getDeclaredMethod("defineClass", new Class[] { String.class,
byte[].class, int.class, int.class, ProtectionDomain.class }); // byte[].class, int.class, int.class, ProtectionDomain.class });
//
defineClass.setAccessible(true); // defineClass.setAccessible(true);
defineClass.invoke(parent, cl.getName(), bytecode, 0, bytecode.length, System.class.getProtectionDomain()); // defineClass.invoke(parent, cl.getName(), bytecode, 0, bytecode.length, System.class.getProtectionDomain());
//
Class< ? > forName = parent.loadClass("org.xerial.snappy.SnappyNativeLoader"); // Class< ? > forName = parent.loadClass("org.xerial.snappy.SnappyNativeLoader");
//Class< ? > snappyClass = L1.loadClass("org.xerial.snappy.Snappy"); // not found //Class< ? > snappyClass = L1.loadClass("org.xerial.snappy.Snappy"); // not found
//_logger.info(snappyClass.getName()); //_logger.info(snappyClass.getName());
} }
@Test @Test
public void load() throws Exception { public void load() throws Exception {
SnappyLoader.load(); SnappyLoader.load();
_logger.debug(Snappy.getNativeLibraryVersion()); _logger.info(Snappy.getNativeLibraryVersion());
} }
} }