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
Use vanilla Java's
DataInputStream
and itsreadFully
Method (exists since at least Java 1.4):There are some other flavors of this method, but I use this all the time for this use case.
The other case to get correct byte array via stream, after send request to server and waiting for the response.
@Adamski: You can avoid buffer entirely.
Code copied from http://www.exampledepot.com/egs/java.io/File2ByteArray.html (Yes, it is very verbose, but needs half the size of memory as the other solution.)
You can try Cactoos:
If you don't want to use the Apache commons-io library, this snippet is taken from the sun.misc.IOUtils class. It's nearly twice as fast as the common implementation using ByteBuffers: