byte java 缓冲区,直接字节缓冲区

I am testing out direct ByteBuffer(java.nio.ByteBuffer) with JNI. So the code below tries to:

Put values into direct ByteBuffer in Java

Change the value in C++

Get the value in Java

I was wondering where exactly did I do wrong? The C++ code manage to get the data from Java but changes in C++ did not reflect back in Java.

This is what I did on java:

public static void main(String[] args){

ByteBuffer bb = ByteBuffer.allocateDirect(3);

byte[] b = {122,121,120};

System.out.println("1: " + new String(b));

bb.put(b);

new JNI.process(bb);

byte[] c = new byte[3];

c[0] = bb.get();

System.out.println("4: " + new String(c));

}

This is what I did on JNI function:

JNIEXPORT void JNICALL Java_MarsJNI_mapreduce

(JNIEnv *env, jobject thisObj, jobject output){

char *out = (char*)env->GetDirectBufferAddress(output);

printf("2: %s\n", out);

out = "ABC";

printf("3: %s\n", out);

}

And the result I get is:

1: zyx

2: zyx

3: ABC

Exception in thread "main" java.nio.BufferUnderflowException

at java.nio.Buffer.nextGetIndex(Buffer.java:474)

at java.nio.DirectByteBuffer.get(DirectByteBuffer.java:208)

at MarsJNI.main(MarsJNI.java:21)

解决方案

First problem: see @TedBigham's answer. You can also use buf.rewind().

Second problem: you only copy the first byte of the buffer into c, not the whole buffer. Do:

byte[] c = new byte[3];

bb.rewind();

bb.put(c);

System.out.println("4: " + new String(c));

Third problem: your C++ code does:

char *out = (char*)env->GetDirectBufferAddress(output);

// ...

out = "ABC";

But what you do here is create { 'A', 'B', 'C', 0 } and assign out to it; you don't actually modify the content of the buffer. You should do:

memcpy(out, "ABC", 3);

Fourth problem: when you create a String out of a byte[], you should specify the encoding:

new String(c, StandardCharsets.UTF_8);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值