Convert InputStream to Reader in Java

May 16, 2020 No comments Java IO Conversions InputStream Reader

1. Introduction

In this tutorial, we are going to learn how to convert InputStream to Reader in Java using built-in Java methods and external libraries likeGuava and Apache Commons IO.

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

2. Converting InputStream to Reader using plain Java

In plain Java converting an InputStream to Reader is straightforward:

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

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

public class InputStreamToReaderUsingGuava {

    public static void main(String[] args) {
        InputStream inputStream = new ByteArrayInputStream("frontbackend.com".getBytes());

        Reader reader = new InputStreamReader(inputStream);
    }
}

In this example we can create Reader instance by wrapping InputStream inside an InputStreamReader object.

If we know the encoding of byte-base stream, we can specify it in the second parameter of InputStreamReader constructor, for example:

Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);

To create BufferedReader use the following:

BufferedReader br = new BufferedReader(reader);

3. Input Stream to Reader with Guava

Guava is an open-source set of common libraries for Java. It comes with nice utility methods for IO operations. The following example shows how to use CharSource to create Reader:

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

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;

import com.google.common.io.ByteStreams;
import com.google.common.io.CharSource;

public class InputStreamToReaderUsingGuava {

    public static void main(String[] args) throws IOException {
        InputStream inputStream = new ByteArrayInputStream("frontbackend.com".getBytes());

        byte[] buffer = ByteStreams.toByteArray(inputStream);
        Reader reader = CharSource.wrap(new String(buffer))
                                  .openStream();
    }
}

This solution is not so obvious like the plain Java one. First, we have to convert the InputStream to the byte array and then use CharSource.wrap method to create a character-based stream.

4. Convert InputStream to Reader using Apache Commons IO

Finally, the solution using Apache Commons IO:

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

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;

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

public class InputStreamToReaderUsingIOUtils {

    public static void main(String[] args) throws IOException {
        InputStream inputStream = new ByteArrayInputStream("frontbackend.com".getBytes());

        byte[] buffer = IOUtils.toByteArray(inputStream);
        Reader reader = new CharSequenceReader(new String(buffer));
    }
}

This solution is not straightforward. Just like in program using Guava library, here we also have to convert InputStream to an array of bytes first. Then, we can create CharSequenceReader object that wraps String with given bytes. CharSequenceReader class is a subclass of a Reader.

5. Conclusion

In this article, we presented several solutions on how to convert InputStream to a Reader in Java. The important thing to remember is that InputStream is a byte-based stream and Readers are character-based. Following this line of thought, in this article, we illustrated how to convert a byte-based stream to a character-based stream. Notice that since a InputStream just provides bytes, converting it to text-based stream means the encoding must be known.

All examples used in this tutorial are available under our GitHub.

{{ message }}

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