Convert InputStream to byte array in Java

前端 未结 30 3522
無奈伤痛
無奈伤痛 2020-11-21 12:08

How do I read an entire InputStream into a byte array?

相关标签:
30条回答
  • 2020-11-21 12:54

    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.

    public int readFully(InputStream in, byte[] data) throws IOException {
        int offset = 0;
        int bytesRead;
        boolean read = false;
        while ((bytesRead = in.read(data, offset, data.length - offset)) != -1) {
            read = true;
            offset += bytesRead;
            if (offset >= data.length) {
                break;
            }
        }
        return (read) ? offset : -1;
    }
    
    0 讨论(0)
  • 2020-11-21 12:56
    public static byte[] getBytesFromInputStream(InputStream is) throws IOException {
        ByteArrayOutputStream os = new ByteArrayOutputStream(); 
        byte[] buffer = new byte[0xFFFF];
        for (int len = is.read(buffer); len != -1; len = is.read(buffer)) { 
            os.write(buffer, 0, len);
        }
        return os.toByteArray();
    }
    
    0 讨论(0)
  • Do you really need the image as a byte[]? What exactly do you expect in the byte[] - the complete content of an image file, encoded in whatever format the image file is in, or RGB pixel values?

    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

    0 讨论(0)
  • 2020-11-21 12:58

    Java 7 and later:

    import sun.misc.IOUtils;
    ...
    InputStream in = ...;
    byte[] buf = IOUtils.readFully(in, -1, false);
    
    0 讨论(0)
  • 2020-11-21 12:59

    Finally, after twenty years, there’s a simple solution without the need for a 3rd party library, thanks to Java 9:

    InputStream is;
    …
    byte[] array = is.readAllBytes();
    

    Note also the convenience methods readNBytes(byte[] b, int off, int len) and transferTo(OutputStream) addressing recurring needs.

    0 讨论(0)
  • 2020-11-21 12:59

    As always, also Spring framework (spring-core since 3.2.2) has something for you: StreamUtils.copyToByteArray()

    0 讨论(0)
提交回复
热议问题