Convert InputStream to byte[] in Java?

You can use Apache commons-io to handle this and similar tasks The IOUtils type has a static method to read an InputStream and return a byte InputStream is; byte bytes = IOUtils. ToByteArray(is) Internally this creates a ByteArrayOutputStream and copies the bytes to the output, then calls to ByteArray(). It handles large files by copying the bytes in blocks of 4MB.

You can use Apache commons-io to handle this and similar tasks. The IOUtils type has a static method to read an InputStream and return a byte. InputStream is; byte bytes = IOUtils.

ToByteArray(is); Internally this creates a ByteArrayOutputStream and copies the bytes to the output, then calls to ByteArray(). It handles large files by copying the bytes in blocks of 4MB.

The jar is only 107KB and if you have need for one method from it, you are likely to use others too – Rich Seller Aug 12 '09 at 10:46 14 @oxbow_lakes: considering the staggering amount of wrong implementations of this feature I've seen in my developer life, I feel that yes it's very much worth the external dependency to get it right. – Joachim Sauer Jun 8 '10 at 12:45 3 Why not go and have a look at Apache commons stuff like FastArrayList or their soft & weak reference Maps and come back to tell me how "well-tested" this library is. It's a pile of rubbish – oxbow_lakes Jun 9 '10 at 7:09.

You need to read each byte from your InputStream and write it to a ByteArrayOutputStream. You can then retrieve the underlying byte array by calling toByteArray(); e.g. InputStream is = ... ByteArrayOutputStream buffer = new ByteArrayOutputStream(); int nRead; byte data = new byte16384; while ((nRead = is. Read(data, 0, data.

Length))! = -1) { buffer. Write(data, 0, nRead); } buffer.flush(); return buffer.toByteArray().

Other answers here show you how to read a file into a byte. Your byte will contain the exact contents of the file, and you'd need to decode that to do anything with the image data. Java's standard API for reading (and writing) images is the ImageIO API, which you can find in the package javax.imageio.

You can read in an image from a file with just a single line of code: BufferedImage image = ImageIO. Read(new File("image. Jpg")); This will give you a BufferedImage, not a byte.

To get at the image data, you can call getRaster() on the BufferedImage. This will give you a Raster object, which has methods to access the pixel data (it has several getPixel() / getPixels() methods). Lookup the API documentation for javax.imageio.

ImageIO, java.awt.image. BufferedImage, java.awt.image. Raster etc. ImageIO supports a number of image formats by default: JPEG, PNG, BMP, WBMP and GIF.It's possible to add support for more formats (you'd need a plug-in that implements the ImageIO service provider interface).

See also the following tutorial: Working with Images.

Below Codes public static byte serializeObj(Object obj) throws IOException { ByteArrayOutputStream baOStream = new ByteArrayOutputStream(); ObjectOutputStream objOStream = new ObjectOutputStream(baOStream); objOStream. WriteObject(obj); objOStream.flush(); objOStream.close(); return baOStream.toByteArray(); } OR BufferedImage img = ... ByteArrayOutputStream baos = new ByteArrayOutputStream(1000); ImageIO. Write(img, "jpeg", baos); baos.flush(); byte result = baos.toByteArray(); baos.close().

Input Stream is ... ByteArrayOutputStream bos = new ByteArrayOutputStream(); int next = in.read(); while (next > -1) { bos. Write(next); next = in.read(); } bos.flush(); byte result = bos.toByteArray();`.

InputStream class_InputStream = null; I am reading class from DB class_InputStream = rs. GetBinaryStream(1); Your Input stream could be from any source */ int thisLine; ByteArrayOutputStream bos = new ByteArrayOutputStream(); while ((thisLine = class_InputStream.read())! = -1) { bos.

Write(thisLine); } bos.flush(); byte yourBytes = bos.toByteArray(); /*Don't forget in the finally block to close ByteArrayOutputStream & InputStream In my case the IS is from resultset so just closing the rs will do it*/ if (bos! = null){ bos.close(); }.

Adamski: You can avoid buffer entirely. Code copied from exampledepot.com/egs/java.io/File2ByteAr... (Yes, it is very verbose, but needs half the size of memory as the other solution. ) // Returns the contents of the file in a byte array.

Public static byte getBytesFromFile(File file) throws IOException { InputStream is = new FileInputStream(file); // Get the size of the file long length = file.length(); // You cannot create an array using a long type. // It needs to be an int type. // Before converting to an int type, check // to ensure that file is not larger than Integer.

MAX_VALUE. If (length > Integer. MAX_VALUE) { // File is too large } // Create the byte array to hold the data byte bytes = new byte(int)length; // Read in the bytes int offset = 0; int numRead = 0; while (offset = 0) { offset += numRead; } // Ensure all the bytes have been read in if (offset Length) { throw new IOException("Could not completely read file "+file.getName()); } // Close the input stream and return bytes is.close(); return bytes; }.

This class is access-restricted, may break your build. – Markos Fragkakis 5 hours ago It starts with: public class IOUtils – pihentagy 1 hour ago.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions