mirror of
https://github.com/xerial/snappy-java.git
synced 2025-07-23 14:04:39 +02:00
Use ByteBuffer.allocateDirect(). If you use ByteBuffer.allocate() to create a byte buffer, JNI call GetDirectBufferAddress(jobject:ByteBuffer) returns NULL.
This commit is contained in:
parent
e39178e719
commit
23bc6f6438
@ -27,6 +27,11 @@ public class Snappy
|
||||
*/
|
||||
public static int compress(ByteBuffer uncompressed, ByteBuffer compressed) {
|
||||
|
||||
if (!uncompressed.isDirect())
|
||||
throw new IllegalArgumentException("input is not a direct buffer");
|
||||
if (!compressed.isDirect())
|
||||
throw new IllegalArgumentException("destination is not a direct buffer");
|
||||
|
||||
// input: uncompressed[pos(), limit())
|
||||
// output: compressed
|
||||
int uPos = uncompressed.position();
|
||||
@ -54,6 +59,11 @@ public class Snappy
|
||||
*/
|
||||
public static boolean decompress(ByteBuffer compressed, ByteBuffer decompressed) {
|
||||
|
||||
if (!compressed.isDirect())
|
||||
throw new IllegalArgumentException("input is not a direct buffer");
|
||||
if (!decompressed.isDirect())
|
||||
throw new IllegalArgumentException("destination is not a direct buffer");
|
||||
|
||||
int cPos = compressed.position();
|
||||
int cLen = compressed.remaining();
|
||||
|
||||
@ -66,6 +76,9 @@ public class Snappy
|
||||
}
|
||||
|
||||
public static int getUncompressedLength(ByteBuffer compressed) {
|
||||
if (!compressed.isDirect())
|
||||
throw new IllegalArgumentException("input is not a direct buffer");
|
||||
|
||||
return SnappyNative.getUncompressedLength(compressed, compressed.position(), compressed.remaining());
|
||||
}
|
||||
|
||||
|
@ -16,8 +16,8 @@ JNIEXPORT jstring JNICALL Java_org_xerial_snappy_SnappyNative_nativeLibraryVersi
|
||||
JNIEXPORT jint JNICALL Java_org_xerial_snappy_SnappyNative_rawCompress
|
||||
(JNIEnv* env, jclass self, jobject uncompressed, jint upos, jint ulen, jobject compressed, jint cpos)
|
||||
{
|
||||
char* uncompressedBuffer = (char*) env->GetDirectBufferAddress(uncompressed);
|
||||
char* compressedBuffer = (char*) env->GetDirectBufferAddress(compressed);
|
||||
char* uncompressedBuffer = (char*) env->GetDirectBufferAddress(uncompressed) + upos;
|
||||
char* compressedBuffer = (char*) env->GetDirectBufferAddress(compressed) + cpos;
|
||||
size_t compressedLength;
|
||||
|
||||
snappy::RawCompress(uncompressedBuffer, (size_t) ulen, compressedBuffer, &compressedLength);
|
||||
@ -63,11 +63,11 @@ 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);
|
||||
if(compressedBuffer == 0)
|
||||
return (jint) -1;
|
||||
compressedBuffer += cpos;
|
||||
|
||||
snappy::GetUncompressedLength(compressedBuffer, (size_t) clen, &result);
|
||||
return (jint) result;
|
||||
}
|
||||
|
||||
|
@ -9,6 +9,8 @@
|
||||
//--------------------------------------
|
||||
package org.xerial.snappy;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.junit.Test;
|
||||
@ -24,17 +26,40 @@ public class SnappyTest
|
||||
_logger.info("version: " + version);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void directBufferCheck() throws Exception {
|
||||
|
||||
try {
|
||||
ByteBuffer src = ByteBuffer.allocate(1024);
|
||||
src.put("hello world".getBytes());
|
||||
src.flip();
|
||||
ByteBuffer dest = ByteBuffer.allocate(1024);
|
||||
int maxCompressedLen = Snappy.compress(src, dest);
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
// detected non-direct buffer. OK
|
||||
return;
|
||||
}
|
||||
|
||||
fail("shouldn't reach here");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void load() throws Exception {
|
||||
|
||||
ByteBuffer src = ByteBuffer.allocate(1024);
|
||||
ByteBuffer src = ByteBuffer.allocateDirect(1024);
|
||||
src.put("hello world".getBytes());
|
||||
ByteBuffer dest = ByteBuffer.allocate(1024);
|
||||
|
||||
src.flip();
|
||||
int maxCompressedLen = Snappy.getMaxCompressedLength(src.remaining());
|
||||
_logger.info("max compressed length:" + maxCompressedLen);
|
||||
//long uncompressedLen = Snappy.getUncompressedLength(dest);
|
||||
//
|
||||
|
||||
int uncompressedLen = Snappy.getUncompressedLength(src);
|
||||
_logger.info("uncompressed length: " + uncompressedLen);
|
||||
|
||||
ByteBuffer dest = ByteBuffer.allocateDirect(1024);
|
||||
Snappy.compress(src, dest);
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user