1 7 8 package java.io; 9 10 26 public 27 class ByteArrayInputStream extends InputStream { 28 29 37 protected byte buf[]; 38 39 46 protected int pos; 47 48 61 protected int mark = 0; 62 63 72 protected int count; 73 74 86 public ByteArrayInputStream(byte buf[]) { 87 this.buf = buf; 88 this.pos = 0; 89 this.count = buf.length; 90 } 91 92 106 public ByteArrayInputStream(byte buf[], int offset, int length) { 107 this.buf = buf; 108 this.pos = offset; 109 this.count = Math.min(offset + length, buf.length); 110 this.mark = offset; 111 } 112 113 126 public synchronized int read() { 127 return (pos < count) ? (buf[pos++] & 0xff) : -1; 128 } 129 130 155 public synchronized int read(byte b[], int off, int len) { 156 if (b == null) { 157 throw new NullPointerException (); 158 } else if ((off < 0) || (off > b.length) || (len < 0) || 159 ((off + len) > b.length) || ((off + len) < 0)) { 160 throw new IndexOutOfBoundsException (); 161 } 162 if (pos >= count) { 163 return -1; 164 } 165 if (pos + len > count) { 166 len = count - pos; 167 } 168 if (len <= 0) { 169 return 0; 170 } 171 System.arraycopy(buf, pos, b, off, len); 172 pos += len; 173 return len; 174 } 175 176 188 public synchronized long skip(long n) { 189 if (pos + n > count) { 190 n = count - pos; 191 } 192 if (n < 0) { 193 return 0; 194 } 195 pos += n; 196 return n; 197 } 198 199 209 public synchronized int available() { 210 return count - pos; 211 } 212 213 220 public boolean markSupported() { 221 return true; 222 } 223 224 239 public void mark(int readAheadLimit) { 240 mark = pos; 241 } 242 243 248 public synchronized void reset() { 249 pos = mark; 250 } 251 252 258 public void close() throws IOException { 259 } 260 261 } 262 | Popular Tags |