aboutsummaryrefslogtreecommitdiff
path: root/libjava/gnu/java
diff options
context:
space:
mode:
authorChris Burdess <dog@gnu.org>2005-04-21 06:17:03 +0000
committerMichael Koch <mkoch@gcc.gnu.org>2005-04-21 06:17:03 +0000
commitb9850b3d44898c346b98e702eef0363b47fd0cbb (patch)
treef01d50243627d6d8e181d80f81e535c2734a4793 /libjava/gnu/java
parentd3e53108dde9a1c1826fbd227339bbc51a84909f (diff)
downloadgcc-b9850b3d44898c346b98e702eef0363b47fd0cbb.zip
gcc-b9850b3d44898c346b98e702eef0363b47fd0cbb.tar.gz
gcc-b9850b3d44898c346b98e702eef0363b47fd0cbb.tar.bz2
CRLFInputStream.java: Rewrite to return CRLF-delimited chunks.
2005-04-21 Chris Burdess <dog@gnu.org> * gnu/java/net/CRLFInputStream.java: Rewrite to return CRLF-delimited chunks. From-SVN: r98493
Diffstat (limited to 'libjava/gnu/java')
-rw-r--r--libjava/gnu/java/net/CRLFInputStream.java129
1 files changed, 49 insertions, 80 deletions
diff --git a/libjava/gnu/java/net/CRLFInputStream.java b/libjava/gnu/java/net/CRLFInputStream.java
index 9bfb50d..927201c 100644
--- a/libjava/gnu/java/net/CRLFInputStream.java
+++ b/libjava/gnu/java/net/CRLFInputStream.java
@@ -38,6 +38,7 @@ exception statement from your version. */
package gnu.java.net;
+import java.io.BufferedInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -60,15 +61,7 @@ public class CRLFInputStream
*/
public static final int LF = 10;
- /**
- * Buffer.
- */
- protected int buf = -1;
-
- /**
- * Buffer at time of mark.
- */
- protected int markBuf = -1;
+ private boolean doReset;
/**
* Constructs a CR/LF input stream connected to the specified input
@@ -76,7 +69,7 @@ public class CRLFInputStream
*/
public CRLFInputStream(InputStream in)
{
- super(in);
+ super(in.markSupported() ? in : new BufferedInputStream(in));
}
/**
@@ -87,24 +80,18 @@ public class CRLFInputStream
public int read()
throws IOException
{
- int c;
- if (buf != -1)
+ int c = in.read();
+ if (c == CR)
{
- c = buf;
- buf = -1;
- return c;
- }
- else
- {
- c = super.read();
- if (c == CR)
+ in.mark(1);
+ int d = in.read();
+ if (d == LF)
{
- buf = super.read();
- if (buf == LF)
- {
- c = buf;
- buf = -1;
- }
+ c = d;
+ }
+ else
+ {
+ in.reset();
}
}
return c;
@@ -131,75 +118,57 @@ public class CRLFInputStream
public int read(byte[] b, int off, int len)
throws IOException
{
- int shift = 0;
- if (buf != -1)
+ in.mark(len + 1);
+ int l = in.read(b, off, len);
+ if (l > 0)
{
- // Push buf onto start of byte array
- b[off] = (byte) buf;
- off++;
- len--;
- buf = -1;
- shift++;
+ int i = indexOfCRLF(b, off, l);
+ if (doReset)
+ {
+ in.reset();
+ if (i != -1)
+ {
+ l = in.read(b, off, i + 1); // read to CR
+ in.read(); // skip LF
+ b[i] = LF; // fix CR as LF
+ }
+ else
+ {
+ l = in.read(b, off, len); // CR(s) but no LF
+ }
+ }
}
- int l = super.read(b, off, len);
- l = removeCRLF(b, off - shift, l);
return l;
}
- /**
- * Indicates whether this stream supports the mark and reset methods.
- */
- public boolean markSupported()
- {
- return in.markSupported();
- }
-
- /**
- * Marks the current position in this stream.
- */
- public void mark(int readlimit)
- {
- in.mark(readlimit);
- markBuf = buf;
- }
-
- /**
- * Repositions this stream to the position at the time the mark method was
- * called.
- */
- public void reset()
+ private int indexOfCRLF(byte[] b, int off, int len)
throws IOException
{
- in.reset();
- buf = markBuf;
- }
-
- private int removeCRLF(byte[] b, int off, int len)
- {
- int end = off + len;
- for (int i = off; i < end; i++)
+ doReset = false;
+ int lm1 = len - 1;
+ for (int i = off; i < len; i++)
{
if (b[i] == CR)
{
- if (i + 1 == end)
+ int d;
+ if (i == lm1)
{
- // This is the last byte, impossible to determine whether CRLF
- buf = CR;
- len--;
+ d = in.read();
+ doReset = true;
}
- else if (b[i + 1] == LF)
+ else
{
- // Shift left
- end--;
- for (int j = i; j < end; j++)
- {
- b[j] = b[j + 1];
- }
- len--;
- end = off + len;
+ d = b[i + 1];
+ }
+ if (d == LF)
+ {
+ doReset = true;
+ return i;
}
}
}
- return len;
+ return -1;
}
+
}
+