Wrapping a ByteBuffer with an InputStream

后端 未结 6 1460
青春惊慌失措
青春惊慌失措 2020-12-01 04:46

I have a method that takes an InputStream and reads data from it. I would like to use this method with a ByteBuffer also. Is there a way to wrap a ByteBuffer so it can be ac

6条回答
  •  囚心锁ツ
    2020-12-01 05:42

    Nothing in the JDK, but there are lots of implementations out there, google for ByteBufferInputStream. Basically they wrap one or more ByteBuffers and keep track of an index into them that records how much has already been read. Something like this comes up a lot, but apparently is buggy, see @Mike Houston's answer for an improved version).

提交回复
热议问题