Java ByteArrayOutputStream Write readAll(InputStream in)

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

Description

read All

License

Apache License

Declaration

public static byte[] readAll(InputStream in) 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.io.OutputStream;

public class Main {
    public static byte[] readAll(InputStream in) throws IOException {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        transferStream(in, bout);//from  w w  w  .  j  a va  2 s . com
        byte[] bytes = bout.toByteArray();
        bout.close();
        return bytes;
    }

    public static void transferStream(InputStream in, OutputStream out) throws IOException {
        byte[] bytes = new byte[1024];
        int numRead = in.read(bytes);
        while (numRead != -1) {
            out.write(bytes, 0, numRead);
            numRead = in.read(bytes);
        }
    }

    public static void close(InputStream in) {
        try {
            in.close();
        } catch (IOException e) {
        }
    }

    public static void close(OutputStream out) {
        try {
            out.close();
        } catch (IOException e) {
        }
    }
}

Related

  1. readAll(InputStream bytes, int bufferSize)
  2. readAll(InputStream in)
  3. readAll(InputStream in)
  4. readAll(InputStream in)
  5. readAll(InputStream in)
  6. readAll(InputStream in)
  7. readAll(InputStream in)
  8. readAll(InputStream in)
  9. readAll(InputStream inputStream)