Java InputStream to Byte Array inputStreamToByteArray(InputStream ins)

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

Description

Returns the given java.io.InputStream as a byte array.

License

Open Source License

Parameter

Parameter Description
ins a parameter

Declaration

public static byte[] inputStreamToByteArray(InputStream ins) 

Method Source Code

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

import java.io.*;

public class Main {
    /**//from  www . jav  a  2  s .  com
     * Returns the given {@link java.io.InputStream} as a byte array.
     * @param ins
     * @return
     */
    public static byte[] inputStreamToByteArray(InputStream ins) {
        byte[] availableBytes = new byte[0];
        try {
            byte[] buffer = new byte[4096];
            ByteArrayOutputStream outs = new ByteArrayOutputStream();

            int read = 0;
            while ((read = ins.read(buffer)) != -1) {
                outs.write(buffer, 0, read);
            }

            ins.close();
            outs.close();
            availableBytes = outs.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return availableBytes;
    }
}

Related

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