Commit ae30b3b2 by Mohan Embar Committed by Mohan Embar

BufferedReader.java (sbuf): New field.

	* java/io/BufferedReader.java (sbuf): New field.
	(readLine): Use String.valueOf instead of new String() as per
	Per Bothner's suggestion. Use instance sbuf field instead of a
	local StringBuffer instance.
	* java/io/InputStreamReader.java (read(char[],int,int)): Pass the
	caller's buffer to refill().
	(read(void)): Pass our internal work buffer to refill if our
	input queue is empty.
	(refill): Changed return type to int. Use the specified buffer
	instead of our work buffer as per Bryce McKinlay's suggestion.
	Return the number of characters read or -1 for EOF.

From-SVN: r76927
parent eadccbea
2004-01-29 Mohan Embar <gnustuff@thisiscool.com>
* java/io/BufferedReader.java (sbuf): New field.
(readLine): Use String.valueOf instead of new String() as per
Per Bothner's suggestion. Use instance sbuf field instead of a
local StringBuffer instance.
* java/io/InputStreamReader.java (read(char[],int,int)): Pass the
caller's buffer to refill().
(read(void)): Pass our internal work buffer to refill if our
input queue is empty.
(refill): Changed return type to int. Use the specified buffer
instead of our work buffer as per Bryce McKinlay's suggestion.
Return the number of characters read or -1 for EOF.
2004-01-29 Scott Gilbertson <scottg@mantatest.com> 2004-01-29 Scott Gilbertson <scottg@mantatest.com>
* gnu/awt/xlib/XCanvasPeer.java (handleEvent): Implemented. * gnu/awt/xlib/XCanvasPeer.java (handleEvent): Implemented.
......
/* BufferedReader.java /* BufferedReader.java
Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004
Free Software Foundation, Inc. Free Software Foundation, Inc.
This file is part of GNU Classpath. This file is part of GNU Classpath.
...@@ -89,6 +89,11 @@ public class BufferedReader extends Reader ...@@ -89,6 +89,11 @@ public class BufferedReader extends Reader
static final int DEFAULT_BUFFER_SIZE = 8192; static final int DEFAULT_BUFFER_SIZE = 8192;
/** /**
* The line buffer for <code>readLine</code>.
*/
private StringBuffer sbuf = null;
/**
* Create a new <code>BufferedReader</code> that will read from the * Create a new <code>BufferedReader</code> that will read from the
* specified subordinate stream with a default buffer size of 8192 chars. * specified subordinate stream with a default buffer size of 8192 chars.
* *
...@@ -439,7 +444,7 @@ public class BufferedReader extends Reader ...@@ -439,7 +444,7 @@ public class BufferedReader extends Reader
int i = lineEnd(limit); int i = lineEnd(limit);
if (i < limit) if (i < limit)
{ {
String str = new String(buffer, pos, i - pos); String str = String.valueOf(buffer, pos, i - pos);
pos = i + 1; pos = i + 1;
// If the last char in the buffer is a '\r', we must remember // If the last char in the buffer is a '\r', we must remember
// to check if the next char to be read after the buffer is refilled // to check if the next char to be read after the buffer is refilled
...@@ -450,7 +455,10 @@ public class BufferedReader extends Reader ...@@ -450,7 +455,10 @@ public class BufferedReader extends Reader
pos++; pos++;
return str; return str;
} }
StringBuffer sbuf = new StringBuffer(200); if (sbuf == null)
sbuf = new StringBuffer(200);
else
sbuf.setLength(0);
sbuf.append(buffer, pos, i - pos); sbuf.append(buffer, pos, i - pos);
pos = i; pos = i;
// We only want to return null when no characters were read before // We only want to return null when no characters were read before
......
/* InputStreamReader.java -- Reader than transforms bytes to chars /* InputStreamReader.java -- Reader than transforms bytes to chars
Copyright (C) 1998, 1999, 2001, 2003 Free Software Foundation, Inc. Copyright (C) 1998, 1999, 2001, 2003, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath. This file is part of GNU Classpath.
...@@ -231,10 +231,8 @@ public class InputStreamReader extends Reader ...@@ -231,10 +231,8 @@ public class InputStreamReader extends Reader
int wavail = wcount - wpos; int wavail = wcount - wpos;
if (wavail <= 0) if (wavail <= 0)
{ {
// Nothing waiting, so refill our buffer. // Nothing waiting, so refill their buffer.
if (! refill ()) return refill(buf, offset, length);
return -1;
wavail = wcount - wpos;
} }
if (length > wavail) if (length > wavail)
...@@ -262,24 +260,24 @@ public class InputStreamReader extends Reader ...@@ -262,24 +260,24 @@ public class InputStreamReader extends Reader
int wavail = wcount - wpos; int wavail = wcount - wpos;
if (wavail <= 0) if (wavail <= 0)
{ {
// Nothing waiting, so refill our buffer. // Nothing waiting, so refill our internal buffer.
if (! refill ()) wpos = wcount = 0;
if (work == null)
work = new char[100];
int count = refill(work, 0, work.length);
if (count == -1)
return -1; return -1;
wcount += count;
} }
return work[wpos++]; return work[wpos++];
} }
} }
// Read more bytes and convert them into the WORK buffer. // Read more bytes and convert them into the specified buffer.
// Return false on EOF. // Returns the number of converted characters or -1 on EOF.
private boolean refill () throws IOException private int refill(char[] buf, int offset, int length) throws IOException
{ {
wcount = wpos = 0;
if (work == null)
work = new char[100];
for (;;) for (;;)
{ {
// We have knowledge of the internals of BufferedInputStream // We have knowledge of the internals of BufferedInputStream
...@@ -290,17 +288,13 @@ public class InputStreamReader extends Reader ...@@ -290,17 +288,13 @@ public class InputStreamReader extends Reader
boolean r = in.pos < in.count || in.refill (); boolean r = in.pos < in.count || in.refill ();
in.reset (); in.reset ();
if (! r) if (! r)
return false; return -1;
converter.setInput(in.buf, in.pos, in.count); converter.setInput(in.buf, in.pos, in.count);
int count = converter.read (work, wpos, work.length - wpos); int count = converter.read(buf, offset, length);
in.skip(converter.inpos - in.pos); in.skip(converter.inpos - in.pos);
if (count > 0) if (count > 0)
{ return count;
wcount += count;
return true;
}
} }
} }
} // class InputStreamReader } // class InputStreamReader
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