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
This commit is contained in:
Chris Burdess 2005-04-21 06:17:03 +00:00 committed by Michael Koch
parent d3e53108dd
commit b9850b3d44
2 changed files with 62 additions and 88 deletions

View file

@ -1,3 +1,8 @@
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:

View file

@ -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
*/ */
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
*/ */
public CRLFInputStream(InputStream in) public CRLFInputStream(InputStream in)
{ {
super(in); super(in.markSupported() ? in : new BufferedInputStream(in));
} }
/** /**
@ -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) if (c == CR)
{ {
c = buf; in.mark(1);
buf = -1; int d = in.read();
return c; if (d == LF)
{
c = d;
} }
else else
{ {
c = super.read(); in.reset();
if (c == CR)
{
buf = super.read();
if (buf == LF)
{
c = buf;
buf = -1;
}
} }
} }
return c; return c;
@ -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--; in.reset();
buf = -1; if (i != -1)
shift++; {
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; return l;
} }
/** private int indexOfCRLF(byte[] b, int off, int len)
* 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()
throws IOException throws IOException
{ {
in.reset(); doReset = false;
buf = markBuf; int lm1 = len - 1;
} for (int i = off; i < len; i++)
private int removeCRLF(byte[] b, int off, int len)
{
int end = off + len;
for (int i = off; i < end; 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 d = b[i + 1];
end--; }
for (int j = i; j < end; j++) if (d == LF)
{ {
b[j] = b[j + 1]; doReset = true;
} return i;
len--;
end = off + len;
} }
} }
} }
return len; return -1;
} }
} }