Reader to Byte Array in Java

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

1. Introduction

In this article, we are going to present various ways to convert Reader to Byte Array in Java. The solutions presented here will be based on pure Java and using libraries such as Guava and Apache Commons IO.

If you are looking for more Java I/O related articles, please check the following links:

2. Convert Reader to Byte Array using plain Java

Let's start with the solution in plain Java. The first part of the conversion is actually the Reader to String transformation.

package com.frontbackend.java.io.conversions.fromreader.tobytearray;

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

public class ReaderToByteArrayInPlainJava {

    public static void main(String[] args) throws IOException {
        Reader reader = new StringReader("frontbackend.com");

        char[] buffer = new char[4096];
        StringBuilder builder = new StringBuilder();
        int numChars;

        while ((numChars = reader.read(buffer)) >= 0) {
            builder.append(buffer, 0, numChars);
        }

        byte[] bytes = builder.toString()
                              .getBytes(StandardCharsets.UTF_8);
    }
}

In this example first we covert Reader to String and then we get bytes from that String object.

3. Reader to a byte array using Guava

The Guava library also can we used to convert Reader to byte[].

package com.frontbackend.java.io.conversions.fromreader.tobytearray;

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

import com.google.common.io.CharStreams;

public class ReaderToByteArrayUsingGuava {

    public static void main(String[] args) throws IOException {
        Reader reader = new StringReader("frontbackend.com");

        byte[] bytes = CharStreams.toString(reader)
                                  .getBytes(StandardCharsets.UTF_8);
    }
}

In this example, we used Guava utility class CharStreams which gets String from the given Reader object. Then, we get bytes from that resulted String.

4. Convert Reader to byte[] using Apache Commons IO library

Apache Commons IO comes with a dedicated method that converts Reader to Byte Array directly:

package com.frontbackend.java.io.conversions.fromreader.tobytearray;

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

import org.apache.commons.io.IOUtils;

public class ReaderToByteArrayUsingIOUtils {

    public static void main(String[] args) throws IOException {
        Reader reader = new StringReader("frontbackend.com");

        byte[] bytes = IOUtils.toByteArray(reader, StandardCharsets.UTF_8);
    }
}

In this example we used IOUtils.toByteArray(..) method. Note that we should always give encoding of the given String in such transformations.

5. Conclusion

In this article, we presented several ways to convert Reader to a byte[] using plain Java, Guava, and Apache Commons IO library.

Like always, examples used in this article can we found at our GitHub repository.

{{ message }}

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