Author: nextgens
Date: 2009-02-26 21:23:27 +0000 (Thu, 26 Feb 2009)
New Revision: 25844

Modified:
   trunk/freenet/src/freenet/support/io/LineReadingInputStream.java
Log:
maybe fix LineReadingInputStream:
1) get rid of the potential ArrayIndexOutOfBounds toad has spotted
2) optimize the trivial case where maxLength < 1
3) paranoia


Modified: trunk/freenet/src/freenet/support/io/LineReadingInputStream.java
===================================================================
--- trunk/freenet/src/freenet/support/io/LineReadingInputStream.java    
2009-02-26 19:43:41 UTC (rev 25843)
+++ trunk/freenet/src/freenet/support/io/LineReadingInputStream.java    
2009-02-26 21:23:27 UTC (rev 25844)
@@ -25,6 +25,8 @@
         * @param utf If true, read as UTF-8, if false, read as ISO-8859-1.
         */
        public String readLine(int maxLength, int bufferSize, boolean utf) 
throws IOException {
+               if(maxLength < 1)
+                       return null;
                if(maxLength <= bufferSize)
                        bufferSize = maxLength + 1; // Buffer too big, shrink 
it (add 1 for the optional \r)
 
@@ -35,7 +37,7 @@
                int ctr = 0;
                mark(maxLength + 2); // in case we have both a \r and a \n
                while(true) {
-                       int x = read(buf, ctr, Math.max(1, buf.length - ctr));
+                       int x = read(buf, ctr, buf.length - ctr);
                        if(x == -1) {
                                if(ctr == 0)
                                        return null;
@@ -57,7 +59,7 @@
                                if(ctr >= maxLength)
                                        throw new TooLongException("We reached 
maxLength="+maxLength+ " parsing\n "+HexUtil.bytesToHex(buf, 0, ctr) + "\n" + 
new String(buf, 0, ctr, utf ? "UTF-8" : "ISO-8859-1"));
                        }
-                       if((buf.length != maxLength) && (buf.length - ctr < 
bufferSize)) {
+                       if((buf.length < maxLength) && (buf.length - ctr < 
bufferSize)) {
                                byte[] newBuf = new byte[Math.min(buf.length * 
2, maxLength)];
                                System.arraycopy(buf, 0, newBuf, 0, ctr);
                                buf = newBuf;

_______________________________________________
cvs mailing list
[email protected]
http://emu.freenetproject.org/cgi-bin/mailman/listinfo/cvs

Reply via email to