From 6954c9c37707f35dc0e02b07626f0271696f702d Mon Sep 17 00:00:00 2001 From: Eric Scharff Date: Tue, 18 Dec 2007 12:19:52 -0700 Subject: [PATCH] Fixes for PushbackReader (which only can push back one char) and StringReader. --- classpath/java/io/PushbackReader.java | 47 ++++++++++++--------------- classpath/java/io/StringReader.java | 2 +- 2 files changed, 22 insertions(+), 27 deletions(-) diff --git a/classpath/java/io/PushbackReader.java b/classpath/java/io/PushbackReader.java index 1df36fbc80..9981bd1d86 100644 --- a/classpath/java/io/PushbackReader.java +++ b/classpath/java/io/PushbackReader.java @@ -2,13 +2,15 @@ package java.io; public class PushbackReader extends Reader { private final Reader in; - private final char[] buffer; - private int position; - private int limit; + private char savedChar; + private boolean hasSavedChar; public PushbackReader(Reader in, int bufferSize) { + if (bufferSize > 1) { + throw new IllegalArgumentException(bufferSize + " > 1"); + } this.in = in; - this.buffer = new char[bufferSize]; + this.hasSavedChar = false; } public PushbackReader(Reader in) { @@ -17,29 +19,20 @@ public class PushbackReader extends Reader { public int read(char[] b, int offset, int length) throws IOException { int count = 0; - - if (position < limit) { - int remaining = limit - position; - if (remaining > length) { - remaining = length; - } - - System.arraycopy(buffer, position, b, offset, remaining); - - count += remaining; - position += remaining; - offset += remaining; - length -= remaining; + if (hasSavedChar && length > 0) { + length--; + b[offset++] = savedChar; + hasSavedChar = false; + count = 1; } - if (length > 0) { int c = in.read(b, offset, length); if (c == -1) { - if (count == 0) { - count = -1; - } + if (count == 0) { + count = -1; + } } else { - count += c; + count += c; } } @@ -47,11 +40,13 @@ public class PushbackReader extends Reader { } public void unread(char[] b, int offset, int length) throws IOException { - if (position < length) { - throw new IOException(length + " not in [0," + position + "]"); + if (length != 1) { + throw new IOException("Can only push back 1 char, not " + length); + } else if (hasSavedChar) { + throw new IOException("Already have a saved char"); } else { - System.arraycopy(buffer, position - length, b, offset, length); - position -= length; + hasSavedChar = true; + savedChar = b[offset]; } } diff --git a/classpath/java/io/StringReader.java b/classpath/java/io/StringReader.java index d2b536af2c..9c4f1f8a05 100644 --- a/classpath/java/io/StringReader.java +++ b/classpath/java/io/StringReader.java @@ -15,7 +15,7 @@ public class StringReader extends Reader { return -1; } } - in.getChars(position, length, b, offset); + in.getChars(position, position+length, b, offset); position += length; return length; }