1 7 8 package java.io; 9 10 11 33 34 public abstract class Reader implements Readable , Closeable { 35 36 43 protected Object lock; 44 45 49 protected Reader() { 50 this.lock = this; 51 } 52 53 59 protected Reader(Object lock) { 60 if (lock == null) { 61 throw new NullPointerException (); 62 } 63 this.lock = lock; 64 } 65 66 79 public int read(java.nio.CharBuffer target) throws IOException { 80 int len = target.remaining(); 81 char[] cbuf = new char[len]; 82 int n = read(cbuf, 0, len); 83 if (n > 0) 84 target.put(cbuf, 0, n); 85 return n; 86 } 87 88 101 public int read() throws IOException { 102 char cb[] = new char[1]; 103 if (read(cb, 0, 1) == -1) 104 return -1; 105 else 106 return cb[0]; 107 } 108 109 121 public int read(char cbuf[]) throws IOException { 122 return read(cbuf, 0, cbuf.length); 123 } 124 125 139 abstract public int read(char cbuf[], int off, int len) throws IOException ; 140 141 142 private static final int maxSkipBufferSize = 8192; 143 144 145 private char skipBuffer[] = null; 146 147 158 public long skip(long n) throws IOException { 159 if (n < 0L) 160 throw new IllegalArgumentException ("skip value is negative"); 161 int nn = (int) Math.min(n, maxSkipBufferSize); 162 synchronized (lock) { 163 if ((skipBuffer == null) || (skipBuffer.length < nn)) 164 skipBuffer = new char[nn]; 165 long r = n; 166 while (r > 0) { 167 int nc = read(skipBuffer, 0, (int)Math.min(r, nn)); 168 if (nc == -1) 169 break; 170 r -= nc; 171 } 172 return n - r; 173 } 174 } 175 176 185 public boolean ready() throws IOException { 186 return false; 187 } 188 189 196 public boolean markSupported() { 197 return false; 198 } 199 200 213 public void mark(int readAheadLimit) throws IOException { 214 throw new IOException ("mark() not supported"); 215 } 216 217 230 public void reset() throws IOException { 231 throw new IOException ("reset() not supported"); 232 } 233 234 241 abstract public void close() throws IOException ; 242 243 } 244 | Popular Tags |