1 7 8 package java.io; 9 10 11 19 20 public class PushbackReader extends FilterReader { 21 22 23 private char[] buf; 24 25 26 private int pos; 27 28 35 public PushbackReader(Reader in, int size) { 36 super(in); 37 if (size <= 0) { 38 throw new IllegalArgumentException ("size <= 0"); 39 } 40 this.buf = new char[size]; 41 this.pos = size; 42 } 43 44 49 public PushbackReader(Reader in) { 50 this(in, 1); 51 } 52 53 54 private void ensureOpen() throws IOException { 55 if (buf == null) 56 throw new IOException ("Stream closed"); 57 } 58 59 67 public int read() throws IOException { 68 synchronized (lock) { 69 ensureOpen(); 70 if (pos < buf.length) 71 return buf[pos++]; 72 else 73 return super.read(); 74 } 75 } 76 77 89 public int read(char cbuf[], int off, int len) throws IOException { 90 synchronized (lock) { 91 ensureOpen(); 92 try { 93 if (len <= 0) { 94 if (len < 0) { 95 throw new IndexOutOfBoundsException (); 96 } else if ((off < 0) || (off > cbuf.length)) { 97 throw new IndexOutOfBoundsException (); 98 } 99 return 0; 100 } 101 int avail = buf.length - pos; 102 if (avail > 0) { 103 if (len < avail) 104 avail = len; 105 System.arraycopy(buf, pos, cbuf, off, avail); 106 pos += avail; 107 off += avail; 108 len -= avail; 109 } 110 if (len > 0) { 111 len = super.read(cbuf, off, len); 112 if (len == -1) { 113 return (avail == 0) ? -1 : avail; 114 } 115 return avail + len; 116 } 117 return avail; 118 } catch (ArrayIndexOutOfBoundsException e) { 119 throw new IndexOutOfBoundsException (); 120 } 121 } 122 } 123 124 132 public void unread(int c) throws IOException { 133 synchronized (lock) { 134 ensureOpen(); 135 if (pos == 0) 136 throw new IOException ("Pushback buffer overflow"); 137 buf[--pos] = (char) c; 138 } 139 } 140 141 155 public void unread(char cbuf[], int off, int len) throws IOException { 156 synchronized (lock) { 157 ensureOpen(); 158 if (len > pos) 159 throw new IOException ("Pushback buffer overflow"); 160 pos -= len; 161 System.arraycopy(cbuf, off, buf, pos, len); 162 } 163 } 164 165 176 public void unread(char cbuf[]) throws IOException { 177 unread(cbuf, 0, cbuf.length); 178 } 179 180 185 public boolean ready() throws IOException { 186 synchronized (lock) { 187 ensureOpen(); 188 return (pos < buf.length) || super.ready(); 189 } 190 } 191 192 198 public void mark(int readAheadLimit) throws IOException { 199 throw new IOException ("mark/reset not supported"); 200 } 201 202 208 public void reset() throws IOException { 209 throw new IOException ("mark/reset not supported"); 210 } 211 212 216 public boolean markSupported() { 217 return false; 218 } 219 220 225 public void close() throws IOException { 226 super.close(); 227 buf = null; 228 } 229 230 241 public long skip(long n) throws IOException { 242 if (n < 0L) 243 throw new IllegalArgumentException ("skip value is negative"); 244 synchronized (lock) { 245 ensureOpen(); 246 int avail = buf.length - pos; 247 if (avail > 0) { 248 if (n <= avail) { 249 pos += n; 250 return n; 251 } else { 252 pos = buf.length; 253 n -= avail; 254 } 255 } 256 return avail + super.skip(n); 257 } 258 } 259 260 } 261 | Popular Tags |