Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

OutputStreamWriter doesn't seem to obey flush()

843790Feb 9 2007 — edited Feb 11 2007
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.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 11 2007
Added on Feb 9 2007
10 comments
758 views