byte[]与ByteBuffer相互转化
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);
肘子要加油做毕设!!!
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇: springMVC与freemarker整合