Java InputStream Read Bytes readBytes(InputStream inputStream)

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

Description

read Bytes

License

Apache License

Declaration

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

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

import java.util.List;

public class Main {
    public static byte[] readBytes(InputStream inputStream) throws IOException {
        // this dynamically extends to take the bytes you read
        ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();

        // this is storage overwritten on each iteration with bytes
        int bufferSize = 1024;
        byte[] buffer = new byte[bufferSize];

        // we need to know how may bytes were read to write them to the byteBuffer
        int len = 0;
        while ((len = inputStream.read(buffer)) != -1) {
            byteBuffer.write(buffer, 0, len);
        }/*ww  w  . jav a  2  s. co  m*/

        // and then we can return your byte array.
        return byteBuffer.toByteArray();
    }

    public static byte[] ToByteArray(List<Byte> list) {
        byte[] array = new byte[list.size()];

        for (int i = 0; i < list.size(); i++) {
            array[i] = (byte) list.get(i);
        }

        return array;
    }
}

Related

  1. readbytes(InputStream input, int n)
  2. readBytes(InputStream input, int size, byte[] buffer)
  3. readBytes(InputStream inputStream)
  4. readBytes(InputStream inputStream)
  5. readBytes(InputStream inputStream)
  6. readBytes(InputStream inputStream)
  7. readBytes(InputStream inputStream)
  8. readBytes(InputStream inputStream)
  9. readBytes(InputStream inputStream)