How to put data from an OutputStream into a ByteBuffer?

后端 未结 6 1843
庸人自扰
庸人自扰 2021-02-18 15:23

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?

6条回答
  •  广开言路
    2021-02-18 15:44

    // access the protected member buf & count from the extend class

    class ByteArrayOutputStream2ByteBuffer extends ByteArrayOutputStream {
        public ByteBuffer toByteBuffer() {
            return ByteBuffer.wrap(buf, 0, count);
        }
    }
    

提交回复
热议问题