1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package java.io; 19 20import org.apache.harmony.luni.util.Msg; 21 22// BEGIN android-added 23import java.util.logging.Logger; 24// END android-added 25 26/** 27 * Wraps an existing {@link InputStream} and <em>buffers</em> the input. 28 * Expensive interaction with the underlying input stream is minimized, since 29 * most (smaller) requests can be satisfied by accessing the buffer alone. The 30 * drawback is that some extra space is required to hold the buffer and that 31 * copying takes place when filling that buffer, but this is usually outweighed 32 * by the performance benefits. 33 * 34 * <p/>A typical application pattern for the class looks like this:<p/> 35 * 36 * <pre> 37 * BufferedInputStream buf = new BufferedInputStream(new FileInputStream("file.java")); 38 * </pre> 39 * 40 * @see BufferedOutputStream 41 */ 42public class BufferedInputStream extends FilterInputStream { 43 /** 44 * The buffer containing the current bytes read from the target InputStream. 45 */ 46 protected volatile byte[] buf; 47 48 /** 49 * The total number of bytes inside the byte array {@code buf}. 50 */ 51 protected int count; 52 53 /** 54 * The current limit, which when passed, invalidates the current mark. 55 */ 56 protected int marklimit; 57 58 /** 59 * The currently marked position. -1 indicates no mark has been set or the 60 * mark has been invalidated. 61 */ 62 protected int markpos = -1; 63 64 /** 65 * The current position within the byte array {@code buf}. 66 */ 67 protected int pos; 68 69 /** 70 * Constructs a new {@code BufferedInputStream} on the {@link InputStream} 71 * {@code in}. The default buffer size (8 KB) is allocated and all reads 72 * can now be filtered through this stream. 73 * 74 * @param in 75 * the InputStream the buffer reads from. 76 */ 77 public BufferedInputStream(InputStream in) { 78 super(in); 79 buf = new byte[8192]; 80 81 // BEGIN android-added 82 /* 83 * For Android, we want to discourage the use of this constructor (with 84 * its arguably too-large default), so we note its use in the log. We 85 * don't disable it, nor do we alter the default, however, because we 86 * still aim to behave compatibly, and the default value, though not 87 * documented, is established by convention. 88 */ 89 Logger.global.info( 90 "Default buffer size used in BufferedInputStream " + 91 "constructor. It would be " + 92 "better to be explicit if an 8k buffer is required."); 93 // END android-added 94 } 95 96 /** 97 * Constructs a new {@code BufferedInputStream} on the {@link InputStream} 98 * {@code in}. The buffer size is specified by the parameter {@code size} 99 * and all reads are now filtered through this stream. 100 * 101 * @param in 102 * the input stream the buffer reads from. 103 * @param size 104 * the size of buffer to allocate. 105 * @throws IllegalArgumentException 106 * if {@code size < 0}. 107 */ 108 public BufferedInputStream(InputStream in, int size) { 109 super(in); 110 if (size <= 0) { 111 // K0058=size must be > 0 112 throw new IllegalArgumentException(Msg.getString("K0058")); //$NON-NLS-1$ 113 } 114 buf = new byte[size]; 115 } 116 117 /** 118 * Returns an estimated number of bytes that can be read or skipped without blocking for more 119 * input. This method returns the number of bytes available in the buffer 120 * plus those available in the source stream, but see {@link InputStream#available} for 121 * important caveats. 122 * 123 * @return the estimated number of bytes available 124 * @throws IOException if this stream is closed or an error occurs 125 */ 126 @Override 127 public synchronized int available() throws IOException { 128 InputStream localIn = in; // 'in' could be invalidated by close() 129 if (buf == null || localIn == null) { 130 // K0059=Stream is closed 131 throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$ 132 } 133 return count - pos + localIn.available(); 134 } 135 136 /** 137 * Closes this stream. The source stream is closed and any resources 138 * associated with it are released. 139 * 140 * @throws IOException 141 * if an error occurs while closing this stream. 142 */ 143 @Override 144 public void close() throws IOException { 145 buf = null; 146 InputStream localIn = in; 147 in = null; 148 if (localIn != null) { 149 localIn.close(); 150 } 151 } 152 153 private int fillbuf(InputStream localIn, byte[] localBuf) 154 throws IOException { 155 if (markpos == -1 || (pos - markpos >= marklimit)) { 156 /* Mark position not set or exceeded readlimit */ 157 int result = localIn.read(localBuf); 158 if (result > 0) { 159 markpos = -1; 160 pos = 0; 161 count = result == -1 ? 0 : result; 162 } 163 return result; 164 } 165 if (markpos == 0 && marklimit > localBuf.length) { 166 /* Increase buffer size to accommodate the readlimit */ 167 int newLength = localBuf.length * 2; 168 if (newLength > marklimit) { 169 newLength = marklimit; 170 } 171 byte[] newbuf = new byte[newLength]; 172 System.arraycopy(localBuf, 0, newbuf, 0, localBuf.length); 173 // Reassign buf, which will invalidate any local references 174 // FIXME: what if buf was null? 175 localBuf = buf = newbuf; 176 } else if (markpos > 0) { 177 System.arraycopy(localBuf, markpos, localBuf, 0, localBuf.length 178 - markpos); 179 } 180 /* Set the new position and mark position */ 181 pos -= markpos; 182 count = markpos = 0; 183 int bytesread = localIn.read(localBuf, pos, localBuf.length - pos); 184 count = bytesread <= 0 ? pos : pos + bytesread; 185 return bytesread; 186 } 187 188 /** 189 * Sets a mark position in this stream. The parameter {@code readlimit} 190 * indicates how many bytes can be read before a mark is invalidated. 191 * Calling {@code reset()} will reposition the stream back to the marked 192 * position if {@code readlimit} has not been surpassed. The underlying 193 * buffer may be increased in size to allow {@code readlimit} number of 194 * bytes to be supported. 195 * 196 * @param readlimit 197 * the number of bytes that can be read before the mark is 198 * invalidated. 199 * @see #reset() 200 */ 201 @Override 202 public synchronized void mark(int readlimit) { 203 marklimit = readlimit; 204 markpos = pos; 205 } 206 207 /** 208 * Indicates whether {@code BufferedInputStream} supports the {@code mark()} 209 * and {@code reset()} methods. 210 * 211 * @return {@code true} for BufferedInputStreams. 212 * @see #mark(int) 213 * @see #reset() 214 */ 215 @Override 216 public boolean markSupported() { 217 return true; 218 } 219 220 /** 221 * Reads a single byte from this stream and returns it as an integer in the 222 * range from 0 to 255. Returns -1 if the end of the source string has been 223 * reached. If the internal buffer does not contain any available bytes then 224 * it is filled from the source stream and the first byte is returned. 225 * 226 * @return the byte read or -1 if the end of the source stream has been 227 * reached. 228 * @throws IOException 229 * if this stream is closed or another IOException occurs. 230 */ 231 @Override 232 public synchronized int read() throws IOException { 233 // Use local refs since buf and in may be invalidated by an 234 // unsynchronized close() 235 byte[] localBuf = buf; 236 InputStream localIn = in; 237 if (localBuf == null || localIn == null) { 238 // K0059=Stream is closed 239 throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$ 240 } 241 242 /* Are there buffered bytes available? */ 243 if (pos >= count && fillbuf(localIn, localBuf) == -1) { 244 return -1; /* no, fill buffer */ 245 } 246 // localBuf may have been invalidated by fillbuf 247 if (localBuf != buf) { 248 localBuf = buf; 249 if (localBuf == null) { 250 // K0059=Stream is closed 251 throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$ 252 } 253 } 254 255 /* Did filling the buffer fail with -1 (EOF)? */ 256 if (count - pos > 0) { 257 return localBuf[pos++] & 0xFF; 258 } 259 return -1; 260 } 261 262 /** 263 * Reads at most {@code length} bytes from this stream and stores them in 264 * byte array {@code buffer} starting at offset {@code offset}. Returns the 265 * number of bytes actually read or -1 if no bytes were read and the end of 266 * the stream was encountered. If all the buffered bytes have been used, a 267 * mark has not been set and the requested number of bytes is larger than 268 * the receiver's buffer size, this implementation bypasses the buffer and 269 * simply places the results directly into {@code buffer}. 270 * 271 * @param buffer 272 * the byte array in which to store the bytes read. 273 * @param offset 274 * the initial position in {@code buffer} to store the bytes read 275 * from this stream. 276 * @param length 277 * the maximum number of bytes to store in {@code buffer}. 278 * @return the number of bytes actually read or -1 if end of stream. 279 * @throws IndexOutOfBoundsException 280 * if {@code offset < 0} or {@code length < 0}, or if 281 * {@code offset + length} is greater than the size of 282 * {@code buffer}. 283 * @throws IOException 284 * if the stream is already closed or another IOException 285 * occurs. 286 */ 287 @Override 288 public synchronized int read(byte[] buffer, int offset, int length) 289 throws IOException { 290 // Use local ref since buf may be invalidated by an unsynchronized 291 // close() 292 byte[] localBuf = buf; 293 if (localBuf == null) { 294 // K0059=Stream is closed 295 throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$ 296 } 297 // avoid int overflow 298 // BEGIN android-changed 299 // Exception priorities (in case of multiple errors) differ from 300 // RI, but are spec-compliant. 301 // made implicit null check explicit, used (offset | length) < 0 302 // instead of (offset < 0) || (length < 0) to safe one operation 303 if (buffer == null) { 304 throw new NullPointerException(Msg.getString("K0047")); //$NON-NLS-1$ 305 } 306 if ((offset | length) < 0 || offset > buffer.length - length) { 307 throw new IndexOutOfBoundsException(Msg.getString("K002f")); //$NON-NLS-1$ 308 } 309 // END android-changed 310 if (length == 0) { 311 return 0; 312 } 313 InputStream localIn = in; 314 if (localIn == null) { 315 // K0059=Stream is closed 316 throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$ 317 } 318 319 int required; 320 if (pos < count) { 321 /* There are bytes available in the buffer. */ 322 int copylength = count - pos >= length ? length : count - pos; 323 System.arraycopy(localBuf, pos, buffer, offset, copylength); 324 pos += copylength; 325 if (copylength == length || localIn.available() == 0) { 326 return copylength; 327 } 328 offset += copylength; 329 required = length - copylength; 330 } else { 331 required = length; 332 } 333 334 while (true) { 335 int read; 336 /* 337 * If we're not marked and the required size is greater than the 338 * buffer, simply read the bytes directly bypassing the buffer. 339 */ 340 if (markpos == -1 && required >= localBuf.length) { 341 read = localIn.read(buffer, offset, required); 342 if (read == -1) { 343 return required == length ? -1 : length - required; 344 } 345 } else { 346 if (fillbuf(localIn, localBuf) == -1) { 347 return required == length ? -1 : length - required; 348 } 349 // localBuf may have been invalidated by fillbuf 350 if (localBuf != buf) { 351 localBuf = buf; 352 if (localBuf == null) { 353 // K0059=Stream is closed 354 throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$ 355 } 356 } 357 358 read = count - pos >= required ? required : count - pos; 359 System.arraycopy(localBuf, pos, buffer, offset, read); 360 pos += read; 361 } 362 required -= read; 363 if (required == 0) { 364 return length; 365 } 366 if (localIn.available() == 0) { 367 return length - required; 368 } 369 offset += read; 370 } 371 } 372 373 /** 374 * Resets this stream to the last marked location. 375 * 376 * @throws IOException 377 * if this stream is closed, no mark has been set or the mark is 378 * no longer valid because more than {@code readlimit} bytes 379 * have been read since setting the mark. 380 * @see #mark(int) 381 */ 382 @Override 383 public synchronized void reset() throws IOException { 384 // BEGIN android-changed 385 /* 386 * These exceptions get thrown in some "normalish" circumstances, 387 * so it is preferable to avoid loading up the whole big set of 388 * messages just for these cases. 389 */ 390 if (buf == null) { 391 throw new IOException("Stream is closed"); 392 } 393 if (-1 == markpos) { 394 throw new IOException("Mark has been invalidated."); 395 } 396 // END android-changed 397 pos = markpos; 398 } 399 400 /** 401 * Skips {@code amount} number of bytes in this stream. Subsequent 402 * {@code read()}'s will not return these bytes unless {@code reset()} is 403 * used. 404 * 405 * @param amount 406 * the number of bytes to skip. {@code skip} does nothing and 407 * returns 0 if {@code amount} is less than zero. 408 * @return the number of bytes actually skipped. 409 * @throws IOException 410 * if this stream is closed or another IOException occurs. 411 */ 412 @Override 413 public synchronized long skip(long amount) throws IOException { 414 // Use local refs since buf and in may be invalidated by an 415 // unsynchronized close() 416 byte[] localBuf = buf; 417 InputStream localIn = in; 418 if (localBuf == null) { 419 // K0059=Stream is closed 420 throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$ 421 } 422 if (amount < 1) { 423 return 0; 424 } 425 if (localIn == null) { 426 // K0059=Stream is closed 427 throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$ 428 } 429 430 if (count - pos >= amount) { 431 pos += amount; 432 return amount; 433 } 434 long read = count - pos; 435 pos = count; 436 437 if (markpos != -1) { 438 if (amount <= marklimit) { 439 if (fillbuf(localIn, localBuf) == -1) { 440 return read; 441 } 442 if (count - pos >= amount - read) { 443 pos += amount - read; 444 return amount; 445 } 446 // Couldn't get all the bytes, skip what we read 447 read += (count - pos); 448 pos = count; 449 return read; 450 } 451 } 452 return read + localIn.skip(amount - read); 453 } 454} 455