将ByteBuffer转换为字节数组Java [英] Convert ByteBuffer to byte array java

查看:1370
本文介绍了将ByteBuffer转换为字节数组Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何将ByteBuffer转换为byte []数组吗?我需要从我的ByteBuffer中获取字节数组.当我运行bytebuffer.hasArrray()时,它返回否.到目前为止,我看到的每个问题都是将字节数组转换为byteBuffer,但是我需要其他方式. 谢谢.

Does anyone know how to convert ByteBuffer to byte[] array? I need to get byte array from my ByteBuffer. When I run bytebuffer.hasArrray() it returns no. Every question I looked so far is converting byte array to byteBuffer, but I need it other way around. Thank you.

推荐答案

ByteBuffer公开了批量get(byte[])方法,该方法将字节从缓冲区传输到数组中.您需要实例化一个长度等于缓冲区中剩余字节数的数组.

ByteBuffer exposes the bulk get(byte[]) method which transfers bytes from the buffer into the array. You'll need to instantiate an array of length equal to the number of remaining bytes in the buffer.

ByteBuffer buf = ...
byte[] arr = new byte[buf.remaining()];
buf.get(arr);

这篇关于将ByteBuffer转换为字节数组Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