Android InputStream to Byte Array Convert readBytes(InputStream s)

Here you can find the source of readBytes(InputStream s)

Description

read fully the contents of s and return a byte array holding the result

License

Open Source License

Exception

Parameter Description
IOException an exception

Declaration

public static byte[] readBytes(InputStream s) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from w  ww  . ja  v a 2  s . c  om*/
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.io.ByteArrayOutputStream;

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

public class Main {
    /**
     * read fully the contents of s and return a byte array holding the result
     * 
     * @throws IOException
     */
    public static byte[] readBytes(InputStream s) throws IOException {
        if (s == null) {
            throw new IllegalArgumentException("null s");
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] b = new byte[1024];
        int n = s.read(b);
        while (n != -1) {
            out.write(b, 0, n);
            n = s.read(b);
        }
        byte[] bb = out.toByteArray();
        out.close();
        return bb;
    }
}

Related

  1. getBytes(InputStream inputStream)
  2. getFileByte(InputStream inputStream)
  3. readFile(InputStream in, String encoding)
  4. readFile(InputStream in, int size)
  5. stream2byte(InputStream inStream)
  6. inStream2byte(InputStream inStream)