|
|
@@ -44,6 +44,7 @@ public class CharStreamer<T> implements Closeable {
|
|
|
private static final char[] EMPTY = new char[0];
|
|
|
private final BiConsumer<T, char[]> consumer;
|
|
|
private char[] array;
|
|
|
+ private int length;
|
|
|
private int recent;
|
|
|
private int position;
|
|
|
|
|
|
@@ -55,6 +56,7 @@ public class CharStreamer<T> implements Closeable {
|
|
|
*/
|
|
|
public CharStreamer(BiConsumer<T, char[]> consumer) {
|
|
|
this.array = EMPTY;
|
|
|
+ this.length = 0;
|
|
|
this.recent = 0;
|
|
|
this.position = 0;
|
|
|
this.consumer = consumer;
|
|
|
@@ -83,14 +85,21 @@ public class CharStreamer<T> implements Closeable {
|
|
|
* @return this
|
|
|
*/
|
|
|
public CharStreamer<T> put(char[] input) {
|
|
|
- int pending = array.length - recent;
|
|
|
- char[] appended = new char[pending + input.length];
|
|
|
- // @todo #90
|
|
|
+ int pending = length - recent;
|
|
|
|
|
|
- System.arraycopy(array, recent, appended, 0, pending);
|
|
|
- System.arraycopy(input, 0, appended, pending, input.length);
|
|
|
+ length = pending + input.length;
|
|
|
+
|
|
|
+ char[] appended;
|
|
|
+ if(length <= array.length) {
|
|
|
+ appended = array;
|
|
|
+ } else {
|
|
|
+ appended = new char[length];
|
|
|
+ }
|
|
|
|
|
|
+ System.arraycopy(array, recent, appended, 0, pending);
|
|
|
array = appended;
|
|
|
+ System.arraycopy(input, 0, array, pending, input.length);
|
|
|
+
|
|
|
position = position - recent;
|
|
|
recent = 0;
|
|
|
|
|
|
@@ -163,7 +172,7 @@ public class CharStreamer<T> implements Closeable {
|
|
|
* @return boolean
|
|
|
*/
|
|
|
public boolean end() {
|
|
|
- return position >= array.length;
|
|
|
+ return position >= length;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -172,7 +181,7 @@ public class CharStreamer<T> implements Closeable {
|
|
|
* @return int
|
|
|
*/
|
|
|
public int available() {
|
|
|
- return Math.max(0, array.length - position);
|
|
|
+ return Math.max(0, length - position);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -206,7 +215,7 @@ public class CharStreamer<T> implements Closeable {
|
|
|
* @param right right
|
|
|
*/
|
|
|
public void send(T extra, int left, int right) {
|
|
|
- if(recent < position && position <= array.length) {
|
|
|
+ if(recent < position && position <= length) {
|
|
|
send(extra, Arrays.copyOfRange(array, recent+left, position-right) );
|
|
|
}
|
|
|
mark();
|
|
|
@@ -223,14 +232,14 @@ public class CharStreamer<T> implements Closeable {
|
|
|
}
|
|
|
|
|
|
private void check() {
|
|
|
- if(position >= array.length) {
|
|
|
+ if(position >= length) {
|
|
|
throw new IllegalStateException("Steamer: EOS");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void check(int offset) {
|
|
|
- if(position+offset > array.length) {
|
|
|
- position = array.length;
|
|
|
+ if(position+offset > length) {
|
|
|
+ position = length;
|
|
|
throw new IllegalStateException("EOS");
|
|
|
}
|
|
|
}
|