Learn to convert a Reader to InputStream and also convert InputStream to Reader in this short Java IO tutorial.
Note that Reader is used for reading the characters and InputStream is used for reading the raw bytes. Both have been designed for separate purposes so be careful how you use them in the application.
1. Converting Reader to InputStream
A Reader holds character data and typically a string or character array. If we have access to String or char[] then we can directly get the InputStream from it:
try(InputStream inputStream = new ByteArrayInputStream(
content.getBytes(StandardCharsets.UTF_8))){
//Use InputStream
}
If we have reference to an existing Reader then we can use the following techniques to get InputStream.
1.1. Reader -> byte[] -> InputStream
We first get the content from Reader to byte[]. Use BufferedReader for better performance. Then we use the byte[] to create an InputStream.
This conversion process can be achieved in many ways and with different libraries. For example, let’s start with native IO APIs.
try(Reader reader = new BufferedReader(
new StringReader(content))){
char[] charBuffer = new char[8 * 1024];
int numCharsRead;
while ((numCharsRead = reader.read(charBuffer, 0,
charBuffer.length)) != -1) {
builder.append(charBuffer, 0, numCharsRead);
}
}
try(InputStream inputStream = new ByteArrayInputStream(
builder.toString().getBytes(StandardCharsets.UTF_8))){
//Use InputStream
}
Similarly, we can use Common IO’s IOUtils.toString(reader)
class to read the content from Reader to String.
String content = "Hello world";
try(Reader reader = new BufferedReader(new StringReader(content));
InputStream inputStream = IOUtils.toInputStream(IOUtils.toString(reader), Charsets.UTF_8);) {
//Use InputStream
}
We can also use Guava’s CharStreams.toString(reader)
class as similar to the previous solution.
try(
Reader reader = new BufferedReader(new StringReader(content));
InputStream inputStream = new ByteArrayInputStream(
CharStreams.toString(reader).getBytes(StandardCharsets.UTF_8))){
//Use InputStream
}
1.2. Commons IO’s ReaderInputStream
The ReaderInputStream is an InputStream implementation that reads a character stream from a Reader.
All the read() operations are buffered so there is no need to wrap the underlying Reader in a BufferedReader.
try(Reader reader = new StringReader("Hello world");
InputStream inputStream = new ReaderInputStream(reader, StandardCharsets.UTF_8);){
//Use inputStream
}
2. Converting InputStream to Reader
Java has InputStreamReader
that has been specifically designed for this purpose. This class works as a bridge from byte streams to character streams.
For top efficiency, consider wrapping an InputStreamReader within a BufferedReader.
InputStream inputStream = new ByteArrayInputStream("Hello world".getBytes());
Reader reader = new BufferedReader(new InputStreamReader(inputStream));
3. Conclusion
In this Java IO tutorial, we learned to convert between the Reader and InputStream using simple and easy-to-follow examples.
Happy Learning !!