classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] RFA: Fix LineInputStream (PR classpath/24259)


From: David Daney
Subject: [cp-patches] RFA: Fix LineInputStream (PR classpath/24259)
Date: Fri, 7 Oct 2005 12:38:16 -0700

This patch corrects the run-away buffering condition documented in the
PR by limiting the maximum buffer size.

In addition buffering is disabled if the underlying stream is a
BufferedInputStream.  Because in this case the redundant buffering
causes more memory and CPU resources to be used to allocate and move
data through all the buffers.

Tested with make -k check in GCJ libjava(HEAD) + mauve + jacks with no
regressions.


2005-10-07  David Daney  <address@hidden>

        PR classpath/24259
        * gnu/java/net/LineInputStream.java: Import BufferedInputStream.
        (Constructor): Don't do blockReads on BufferedInputStreams.
        (readLine): Rename MIN_LENGTH to MAX_LENGTH.  Limit buffer size to
        MAX_LENGTH.


OK to commit?

David Daney



Index: gnu/java/net/LineInputStream.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/java/net/LineInputStream.java,v
retrieving revision 1.4
diff -u -p -r1.4 LineInputStream.java
--- gnu/java/net/LineInputStream.java   2 Jul 2005 20:32:13 -0000       1.4
+++ gnu/java/net/LineInputStream.java   7 Oct 2005 18:23:21 -0000
@@ -1,5 +1,5 @@
 /* LineInputStream.java --
-   Copyright (C) 2002, 2003, 2004  Free Software Foundation, Inc.
+   Copyright (C) 2002, 2003, 2004, 2005  Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -38,6 +38,7 @@ exception statement from your version. *
 
 package gnu.java.net;
 
+import java.io.BufferedInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.FilterInputStream;
 import java.io.IOException;
@@ -91,7 +92,8 @@ public class LineInputStream
     buf = new ByteArrayOutputStream();
     this.encoding = encoding;
     eof = false;
-    blockReads = in.markSupported();
+    // If it is already buffered, additional buffering gains nothing.
+    blockReads = !(in instanceof BufferedInputStream) && in.markSupported();
   }
 
   /**
@@ -109,11 +111,12 @@ public class LineInputStream
         if (blockReads)
           {
             // Use mark and reset to read chunks of bytes
-            final int MIN_LENGTH = 1024;
+            final int MAX_LENGTH = 1024;
             int len, pos;
-            
+
             len = in.available();
-            len = (len < MIN_LENGTH) ? MIN_LENGTH : len;
+            if (len == 0 || len > MAX_LENGTH)
+              len = MAX_LENGTH;
             byte[] b = new byte[len];
             in.mark(len);
             // Read into buffer b





reply via email to

[Prev in Thread] Current Thread [Next in Thread]