How to put the content of a ByteBuffer into an OutputStream?

后端 未结 1 442
[愿得一人]
[愿得一人] 2020-12-24 12:37

I need to put the contents of a java.nio.ByteBuffer into an java.io.OutputStream. (wish I had a Channel instead but I don\'t) What\'s

相关标签:
1条回答
  • 2020-12-24 13:21

    Look at Channels.newChannel(OutputStream). It will give you a channel given an OutputStream. With the WritableByteChannel adapter you can provide the ByteBuffer which will write it to the OutputStream.

    public void writeBuffer(ByteBuffer buffer, OutputStream stream) {
       WritableByteChannel channel = Channels.newChannel(stream);
    
       channel.write(buffer);
    }
    

    This should do the trick!

    0 讨论(0)
提交回复
热议问题