in.close() closes out's socket -- is this a bug?
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? I would expect it to close the streams, but
leave the socket alone. The following code demonstrates this problem.
Invert the order of in.close() and out.close() and the program works.
Here is my test code:
Run this program by executing the code then running "telnet localhost
8080"
import java.io.*;
import java.net.*;
public class TestFlush2 {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8080);
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();
if(clientSocket.isClosed())
System.out.println("clientSocket is closed by in.close");
out.close();
clientSocket.close();
serverSocket.close();
System.out.println("Done");
}
}