Re: in.close() closes out's socket -- is this a bug?



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
.



Relevant Pages

  • Re: in.close() closes outs socket -- is this a bug?
    ... it closes the socket without first flushing the output ... That's pretty counter intuitive that a close does not flush. ... socket streams do not act like file streams. ...
    (comp.lang.java.programmer)
  • in.close() closes outs socket -- is this a bug?
    ... I have buffered input and output streams going to a socket. ... BufferedOutputStream out = new BufferedOutputStream( ...
    (comp.lang.java.programmer)
  • Re: SCTP using questions (API etc.)
    ... How long can be one particular SCTP message? ... You do not have to deal with the recv buffer ... size of the send socket buffer.. ... What is the minimum number of streams application can rely ...
    (freebsd-net)
  • Re: [PATCH 4/6] sctp multistream scheduling: extend socket API
    ... The choice and configuration of the scheduling algorithm is still ... declaring the socket but before association establishment. ... socket option is set, the configurations are stored in sctp_sock. ... If the negotiated number of streams is smaller that what the user ...
    (Linux-Kernel)
  • Re: Networking with CL
    ... is not good even for it's socket brother UDP. ... Streams are actually a lousy abstraction for reading from a network ... because for many protocols you don't know how ...
    (comp.lang.lisp)