I want to convert an InputStream is
into a Stream<String> stream
given a Charset cs
in such a way that stream
consists of the lines of is
. Furthermore a line of is
should not be read immediately but only in case stream
needs it.
To convert an InputStream Object int to a String using this method. Instantiate the Scanner class by passing your InputStream object as parameter. Read each line from this Scanner using the nextLine() method and append it to a StringBuffer object. Finally convert the StringBuffer to String using the toString() method.
InputStream. read() method reads the next byte of the data from the the input stream and returns int in the range of 0 to 255. If no byte is available because the end of the stream has been reached, the returned value is -1.
Example 1: Java Program to Convert InputStream to Byte Array byte[] array = stream. readAllBytes(); Here, the readAllBytes() method returns all the data from the stream and stores in the byte array. Note: We have used the Arrays.
I think you can try:
Stream<String> lines = new BufferedReader(new InputStreamReader(is, cs)).lines();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With