Java Byte Array from getBytes(final InputStream is)

Here you can find the source of getBytes(final InputStream is)

Description

Reads an InputStream and returns the read byte[]

License

Open Source License

Parameter

Parameter Description
the InputStream

Exception

Parameter Description
IOException an exception

Return

the read byte[]

Declaration

public static byte[] getBytes(final InputStream is) throws IOException 

Method Source Code

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

import java.io.*;

public class Main {
    /**/*from  w w w. j a  v a  2s. c o  m*/
     * Reads an InputStream and returns the read byte[]
     *
     * @param the InputStream
     * @return the read byte[]
     * @throws IOException
     */
    public static byte[] getBytes(final InputStream is) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int a = 0;
        while ((a = is.read(buffer)) != -1) {
            baos.write(buffer, 0, a);
        }
        baos.close();
        buffer = null;
        return baos.toByteArray();
    }
}

Related

  1. getBytes(byte value)
  2. getBytes(byte[] buffer, int offset)
  3. getBytes(Class clazz)
  4. getBytes(Class clazz)
  5. getBytes(Class cls, String resourceName)
  6. getBytes(final Serializable obj)
  7. getBytes(final String data, String charset)
  8. getBytes(final String data, String charset)
  9. getBytes(final String value, final String characterEncoding)