I'm writing characters to a BufferedWriter whose underlying OutputStream is a TCP socket connection. At the other end of the socket, I see each full buffer of data, but I don't see the last (partial) buffer of data. On the client side, I verify with debug code that I'm writing all the data to the stream, and then I flush the BufferedWriter and all its underlying output streams. But the last partial buffer of data never makes it to the other end of the connection.
here's my client code:
int i = 0;
char[] buf = new char[1024];
oos = new ObjectOutputStream(tcpSock.getOutputStream());
<some code that reads and writes objects on oos>
out = new BuffereredWriter(new OutputStreamWriter(OOS));
while((i=in.read(buf))!=-1) {
out.write(buf, 0, i);
System.err.println("Client: wrote " + i + " chars");
}
out.flush();
oos.flush();
tcpSock.getOutputStream().flush();
server code:
byte[] buf = new byte[1024];
int i = 0;
long bytesRead = 0;
<verify that fileSize = number of bytes sent by client>
ois = new ObjectInputStream(tcpConnection.getInputStream());
while((i=ois.read(buf))!=-1 && bytesRead + i < fileSize ) {
fos.write(buf, 0, i);
bytesRead = bytesRead + i;
System.err.println("Server: bytes read = " + bytesRead);
}
fos.flush();
output shows that only 1024 bytes are read, and the output file only has 1024 characters.
I have a different method on the client that does the same exact thing except it transfers bytes out an OutputStream rather than chars out a BufferedWriter/OutputStreamWriter, and it does not have this problem. The last partial buffer is read by the server in this other method.