get Byte Array from Input Stream - Java java.io

Java examples for java.io:InputStream Read

Description

get Byte Array from Input Stream

Demo Code


//package com.java2s;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

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

public class Main {
    public static ByteArrayInputStream getByteArrayInputStreamFromInputStream(
            InputStream inputStream) {
        byte[] b = getBytesFromInputStream(inputStream);
        return new ByteArrayInputStream(b);
    }/*from w w w  .  j a  v  a 2 s  . c o  m*/

    public static byte[] getBytesFromInputStream(InputStream is) {
        try (ByteArrayOutputStream os = new ByteArrayOutputStream();) {
            byte[] buffer = new byte[0xFFFF];

            for (int len; (len = is.read(buffer)) != -1;)
                os.write(buffer, 0, len);

            os.flush();

            return os.toByteArray();
        } catch (IOException e) {
            return null;
        }
    }
}

Related Tutorials