Re: in.close() closes out's socket -- is this a bug?
- From: Steven Simpson <ss@xxxxxxxxxxxxxx>
- Date: Sat, 04 Jul 2009 10:15:21 +0100
Duane Evenson wrote:
I have buffered input and output streams going to a socket. When I close
the input stream, it closes the socket without first flushing the output
buffer.
Should it do this?
Socket clientSocket = serverSocket.accept();
BufferedOutputStream out = new BufferedOutputStream(
clientSocket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
out.write("good output test string\n".getBytes("US-ASCII"));
in.close();
The output buffering is being done in an object which the Socket knows
nothing about, so even if the socket calls flush on its output stream
before it actually closes down, it's not flushing the right object far
enough up the chain of output streams.
You could wrap the InputStream from the socket in a FilterInputStream
that intercepts the close, and calls flush on the BufferedOutputStream,
but...
I would expect it to close the streams, but
leave the socket alone.
You could achieve this by wrapping both streams with filters, detecting
the closes, and turning them into shutdownInput()/shutdownOutput()
calls. That's probably a more robust plan than making a close on input
cause a flush on output, as it doesn't really matter then what chain of
streams end up hooked onto those filters.
--
ss at comp dot lancs dot ac dot uk
.
- References:
- in.close() closes out's socket -- is this a bug?
- From: Duane Evenson
- in.close() closes out's socket -- is this a bug?
- Prev by Date: Re: in.close() closes out's socket -- is this a bug?
- Next by Date: Doubt in Strings
- Previous by thread: Re: in.close() closes out's socket -- is this a bug?
- Next by thread: Doubt in Strings
- Index(es):
Relevant Pages
|