get Bytes From Input Stream - Java java.io

Java examples for java.io:InputStream Read

Description

get Bytes From Input Stream

Demo Code


//package com.java2s;

import java.io.ByteArrayOutputStream;

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

public class Main {
    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();/*from   w  w  w .j  a  v a 2s. c o  m*/

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

Related Tutorials