Sometimes we need to convert InputStream to byte array in Java, or you can say reading InputStream as byte array, In order to pass output to a method which accept byte array rather than InputStream. One popular example of this, I have seen is older version of Apache commons codec, while converting byte array to hex string. Though, later version of same library do provide an overloaded method, to accept InputStream. Java File API provides excellent support to read files like image, text as InputStream in Java program, but as I said, sometime you need need String or byte array, instead of InputStream . Earlier we have seen 5 ways to convert InputStream to String in Java , we can use some of the techniques from there while getting byte array from InputStream in Java. If you like to use Apache commons library, which I think you should, there is a utility class called IOUtils, which can be used to easily convert InputStream to byte array in Java. If you don't like using open source library for such kind of thinks, and like to write your own method, you can easily do so by using standard Java File API. In this Java tutorial we will see examples of both ways to convert InputStream to byte array in Java.
Langganan:
Posting Komentar (Atom)
0 komentar:
Posting Komentar