Reading a binary input stream into a single byte array in Java

前端 未结 6 1905
陌清茗
陌清茗 2020-11-27 18:10

The documentation says that one should not use available() method to determine the size of an InputStream. How can I read the whole content of an <

6条回答
  •  一向
    一向 (楼主)
    2020-11-27 18:51

    You can read it by chunks (byte buffer[] = new byte[2048]) and write the chunks to a ByteArrayOutputStream. From the ByteArrayOutputStream you can retrieve the contents as a byte[], without needing to determine its size beforehand.

提交回复
热议问题