1 /* BufferedInputStream.java -- An input stream that implements buffering 2 Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc. 3 4 This file is part of GNU Classpath. 5 6 GNU Classpath is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2, or (at your option) 9 any later version. 10 11 GNU Classpath is distributed in the hope that it will be useful, but 12 WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with GNU Classpath; see the file COPYING. If not, write to the 18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 19 02111-1307 USA. 20 21 Linking this library statically or dynamically with other modules is 22 making a combined work based on this library. Thus, the terms and 23 conditions of the GNU General Public License cover the whole 24 combination. 25 26 As a special exception, the copyright holders of this library give you 27 permission to link this library with independent modules to produce an 28 executable, regardless of the license terms of these independent 29 modules, and to copy and distribute the resulting executable under 30 terms of your choice, provided that you also meet, for each linked 31 independent module, the terms and conditions of the license of that 32 module. An independent module is a module which is not derived from 33 or based on this library. If you modify this library, you may extend 34 this exception to your version of the library, but you are not 35 obligated to do so. If you do not wish to do so, delete this 36 exception statement from your version. */ 37 38 39 package java.io; 40 41 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3 42 * "The Java Language Specification", ISBN 0-201-63451-1 43 * plus online API docs for JDK 1.2 beta from http://www.javasoft.com. 44 * Status: Believed complete and correct. 45 */ 46 47 /** 48 * This subclass of <code>FilterInputStream</code> buffers input from an 49 * underlying implementation to provide a possibly more efficient read 50 * mechanism. It maintains the buffer and buffer state in instance 51 * variables that are available to subclasses. The default buffer size 52 * of 2048 bytes can be overridden by the creator of the stream. 53 * <p> 54 * This class also implements mark/reset functionality. It is capable 55 * of remembering any number of input bytes, to the limits of 56 * system memory or the size of <code>Integer.MAX_VALUE</code> 57 * <p> 58 * Please note that this class does not properly handle character 59 * encodings. Consider using the <code>BufferedReader</code> class which 60 * does. 61 * 62 * @author Aaron M. Renn (arenn@urbanophile.com) 63 * @author Warren Levy <warrenl@cygnus.com> 64 */ 65 public class BufferedInputStream extends FilterInputStream 66 { 67 68 /** 69 * This is the default buffer size 70 */ 71 private static final int DEFAULT_BUFFER_SIZE = 2048; 72 73 /** 74 * The buffer used for storing data from the underlying stream. 75 */ 76 protected byte[] buf; 77 78 /** 79 * The number of valid bytes currently in the buffer. It is also the index 80 * of the buffer position one byte past the end of the valid data. 81 */ 82 protected int count = 0; 83 84 /** 85 * The index of the next character that will by read from the buffer. 86 * When <code>pos == count</code>, the buffer is empty. 87 */ 88 protected int pos = 0; 89 90 /** 91 * The value of <code>pos</code> when the <code>mark()</code> method was 92 * called. 93 * This is set to -1 if there is no mark set. 94 */ 95 protected int markpos = -1; 96 97 /** 98 * This is the maximum number of bytes than can be read after a 99 * call to <code>mark()</code> before the mark can be discarded. 100 * After this may bytes are read, the <code>reset()</code> method 101 * may not be called successfully. 102 */ 103 protected int marklimit = 0; 104 105 /** 106 * This method initializes a new <code>BufferedInputStream</code> that will 107 * read from the specified subordinate stream with a default buffer size 108 * of 2048 bytes 109 * 110 * @param in The subordinate stream to read from 111 */ BufferedInputStream(InputStream in)112 public BufferedInputStream(InputStream in) 113 { 114 this(in, DEFAULT_BUFFER_SIZE); 115 } 116 117 /** 118 * This method initializes a new <code>BufferedInputStream</code> that will 119 * read from the specified subordinate stream with a buffer size that 120 * is specified by the caller. 121 * 122 * @param in The subordinate stream to read from 123 * @param size The buffer size to use 124 * 125 * @exception IllegalArgumentException when size is smaller then 1 126 */ BufferedInputStream(InputStream in, int size)127 public BufferedInputStream(InputStream in, int size) 128 { 129 super(in); 130 if (size <= 0) 131 throw new IllegalArgumentException(); 132 buf = new byte[size]; 133 } 134 135 /** 136 * This method returns the number of bytes that can be read from this 137 * stream before a read can block. A return of 0 indicates that blocking 138 * might (or might not) occur on the very next read attempt. 139 * <p> 140 * The number of available bytes will be the number of read ahead bytes 141 * stored in the internal buffer plus the number of available bytes in 142 * the underlying stream. 143 * 144 * @return The number of bytes that can be read before blocking could occur 145 * 146 * @exception IOException If an error occurs 147 */ available()148 public synchronized int available() throws IOException 149 { 150 return count - pos + super.available(); 151 } 152 153 /** 154 * This method closes the underlying input stream and frees any 155 * resources associated with it. Sets <code>buf</code> to <code>null</code>. 156 * 157 * @exception IOException If an error occurs. 158 */ close()159 public void close() throws IOException 160 { 161 // Free up the array memory. 162 buf = null; 163 super.close(); 164 } 165 166 /** 167 * This method marks a position in the input to which the stream can be 168 * "reset" by calling the <code>reset()</code> method. The parameter 169 * <code>readlimit</code> is the number of bytes that can be read from the 170 * stream after setting the mark before the mark becomes invalid. For 171 * example, if <code>mark()</code> is called with a read limit of 10, then 172 * when 11 bytes of data are read from the stream before the 173 * <code>reset()</code> method is called, then the mark is invalid and the 174 * stream object instance is not required to remember the mark. 175 * <p> 176 * Note that the number of bytes that can be remembered by this method 177 * can be greater than the size of the internal read buffer. It is also 178 * not dependent on the subordinate stream supporting mark/reset 179 * functionality. 180 * 181 * @param readlimit The number of bytes that can be read before the mark 182 * becomes invalid 183 */ mark(int readlimit)184 public synchronized void mark(int readlimit) 185 { 186 marklimit = readlimit; 187 markpos = pos; 188 } 189 190 /** 191 * This method returns <code>true</code> to indicate that this class 192 * supports mark/reset functionality. 193 * 194 * @return <code>true</code> to indicate that mark/reset functionality is 195 * supported 196 * 197 */ markSupported()198 public boolean markSupported() 199 { 200 return true; 201 } 202 203 /** 204 * This method reads an unsigned byte from the input stream and returns it 205 * as an int in the range of 0-255. This method also will return -1 if 206 * the end of the stream has been reached. 207 * <p> 208 * This method will block until the byte can be read. 209 * 210 * @return The byte read or -1 if end of stream 211 * 212 * @exception IOException If an error occurs 213 */ read()214 public synchronized int read() throws IOException 215 { 216 if (pos >= count && !refill()) 217 return -1; // EOF 218 219 if (markpos >= 0 && pos - markpos > marklimit) 220 markpos = -1; 221 222 return ((int) buf[pos++]) & 0xFF; 223 } 224 225 /** 226 * This method reads bytes from a stream and stores them into a caller 227 * supplied buffer. It starts storing the data at index <code>off</code> 228 * into the buffer and attempts to read <code>len</code> bytes. This method 229 * can return before reading the number of bytes requested. The actual 230 * number of bytes read is returned as an int. A -1 is returned to indicate 231 * the end of the stream. 232 * <p> 233 * This method will block until some data can be read. 234 * 235 * @param b The array into which the bytes read should be stored 236 * @param off The offset into the array to start storing bytes 237 * @param len The requested number of bytes to read 238 * 239 * @return The actual number of bytes read, or -1 if end of stream. 240 * 241 * @exception IOException If an error occurs. 242 * @exception IndexOutOfBoundsException when <code>off</code> or 243 * <code>len</code> are negative, or when <code>off + len</code> 244 * is larger then the size of <code>b</code>, 245 */ read(byte[] b, int off, int len)246 public synchronized int read(byte[] b, int off, int len) throws IOException 247 { 248 if (off < 0 || len < 0 || off + len > b.length) 249 throw new IndexOutOfBoundsException(); 250 251 if (pos >= count && !refill()) 252 return -1; // No bytes were read before EOF. 253 254 int remain = Math.min(count - pos, len); 255 System.arraycopy(buf, pos, b, off, remain); 256 pos += remain; 257 258 if (markpos >= 0 && pos - markpos > marklimit) 259 markpos = -1; 260 261 return remain; 262 } 263 264 /** 265 * This method resets a stream to the point where the <code>mark()</code> 266 * method was called. Any bytes that were read after the mark point was 267 * set will be re-read during subsequent reads. 268 * <p> 269 * This method will throw an IOException if the number of bytes read from 270 * the stream since the call to <code>mark()</code> exceeds the mark limit 271 * passed when establishing the mark. 272 * 273 * @exception IOException If <code>mark()</code> was never called or more 274 * then <code>markLimit</code> bytes were read since the last 275 * call to <code>mark()</code> 276 */ reset()277 public synchronized void reset() throws IOException 278 { 279 if (markpos < 0) 280 throw new IOException(); 281 282 pos = markpos; 283 } 284 285 /** 286 * This method skips the specified number of bytes in the stream. It 287 * returns the actual number of bytes skipped, which may be less than the 288 * requested amount. 289 * 290 * @param n The requested number of bytes to skip 291 * 292 * @return The actual number of bytes skipped. 293 * 294 * @exception IOException If an error occurs 295 */ skip(long n)296 public synchronized long skip(long n) throws IOException 297 { 298 final long origN = n; 299 300 while (n > 0L) 301 { 302 if (pos >= count && !refill()) 303 if (n < origN) 304 break; 305 else 306 return -1; // No bytes were read before EOF. 307 308 int numread = (int) Math.min((long) (count - pos), n); 309 pos += numread; 310 n -= numread; 311 312 if (markpos >= 0 && pos - markpos > marklimit) 313 markpos = -1; 314 } 315 316 return origN - n; 317 } 318 319 /** 320 * Called to refill the buffer (when count is equal or greater the pos). 321 * Package local so BufferedReader can call it when needed. 322 * 323 * @return <code>true</code> when <code>buf</code> can be (partly) refilled, 324 * <code>false</code> otherwise. 325 */ refill()326 boolean refill() throws IOException 327 { 328 if (markpos < 0) 329 count = pos = 0; 330 else if (markpos > 0) 331 { 332 // Shift the marked bytes (if any) to the beginning of the array 333 // but don't grow it. This saves space in case a reset is done 334 // before we reach the max capacity of this array. 335 System.arraycopy(buf, markpos, buf, 0, count - markpos); 336 count -= markpos; 337 pos -= markpos; 338 markpos = 0; 339 } 340 else if (marklimit >= buf.length) // BTW, markpos == 0 341 { 342 // Need to grow the buffer now to have room for marklimit bytes. 343 // Note that the new buffer is one greater than marklimit. 344 // This is so that there will be one byte past marklimit to be read 345 // before having to call refill again, thus allowing marklimit to be 346 // invalidated. That way refill doesn't have to check marklimit. 347 byte[] newbuf = new byte[marklimit + 1]; 348 System.arraycopy(buf, 0, newbuf, 0, count); 349 buf = newbuf; 350 } 351 352 int numread = super.read(buf, count, buf.length - count); 353 354 if (numread < 0) // EOF 355 return false; 356 357 count += numread; 358 return true; 359 } 360 } 361