Skip to content

Commit

Permalink
Merge fixes for #27
Browse files Browse the repository at this point in the history
  • Loading branch information
xerial committed Aug 13, 2013
2 parents 2bb1165 + b7755bf commit dc20eaf
Show file tree
Hide file tree
Showing 14 changed files with 180 additions and 358 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,22 @@
<Import-Package>org.osgi.framework;version="[1.5,2)"</Import-Package>
<Bundle-ActivationPolicy>lazy</Bundle-ActivationPolicy>
<Bundle-NativeCode>
<<<<<<< HEAD
org/xerial/snappy/native/Windows/amd64/snappyjava.dll;osname=win32;processor=x86-64,
org/xerial/snappy/native/Windows/x86/snappyjava.dll;osname=win32;processor=x86,
org/xerial/snappy/native/Mac/x86_64/libsnappyjava.jnilib;osname=macosx;processor=x86-64,
org/xerial/snappy/native/Linux/amd64/libsnappyjava.so;osname=linux;processor=x86-64,
org/xerial/snappy/native/Linux/i386/libsnappyjava.so;osname=linux;processor=x86,
org/xerial/snappy/native/Linux/arm/libsnappyjava.so;osname=linux;processor=arm
=======
org/xerial/snappy/native/Windows/x86_64/snappyjava.dll;selection-filter="(&amp;(osgi.arch=x86_64)(osgi.os=win32))",
org/xerial/snappy/native/Windows/x86/snappyjava.dll;selection-filter="(&amp;(osgi.arch=x86)(osgi.os=win32))",
org/xerial/snappy/native/Mac/x86/libsnappyjava.jnilib;selection-filter="(&amp;(osgi.arch=x86)(osgi.os=macosx))",
org/xerial/snappy/native/Mac/x86_64/libsnappyjava.jnilib;selection-filter="(&amp;(osgi.arch=x86_64)(osgi.os=macosx))",
org/xerial/snappy/native/Linux/x86_64/libsnappyjava.so;selection-filter="(&amp;(osgi.arch=x86_64)(osgi.os=linux))",
org/xerial/snappy/native/Linux/x86/libsnappyjava.so;selection-filter="(&amp;(osgi.arch=x86)(osgi.os=linux))",
org/xerial/snappy/native/Linux/arm/libsnappyjava.so;selection-filter="(&amp;(osgi.arch=arm)(osgi.os=linux))"
>>>>>>> feature/lib-loader
</Bundle-NativeCode>
<!-- TODO: unsure about ARMHF -->
</instructions>
Expand Down
50 changes: 48 additions & 2 deletions src/main/java/org/xerial/snappy/OSInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
package org.xerial.snappy;

import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;

