Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert InputStream into Stream<String> given a Charset

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.

like image 784
principal-ideal-domain Avatar asked May 19 '15 21:05

principal-ideal-domain


People also ask

How do I change InputStream to string?

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.

How do I get input stream data?

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.

How do you convert input streams to bytes?

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.


1 Answers

I think you can try:

Stream<String> lines = new BufferedReader(new InputStreamReader(is, cs)).lines(); 
like image 95
Costi Ciudatu Avatar answered Oct 18 '22 03:10

Costi Ciudatu