Removed SnappyNativeAPI, since native loader injection is no longer necessary

This commit is contained in:
Taro L. Saito 2013-08-13 15:55:58 +09:00
parent 1c3c565352
commit 2891c692c0
5 changed files with 34 additions and 116 deletions

View File

@ -52,9 +52,9 @@ public class Snappy
}
/**
* An instance of SnappyNativeAPI
* An instance of SnappyNative
*/
private static Object impl;
private static SnappyNative impl;
/**
* Copy bytes from source to destination
@ -73,7 +73,7 @@ public class Snappy
*/
public static void arrayCopy(Object src, int offset, int byteLength, Object dest, int dest_offset)
throws IOException {
((SnappyNativeAPI) impl).arrayCopy(src, offset, byteLength, dest, dest_offset);
impl.arrayCopy(src, offset, byteLength, dest, dest_offset);
}
/**
@ -134,7 +134,7 @@ public class Snappy
// output: compressed
int uPos = uncompressed.position();
int uLen = uncompressed.remaining();
int compressedSize = ((SnappyNativeAPI) impl).rawCompress(uncompressed, uPos, uLen, compressed,
int compressedSize = impl.rawCompress(uncompressed, uPos, uLen, compressed,
compressed.position());
// pos limit
@ -283,7 +283,7 @@ public class Snappy
public static boolean isValidCompressedBuffer(byte[] input, int offset, int length) throws IOException {
if (input == null)
throw new NullPointerException("input is null");
return ((SnappyNativeAPI) impl).isValidCompressedBuffer(input, offset, length);
return impl.isValidCompressedBuffer(input, offset, length);
}
/**
@ -303,7 +303,7 @@ public class Snappy
* factor of four faster than actual decompression.
*/
public static boolean isValidCompressedBuffer(ByteBuffer compressed) throws IOException {
return ((SnappyNativeAPI) impl).isValidCompressedBuffer(compressed, compressed.position(),
return impl.isValidCompressedBuffer(compressed, compressed.position(),
compressed.remaining());
}
@ -316,7 +316,7 @@ public class Snappy
* @return maximum byte size of the compressed data
*/
public static int maxCompressedLength(int byteSize) {
return ((SnappyNativeAPI) impl).maxCompressedLength(byteSize);
return impl.maxCompressedLength(byteSize);
}
/**
@ -330,7 +330,7 @@ public class Snappy
*/
public static byte[] rawCompress(Object data, int byteSize) {
byte[] buf = new byte[Snappy.maxCompressedLength(byteSize)];
int compressedByteSize = ((SnappyNativeAPI) impl).rawCompress(data, 0, byteSize, buf, 0);
int compressedByteSize = impl.rawCompress(data, 0, byteSize, buf, 0);
byte[] result = new byte[compressedByteSize];
System.arraycopy(buf, 0, result, 0, compressedByteSize);
return result;
@ -358,7 +358,7 @@ public class Snappy
if (input == null || output == null)
throw new NullPointerException("input or output is null");
int compressedSize = ((SnappyNativeAPI) impl)
int compressedSize = impl
.rawCompress(input, inputOffset, inputLength, output, outputOffset);
return compressedSize;
}
@ -391,7 +391,7 @@ public class Snappy
throws IOException {
if (input == null || output == null)
throw new NullPointerException("input or output is null");
return ((SnappyNativeAPI) impl).rawUncompress(input, inputOffset, inputLength, output, outputOffset);
return impl.rawUncompress(input, inputOffset, inputLength, output, outputOffset);
}
/**
@ -463,7 +463,7 @@ public class Snappy
// pos limit
// [ ......UUUUUU.........]
int decompressedSize = ((SnappyNativeAPI) impl).rawUncompress(compressed, cPos, cLen, uncompressed,
int decompressedSize = impl.rawUncompress(compressed, cPos, cLen, uncompressed,
uncompressed.position());
uncompressed.limit(uncompressed.position() + decompressedSize);
@ -493,7 +493,7 @@ public class Snappy
public static char[] uncompressCharArray(byte[] input, int offset, int length) throws IOException {
int uncompressedLength = Snappy.uncompressedLength(input, offset, length);
char[] result = new char[uncompressedLength / 2];
int byteSize = ((SnappyNativeAPI) impl).rawUncompress(input, offset, length, result, 0);
int byteSize = impl.rawUncompress(input, offset, length, result, 0);
return result;
}
@ -507,7 +507,7 @@ public class Snappy
public static double[] uncompressDoubleArray(byte[] input) throws IOException {
int uncompressedLength = Snappy.uncompressedLength(input, 0, input.length);
double[] result = new double[uncompressedLength / 8];
int byteSize = ((SnappyNativeAPI) impl).rawUncompress(input, 0, input.length, result, 0);
int byteSize = impl.rawUncompress(input, 0, input.length, result, 0);
return result;
}
@ -522,7 +522,7 @@ public class Snappy
* {@link SnappyErrorCode#PARSING_ERROR}
*/
public static int uncompressedLength(byte[] input) throws IOException {
return ((SnappyNativeAPI) impl).uncompressedLength(input, 0, input.length);
return impl.uncompressedLength(input, 0, input.length);
}
/**
@ -541,7 +541,7 @@ public class Snappy
if (input == null)
throw new NullPointerException("input is null");
return ((SnappyNativeAPI) impl).uncompressedLength(input, offset, length);
return impl.uncompressedLength(input, offset, length);
}
/**
@ -561,7 +561,7 @@ public class Snappy
if (!compressed.isDirect())
throw new SnappyError(SnappyErrorCode.NOT_A_DIRECT_BUFFER, "input is not a direct buffer");
return ((SnappyNativeAPI) impl).uncompressedLength(compressed, compressed.position(), compressed.remaining());
return impl.uncompressedLength(compressed, compressed.position(), compressed.remaining());
}
/**
@ -587,7 +587,7 @@ public class Snappy
public static float[] uncompressFloatArray(byte[] input, int offset, int length) throws IOException {
int uncompressedLength = Snappy.uncompressedLength(input, offset, length);
float[] result = new float[uncompressedLength / 4];
int byteSize = ((SnappyNativeAPI) impl).rawUncompress(input, offset, length, result, 0);
int byteSize = impl.rawUncompress(input, offset, length, result, 0);
return result;
}
@ -614,7 +614,7 @@ public class Snappy
public static int[] uncompressIntArray(byte[] input, int offset, int length) throws IOException {
int uncompressedLength = Snappy.uncompressedLength(input, offset, length);
int[] result = new int[uncompressedLength / 4];
int byteSize = ((SnappyNativeAPI) impl).rawUncompress(input, offset, length, result, 0);
int byteSize = impl.rawUncompress(input, offset, length, result, 0);
return result;
}
@ -641,7 +641,7 @@ public class Snappy
public static long[] uncompressLongArray(byte[] input, int offset, int length) throws IOException {
int uncompressedLength = Snappy.uncompressedLength(input, offset, length);
long[] result = new long[uncompressedLength / 8];
int byteSize = ((SnappyNativeAPI) impl).rawUncompress(input, offset, length, result, 0);
int byteSize = impl.rawUncompress(input, offset, length, result, 0);
return result;
}
@ -668,7 +668,7 @@ public class Snappy
public static short[] uncompressShortArray(byte[] input, int offset, int length) throws IOException {
int uncompressedLength = Snappy.uncompressedLength(input, offset, length);
short[] result = new short[uncompressedLength / 2];
int byteSize = ((SnappyNativeAPI) impl).rawUncompress(input, offset, length, result, 0);
int byteSize = impl.rawUncompress(input, offset, length, result, 0);
return result;
}

View File

@ -81,14 +81,14 @@ public class SnappyLoader
public static final String KEY_SNAPPY_DISABLE_BUNDLED_LIBS = "org.xerial.snappy.disable.bundled.libs"; // Depreciated, but preserved for backward compatibility
private static volatile boolean isLoaded = false;
private static volatile Object api = null;
private static volatile SnappyNative api = null;
/**
* Set the api instance.
*
* @param nativeCode
*/
static synchronized void setApi(Object nativeCode)
static synchronized void setApi(SnappyNative nativeCode)
{
api = nativeCode;
}
@ -129,7 +129,7 @@ public class SnappyLoader
loadSnappySystemProperties();
}
static synchronized Object load()
static synchronized SnappyNative load()
{
if (api != null)
return api;
@ -137,10 +137,8 @@ public class SnappyLoader
try {
loadNativeLibrary();
setApi(new SnappyNative());
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.
Object nativeCode = Class.forName("org.xerial.snappy.SnappyNative").newInstance();
setApi(nativeCode);
}
catch (Exception e) {
e.printStackTrace();

View File

@ -28,8 +28,7 @@ import java.io.IOException;
import java.nio.ByteBuffer;
/**
* <b>Internal only - Do not use this class.</b> JNI interface of the
* {@link SnappyNativeAPI} implementation. The native method in this class is
* JNI interface of the {@link Snappy} implementation. The native method in this class is
* defined in SnappyNative.h (genereted by javah) and SnappyNative.cpp
*
* <p>
@ -40,7 +39,7 @@ import java.nio.ByteBuffer;
* @author leo
*
*/
public class SnappyNative implements SnappyNativeAPI
public class SnappyNative
{
public native String nativeLibraryVersion();

View File

@ -1,76 +0,0 @@
/*--------------------------------------------------------------------------
* 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;
/**
* <b>Internal only - Do not use this class.</b>
*
* Interface to access the native code of Snappy. Although this class members
* are public, do not use them directly. Use {@link Snappy} API instead.
*
*
* @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 arrayCopy(Object src, int offset, int byteLength, Object dest, int dOffset) throws IOException;
public void throw_error(int errorCode) throws IOException;
}

View File

@ -24,23 +24,20 @@
//--------------------------------------
package org.xerial.snappy;
import static org.junit.Assert.*;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import org.codehaus.plexus.classworlds.ClassWorld;
import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.junit.Test;
import org.xerial.util.FileResource;
import org.xerial.util.log.Logger;
import java.io.*;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.fail;
public class SnappyLoaderTest
{
private static Logger _logger = Logger.getLogger(SnappyLoaderTest.class);