Use Arrays.copyOf

This commit is contained in:
Sam Harwell 2012-12-20 16:52:35 -06:00
parent 1894017eb7
commit b7e5cbd7f5
3 changed files with 7 additions and 9 deletions

View File

@ -35,6 +35,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Arrays;
/** Vacuum all input from a Reader/InputStream and then treat it like a char[] buffer.
* Can also pass in a string or char[] to use.
@ -117,9 +118,7 @@ public class ANTLRInputStream implements CharStream {
do {
if ( p+readChunkSize > data.length ) { // overflow?
// System.out.println("### overflow p="+p+", data.length="+data.length);
char[] newdata = new char[data.length*2]; // resize
System.arraycopy(data, 0, newdata, 0, data.length);
data = newdata;
data = Arrays.copyOf(data, data.length * 2);
}
numRead = r.read(data, p, readChunkSize);
// System.out.println("read "+numRead+" chars; p was "+p+" is now "+(p+numRead));

View File

@ -36,6 +36,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Arrays;
/** Do not buffer up the entire char stream. It does keep a small buffer
* for efficiency and also buffers while a mark exists (set by the
@ -194,9 +195,7 @@ public class UnbufferedCharStream implements CharStream {
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);
data = newdata;
data = Arrays.copyOf(data, data.length * 2);
}
data[n++] = (char)c;
}

View File

@ -33,6 +33,8 @@ package org.antlr.v4.runtime;
import org.antlr.v4.runtime.misc.Interval;
import org.antlr.v4.runtime.misc.NotNull;
import java.util.Arrays;
public class UnbufferedTokenStream<T extends Token> implements TokenStream {
protected TokenSource tokenSource;
@ -208,9 +210,7 @@ public class UnbufferedTokenStream<T extends Token> implements TokenStream {
protected void add(@NotNull Token t) {
if ( n>=tokens.length ) {
Token[] newtokens = new Token[tokens.length*2]; // resize
System.arraycopy(tokens, 0, newtokens, 0, tokens.length);
tokens = newtokens;
tokens = Arrays.copyOf(tokens, tokens.length * 2);
}
if (t instanceof WritableToken) {