BufferedOutputStream

Steve Ratcliffe sterat at dial.pipex.com
Sat Sep 12 10:20:48 PDT 1998



The BufferedOutputStream implementation causes a mixture of large and
small writes of odd sizes to the underlying stream.  This results in up
to twice as many system calls as necessary depending on the circumstances.

The following fix causes writes to the underlying stream to occur only
in buffer sized chunks (unless flushed).  This gave me a small but
measurable increase in the writing speed.

..Steve

--- BufferedOutputStream.java.orig	Sat Sep 12 13:15:05 1998
+++ BufferedOutputStream.java	Sat Sep 12 18:15:52 1998
@@ -39,10 +39,15 @@
 }
 
 public synchronized void write(byte b[], int off, int len) throws IOException {
-	if (count + len > buf.length) {
+	while (count + len > buf.length) {
+		int left = buf.length - count;
+
+		System.arraycopy(b, off, buf, count, left);
+		count += left;
 		writeBuffer();
-		out.write(b, off, len);
-		return;
+
+		off += left;
+		len -= left;
 	}
 	System.arraycopy(b, off, buf, count, len);
 	count += len;


More information about the kaffe mailing list