Conversion of InputStream to Stream in Java
In this article, we will explore different approaches for InputStream to Stream conversion in Java.
1. Introduction
In Java, InputStream is a fundamental class in the java.io
package that represents an input stream of bytes. It serves as a superclass for all classes that represent an input stream of bytes, such as FileInputStream
, ByteArrayInputStream
, and FilterInputStream
. The primary purpose of InputStream
is to provide a way to read data from various sources, including files, network connections, and byte arrays. This is achieved through a series of read methods that read bytes of data from the input stream. The read()
method, for example, reads a single byte of data and returns it as an integer value. If the end of the stream is reached, the method returns -1. The read(byte[] b)
method reads up to b.length
bytes of data into an array of bytes.
On the other hand, a Stream in Java, introduced in Java 8 as part of the java.util.stream
package is a sequence of elements supporting sequential and parallel aggregate operations. Unlike collections, which are primarily concerned with the storage and retrieval of data, streams are designed to perform complex data processing tasks such as filtering, mapping, and reducing. A stream can be obtained from various data sources such as collections, arrays, or I/O channels. The key advantage of streams is that they allow for a functional approach to processing data, enabling developers to write more concise and readable code. For instance, the filter
method can be used to select elements based on a predicate, while the map
method can transform each element into a new form. Streams support both intermediate operations (such as filter
and map
) that transform the stream into another stream and terminal operations (such as forEach
and reduce
) that produce a result or a side effect.
2. Converting With BufferedReader and lines() Method
In this method, we use BufferedReader and the lines() method from Java 8’s Stream API to convert an InputStream into a Stream<String>.
import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.util.stream.Stream; public class InputStreamToStreamExampleBufferedReader { public Stream<String> convertInputStreamToStream(InputStream inputStream) { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); return reader.lines(); } public static void main(String[] args) { String inputString = "Hello, World!\nThis is a test.\nConvert InputStream to Stream."; InputStream inputStream = new ByteArrayInputStream(inputString.getBytes()); InputStreamToStreamExampleBufferedReader converter = new InputStreamToStreamExampleBufferedReader(); Stream<String> linesStream = converter.convertInputStreamToStream(inputStream); // Process the linesStream as needed linesStream.forEach(System.out::println); } }
In the above code:
The convertInputStreamToStream
method initializes a BufferedReader with the InputStreamReader wrapping the InputStream. The lines()
method from BufferedReader returns a Stream<String> containing lines from the input.
The code returns the following output:
Hello, World! This is a test. Convert InputStream to Stream.
3. Converting with Scanner
Another way to convert an InputStream to Stream in Java is by using Scanner.
import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.Scanner; import java.util.stream.Stream; public class InputStreamToStreamExampleScanner { public Stream<String> convertInputStreamToStream(InputStream inputStream) { Scanner scanner = new Scanner(inputStream).useDelimiter("\\A"); return scanner.hasNext() ? Stream.of(scanner.next()) : Stream.empty(); } public static void main(String[] args) { String inputString = "Hello, World!\nThis is a test.\nConvert InputStream to Stream."; InputStream inputStream = new ByteArrayInputStream(inputString.getBytes()); InputStreamToStreamExampleScanner converter = new InputStreamToStreamExampleScanner(); Stream<String> linesStream = converter.convertInputStreamToStream(inputStream); // Process the linesStream as needed linesStream.forEach(System.out::println); } }
In the above code:
The convertInputStreamToStream
method creates a Scanner with the entire InputStream as the delimiter (\\A
matches the beginning of input). It then checks if there’s any input available and returns a Stream containing that input.
The code returns the following output:
Hello, World! This is a test. Convert InputStream to Stream.
6. Conclusion
Converting InputStream to Stream in Java can be achieved efficiently using BufferedReader with the lines() method for handling line-based input, or Scanner for more flexible token-based input. Depending on your specific use case, you can choose the method that best suits your needs.