Multiple readers for InputStream in Java

前端 未结 7 983
名媛妹妹
名媛妹妹 2020-12-03 11:28

I have an InputStream from which I\'m reading characters. I would like multiple readers to access this InputStream. It seems that a reasonable way to achieve this is to writ

7条回答
  •  醉酒成梦
    2020-12-03 11:58

    Instead of using StringWriter/StringBufferInputStream, write your original InputStream to a ByteArrayOutputStream. Once you've finished reading from the original InputStream, pass the byte array returned from ByteArrayOutputStream.toByteArray to a ByteArrayInputStream. Use this InputStream as the InputStream of choice for passing around other things that need to read from it.

    Essentially, all you'd be doing here is storing the contents of the original InputStream into a byte[] cache in memory as you tried to do originally with StringWriter/StringBufferInputStream.

提交回复
热议问题