/**
* Provides OS name and architecture name.
Expand All @@ -34,6 +36,48 @@
*/
public class OSInfo
{
private static HashMap<String, String> archMapping = new HashMap<String, String>();

public static final String X86 = "x86";
public static final String X86_64 = "x86_64";
public static final String IA64_32 = "ia64_32";
public static final String IA64 = "ia64";
public static final String PPC = "ppc";

static {
// x86 mappings
archMapping.put(X86, X86);
archMapping.put("i386", X86);
archMapping.put("i486", X86);
archMapping.put("i586", X86);
archMapping.put("i686", X86);
archMapping.put("pentium", X86);

// x86_64 mappings
archMapping.put(X86_64, X86_64);
archMapping.put("amd64", X86_64);
archMapping.put("em64t", X86_64);
archMapping.put("universal", X86_64); // Needed for openjdk7 in Mac

// Itenium 64-bit mappings
archMapping.put(IA64, IA64);
archMapping.put("ia64w", IA64);

// Itenium 32-bit mappings, usually an HP-UX construct
archMapping.put(IA64_32, IA64_32);
archMapping.put("ia64n", IA64_32);

// PowerPC mappings
archMapping.put(PPC, PPC);
archMapping.put("pwoer", PPC);
archMapping.put("powerpc", PPC);
archMapping.put("power_pc", PPC);
archMapping.put("power_rs", PPC);

// TODO: PowerPC 64bit mappings
}


public static void main(String[] args) {
if (args.length >= 1) {
if ("--os".equals(args[0])) {
Expand Down Expand Up @@ -78,8 +122,10 @@ public static String getArchName() {
// ignored: fall back to "arm" arch (soft-float ABI)
}
}
else if(getOSName().equals("Mac") && (osArch.equals("universal") || osArch.equals("amd64"))) {
return "x86_64"; // Fix for openjdk7
else {
String lc = osArch.toLowerCase(Locale.US);
if(archMapping.containsKey(lc))
return archMapping.get(lc);
}
return translateArchNameToFolderName(osArch);
}
Expand Down
62 changes: 37 additions & 25 deletions src/main/java/org/xerial/snappy/Snappy.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.ExceptionInInitializerError;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
Expand All @@ -35,7 +34,7 @@
/**
* Snappy API for data compression/decompression
*
* Note: if the native libraries cannot be loaded, then an ExceptionInInitializerError
* Note: if the native libraries cannot be loaded, an ExceptionInInitializerError
* will be thrown at first use of this class.
*
* @author leo
Expand All @@ -53,9 +52,22 @@ public class Snappy
}

/**
* An instance of SnappyNativeAPI
* An instance of SnappyNative
*/
private static Object impl;
private static SnappyNative impl;


/**
* Clean up a temporary file (native lib) generated by snappy-java.
* General users do not need to call this method, since the native library extracted in snappy-java
* is deleted upon JVM termination (vie deleteOnExit()).
* This method is useful when using a J2EE container, which will restart servlet containers multiple times without
* restarting JVM.
*/
static void cleanUp() {
SnappyLoader.cleanUpExtractedNativeLib();
}


/**
* Copy bytes from source to destination
Expand All @@ -74,7 +86,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);
}

/**
Expand Down Expand Up @@ -135,7 +147,7 @@ public static int compress(ByteBuffer uncompressed, ByteBuffer compressed) throw
// 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
Expand Down Expand Up @@ -284,7 +296,7 @@ public static String getNativeLibraryVersion() {
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);
}

/**
Expand All @@ -304,7 +316,7 @@ public static boolean isValidCompressedBuffer(byte[] input) throws IOException {
* 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());
}

Expand All @@ -317,7 +329,7 @@ public static boolean isValidCompressedBuffer(ByteBuffer compressed) throws IOEx
* @return maximum byte size of the compressed data
*/
public static int maxCompressedLength(int byteSize) {
return ((SnappyNativeAPI) impl).maxCompressedLength(byteSize);
return impl.maxCompressedLength(byteSize);
}

/**
Expand All @@ -329,7 +341,7 @@ public static int maxCompressedLength(int byteSize) {
* @throws IOException
*/
public static long rawCompress(long inputAddr, long inputSize, long destAddr) throws IOException {
return ((SnappyNativeAPI) impl).rawCompress(inputAddr, inputSize, destAddr);
return impl.rawCompress(inputAddr, inputSize, destAddr);
}

/**
Expand All @@ -341,7 +353,7 @@ public static long rawCompress(long inputAddr, long inputSize, long destAddr) th
* @throws IOException
*/
public static long rawUncompress(long inputAddr, long inputSize, long destAddr) throws IOException {
return ((SnappyNativeAPI) impl).rawUncompress(inputAddr, inputSize, destAddr);
return impl.rawUncompress(inputAddr, inputSize, destAddr);
}


Expand All @@ -356,7 +368,7 @@ public static long rawUncompress(long inputAddr, long inputSize, long destAddr)
*/
public static byte[] rawCompress(Object data, int byteSize) throws IOException {
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;
Expand Down Expand Up @@ -384,7 +396,7 @@ public static int rawCompress(Object input, int inputOffset, int inputLength, by
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;
}
Expand Down Expand Up @@ -417,7 +429,7 @@ public static int rawUncompress(byte[] input, int inputOffset, int inputLength,
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);
}

/**
Expand Down Expand Up @@ -489,7 +501,7 @@ public static int uncompress(ByteBuffer compressed, ByteBuffer uncompressed) thr

// 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);

Expand Down Expand Up @@ -519,7 +531,7 @@ public static char[] uncompressCharArray(byte[] input) throws IOException {
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;
}

Expand All @@ -533,7 +545,7 @@ public static char[] uncompressCharArray(byte[] input, int offset, int length) t
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;
}

Expand All @@ -548,7 +560,7 @@ public static double[] uncompressDoubleArray(byte[] input) throws IOException {
* {@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);
}

/**
Expand All @@ -567,7 +579,7 @@ public static int uncompressedLength(byte[] input, int offset, int length) throw
if (input == null)
throw new NullPointerException("input is null");

return ((SnappyNativeAPI) impl).uncompressedLength(input, offset, length);
return impl.uncompressedLength(input, offset, length);
}

/**
Expand All @@ -587,7 +599,7 @@ public static int uncompressedLength(ByteBuffer compressed) throws IOException {
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());
}

/**
Expand All @@ -599,7 +611,7 @@ public static int uncompressedLength(ByteBuffer compressed) throws IOException {
* {@link SnappyErrorCode#PARSING_ERROR}
*/
public static long uncompressedLength(long inputAddr, long len) throws IOException {
return ((SnappyNativeAPI) impl).uncompressedLength(inputAddr, len);
return impl.uncompressedLength(inputAddr, len);
}

/**
Expand All @@ -625,7 +637,7 @@ public static float[] uncompressFloatArray(byte[] input) throws IOException {
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;
}

Expand All @@ -652,7 +664,7 @@ public static int[] uncompressIntArray(byte[] input) throws IOException {
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;
}

Expand All @@ -679,7 +691,7 @@ public static long[] uncompressLongArray(byte[] input) throws IOException {
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;
}

Expand All @@ -706,7 +718,7 @@ public static short[] uncompressShortArray(byte[] input) throws IOException {
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;
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/xerial/snappy/SnappyBundleActivator.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
//--------------------------------------
package org.xerial.snappy;

import java.util.jar.Manifest;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

import java.util.jar.Manifest;

/**
* OSGi bundle entry point
*
Expand All @@ -56,5 +56,6 @@ public void start(BundleContext context) throws Exception
public void stop(BundleContext context) throws Exception
{
SnappyLoader.setApi(null);
Snappy.cleanUp();
}
}
Loading

0 comments on commit dc20eaf

Please sign in to comment.