From ae7313f36a79d4a13742554e5a4851047556d68d Mon Sep 17 00:00:00 2001 From: Terence Parr Date: Mon, 2 Jul 2012 12:19:20 -0700 Subject: [PATCH] factor out input.read() and make ctor for easy subclass. --- .../v4/runtime/UnbufferedCharStream.java | 48 ++++++++++++------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/UnbufferedCharStream.java b/runtime/Java/src/org/antlr/v4/runtime/UnbufferedCharStream.java index 571b75b53..85fb5810c 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/UnbufferedCharStream.java +++ b/runtime/Java/src/org/antlr/v4/runtime/UnbufferedCharStream.java @@ -73,25 +73,36 @@ public class UnbufferedCharStream implements CharStream { /** What is name or source of this char stream? */ public String name; - public UnbufferedCharStream(InputStream input) { - this(input, 256); - } + /** Useful for sublasses that pull char from other than this.input. */ + public UnbufferedCharStream() { + this(256); + } - public UnbufferedCharStream(Reader input) { - this(input, 256); - } + /** Useful for sublasses that pull char from other than this.input. */ + public UnbufferedCharStream(int bufferSize) { + n = 0; + data = new char[bufferSize]; + } - public UnbufferedCharStream(InputStream input, int bufferSize) { - this.input = new InputStreamReader(input); - data = new char[bufferSize]; + public UnbufferedCharStream(InputStream input) { + this(input, 256); + } + + public UnbufferedCharStream(Reader input) { + this(input, 256); + } + + public UnbufferedCharStream(InputStream input, int bufferSize) { + this(bufferSize); + this.input = new InputStreamReader(input); fill(1); // prime - } + } - public UnbufferedCharStream(Reader input, int bufferSize) { - this.input = input; - data = new char[bufferSize]; + public UnbufferedCharStream(Reader input, int bufferSize) { + this(bufferSize); + this.input = input; fill(1); // prime - } + } @Override public void consume() { @@ -121,7 +132,7 @@ public class UnbufferedCharStream implements CharStream { public void fill(int n) { for (int i=1; i<=n; i++) { try { - int c = input.read(); + int c = nextChar(); add(c); } catch (IOException ioe) { @@ -130,7 +141,12 @@ public class UnbufferedCharStream implements CharStream { } } - protected void add(int c) { + /** Override to provide different source of characters than this.input */ + protected int nextChar() throws IOException { + return input.read(); + } + + protected void add(int c) { if ( n>=data.length ) { char[] newdata = new char[data.length*2]; // resize System.arraycopy(data, 0, newdata, 0, data.length);