How to convert InputStream to ByteBuffer in Java

May 16, 2020 No comments Java iO Conversions InputStream ByteBuffer

1. Introduction

In this short tutorial, we will present how to convert an InputStream to ByteBuffer using Java built-in methods and libraries like: Guava and Apache Commons IO. Buffer is a container for data of a specific primitive type introduced in Java 8. In this case, byte will be our primitive type.

For more Java I/O related articles, check the following links:

2. Convert InputStream to ByteBuffer using plain Java

In this example, we will use ByteArrayInputStream to read bytes and put them into byteBuffer until the end of the stream initialStream.available() == 0. Before the main loop the method initialStream.available()) will return the exact size of the underlying data:

package com.frontbackend.java.io.conversions;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

public class InputStreamToByteBufferUsingByteArrayInputStream {

    public static void main(String[] args) throws IOException {
        InputStream initialStream = new ByteArrayInputStream(new byte[] { 102, 114, 111, 110, 116, 98, 97, 99, 107, 101, 110, 100 });

        ByteBuffer byteBuffer = ByteBuffer.allocate(initialStream.available());
        while (initialStream.available() > 0) {
            byteBuffer.put((byte) initialStream.read());
        }
    }
}

3. Convert InputStream to ByteBuffer using Guava

Converting InputStream to ByteByffer could be achieved with external libraries like Guava:

package com.frontbackend.java.io.conversions;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

import com.google.common.io.ByteStreams;

public class InputStreamtoByteBufferUsingGuava {

    public static void main(String[] args) throws IOException {
        InputStream initialStream = new ByteArrayInputStream(new byte[] { 102, 114, 111, 110, 116, 98, 97, 99, 107, 101, 110, 100 });

        byte[] targetArray = ByteStreams.toByteArray(initialStream);
        ByteBuffer bufferByte = ByteBuffer.wrap(targetArray);
    }
}

In this example we use ByteStreams.toByteArray method from Guava that converts an InputStream into a byte array. Then, we create ByteBuffer object that wraps that byte array.

The latest version of Guava library is available under the following links:

4. InputStream to ByteBuffer using Apache Commons IO library

Apache Commons IO library comes with method IOUtils.readFully that allows us to rewrite ReadableByteChannel into ByteBuffer:

package com.frontbackend.java.io.conversions;

import static java.nio.channels.Channels.newChannel;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;

import org.apache.commons.io.IOUtils;

public class InputStreamToByteBufferUsingIOUtils {

    public static void main(String[] args) throws IOException {
        InputStream initialStream = new ByteArrayInputStream(new byte[] { 102, 114, 111, 110, 116, 98, 97, 99, 107, 101, 110, 100 });

        ByteBuffer byteBuffer = ByteBuffer.allocate(initialStream.available());
        ReadableByteChannel channel = newChannel(initialStream);
        IOUtils.readFully(channel, byteBuffer);
    }
}

The ReadableByteChannel was created using newChannel method from java.nio.channels.Channels package.

The latest version of the library could be found here: Maven Apache Commons IO 2.6

5. Conclusion

In this tutorial, we illustrated how to convert InputStream into the ByteBuffer object that is a wrapper object for a byte array in Java.

As always, code use in this article is available under our GitHub repository.

{{ message }}

{{ 'Comments are closed.' | trans }}