Move native API to SnappyNative.java
This commit is contained in:
parent
4e59477bf6
commit
158991f100
6
Makefile
6
Makefile
|
@ -17,9 +17,10 @@ $(TARGET)/snappy-$(VERSION): $(SNAPPY_ARCHIVE)
|
||||||
tar xvfz $< -C $(TARGET)
|
tar xvfz $< -C $(TARGET)
|
||||||
|
|
||||||
|
|
||||||
$(SRC)/org/xerial/snappy/SnappyNative.h: $(SRC)/org/xerial/snappy/Snappy.java
|
jni-header: $(SRC)/org/xerial/snappy/SnappyNative.h
|
||||||
javah -classpath $(TARGET)/classes -o $@ org.xerial.snappy.Snappy
|
|
||||||
|
|
||||||
|
$(SRC)/org/xerial/snappy/SnappyNative.h: $(SRC)/org/xerial/snappy/SnappyNative.java
|
||||||
|
$(JAVAH) -classpath $(TARGET)/classes -o $@ org.xerial.snappy.SnappyNative
|
||||||
|
|
||||||
|
|
||||||
$(SNAPPY_OUT)/%.o : $(TARGET)/snappy-$(VERSION)/%.cc
|
$(SNAPPY_OUT)/%.o : $(TARGET)/snappy-$(VERSION)/%.cc
|
||||||
|
@ -46,5 +47,6 @@ snappy: $(NATIVE_DLL)
|
||||||
$(NATIVE_DLL): $(SNAPPY_OUT)/$(LIBNAME)
|
$(NATIVE_DLL): $(SNAPPY_OUT)/$(LIBNAME)
|
||||||
@mkdir -p $(@D)
|
@mkdir -p $(@D)
|
||||||
cp $< $@
|
cp $< $@
|
||||||
|
cp $< $(TARGET)/classes/org/xerial/snappy/native/$(OS_NAME)/$(OS_ARCH)/$(LIBNAME)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,39 +0,0 @@
|
||||||
/* byteswap.h
|
|
||||||
|
|
||||||
Copyright 2005 Red Hat, Inc.
|
|
||||||
|
|
||||||
This file is part of Cygwin.
|
|
||||||
|
|
||||||
This software is a copyrighted work licensed under the terms of the
|
|
||||||
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
||||||
details. */
|
|
||||||
|
|
||||||
#ifndef _BYTESWAP_H
|
|
||||||
#define _BYTESWAP_H
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static __inline unsigned short
|
|
||||||
bswap_16 (unsigned short __x)
|
|
||||||
{
|
|
||||||
return (__x >> 8) | (__x << 8);
|
|
||||||
}
|
|
||||||
|
|
||||||
static __inline unsigned int
|
|
||||||
bswap_32 (unsigned int __x)
|
|
||||||
{
|
|
||||||
return (bswap_16 (__x & 0xffff) << 16) | (bswap_16 (__x >> 16));
|
|
||||||
}
|
|
||||||
|
|
||||||
static __inline unsigned long long
|
|
||||||
bswap_64 (unsigned long long __x)
|
|
||||||
{
|
|
||||||
return (((unsigned long long) bswap_32 (__x & 0xffffffffull)) << 32) | (bswap_32 (__x >> 32));
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
#endif /* _BYTESWAP_H */
|
|
|
@ -13,25 +13,64 @@ import java.nio.ByteBuffer;
|
||||||
|
|
||||||
public class Snappy
|
public class Snappy
|
||||||
{
|
{
|
||||||
static {
|
|
||||||
LoadSnappy.initialize();
|
public static String getNativeLibraryVersion() {
|
||||||
|
return SnappyNative.nativeLibraryVersion();
|
||||||
}
|
}
|
||||||
|
|
||||||
public native static String nativeLibraryVersion();
|
/**
|
||||||
|
* @param uncompressed
|
||||||
|
* input is at buffer[pos() ... limit())
|
||||||
|
* @param compressed
|
||||||
|
* output compressed data to buffer[pos()]
|
||||||
|
* @return byte size of the compressed data
|
||||||
|
*/
|
||||||
|
public static int compress(ByteBuffer uncompressed, ByteBuffer compressed) {
|
||||||
|
|
||||||
// ------------------------------------------------------------------------
|
// input: uncompressed[pos(), limit())
|
||||||
// Generic compression/decompression routines.
|
// output: compressed
|
||||||
// ------------------------------------------------------------------------
|
int uPos = uncompressed.position();
|
||||||
|
int uLen = uncompressed.remaining();
|
||||||
|
int compressedSize = SnappyNative.rawCompress(uncompressed, uPos, uLen, compressed, compressed.position());
|
||||||
|
|
||||||
public native static long compress(ByteBuffer uncompressed, ByteBuffer compressed);
|
// pos limit
|
||||||
|
// [ ....XXXXXX.........]
|
||||||
|
uncompressed.limit(uncompressed.capacity());
|
||||||
|
uncompressed.position(uPos + uLen);
|
||||||
|
|
||||||
public native static boolean uncompress(ByteBuffer compressed, ByteBuffer uncompressed);
|
// pos limit
|
||||||
|
// [ ......BBBBBBB.........]
|
||||||
|
compressed.limit(compressed.position() + compressedSize);
|
||||||
|
|
||||||
// Returns the maximal size of the compressed representation of
|
return compressedSize;
|
||||||
// input data that is "source_bytes" bytes in length;
|
}
|
||||||
public native static long maxCompressedLength(long source_bytes);
|
|
||||||
|
|
||||||
// This operation takes O(1) time.
|
/**
|
||||||
public native static long getUncompressedLength(ByteBuffer compressed);
|
* @param compressed
|
||||||
|
* input is at buffer[pos() ... limit())
|
||||||
|
* @param decompressed
|
||||||
|
* output decompressed data to buffer[pot())
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static boolean decompress(ByteBuffer compressed, ByteBuffer decompressed) {
|
||||||
|
|
||||||
|
int cPos = compressed.position();
|
||||||
|
int cLen = compressed.remaining();
|
||||||
|
|
||||||
|
boolean ret = SnappyNative.rawDecompress(compressed, cPos, cLen, decompressed, decompressed.position());
|
||||||
|
|
||||||
|
compressed.limit(compressed.capacity());
|
||||||
|
compressed.position(cPos + cLen);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getUncompressedLength(ByteBuffer compressed) {
|
||||||
|
return SnappyNative.getUncompressedLength(compressed, compressed.position(), compressed.remaining());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getMaxCompressedLength(int byteSize) {
|
||||||
|
return SnappyNative.maxCompressedLength(byteSize);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
|
#include <string>
|
||||||
#include <snappy.h>
|
#include <snappy.h>
|
||||||
#include "SnappyNative.h"
|
#include "SnappyNative.h"
|
||||||
|
|
||||||
JNIEXPORT jstring JNICALL Java_org_xerial_snappy_Snappy_nativeLibraryVersion
|
JNIEXPORT jstring JNICALL Java_org_xerial_snappy_SnappyNative_nativeLibraryVersion
|
||||||
(JNIEnv * env, jclass self)
|
(JNIEnv * env, jclass self)
|
||||||
{
|
{
|
||||||
return env->NewStringUTF("1.0.1");
|
return env->NewStringUTF("1.0.1");
|
||||||
|
@ -12,13 +13,15 @@ JNIEXPORT jstring JNICALL Java_org_xerial_snappy_Snappy_nativeLibraryVersion
|
||||||
* Method: compress
|
* Method: compress
|
||||||
* Signature: (Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;)J
|
* Signature: (Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;)J
|
||||||
*/
|
*/
|
||||||
JNIEXPORT jlong JNICALL Java_org_xerial_snappy_Snappy_compress
|
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress
|
||||||
(JNIEnv* env, jclass self, jobject uncompressed, jobject compressed)
|
(JNIEnv* env, jclass self, jobject uncompressed, jint upos, jint ulen, jobject compressed, jint cpos)
|
||||||
{
|
{
|
||||||
void* uncompressedBuffer = env->GetDirectBufferAddress(uncompressed);
|
char* uncompressedBuffer = (char*) env->GetDirectBufferAddress(uncompressed);
|
||||||
|
char* compressedBuffer = (char*) env->GetDirectBufferAddress(compressed);
|
||||||
|
size_t compressedLength;
|
||||||
|
|
||||||
|
snappy::RawCompress(uncompressedBuffer, (size_t) ulen, compressedBuffer, &compressedLength);
|
||||||
return (jlong) 0;
|
return (jint) compressedLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -26,11 +29,17 @@ JNIEXPORT jlong JNICALL Java_org_xerial_snappy_Snappy_compress
|
||||||
* Method: uncompress
|
* Method: uncompress
|
||||||
* Signature: (Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;)Z
|
* Signature: (Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;)Z
|
||||||
*/
|
*/
|
||||||
JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_Snappy_uncompress
|
JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_rawDecompress
|
||||||
(JNIEnv *, jclass, jobject, jobject)
|
(JNIEnv * env, jclass self, jobject compressed, jint cpos, jint clen, jobject decompressed, jint dpos)
|
||||||
{
|
{
|
||||||
|
char* compressedBuffer = (char*) env->GetDirectBufferAddress(compressed) + cpos;
|
||||||
|
char* decompressedBuffer = (char*) env->GetDirectBufferAddress(decompressed) + dpos;
|
||||||
|
|
||||||
return (jboolean) true;
|
size_t decompressedLength;
|
||||||
|
snappy::GetUncompressedLength(compressedBuffer, (size_t) clen, &decompressedLength);
|
||||||
|
bool ret = snappy::RawUncompress(compressedBuffer, (size_t) clen, decompressedBuffer);
|
||||||
|
|
||||||
|
return (jboolean) ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -38,11 +47,11 @@ JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_Snappy_uncompress
|
||||||
* Method: maxCompressedLength
|
* Method: maxCompressedLength
|
||||||
* Signature: (J)J
|
* Signature: (J)J
|
||||||
*/
|
*/
|
||||||
JNIEXPORT jlong JNICALL Java_org_xerial_snappy_Snappy_maxCompressedLength
|
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_maxCompressedLength
|
||||||
(JNIEnv *, jclass, jlong)
|
(JNIEnv *, jclass, jint size)
|
||||||
{
|
{
|
||||||
|
size_t l = snappy::MaxCompressedLength((size_t) size);
|
||||||
return (jlong) 0;
|
return (jint) l;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -50,11 +59,16 @@ JNIEXPORT jlong JNICALL Java_org_xerial_snappy_Snappy_maxCompressedLength
|
||||||
* Method: getUncompressedLength
|
* Method: getUncompressedLength
|
||||||
* Signature: (Ljava/nio/ByteBuffer;)J
|
* Signature: (Ljava/nio/ByteBuffer;)J
|
||||||
*/
|
*/
|
||||||
JNIEXPORT jlong JNICALL Java_org_xerial_snappy_Snappy_getUncompressedLength
|
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_getUncompressedLength
|
||||||
(JNIEnv *, jclass, jobject)
|
(JNIEnv * env, jclass self, jobject compressed, jint cpos, jint clen)
|
||||||
{
|
{
|
||||||
|
char* compressedBuffer = (char*) env->GetDirectBufferAddress(compressed);
|
||||||
|
size_t result;
|
||||||
|
std::string s = "hello world";
|
||||||
|
//snappy::GetUncompressedLength(compressedBuffer, (size_t) clen, &result);
|
||||||
|
snappy::GetUncompressedLength(s.c_str(), s.length(), &result);
|
||||||
|
|
||||||
return (jlong) 0;
|
return (jint) result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,51 +1,51 @@
|
||||||
/* DO NOT EDIT THIS FILE - it is machine generated */
|
/* DO NOT EDIT THIS FILE - it is machine generated */
|
||||||
#include <jni.h>
|
#include <jni.h>
|
||||||
/* Header for class org_xerial_snappy_Snappy */
|
/* Header for class org_xerial_snappy_SnappyNative */
|
||||||
|
|
||||||
#ifndef _Included_org_xerial_snappy_Snappy
|
#ifndef _Included_org_xerial_snappy_SnappyNative
|
||||||
#define _Included_org_xerial_snappy_Snappy
|
#define _Included_org_xerial_snappy_SnappyNative
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
/*
|
/*
|
||||||
* Class: org_xerial_snappy_Snappy
|
* Class: org_xerial_snappy_SnappyNative
|
||||||
* Method: nativeLibraryVersion
|
* Method: nativeLibraryVersion
|
||||||
* Signature: ()Ljava/lang/String;
|
* Signature: ()Ljava/lang/String;
|
||||||
*/
|
*/
|
||||||
JNIEXPORT jstring JNICALL Java_org_xerial_snappy_Snappy_nativeLibraryVersion
|
JNIEXPORT jstring JNICALL Java_org_xerial_snappy_SnappyNative_nativeLibraryVersion
|
||||||
(JNIEnv *, jclass);
|
(JNIEnv *, jclass);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Class: org_xerial_snappy_Snappy
|
* Class: org_xerial_snappy_SnappyNative
|
||||||
* Method: compress
|
* Method: rawCompress
|
||||||
* Signature: (Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;)J
|
* Signature: (Ljava/nio/ByteBuffer;IILjava/nio/ByteBuffer;I)I
|
||||||
*/
|
*/
|
||||||
JNIEXPORT jlong JNICALL Java_org_xerial_snappy_Snappy_compress
|
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress
|
||||||
(JNIEnv *, jclass, jobject, jobject);
|
(JNIEnv *, jclass, jobject, jint, jint, jobject, jint);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Class: org_xerial_snappy_Snappy
|
* Class: org_xerial_snappy_SnappyNative
|
||||||
* Method: uncompress
|
* Method: rawDecompress
|
||||||
* Signature: (Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;)Z
|
* Signature: (Ljava/nio/ByteBuffer;IILjava/nio/ByteBuffer;I)Z
|
||||||
*/
|
*/
|
||||||
JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_Snappy_uncompress
|
JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_rawDecompress
|
||||||
(JNIEnv *, jclass, jobject, jobject);
|
(JNIEnv *, jclass, jobject, jint, jint, jobject, jint);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Class: org_xerial_snappy_Snappy
|
* Class: org_xerial_snappy_SnappyNative
|
||||||
* Method: maxCompressedLength
|
* Method: maxCompressedLength
|
||||||
* Signature: (J)J
|
* Signature: (I)I
|
||||||
*/
|
*/
|
||||||
JNIEXPORT jlong JNICALL Java_org_xerial_snappy_Snappy_maxCompressedLength
|
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_maxCompressedLength
|
||||||
(JNIEnv *, jclass, jlong);
|
(JNIEnv *, jclass, jint);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Class: org_xerial_snappy_Snappy
|
* Class: org_xerial_snappy_SnappyNative
|
||||||
* Method: getUncompressedLength
|
* Method: getUncompressedLength
|
||||||
* Signature: (Ljava/nio/ByteBuffer;)J
|
* Signature: (Ljava/nio/ByteBuffer;II)I
|
||||||
*/
|
*/
|
||||||
JNIEXPORT jlong JNICALL Java_org_xerial_snappy_Snappy_getUncompressedLength
|
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_getUncompressedLength
|
||||||
(JNIEnv *, jclass, jobject);
|
(JNIEnv *, jclass, jobject, jint, jint);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
//--------------------------------------
|
||||||
|
// snappy-java Project
|
||||||
|
//
|
||||||
|
// SnappyNative.java
|
||||||
|
// Since: 2011/03/30
|
||||||
|
//
|
||||||
|
// $URL$
|
||||||
|
// $Author$
|
||||||
|
//--------------------------------------
|
||||||
|
package org.xerial.snappy;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
|
public class SnappyNative
|
||||||
|
{
|
||||||
|
static {
|
||||||
|
LoadSnappy.initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
public native static String nativeLibraryVersion();
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
// Generic compression/decompression routines.
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
public native static int rawCompress(ByteBuffer input, int inputOffset, int inputLength, ByteBuffer compressed,
|
||||||
|
int outputOffset);
|
||||||
|
|
||||||
|
public native static boolean rawDecompress(ByteBuffer compressed, int inputOffset, int inputLength,
|
||||||
|
ByteBuffer uncompressed, int outputOffset);
|
||||||
|
|
||||||
|
// Returns the maximal size of the compressed representation of
|
||||||
|
// input data that is "source_bytes" bytes in length;
|
||||||
|
public native static int maxCompressedLength(int source_bytes);
|
||||||
|
|
||||||
|
// This operation takes O(1) time.
|
||||||
|
public native static int getUncompressedLength(ByteBuffer compressed, int offset, int len);
|
||||||
|
|
||||||
|
}
|
|
@ -20,7 +20,7 @@ public class SnappyTest
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getVersion() throws Exception {
|
public void getVersion() throws Exception {
|
||||||
String version = Snappy.nativeLibraryVersion();
|
String version = Snappy.getNativeLibraryVersion();
|
||||||
_logger.info("version: " + version);
|
_logger.info("version: " + version);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,9 +30,11 @@ public class SnappyTest
|
||||||
ByteBuffer src = ByteBuffer.allocate(1024);
|
ByteBuffer src = ByteBuffer.allocate(1024);
|
||||||
src.put("hello world".getBytes());
|
src.put("hello world".getBytes());
|
||||||
ByteBuffer dest = ByteBuffer.allocate(1024);
|
ByteBuffer dest = ByteBuffer.allocate(1024);
|
||||||
Snappy.compress(src, dest);
|
|
||||||
|
|
||||||
long uncompressedLen = Snappy.getUncompressedLength(dest);
|
src.flip();
|
||||||
_logger.info("uncompressed length:" + uncompressedLen);
|
int maxCompressedLen = Snappy.getMaxCompressedLength(src.remaining());
|
||||||
|
_logger.info("max compressed length:" + maxCompressedLen);
|
||||||
|
//long uncompressedLen = Snappy.getUncompressedLength(dest);
|
||||||
|
//
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue