Author: dbkr
Date: 2007-11-21 16:32:22 +0000 (Wed, 21 Nov 2007)
New Revision: 15907

Modified:
   trunk/apps/Freemail/src/freemail/RTSFetcher.java
   trunk/apps/Freemail/src/freemail/fcp/FCPMessage.java
   trunk/apps/Freemail/src/freenet/support/io/LineReader.java
   trunk/apps/Freemail/src/freenet/support/io/LineReadingInputStream.java
Log:
Pull in r9105 from freenet on the support/io code and update Freemail to be 
compatible.


Modified: trunk/apps/Freemail/src/freemail/RTSFetcher.java
===================================================================
--- trunk/apps/Freemail/src/freemail/RTSFetcher.java    2007-11-21 15:53:52 UTC 
(rev 15906)
+++ trunk/apps/Freemail/src/freemail/RTSFetcher.java    2007-11-21 16:32:22 UTC 
(rev 15907)
@@ -216,7 +216,7 @@
                        String line;
                        while (true) {
                                try {
-                                       line = lis.readLine(200, 200);
+                                       line = lis.readLine(200, 200, false);
                                } catch (TooLongException tle) {
                                        System.out.println("RTS message has 
lines that are too long. Discarding.");
                                        rtsfile.delete();

Modified: trunk/apps/Freemail/src/freemail/fcp/FCPMessage.java
===================================================================
--- trunk/apps/Freemail/src/freemail/fcp/FCPMessage.java        2007-11-21 
15:53:52 UTC (rev 15906)
+++ trunk/apps/Freemail/src/freemail/fcp/FCPMessage.java        2007-11-21 
16:32:22 UTC (rev 15907)
@@ -59,7 +59,7 @@
                LineReader r = new LineReadingInputStream(is);

                String line;
-               while ( (line = r.readLine(200, 200)) != null) {
+               while ( (line = r.readLine(200, 200, false)) != null) {
                        /***************************************/
                        //System.out.println(line);
                        if (this.messagetype == null) {

Modified: trunk/apps/Freemail/src/freenet/support/io/LineReader.java
===================================================================
--- trunk/apps/Freemail/src/freenet/support/io/LineReader.java  2007-11-21 
15:53:52 UTC (rev 15906)
+++ trunk/apps/Freemail/src/freenet/support/io/LineReader.java  2007-11-21 
16:32:22 UTC (rev 15907)
@@ -4,6 +4,9 @@

 public interface LineReader {

-       public String readLine(int maxLength, int bufferSize) throws 
IOException;
+       /**
+        * Read a \n or \r\n terminated line of UTF-8 or ISO-8859-1.
+        */
+       public String readLine(int maxLength, int bufferSize, boolean utf) 
throws IOException;

 }

Modified: trunk/apps/Freemail/src/freenet/support/io/LineReadingInputStream.java
===================================================================
--- trunk/apps/Freemail/src/freenet/support/io/LineReadingInputStream.java      
2007-11-21 15:53:52 UTC (rev 15906)
+++ trunk/apps/Freemail/src/freenet/support/io/LineReadingInputStream.java      
2007-11-21 16:32:22 UTC (rev 15907)
@@ -14,30 +14,36 @@
                super(in);
        }

+       private byte[] buf;
+
        /**
-        * Read a line of US-ASCII. Used for e.g. HTTP. @return Null if end of 
file.
+        * Read a \n or \r\n terminated line of UTF-8 or ISO-8859-1.
         */
-       public String readLine(int maxLength, int bufferSize) throws 
IOException {
-               StringBuffer sb = new StringBuffer(bufferSize);
+       public String readLine(int maxLength, int bufferSize, boolean utf) 
throws IOException {
+               if(buf == null)
+                       buf = new byte[Math.max(128, Math.min(1024, 
bufferSize))];
+               int ctr = 0;
                this.lastBytesRead = 0;
                while(true) {
                        int x = read();
                        this.lastBytesRead++;
                        if(x == -1) {
-                               if(sb.length() == 0) return null;
-                               return sb.toString();
+                               if(ctr == 0) return null;
+                               return new String(buf, 0, ctr, utf ? "UTF-8" : 
"ISO-8859-1");
                        }
-                       char c = (char) x;
-                       if(c == '\n') {
-                               if(sb.length() > 0) {
-                                       if(sb.charAt(sb.length()-1) == '\r')
-                                               sb.setLength(sb.length()-1);
-                               }
-                               return sb.toString();
+                       // REDFLAG this is definitely safe with the above 
charsets, it may not be safe with some wierd ones. 
+                       if(x == (int)'\n') {
+                               if(ctr == 0) return "";
+                               if(buf[ctr-1] == '\r') ctr--;
+                               return new String(buf, 0, ctr, utf ? "UTF-8" : 
"ISO-8859-1");
                        }
-                       sb.append(c);
-                       if(sb.length() >= maxLength)
-                               throw new TooLongException();
+                       if(ctr >= buf.length) {
+                               if(buf.length == bufferSize) throw new 
TooLongException();
+                               byte[] newBuf = new byte[Math.min(buf.length * 
2, bufferSize)];
+                               System.arraycopy(buf, 0, newBuf, 0, buf.length);
+                               buf = newBuf;
+                       }
+                       buf[ctr++] = (byte)x;
                }
        }



Reply via email to