Trying to find solution to read all bytes from InputStream
in non blocking mode. Section inputStream.read()
in function below blocks forever when no data are available.
private static String extract(InputStream inputStream) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int read = 0;
while ((read = inputStream.read(buffer, 0, buffer.length)) != -1) {
baos.write(buffer, 0, read);
}
baos.flush();
return new String(baos.toByteArray(), "UTF-8");
}
How to read all bytes without blocking?