Java InputStream to Byte Array getBytesFromStream(InputStream is)

Here you can find the source of getBytesFromStream(InputStream is)

Description

Extracts a byte array from an InputStream

License

Open Source License

Exception

Parameter Description
IOException an exception

Return

the byte array

Declaration

public static byte[] getBytesFromStream(InputStream is) throws IOException 

Method Source Code


//package com.java2s;
/*//from   w  w w .  jav  a2  s  . c  om
 * Copyright (C) 2006-2016 Talend Inc. - www.talend.com
 * 
 * This source code is available under agreement available at
 * %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt
 * 
 * You should have received a copy of the agreement along with this program; if not, write to Talend SA 9 rue Pages
 * 92150 Suresnes, France
 */

import java.io.ByteArrayOutputStream;

import java.io.IOException;
import java.io.InputStream;

public class Main {
    /**
     * Extracts a byte array from an InputStream
     * 
     * @return the byte array
     * @throws IOException
     */
    public static byte[] getBytesFromStream(InputStream is) throws IOException {
        if (is == null) {
            return null;
        }
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        int b;
        while ((b = is.read()) != -1) {
            bos.write(b);
        }
        return bos.toByteArray();
    }
}

Related

  1. getBytesFromStream(InputStream is)
  2. getBytesFromStream(InputStream is)
  3. getBytesFromStream(InputStream is)
  4. getBytesFromStream(InputStream is)
  5. getBytesFromStream(InputStream is)
  6. getInputStreamAsByteArray(InputStream stream, int length)
  7. getInputStreamAsByteArray(InputStream stream, int length)
  8. getStreamAsByteArray(InputStream input)
  9. getStreamAsByteArray(InputStream stream)