Java InputStream Read Bytes readBytes(InputStream is)

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

Description

read Bytes

License

Apache License

Declaration

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

Method Source Code


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

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

public class Main {
    public static byte[] readBytes(InputStream is) throws IOException {
        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
        try {//from www.  jav  a2  s  .c  o  m
            byte[] transfer = new byte[1024];
            int read = -1;
            while ((read = is.read(transfer)) > 0) {
                bytesOut.write(transfer, 0, read);
            }
        } finally {
            safeClose(is);
        }
        return bytesOut.toByteArray();
    }

    public static void safeClose(Closeable closeable) {
        try {
            closeable.close();
        } catch (IOException ignored) {
        }
    }
}

Related

  1. readBytes(InputStream inputStream, int bufSize)
  2. readBytes(InputStream inputStream, int length)
  3. readBytes(InputStream inputStream, int numberOfBytes)
  4. readBytes(InputStream inputStream, int numberOfBytes)
  5. readBytes(InputStream ins, byte[] buf, int size)
  6. readBytes(InputStream is)
  7. readBytes(InputStream is)
  8. readBytes(InputStream is)
  9. readBytes(InputStream is)