Java InputStream Read All readAllFromStream(InputStream is)

Here you can find the source of readAllFromStream(InputStream is)

Description

read All From Stream

License

Open Source License

Declaration

public static byte[] readAllFromStream(InputStream is) throws IOException 

Method Source Code

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

import java.io.*;

public class Main {
    public static byte[] readAllFromStream(InputStream is) throws IOException {
        DataInputStream dis = new DataInputStream(is);
        byte[] buffer = new byte[4096];
        int totalBytesRead = 0, bytesRead;
        while ((bytesRead = dis.read(buffer, totalBytesRead, buffer.length - totalBytesRead)) >= 0) {
            if (totalBytesRead + bytesRead >= buffer.length) {
                byte[] oldBuffer = buffer;
                buffer = new byte[oldBuffer.length * 2];
                System.arraycopy(oldBuffer, 0, buffer, 0, oldBuffer.length);
            }/*w w w .ja  v a 2  s  .c o  m*/
            totalBytesRead += bytesRead;
        }

        byte[] ret = new byte[totalBytesRead];
        System.arraycopy(buffer, 0, ret, 0, totalBytesRead);
        return ret;
    }
}

Related

  1. readAll(InputStream is, byte[] buffer, int offset, int length)
  2. readAll(InputStream stream)
  3. readAll(InputStream stream)
  4. readAllBuffer(InputStream stream, byte[] buffer)
  5. readAllFrom(java.io.InputStream is)
  6. readAllInString(InputStream in)