1. Introduction
In this article, we are going to present how to convert Reader to InputString with plain Java, Guava
, and Apache Commons IO
library. There is no straightforward solution, but let's take a look at several approaches.
Check some other useful articles about Java I/O:
2. Convert Reader to InputStream using plain Java
Let's see how to achieve that in a plain Java:
package com.frontbackend.java.io.conversions.fromreader.toinputstream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
public class ReaderToInputStreamInPlainJava {
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);
}
InputStream inputStream = new ByteArrayInputStream(builder.toString()
.getBytes(StandardCharsets.UTF_8));
}
}
In this example, first, we convert Reader
to String
, then we get byte array from that String
and create the ByteArrayInputStream
from it.
3. Reader to InputStream using Guava
Using Guava
gives us a one-liner solution:
package com.frontbackend.java.io.conversions.fromreader.toinputstream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import com.google.common.io.CharStreams;
public class ReaderToInputStreamUsingGuava {
public static void main(String[] args) throws IOException {
Reader reader = new StringReader("frontbackend.com");
InputStream inputStream = new ByteArrayInputStream(CharStreams.toString(reader)
.getBytes(StandardCharsets.UTF_8));
}
}
In this example, we used utility class CharStreams
that converts Reader
to a String
. Then, we get bytes from that String
and created an instance of ByteArrayInputStream
using that byte array
.
4. Convert Reader to InputStream using Apache Commons IO
library
In Apache Commons IO
library we have also dedicated method that can do the conversion in a single line of code:
package com.frontbackend.java.io.conversions.fromreader.toinputstream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
public class ReaderToInputStreamUsingIOUtils {
public static void main(String[] args) throws IOException {
Reader reader = new StringReader("frontbackend.com");
InputStream inputStream = new ByteArrayInputStream(IOUtils.toByteArray(reader, StandardCharsets.UTF_8));
}
}
In this example, we used IOUtils.toByteArray(..)
method that returns byte array
from given Reader
. Then we created ByteArrayInputStream
that based on that bytes
.
5. Conclusion
In this article, we illustrated several ways to convert Reader to InputStream in Java. There is no straightforward solution to that transformation, so to achieve that we had to first convert Reader to Byte Array and then convert Byte Array to InputStream.
As usual, examples used in this article are available under the GitHub repository.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}