I'm trying to write some simple socket code that has the following basic form -
try(BufferedReader request = new BufferedReader(new InputStreamReader(sock.getInputStream()))){
//Do some work...
}
//BufferedReader gets closed, but also makes the socket close
...
...
response.write(blah);//Causes exception because socket is closed
My problem is that the socket is being closed, but I don't think it should be. The try-with-resources creates a BufferedReader and then auto-closes it when I leave the try block, but for some reason it also closes the entire socket! So when I get to my response code later on that uses the same socket I get an exception. Is there any way to fix this? Or do I just have to not use a try-with-resources (which would be less than ideal)?
BufferedReader.close() closes the backing stream.
The way to work around is a "wrapping" stream that doesn't propagate the close()