01: /*
02: * Created on Nov 11, 2005
03: */
04: package uk.org.ponder.streamutil.read;
05:
06: import uk.org.ponder.stringutil.CharWrap;
07:
08: public class PushbackRIS implements ReadInputStream {
09: private CharWrap pushback = new CharWrap(8);
10: private ReadInputStream wrapped;
11:
12: public PushbackRIS(ReadInputStream wrapped) {
13: this .wrapped = wrapped;
14: }
15:
16: public char get() {
17: if (pushback.size > 0) {
18: return pushback.charAt(--pushback.size);
19: } else
20: return wrapped.get();
21: }
22:
23: public void unread(char c) {
24: pushback.append(c);
25: }
26:
27: public int read(char[] target, int start, int length) {
28: int read = 0;
29: while (pushback.size > 0 && read < length) {
30: target[start + read] = get(); // may as well do these slowly, there will not be very many
31: ++read;
32: }
33: read += wrapped.read(target, start + read, length - read);
34: return read;
35: }
36:
37: public boolean EOF() {
38: return pushback.size == 0 && wrapped.EOF();
39: }
40:
41: public void close() {
42: wrapped.close();
43: }
44:
45: }
|