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
This is my copy-paste version:
Safe solution (with capability of
close
streams correctly):Java 9+ version:
Java 8 version:
Kotlin version (compatible with Java ≤8):
To avoid nested
use
see here.I tried to edit @numan's answer with a fix for writing garbage data but edit was rejected. While this short piece of code is nothing brilliant I can't see any other better answer. Here's what makes most sense to me:
btw ByteArrayOutputStream need not be closed. try/finally constructs omitted for readability
Java 8 way (thanks to BufferedReader and Adam Bien)
Note that this solution wipes carriage return ('\r') and can be inappropriate.
We are seeing some delay for few AWS transaction, while converting S3 object to ByteArray.
Note: S3 Object is PDF document (max size is 3 mb).
We are using the option #1 (org.apache.commons.io.IOUtils) to convert the S3 object to ByteArray. We have noticed S3 provide the inbuild IOUtils method to convert the S3 object to ByteArray, we are request you to confirm what is the best way to convert the S3 object to ByteArray to avoid the delay.
Option #1:
Option #2:
Also let me know if we have any other better way to convert the s3 object to bytearray