Reputation: 1562
I have an InputStream
and the following code works, reading some bytes from the input stream and creating a string with them:
byte[] buffer = new byte[1024];
int i = in.read(buffer, 0, 1024); //"in" is the input stream
String line = new String(buffer, 0, i);
System.out.println(line);
but the following doesn't, wrapping the input stream in InputStreamReader and trying to read from it never returns and stucks waiting:
InputStreamReader reader = new InputStreamReader(in, StandardCharsets.UTF_8);
reader.read(); //this call never returns...
When I debug I can see that the underlying read()
of the input stream gets called multiple times and returns bytes as expected, but the reader doesn't return.
Both of the above code blocks are inside an executor task, maybe it's related:
ExecutorService executor = Executors.newCachedThreadPool();
executor.submit(
() -> {
//Read from streams..
});
I have the same problem with BufferedReader, for it the readLine
method never returns.
Why can reading the raw input stream work but using readers not?
Upvotes: 0
Views: 31