如何将OutputStream中的数据放入ByteBuffer? [英] How to put data from an OutputStream into a ByteBuffer?

查看:1446
本文介绍了如何将OutputStream中的数据放入ByteBuffer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,我需要将OutputStream中的内容(我自己将数据填充到该流中)放入ByteBuffer中。如何以简单的方式做到这一点?

In Java I need to put content from an OutputStream (I fill data to that stream myself) into a ByteBuffer. How to do it in a simple way?

推荐答案

你可以创建一个 ByteArrayOutputStream 并写入,并使用 toByteArray()将内容提取为 byte [] 。然后 ByteBuffer.wrap(byte [])将使用输出字节数组的内容创建 ByteBuffer

You can create a ByteArrayOutputStream and write to it, and extract the contents as a byte[] using toByteArray(). Then ByteBuffer.wrap(byte []) will create a ByteBuffer with the contents of the output byte array.

这篇关于如何将OutputStream中的数据放入ByteBuffer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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