How to convert Byte Array to Reader in Java

May 16, 2020 No comments Java IO Conversions Byte Array Reader

1. Introduction

In this article, we will showcase several ways how to convert Byte Array to Reader using plain Java, Guava, and Apache Commons IO library.

Check the following links for other useful articles:

2. Convert Byte Array to Reader using plain Java

Pure Java gives us an almost straightforward solution. We can use StringReader or InputStreamReader:

Let's take a look at a solution using StringReader:

package com.frontbackend.java.io.conversions.toreader.frombytearray;

import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;

public class ByteArrayToReaderUsingStringReader {

    public static void main(String[] args) {
        byte[] bytes = "frontbackend.com".getBytes(StandardCharsets.UTF_8);

        Reader reader = new StringReader(new String(bytes));
    }
}

In this example first, we created String from the given bytes. Then, we create an instance of StringReader that takes a byte array as a parameter.

In the next approach in pure Java we will use InputStreamReader:

package com.frontbackend.java.io.conversions.toreader.frombytearray;

import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;

public class ByteArrayToReaderUsingInputStreamReader {

    public static void main(String[] args) {
        byte[] bytes = "frontbackend.com".getBytes(StandardCharsets.UTF_8);

        Reader targetReader = new InputStreamReader(new ByteArrayInputStream(bytes));
    }
}

In this example, we make use of ByteArrayInputStream to create a byte-based stream, and then, with InputStreamReader we created an instance of the Reader.

3. Byte Array to Reader with Guava library

The next solution will use Guava library. Let's take a look:

package com.frontbackend.java.io.conversions.toreader.frombytearray;

import java.io.IOException;
import java.io.Reader;
import java.nio.charset.StandardCharsets;

import com.google.common.io.CharSource;

public class ByteArrayToReaderUsingGuava {

    public static void main(String[] args) throws IOException {
        byte[] bytes = "frontbackend.com".getBytes(StandardCharsets.UTF_8);

        Reader reader = CharSource.wrap(new String(bytes))
                                  .openStream();
    }
}

In this example, we need to convert bytes to the String first because Guava's ByteSource does not allow direct conversions. Then, we used CharSource to wrap the String and create Reader out of it.

4. Byte Array to Reader conversion using Apache Commons IO

Another library we can use to convert Byte Array to Reader is Apache Commons IO that comes with many helpful utility methods:

package com.frontbackend.java.io.conversions.toreader.frombytearray;

import java.io.Reader;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.input.CharSequenceReader;

public class ByteArrayToReaderUsingApacheCommonsIO {

    public static void main(String[] args) {
        byte[] bytes = "frontbackend.com".getBytes(StandardCharsets.UTF_8);

        Reader reader = new CharSequenceReader(new String(bytes));
    }
}

In this example, we also need to create String from given bytes and then use CharSequenceReader to create a Reader object.

5. Conclusion

In this article, we showed ways to convert byte[] to Reader in Java. The approach using InputStreamReader is a direct solution, that does not require the intermediary String representation.

Example snippets presented in this article are available in our GitHub repository.

{{ message }}

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