Change the native library version retrieval method to look up

a resource file (VERSION) in org.xerial.snappy package.

Update issue 32
Status: Fixed
This commit is contained in:
Taro L. Saito 2011-09-26 23:14:27 +09:00
parent e6b021d3d1
commit 35f7e8edd6
4 changed files with 64 additions and 16 deletions

28
pom.xml
View File

@ -12,6 +12,34 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<developers>
<developer>
<id>leo</id>
<name>Taro L. Saito</name>
<email>leo@xerial.org</email>
<organization>Xerial Project</organization>
<roles>
<role>Architect</role>
<role>Project Manager</role>
<role>Chief Developer</role>
</roles>
<timezone>+9</timezone>
</developer>
</developers>
<organization>
<name>xerial.org</name>
<url>http://www.xerial.org/</url>
</organization>
<build>
<resources>
<resource>

View File

@ -26,8 +26,10 @@ package org.xerial.snappy;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Properties;
/**
* Snappy API for data compression/decompression
@ -249,7 +251,24 @@ public class Snappy
* @return native library version
*/
public static String getNativeLibraryVersion() {
return ((SnappyNativeAPI) impl).nativeLibraryVersion();
URL versionFile = SnappyLoader.class.getResource("/org/xerial/snappy/VERSION");
String version = "unknown";
try {
if (versionFile != null) {
Properties versionData = new Properties();
versionData.load(versionFile.openStream());
version = versionData.getProperty("version", version);
if (version.equals("unknown"))
version = versionData.getProperty("VERSION", version);
version = version.trim().replaceAll("[^0-9\\.]", "");
}
}
catch (IOException e) {
e.printStackTrace();
}
return version;
}
/**
@ -354,15 +373,16 @@ public class Snappy
* @param input
* input byte array
* @param inputOffset
* byte offset
* byte offset in the input byte array
* @param inputLength
* byte length of the input data
* @param output
* output buffer, MUST be a primitive type array
* @param outputOffset
* byte offset
* byte offset in the output buffer
* @return the byte size of the uncompressed data
* @throws IOException
* when failed to uncompress the input data
*/
public static int rawUncompress(byte[] input, int inputOffset, int inputLength, Object output, int outputOffset)
throws IOException {
@ -420,7 +440,7 @@ public class Snappy
* @param compressed
* buffer[pos() ... limit()) containing the input data
* @param uncompressed
* output of the the uncompressed data. It uses buffer[pot()..]
* output of the the uncompressed data. It uses buffer[pos()..]
* @return uncompressed data size
*
* @throws IOException
@ -451,7 +471,7 @@ public class Snappy
* Uncompress the input data as char array
*
* @param input
* @return the umcompressed data
* @return the uncompressed data
* @throws IOException
*/
public static char[] uncompressCharArray(byte[] input) throws IOException {
@ -493,7 +513,7 @@ public class Snappy
* operation takes O(1) time.
*
* @param input
* @return umcompressed byte size of the the given input data
* @return uncompressed byte size of the the given input data
* @throws IOException
* when failed to uncompress the given input. The error code is
* {@link SnappyErrorCode#PARSING_ERROR}
@ -509,7 +529,7 @@ public class Snappy
* @param input
* @param offset
* @param length
* @return umcompressed byte size of the the given input data
* @return uncompressed byte size of the the given input data
* @throws IOException
* when failed to uncompress the given input. The error code is
* {@link SnappyErrorCode#PARSING_ERROR}

View File

@ -33,7 +33,7 @@ void throw_exception(JNIEnv *env, jobject self, int errorCode)
JNIEXPORT jstring JNICALL Java_org_xerial_snappy_SnappyNative_nativeLibraryVersion
(JNIEnv * env, jobject self)
{
return env->NewStringUTF("1.0.3");
return env->NewStringUTF("1.0.4");
}
/*

View File

@ -98,31 +98,31 @@ public class SnappyLoaderTest
// Actually load Snappy.class in a child class loader
Class< ? > S1 = L1.loadClass("org.xerial.snappy.Snappy");
Method m = S1.getMethod("getNativeLibraryVersion");
String v = (String) m.invoke(null);
Method m = S1.getMethod("compress", String.class);
byte[] v = (byte[]) m.invoke(null, "hello world");
// Load Snappy.class from another child class loader
Class< ? > S2 = L2.loadClass("org.xerial.snappy.Snappy");
Method m2 = S2.getMethod("getNativeLibraryVersion");
String v2 = (String) m2.invoke(null);
Method m2 = S2.getMethod("compress", String.class);
byte[] v2 = (byte[]) m2.invoke(null, "hello world");
assertEquals(v, v2);
assertArrayEquals(v, v2);
}
@Test
public void load() throws Exception {
SnappyLoader.load();
_logger.debug(Snappy.getNativeLibraryVersion());
_logger.debug(Snappy.maxCompressedLength(1024));
}
@Test
public void autoLoad() throws Exception {
_logger.debug(Snappy.getNativeLibraryVersion());
_logger.debug(Snappy.maxCompressedLength(1024));
}
public static void main(String[] args) {
// Test for loading native library specified in -Djava.library.path
System.setProperty(SnappyLoader.KEY_SNAPPY_USE_SYSTEMLIB, "true");
_logger.debug(Snappy.getNativeLibraryVersion());
_logger.debug(Snappy.maxCompressedLength(1024));
}
}