mirror of
https://github.com/xerial/snappy-java.git
synced 2025-07-20 20:44:28 +02:00
Load a native library according to the current OS and CPU architecture
This commit is contained in:
parent
19d1233d18
commit
61de0555db
13
Makefile
13
Makefile
@ -10,7 +10,6 @@ $(SNAPPY_ARCHIVE):
|
|||||||
curl -o$@ http://snappy.googlecode.com/files/snappy-$(VERSION).tar.gz
|
curl -o$@ http://snappy.googlecode.com/files/snappy-$(VERSION).tar.gz
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
$(SNAPPY_SRC): $(SNAPPY_ARCHIVE)
|
$(SNAPPY_SRC): $(SNAPPY_ARCHIVE)
|
||||||
tar xvfz $< -C $(TARGET)
|
tar xvfz $< -C $(TARGET)
|
||||||
|
|
||||||
@ -20,10 +19,8 @@ $(SRC)/org/xerial/snappy/SnappyNative.h: $(SRC)/org/xerial/snappy/Snappy.java
|
|||||||
|
|
||||||
|
|
||||||
SNAPPY_CC:=snappy-sinksource.cc snappy-stubs-internal.cc snappy.cc
|
SNAPPY_CC:=snappy-sinksource.cc snappy-stubs-internal.cc snappy.cc
|
||||||
|
|
||||||
SNAPPY_OBJ:=$(addprefix $(SNAPPY_OUT)/,$(patsubst %.cc,%.o,$(SNAPPY_CC)) SnappyNative.o)
|
SNAPPY_OBJ:=$(addprefix $(SNAPPY_OUT)/,$(patsubst %.cc,%.o,$(SNAPPY_CC)) SnappyNative.o)
|
||||||
|
|
||||||
snappy: $(SNAPPY_OUT)/$(LIBNAME)
|
|
||||||
|
|
||||||
|
|
||||||
$(SNAPPY_OUT)/%.o : $(SNAPPY_SRC)/%.cc $(SNAPPY_SRC)
|
$(SNAPPY_OUT)/%.o : $(SNAPPY_SRC)/%.cc $(SNAPPY_SRC)
|
||||||
@ -42,3 +39,13 @@ clean-native:
|
|||||||
rm -rf $(SNAPPY_OBJ) $(SNAPPY_OUT)/$(LIBNAME)
|
rm -rf $(SNAPPY_OBJ) $(SNAPPY_OUT)/$(LIBNAME)
|
||||||
|
|
||||||
|
|
||||||
|
NATIVE_DIR:=src/main/resources/org/xerial/snappy/native/$(OS_NAME)/$(OS_ARCH)
|
||||||
|
NATIVE_DLL:=$(NATIVE_DIR)/$(LIBNAME)
|
||||||
|
|
||||||
|
snappy: $(NATIVE_DLL)
|
||||||
|
|
||||||
|
$(NATIVE_DLL): $(SNAPPY_OUT)/$(LIBNAME)
|
||||||
|
@mkdir -p $(@D)
|
||||||
|
cp $< $@
|
||||||
|
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ public class LoadSnappy
|
|||||||
|
|
||||||
public static boolean initialize() {
|
public static boolean initialize() {
|
||||||
if (!extracted)
|
if (!extracted)
|
||||||
loadSQLiteNativeLibrary();
|
loadSnappyNativeLibrary();
|
||||||
return extracted;
|
return extracted;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,7 +78,7 @@ public class LoadSnappy
|
|||||||
private static boolean extractAndLoadLibraryFile(String libFolderForCurrentOS, String libraryFileName,
|
private static boolean extractAndLoadLibraryFile(String libFolderForCurrentOS, String libraryFileName,
|
||||||
String targetFolder) {
|
String targetFolder) {
|
||||||
String nativeLibraryFilePath = libFolderForCurrentOS + "/" + libraryFileName;
|
String nativeLibraryFilePath = libFolderForCurrentOS + "/" + libraryFileName;
|
||||||
final String prefix = "sqlite-" + getVersion() + "-";
|
final String prefix = "snappy-" + getVersion() + "-";
|
||||||
|
|
||||||
String extractedLibFileName = prefix + libraryFileName;
|
String extractedLibFileName = prefix + libraryFileName;
|
||||||
File extractedLibFile = new File(targetFolder, extractedLibFileName);
|
File extractedLibFile = new File(targetFolder, extractedLibFileName);
|
||||||
@ -149,27 +149,29 @@ public class LoadSnappy
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void loadSQLiteNativeLibrary() {
|
private static void loadSnappyNativeLibrary() {
|
||||||
if (extracted)
|
if (extracted)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Try loading library from org.sqlite.lib.path library path */
|
// Try loading library from org.sqlite.lib.path library path */
|
||||||
String sqliteNativeLibraryPath = System.getProperty("org.sqlite.lib.path");
|
String snappyNativeLibraryPath = System.getProperty("org.xerial.snappy.lib.path");
|
||||||
String sqliteNativeLibraryName = System.getProperty("org.sqlite.lib.name");
|
String snappyNativeLibraryName = System.getProperty("org.xerial.snappy.lib.name");
|
||||||
if (sqliteNativeLibraryName == null)
|
|
||||||
sqliteNativeLibraryName = System.mapLibraryName("sqlitejdbc");
|
|
||||||
|
|
||||||
if (sqliteNativeLibraryPath != null) {
|
// Resolve the library file name with a suffix (e.g., dll, .so, etc.)
|
||||||
if (loadNativeLibrary(sqliteNativeLibraryPath, sqliteNativeLibraryName)) {
|
if (snappyNativeLibraryName == null)
|
||||||
|
snappyNativeLibraryName = System.mapLibraryName("snappy");
|
||||||
|
|
||||||
|
if (snappyNativeLibraryPath != null) {
|
||||||
|
if (loadNativeLibrary(snappyNativeLibraryPath, snappyNativeLibraryName)) {
|
||||||
extracted = true;
|
extracted = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load the os-dependent library from a jar file
|
// Load the os-dependent library from a jar file
|
||||||
sqliteNativeLibraryPath = "/native/" + OSInfo.getNativeLibFolderPathForCurrentOS();
|
snappyNativeLibraryPath = "/org/xerial/snappy/native/" + OSInfo.getNativeLibFolderPathForCurrentOS();
|
||||||
|
|
||||||
if (LoadSnappy.class.getResource(sqliteNativeLibraryPath + "/" + sqliteNativeLibraryName) == null) {
|
if (LoadSnappy.class.getResource(snappyNativeLibraryPath + "/" + snappyNativeLibraryName) == null) {
|
||||||
// use nested VM version
|
// use nested VM version
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -177,7 +179,7 @@ public class LoadSnappy
|
|||||||
// temporary library folder
|
// temporary library folder
|
||||||
String tempFolder = new File(System.getProperty("java.io.tmpdir")).getAbsolutePath();
|
String tempFolder = new File(System.getProperty("java.io.tmpdir")).getAbsolutePath();
|
||||||
// Try extracting the library from jar
|
// Try extracting the library from jar
|
||||||
if (extractAndLoadLibraryFile(sqliteNativeLibraryPath, sqliteNativeLibraryName, tempFolder)) {
|
if (extractAndLoadLibraryFile(snappyNativeLibraryPath, snappyNativeLibraryName, tempFolder)) {
|
||||||
extracted = true;
|
extracted = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -204,9 +206,9 @@ public class LoadSnappy
|
|||||||
|
|
||||||
public static String getVersion() {
|
public static String getVersion() {
|
||||||
|
|
||||||
URL versionFile = LoadSnappy.class.getResource("/META-INF/maven/org.xerial/snappy-java/pom.properties");
|
URL versionFile = LoadSnappy.class.getResource("/META-INF/maven/org.xerial.snappy/snappy-java/pom.properties");
|
||||||
if (versionFile == null)
|
if (versionFile == null)
|
||||||
versionFile = LoadSnappy.class.getResource("/META-INF/maven/org.xerial/snappy-java/VERSION");
|
versionFile = LoadSnappy.class.getResource("/META-INF/maven/org.xerial.snappy/snappy-java/VERSION");
|
||||||
|
|
||||||
String version = "unknown";
|
String version = "unknown";
|
||||||
try {
|
try {
|
||||||
|
27
src/test/java/org/xerial/snappy/SnappyTest.java
Executable file
27
src/test/java/org/xerial/snappy/SnappyTest.java
Executable file
@ -0,0 +1,27 @@
|
|||||||
|
//--------------------------------------
|
||||||
|
// snappy-java Project
|
||||||
|
//
|
||||||
|
// SnappyTest.java
|
||||||
|
// Since: 2011/03/30
|
||||||
|
//
|
||||||
|
// $URL$
|
||||||
|
// $Author$
|
||||||
|
//--------------------------------------
|
||||||
|
package org.xerial.snappy;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class SnappyTest
|
||||||
|
{
|
||||||
|
@Test
|
||||||
|
public void load() throws Exception {
|
||||||
|
|
||||||
|
ByteBuffer src = ByteBuffer.allocate(1024);
|
||||||
|
src.put("hello world".getBytes());
|
||||||
|
ByteBuffer dest = ByteBuffer.allocate(1024);
|
||||||
|
Snappy.compress(src, dest);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user