Java InputStream to Byte Array inputStreamToByteArray(InputStream is)

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

Description

input Stream To Byte Array

License

Open Source License

Declaration

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

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    public static byte[] inputStreamToByteArray(InputStream is) throws IOException {

        ByteArrayOutputStream os = null;

        try {//from w  w  w .java 2s .c  om
            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 ex) {
            return null;
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }
}

Related

  1. inputStreamToByteArray(InputStream in)
  2. inputStreamToByteArray(InputStream in)
  3. inputStreamToByteArray(InputStream input, int size)
  4. InputStreamToByteArray(InputStream inputStream)
  5. inputStreamToByteArray(InputStream ins)
  6. inputStreamToByteArray(InputStream is)
  7. inputStreamToByteArray(InputStream is)
  8. inputStreamToByteArray(InputStream is)
  9. inputStreamToByteArray(InputStream is)