How do I read an entire InputStream
into a byte array?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Wrap it in a DataInputStream if that is off the table for some reason, just use read to hammer on it until it gives you a -1 or the entire block you asked for.
Finally, after twenty years, there’s a simple solution without the need for a 3rd party library, thanks to Java 9:
Note also the convenience methods
readNBytes(byte[] b, int off, int len)
andtransferTo(OutputStream)
addressing recurring needs.If you happen to use google guava, it'll be as simple as :
Java 9 will give you finally a nice method:
I know it's too late but here I think is cleaner solution that's more readable...