Android InputStream to Byte Array Convert readBytes(InputStream in)

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

Description

read Bytes

Declaration

public static final byte[] readBytes(InputStream in) throws IOException 

Method Source Code

//package com.java2s;
import java.io.File;
import java.io.FileInputStream;

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

import java.util.Arrays;

public class Main {
    public static final byte[] readBytes(InputStream in) throws IOException {
        byte[] ret = new byte[0];
        byte[] buf = new byte[2048];
        int len;//from ww w . ja v  a 2  s  .  c o  m
        for (;;) {
            len = in.read(buf);
            if (len > 0) {
                ret = Arrays.copyOf(ret, ret.length + len);
                System.arraycopy(buf, 0, ret, ret.length - len, len);
            } else {
                break;
            }
        }
        return ret;
    }

    public static final byte[] readBytes(String path) throws IOException {
        return readBytes(new File(path));
    }

    public static final byte[] readBytes(File file) throws IOException {
        FileInputStream in = new FileInputStream(file.getAbsolutePath());
        try {
            return readBytes(in);
        } catch (IOException e) {
            throw e;
        } finally {
            in.close();
        }
    }
}

Related

  1. stream2byte(InputStream inStream)
  2. inStream2byte(InputStream inStream)
  3. inStream2byte(InputStream inStream)
  4. InputStram2byteArray(InputStream in)
  5. getBytes(InputStream is)
  6. InputStreamTOByte(InputStream in)
  7. InputStreamTOByte(InputStream in)
  8. InputStreamTOByte(InputStream in)
  9. InputStreamTOByte(InputStream in)