Commit b9850b3d by Chris Burdess Committed by Michael Koch

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
parent d3e53108
2005-04-21 Chris Burdess <dog@gnu.org>
* gnu/java/net/CRLFInputStream.java: Rewrite to return
CRLF-delimited chunks.
2005-04-20 Andrew John Hughes <gnu_andrew@member.fsf.org> 2005-04-20 Andrew John Hughes <gnu_andrew@member.fsf.org>
* java/net/URI.java: * java/net/URI.java:
......
...@@ -38,6 +38,7 @@ exception statement from your version. */ ...@@ -38,6 +38,7 @@ exception statement from your version. */
package gnu.java.net; package gnu.java.net;
import java.io.BufferedInputStream;
import java.io.FilterInputStream; import java.io.FilterInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
...@@ -60,15 +61,7 @@ public class CRLFInputStream ...@@ -60,15 +61,7 @@ public class CRLFInputStream
*/ */
public static final int LF = 10; public static final int LF = 10;
/** private boolean doReset;
* Buffer.
*/
protected int buf = -1;
/**
* Buffer at time of mark.
*/
protected int markBuf = -1;
/** /**
* Constructs a CR/LF input stream connected to the specified input * Constructs a CR/LF input stream connected to the specified input
...@@ -76,7 +69,7 @@ public class CRLFInputStream ...@@ -76,7 +69,7 @@ public class CRLFInputStream
*/ */
public CRLFInputStream(InputStream in) public CRLFInputStream(InputStream in)
{ {
super(in); super(in.markSupported() ? in : new BufferedInputStream(in));
} }
/** /**
...@@ -87,24 +80,18 @@ public class CRLFInputStream ...@@ -87,24 +80,18 @@ public class CRLFInputStream
public int read() public int read()
throws IOException throws IOException
{ {
int c; int c = in.read();
if (buf != -1)
{
c = buf;
buf = -1;
return c;
}
else
{
c = super.read();
if (c == CR) if (c == CR)
{ {
buf = super.read(); in.mark(1);
if (buf == LF) int d = in.read();
if (d == LF)
{ {
c = buf; c = d;
buf = -1;
} }
else
{
in.reset();
} }
} }
return c; return c;
...@@ -131,75 +118,57 @@ public class CRLFInputStream ...@@ -131,75 +118,57 @@ public class CRLFInputStream
public int read(byte[] b, int off, int len) public int read(byte[] b, int off, int len)
throws IOException throws IOException
{ {
int shift = 0; in.mark(len + 1);
if (buf != -1) int l = in.read(b, off, len);
if (l > 0)
{ {
// Push buf onto start of byte array int i = indexOfCRLF(b, off, l);
b[off] = (byte) buf; if (doReset)
off++;
len--;
buf = -1;
shift++;
}
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(); in.reset();
} if (i != -1)
/**
* Marks the current position in this stream.
*/
public void mark(int readlimit)
{ {
in.mark(readlimit); l = in.read(b, off, i + 1); // read to CR
markBuf = buf; in.read(); // skip LF
b[i] = LF; // fix CR as LF
} }
else
/**
* Repositions this stream to the position at the time the mark method was
* called.
*/
public void reset()
throws IOException
{ {
in.reset(); l = in.read(b, off, len); // CR(s) but no LF
buf = markBuf; }
}
}
return l;
} }
private int removeCRLF(byte[] b, int off, int len) private int indexOfCRLF(byte[] b, int off, int len)
throws IOException
{ {
int end = off + len; doReset = false;
for (int i = off; i < end; i++) int lm1 = len - 1;
for (int i = off; i < len; i++)
{ {
if (b[i] == CR) if (b[i] == CR)
{ {
if (i + 1 == end) int d;
if (i == lm1)
{ {
// This is the last byte, impossible to determine whether CRLF d = in.read();
buf = CR; doReset = true;
len--;
} }
else if (b[i + 1] == LF) else
{
// Shift left
end--;
for (int j = i; j < end; j++)
{ {
b[j] = b[j + 1]; d = b[i + 1];
} }
len--; if (d == LF)
end = off + len; {
doReset = true;
return i;
} }
} }
} }
return len; return -1;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment