Android InputStream to Byte Array Convert getFileByte(InputStream inputStream)

Here you can find the source of getFileByte(InputStream inputStream)

Description

get File Byte

Parameter

Parameter Description
inputStream a parameter

Exception

Parameter Description
Exception an exception

Declaration

public static byte[] getFileByte(InputStream inputStream)
        throws Exception 

Method Source Code

//package com.java2s;
import java.io.ByteArrayOutputStream;

import java.io.InputStream;

public class Main {
    /**// w  ww .j a v a  2s .c o  m
     * 
     * @param inputStream
     * @return
     * @throws Exception
     */
    public static byte[] getFileByte(InputStream inputStream)
            throws Exception {
        InputStream fis = null;
        ByteArrayOutputStream sb = null;
        try {
            fis = inputStream;
            sb = new ByteArrayOutputStream();
            byte[] bytes = new byte[128];
            int n = -1;
            n = fis.read(bytes);
            while (n > -1) {
                sb.write(bytes, 0, n);
                n = fis.read(bytes);
            }
            byte[] rs = sb.toByteArray();
            return rs;
        } catch (Exception e) {
            throw e;
        } finally {
            if (fis != null) {
                fis.close();
                fis = null;
            }
            if (sb != null) {
                sb.close();
                sb = null;
            }
        }
    }
}

Related

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