Move native API to SnappyNative.java

This commit is contained in:
Taro L. Saito 2011-03-30 13:08:36 +09:00
parent 4e59477bf6
commit 158991f100
8 changed files with 153 additions and 97 deletions

View File

@ -17,9 +17,10 @@ $(TARGET)/snappy-$(VERSION): $(SNAPPY_ARCHIVE)
tar xvfz $< -C $(TARGET)
$(SRC)/org/xerial/snappy/SnappyNative.h: $(SRC)/org/xerial/snappy/Snappy.java
javah -classpath $(TARGET)/classes -o $@ org.xerial.snappy.Snappy
jni-header: $(SRC)/org/xerial/snappy/SnappyNative.h
$(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
@ -46,5 +47,6 @@ snappy: $(NATIVE_DLL)
$(NATIVE_DLL): $(SNAPPY_OUT)/$(LIBNAME)
@mkdir -p $(@D)
cp $< $@
cp $< $(TARGET)/classes/org/xerial/snappy/native/$(OS_NAME)/$(OS_ARCH)/$(LIBNAME)

View File

@ -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 */

0
lib/include/config.h Normal file
View File

View File

@ -13,25 +13,64 @@ import java.nio.ByteBuffer;
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) {
// ------------------------------------------------------------------------
// Generic compression/decompression routines.
// ------------------------------------------------------------------------
// input: uncompressed[pos(), limit())
// 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
// input data that is "source_bytes" bytes in length;
public native static long maxCompressedLength(long source_bytes);
return compressedSize;
}
// 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);
}
}

View File

@ -1,7 +1,8 @@
#include <string>
#include <snappy.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)
{
return env->NewStringUTF("1.0.1");
@ -12,13 +13,15 @@ JNIEXPORT jstring JNICALL Java_org_xerial_snappy_Snappy_nativeLibraryVersion
* Method: compress
* Signature: (Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;)J
*/
JNIEXPORT jlong JNICALL Java_org_xerial_snappy_Snappy_compress
(JNIEnv* env, jclass self, jobject uncompressed, jobject compressed)
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress
(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;
return (jlong) 0;
snappy::RawCompress(uncompressedBuffer, (size_t) ulen, compressedBuffer, &compressedLength);
return (jint) compressedLength;
}
/*
@ -26,11 +29,17 @@ JNIEXPORT jlong JNICALL Java_org_xerial_snappy_Snappy_compress
* Method: uncompress
* Signature: (Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;)Z
*/
JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_Snappy_uncompress
(JNIEnv *, jclass, jobject, jobject)
JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_rawDecompress
(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
* Signature: (J)J
*/
JNIEXPORT jlong JNICALL Java_org_xerial_snappy_Snappy_maxCompressedLength
(JNIEnv *, jclass, jlong)
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_maxCompressedLength
(JNIEnv *, jclass, jint size)
{
return (jlong) 0;
size_t l = snappy::MaxCompressedLength((size_t) size);
return (jint) l;
}
/*
@ -50,11 +59,16 @@ JNIEXPORT jlong JNICALL Java_org_xerial_snappy_Snappy_maxCompressedLength
* Method: getUncompressedLength
* Signature: (Ljava/nio/ByteBuffer;)J
*/
JNIEXPORT jlong JNICALL Java_org_xerial_snappy_Snappy_getUncompressedLength
(JNIEnv *, jclass, jobject)
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_getUncompressedLength
(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;
}

View File

@ -1,51 +1,51 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class org_xerial_snappy_Snappy */
/* Header for class org_xerial_snappy_SnappyNative */
#ifndef _Included_org_xerial_snappy_Snappy
#define _Included_org_xerial_snappy_Snappy
#ifndef _Included_org_xerial_snappy_SnappyNative
#define _Included_org_xerial_snappy_SnappyNative
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: org_xerial_snappy_Snappy
* Class: org_xerial_snappy_SnappyNative
* Method: nativeLibraryVersion
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_org_xerial_snappy_Snappy_nativeLibraryVersion
JNIEXPORT jstring JNICALL Java_org_xerial_snappy_SnappyNative_nativeLibraryVersion
(JNIEnv *, jclass);
/*
* Class: org_xerial_snappy_Snappy
* Method: compress
* Signature: (Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;)J
* Class: org_xerial_snappy_SnappyNative
* Method: rawCompress
* Signature: (Ljava/nio/ByteBuffer;IILjava/nio/ByteBuffer;I)I
*/
JNIEXPORT jlong JNICALL Java_org_xerial_snappy_Snappy_compress
(JNIEnv *, jclass, jobject, jobject);
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress
(JNIEnv *, jclass, jobject, jint, jint, jobject, jint);
/*
* Class: org_xerial_snappy_Snappy
* Method: uncompress
* Signature: (Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;)Z
* Class: org_xerial_snappy_SnappyNative
* Method: rawDecompress
* Signature: (Ljava/nio/ByteBuffer;IILjava/nio/ByteBuffer;I)Z
*/
JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_Snappy_uncompress
(JNIEnv *, jclass, jobject, jobject);
JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_SnappyNative_rawDecompress
(JNIEnv *, jclass, jobject, jint, jint, jobject, jint);
/*
* Class: org_xerial_snappy_Snappy
* Class: org_xerial_snappy_SnappyNative
* Method: maxCompressedLength
* Signature: (J)J
* Signature: (I)I
*/
JNIEXPORT jlong JNICALL Java_org_xerial_snappy_Snappy_maxCompressedLength
(JNIEnv *, jclass, jlong);
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_maxCompressedLength
(JNIEnv *, jclass, jint);
/*
* Class: org_xerial_snappy_Snappy
* Class: org_xerial_snappy_SnappyNative
* Method: getUncompressedLength
* Signature: (Ljava/nio/ByteBuffer;)J
* Signature: (Ljava/nio/ByteBuffer;II)I
*/
JNIEXPORT jlong JNICALL Java_org_xerial_snappy_Snappy_getUncompressedLength
(JNIEnv *, jclass, jobject);
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_getUncompressedLength
(JNIEnv *, jclass, jobject, jint, jint);
#ifdef __cplusplus
}

View File

@ -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);
}

View File

@ -20,7 +20,7 @@ public class SnappyTest
@Test
public void getVersion() throws Exception {
String version = Snappy.nativeLibraryVersion();
String version = Snappy.getNativeLibraryVersion();
_logger.info("version: " + version);
}
@ -30,9 +30,11 @@ public class SnappyTest
ByteBuffer src = ByteBuffer.allocate(1024);
src.put("hello world".getBytes());
ByteBuffer dest = ByteBuffer.allocate(1024);
Snappy.compress(src, dest);
long uncompressedLen = Snappy.getUncompressedLength(dest);
_logger.info("uncompressed length:" + uncompressedLen);
src.flip();
int maxCompressedLen = Snappy.getMaxCompressedLength(src.remaining());
_logger.info("max compressed length:" + maxCompressedLen);
//long uncompressedLen = Snappy.getUncompressedLength(dest);
//
}
}