NFD在传送大量数据的时候,比如我要传输大文件,或者各类文件的时候,只能使用Blob,但是当接收方接收数据的时候,Blob只能转为ByteBuffer,如何转化为byte[]呢?在查看API文档的时候,发现有个array()的函数,但是被坑惨了!!!根本读不出来好嘛!!!
在stackoverflow上找到问题:
If
hasArray()
reports
false
then, calling
array()
will throw an exception.
In that case, the only way to get the data in a
byte[]
is to allocate a
byte[]
and copy the bytes to the
byte[]
using
get(byte)
or similar.
难道阅读纯正的英文文档也有错么T^T
所以,为避免上述的问题,将ByteBuffer转化为byte[]的正确做法是:
ByteBuffer bb =..
byte[] b = new byte[bb.remaining()]; //byte[] b = new byte[bb.capacity()] is OK
</span>
bb.get(b, 0, b.length); //bb.get(b) is OK
那么,如何将byte[]转化为ByteBuffer:
byte[] bytes = ......;
ByteBuffer buf = ByteBuffer.wrap(bytes);
肘子要加油做毕设!!!
NFD在传送大量数据的时候,比如我要传输大文件,或者各类文件的时候,只能使用Blob,但是当接收方接收数据的时候,Blob只能转为ByteBuffer,如何转化为byte[]呢?在查看API文档的时候,发现有个array()的函数,但是被坑惨了!!!根本读不出来好嘛!!!在stackoverflow上找到问题:If hasArray() reports false then,
//step2 新建一个
byte
Byffer 将文字赛道
byteBuffer
里面
String strInput="strInput ttttt";
ByteBuffer
byteBuffer
=
ByteBuffer
.allocate(1024);
byteBuffer
.put(strInput.get
Byte
s());
@Override
public String decodeKey(
ByteBuffer
byte
s) {
Charset charset = Charset.forName("utf-8");
return charset.decode(
byte
s).toString();
@Override
public
byte
[] decodeValue(
ByteBuffer
by...
1.
byte
[]转
ByteBuffer
使用wrap方法就可以了,no direct
ByteBuffer
是什么原因?
一般直接可以转的
ByteBuffer
.wrap(
byte
s)
然而接口接收处要求的也是java.nio.
ByteBuffer
,这就比较奇怪了。
经过一番研究后,ByeBuffer是一个抽象类,其还有一些其他子类,所以说接口参数要求为
ByteBuffer
没有问题。
那其实报这个错的原因就是
转化
后不是一个Direct
ByteBuffer
对象,这是
ByteBuffer
子类。
因此,需要声明或者
遇到一个问题,就是我想拿到字节缓冲转为
byte
[]数组,然后进行相关的操作,
bytebuffer
.array()一直报这个错Exception in thread "main" java.lang.UnsupportedOperationException,字面意思就是不支持的操作,可是明明提供了这个方法,为什么说是不支持的操作呢?
翻看jdk帮助文档,原话如下
调用此方法之前要调用 ha