Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 1.8 and below equivalent for InputStream.readAllBytes()

I wrote a program that gets all bytes from an InputStream in Java 9 with

InputStream.readAllBytes()

Now, I want to export it to Java 1.8 and below. Is there an equivalent function? Couldn't find one.

like image 933
Flontis Avatar asked Nov 26 '19 10:11

Flontis


People also ask

How do you convert an InputStream into string in Java?

To convert an InputStream Object int to a String using this method. Instantiate the Scanner class by passing your InputStream object as parameter. Read each line from this Scanner using the nextLine() method and append it to a StringBuffer object. Finally convert the StringBuffer to String using the toString() method.

How do you convert an InputStream to Bytearray in Java?

Example 1: Java Program to Convert InputStream to Byte Arraybyte[] array = stream. readAllBytes(); Here, the readAllBytes() method returns all the data from the stream and stores in the byte array. Note: We have used the Arrays.

How do you write InputStream to ByteArrayOutputStream?

InputStream is; byte[] bytes = IOUtils. toByteArray(is); Internally this creates a ByteArrayOutputStream and copies the bytes to the output, then calls toByteArray() . It handles large files by copying the bytes in blocks of 4KiB.

How do you read all bytes from InputStream?

Since Java 9, we can use the readAllBytes() method from InputStream class to read all bytes into a byte array. This method reads all bytes from an InputStream object at once and blocks until all remaining bytes have read and end of a stream is detected, or an exception is thrown.

When to use the readallbytes () method of InputStream in Java 9?

When to use the readAllBytes () method of InputStream in Java 9? Since Java 9, we can use the readAllBytes () method from InputStream class to read all bytes into a byte array. This method reads all bytes from an InputStream object at once and blocks until all remaining bytes have read and end of a stream is detected, or an exception is thrown.

How to convert InputStream to byte array in Java?

For this, we read each byte from a InputStream class and write it to a ByteArrayOutputStreamclass. Later we call toByteArray () that returns the output stream in the form of a byte array. ByteStreams utility class from the Guava Library has a direct method to convert input stream to a byte array.

How to read all bytes available in the input stream?

To overcome the drawback of having to know the input size beforehand, we have another method called readAllBytes () since Java 9. This method can read all bytes available in the input stream. The method is way more faster and efficient in converting input stream to a byte array.

How do you read a byte array in Java 9?

Java Object Oriented Programming Programming Since Java 9, we can use the readAllBytes () method from InputStream class to read all bytes into a byte array. This method reads all bytes from an InputStream object at once and blocks until all remaining bytes have read and end of a stream is detected, or an exception is thrown.


2 Answers

You can use the good old read method like this:

   public static byte[] readAllBytes(InputStream inputStream) throws IOException {
    final int bufLen = 1024;
    byte[] buf = new byte[bufLen];
    int readLen;
    IOException exception = null;

    try {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        while ((readLen = inputStream.read(buf, 0, bufLen)) != -1)
            outputStream.write(buf, 0, readLen);

        return outputStream.toByteArray();
    } catch (IOException e) {
        exception = e;
        throw e;
    } finally {
        if (exception == null) inputStream.close();
        else try {
            inputStream.close();
        } catch (IOException e) {
            exception.addSuppressed(e);
        }
    }
}
like image 34
Shankha057 Avatar answered Nov 15 '22 03:11

Shankha057


InputStream.readAllBytes() is available since java 9 not java 7...

Other than that you can (no thirdparties):

byte[] bytes = new byte[(int) file.length()];
DataInputStream dataInputStream = new DataInputStream(new FileInputStream(file));
dataInputStream .readFully(bytes);

Or if you don't mind using thirdparties (Commons IO):


byte[] bytes = IOUtils.toByteArray(is);

Guava also helps:

byte[] bytes = ByteStreams.toByteArray(inputStream);
like image 67
Mark Bramnik Avatar answered Nov 15 '22 04:11

Mark Bramnik