Java ReaderInputStream-class And Method Code Example
Here is an example of how you might use the ReaderInputStream class from the org.apache.commons.io package in Java:
import java.io.Reader;
import java.io.StringReader;
import org.apache.commons.io.input.ReaderInputStream;
public class MyClass {
public void doSomethingWithReaderInputStream() throws Exception {
// Create a string reader
String data = "Hello World";
Reader reader = new StringReader(data);
// Create a reader input stream
ReaderInputStream readerInputStream = new ReaderInputStream(reader);
// Read data from the reader input stream
int value = readerInputStream.read();
while (value != -1) {
System.out.println(value);
value = readerInputStream.read();
}
}
}
The ReaderInputStream
class is a utility class that adapts a Reader to an InputStream. In this example, we created a StringReader and pass it to the ReaderInputStream constructor, it will convert the stream of characters into bytes, so that you can read the data from the Reader using the read() method of an InputStream.
The ReaderInputStream
class also provides a skip(long n) method to skip a specific number of bytes and available() method to return the number of bytes that can be read from the stream without blocking.
You can also pass a Charset object to the constructor to specify the character set to be used for the conversion.