1 /* 2 * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.io; 27 28 import java.util.Objects; 29 30 /** 31 * This abstract class is the superclass of all classes representing 32 * an input stream of bytes. 33 * 34 * <p> Applications that need to define a subclass of <code>InputStream</code> 35 * must always provide a method that returns the next byte of input. 36 * 37 * @author Arthur van Hoff 38 * @see java.io.BufferedInputStream 39 * @see java.io.ByteArrayInputStream 40 * @see java.io.DataInputStream 41 * @see java.io.FilterInputStream 42 * @see java.io.InputStream#read() 43 * @see java.io.OutputStream 44 * @see java.io.PushbackInputStream 45 * @since 1.0 46 */ 47 public abstract class InputStream implements Closeable { 48 49 // MAX_SKIP_BUFFER_SIZE is used to determine the maximum buffer size to 50 // use when skipping. 51 private static final int MAX_SKIP_BUFFER_SIZE = 2048; 52 53 private static final int TRANSFER_BUFFER_SIZE = 8192; 54 55 /** 56 * Reads the next byte of data from the input stream. The value byte is 57 * returned as an <code>int</code> in the range <code>0</code> to 58 * <code>255</code>. If no byte is available because the end of the stream 59 * has been reached, the value <code>-1</code> is returned. This method 60 * blocks until input data is available, the end of the stream is detected, 61 * or an exception is thrown. 62 * 63 * <p> A subclass must provide an implementation of this method. 64 * 65 * @return the next byte of data, or <code>-1</code> if the end of the 66 * stream is reached. 67 * @exception IOException if an I/O error occurs. 68 */ 69 public abstract int read() throws IOException; 70 71 /** 72 * Reads some number of bytes from the input stream and stores them into 73 * the buffer array <code>b</code>. The number of bytes actually read is 74 * returned as an integer. This method blocks until input data is 75 * available, end of file is detected, or an exception is thrown. 76 * 77 * <p> If the length of <code>b</code> is zero, then no bytes are read and 78 * <code>0</code> is returned; otherwise, there is an attempt to read at 79 * least one byte. If no byte is available because the stream is at the 80 * end of the file, the value <code>-1</code> is returned; otherwise, at 81 * least one byte is read and stored into <code>b</code>. 82 * 83 * <p> The first byte read is stored into element <code>b[0]</code>, the 84 * next one into <code>b[1]</code>, and so on. The number of bytes read is, 85 * at most, equal to the length of <code>b</code>. Let <i>k</i> be the 86 * number of bytes actually read; these bytes will be stored in elements 87 * <code>b[0]</code> through <code>b[</code><i>k</i><code>-1]</code>, 88 * leaving elements <code>b[</code><i>k</i><code>]</code> through 89 * <code>b[b.length-1]</code> unaffected. 90 * 91 * <p> The <code>read(b)</code> method for class <code>InputStream</code> 92 * has the same effect as: <pre><code> read(b, 0, b.length) </code></pre> 93 * 94 * @param b the buffer into which the data is read. 95 * @return the total number of bytes read into the buffer, or 96 * <code>-1</code> if there is no more data because the end of 97 * the stream has been reached. 98 * @exception IOException If the first byte cannot be read for any reason 99 * other than the end of the file, if the input stream has been closed, or 100 * if some other I/O error occurs. 101 * @exception NullPointerException if <code>b</code> is <code>null</code>. 102 * @see java.io.InputStream#read(byte[], int, int) 103 */ 104 public int read(byte b[]) throws IOException { 105 return read(b, 0, b.length); 106 } 107 108 /** 109 * Reads up to <code>len</code> bytes of data from the input stream into 110 * an array of bytes. An attempt is made to read as many as 111 * <code>len</code> bytes, but a smaller number may be read. 112 * The number of bytes actually read is returned as an integer. 113 * 114 * <p> This method blocks until input data is available, end of file is 115 * detected, or an exception is thrown. 116 * 117 * <p> If <code>len</code> is zero, then no bytes are read and 118 * <code>0</code> is returned; otherwise, there is an attempt to read at 119 * least one byte. If no byte is available because the stream is at end of 120 * file, the value <code>-1</code> is returned; otherwise, at least one 121 * byte is read and stored into <code>b</code>. 122 * 123 * <p> The first byte read is stored into element <code>b[off]</code>, the 124 * next one into <code>b[off+1]</code>, and so on. The number of bytes read 125 * is, at most, equal to <code>len</code>. Let <i>k</i> be the number of 126 * bytes actually read; these bytes will be stored in elements 127 * <code>b[off]</code> through <code>b[off+</code><i>k</i><code>-1]</code>, 128 * leaving elements <code>b[off+</code><i>k</i><code>]</code> through 129 * <code>b[off+len-1]</code> unaffected. 130 * 131 * <p> In every case, elements <code>b[0]</code> through 132 * <code>b[off]</code> and elements <code>b[off+len]</code> through 133 * <code>b[b.length-1]</code> are unaffected. 134 * 135 * <p> The <code>read(b,</code> <code>off,</code> <code>len)</code> method 136 * for class <code>InputStream</code> simply calls the method 137 * <code>read()</code> repeatedly. If the first such call results in an 138 * <code>IOException</code>, that exception is returned from the call to 139 * the <code>read(b,</code> <code>off,</code> <code>len)</code> method. If 140 * any subsequent call to <code>read()</code> results in a 141 * <code>IOException</code>, the exception is caught and treated as if it 142 * were end of file; the bytes read up to that point are stored into 143 * <code>b</code> and the number of bytes read before the exception 144 * occurred is returned. The default implementation of this method blocks 145 * until the requested amount of input data <code>len</code> has been read, 146 * end of file is detected, or an exception is thrown. Subclasses are encouraged 147 * to provide a more efficient implementation of this method. 148 * 149 * @param b the buffer into which the data is read. 150 * @param off the start offset in array <code>b</code> 151 * at which the data is written. 152 * @param len the maximum number of bytes to read. 153 * @return the total number of bytes read into the buffer, or 154 * <code>-1</code> if there is no more data because the end of 155 * the stream has been reached. 156 * @exception IOException If the first byte cannot be read for any reason 157 * other than end of file, or if the input stream has been closed, or if 158 * some other I/O error occurs. 159 * @exception NullPointerException If <code>b</code> is <code>null</code>. 160 * @exception IndexOutOfBoundsException If <code>off</code> is negative, 161 * <code>len</code> is negative, or <code>len</code> is greater than 162 * <code>b.length - off</code> 163 * @see java.io.InputStream#read() 164 */ 165 public int read(byte b[], int off, int len) throws IOException { 166 if (b == null) { 167 throw new NullPointerException(); 168 } else if (off < 0 || len < 0 || len > b.length - off) { 169 throw new IndexOutOfBoundsException(); 170 } else if (len == 0) { 171 return 0; 172 } 173 174 int c = read(); 175 if (c == -1) { 176 return -1; 177 } 178 b[off] = (byte)c; 179 180 int i = 1; 181 try { 182 for (; i < len ; i++) { 183 c = read(); 184 if (c == -1) { 185 break; 186 } 187 b[off + i] = (byte)c; 188 } 189 } catch (IOException ee) { 190 } 191 return i; 192 } 193 194 /** 195 * Skips over and discards <code>n</code> bytes of data from this input 196 * stream. The <code>skip</code> method may, for a variety of reasons, end 197 * up skipping over some smaller number of bytes, possibly <code>0</code>. 198 * This may result from any of a number of conditions; reaching end of file 199 * before <code>n</code> bytes have been skipped is only one possibility. 200 * The actual number of bytes skipped is returned. If {@code n} is 201 * negative, the {@code skip} method for class {@code InputStream} always 202 * returns 0, and no bytes are skipped. Subclasses may handle the negative 203 * value differently. 204 * 205 * <p> The <code>skip</code> method of this class creates a 206 * byte array and then repeatedly reads into it until <code>n</code> bytes 207 * have been read or the end of the stream has been reached. Subclasses are 208 * encouraged to provide a more efficient implementation of this method. 209 * For instance, the implementation may depend on the ability to seek. 210 * 211 * @param n the number of bytes to be skipped. 212 * @return the actual number of bytes skipped. 213 * @exception IOException if the stream does not support seek, 214 * or if some other I/O error occurs. 215 */ 216 public long skip(long n) throws IOException { 217 218 long remaining = n; 219 int nr; 220 221 if (n <= 0) { 222 return 0; 223 } 224 225 int size = (int)Math.min(MAX_SKIP_BUFFER_SIZE, remaining); 226 byte[] skipBuffer = new byte[size]; 227 while (remaining > 0) { 228 nr = read(skipBuffer, 0, (int)Math.min(size, remaining)); 229 if (nr < 0) { 230 break; 231 } 232 remaining -= nr; 233 } 234 235 return n - remaining; 236 } 237 238 /** 239 * Returns an estimate of the number of bytes that can be read (or 240 * skipped over) from this input stream without blocking by the next 241 * invocation of a method for this input stream. The next invocation 242 * might be the same thread or another thread. A single read or skip of this 243 * many bytes will not block, but may read or skip fewer bytes. 244 * 245 * <p> Note that while some implementations of {@code InputStream} will return 246 * the total number of bytes in the stream, many will not. It is 247 * never correct to use the return value of this method to allocate 248 * a buffer intended to hold all data in this stream. 249 * 250 * <p> A subclass' implementation of this method may choose to throw an 251 * {@link IOException} if this input stream has been closed by 252 * invoking the {@link #close()} method. 253 * 254 * <p> The {@code available} method for class {@code InputStream} always 255 * returns {@code 0}. 256 * 257 * <p> This method should be overridden by subclasses. 258 * 259 * @return an estimate of the number of bytes that can be read (or skipped 260 * over) from this input stream without blocking or {@code 0} when 261 * it reaches the end of the input stream. 262 * @exception IOException if an I/O error occurs. 263 */ 264 public int available() throws IOException { 265 return 0; 266 } 267 268 /** 269 * Closes this input stream and releases any system resources associated 270 * with the stream. 271 * 272 * <p> The <code>close</code> method of <code>InputStream</code> does 273 * nothing. 274 * 275 * @exception IOException if an I/O error occurs. 276 */ 277 public void close() throws IOException {} 278 279 /** 280 * Marks the current position in this input stream. A subsequent call to 281 * the <code>reset</code> method repositions this stream at the last marked 282 * position so that subsequent reads re-read the same bytes. 283 * 284 * <p> The <code>readlimit</code> arguments tells this input stream to 285 * allow that many bytes to be read before the mark position gets 286 * invalidated. 287 * 288 * <p> The general contract of <code>mark</code> is that, if the method 289 * <code>markSupported</code> returns <code>true</code>, the stream somehow 290 * remembers all the bytes read after the call to <code>mark</code> and 291 * stands ready to supply those same bytes again if and whenever the method 292 * <code>reset</code> is called. However, the stream is not required to 293 * remember any data at all if more than <code>readlimit</code> bytes are 294 * read from the stream before <code>reset</code> is called. 295 * 296 * <p> Marking a closed stream should not have any effect on the stream. 297 * 298 * <p> The <code>mark</code> method of <code>InputStream</code> does 299 * nothing. 300 * 301 * @param readlimit the maximum limit of bytes that can be read before 302 * the mark position becomes invalid. 303 * @see java.io.InputStream#reset() 304 */ 305 public synchronized void mark(int readlimit) {} 306 307 /** 308 * Repositions this stream to the position at the time the 309 * <code>mark</code> method was last called on this input stream. 310 * 311 * <p> The general contract of <code>reset</code> is: 312 * 313 * <ul> 314 * <li> If the method <code>markSupported</code> returns 315 * <code>true</code>, then: 316 * 317 * <ul><li> If the method <code>mark</code> has not been called since 318 * the stream was created, or the number of bytes read from the stream 319 * since <code>mark</code> was last called is larger than the argument 320 * to <code>mark</code> at that last call, then an 321 * <code>IOException</code> might be thrown. 322 * 323 * <li> If such an <code>IOException</code> is not thrown, then the 324 * stream is reset to a state such that all the bytes read since the 325 * most recent call to <code>mark</code> (or since the start of the 326 * file, if <code>mark</code> has not been called) will be resupplied 327 * to subsequent callers of the <code>read</code> method, followed by 328 * any bytes that otherwise would have been the next input data as of 329 * the time of the call to <code>reset</code>. </ul> 330 * 331 * <li> If the method <code>markSupported</code> returns 332 * <code>false</code>, then: 333 * 334 * <ul><li> The call to <code>reset</code> may throw an 335 * <code>IOException</code>. 336 * 337 * <li> If an <code>IOException</code> is not thrown, then the stream 338 * is reset to a fixed state that depends on the particular type of the 339 * input stream and how it was created. The bytes that will be supplied 340 * to subsequent callers of the <code>read</code> method depend on the 341 * particular type of the input stream. </ul></ul> 342 * 343 * <p>The method <code>reset</code> for class <code>InputStream</code> 344 * does nothing except throw an <code>IOException</code>. 345 * 346 * @exception IOException if this stream has not been marked or if the 347 * mark has been invalidated. 348 * @see java.io.InputStream#mark(int) 349 * @see java.io.IOException 350 */ 351 public synchronized void reset() throws IOException { 352 throw new IOException("mark/reset not supported"); 353 } 354 355 /** 356 * Tests if this input stream supports the <code>mark</code> and 357 * <code>reset</code> methods. Whether or not <code>mark</code> and 358 * <code>reset</code> are supported is an invariant property of a 359 * particular input stream instance. The <code>markSupported</code> method 360 * of <code>InputStream</code> returns <code>false</code>. 361 * 362 * @return <code>true</code> if this stream instance supports the mark 363 * and reset methods; <code>false</code> otherwise. 364 * @see java.io.InputStream#mark(int) 365 * @see java.io.InputStream#reset() 366 */ 367 public boolean markSupported() { 368 return false; 369 } 370 371 /** 372 * Reads all bytes from this input stream and writes the bytes to the 373 * given output stream in the order that they are read. On return, this 374 * input stream will be at end of stream. This method does not close either 375 * stream. 376 * <p> 377 * This method may block indefinitely reading from the input stream, or 378 * writing to the output stream. The behavior for the case where the input 379 * and/or output stream is <i>asynchronously closed</i>, or the thread 380 * interrupted during the transfer, is highly input and output stream 381 * specific, and therefore not specified. 382 * <p> 383 * If an I/O error occurs reading from the input stream or writing to the 384 * output stream, then it may do so after some bytes have been read or 385 * written. Consequently the input stream may not be at end of stream and 386 * one, or both, streams may be in an inconsistent state. It is strongly 387 * recommended that both streams be promptly closed if an I/O error occurs. 388 * 389 * @param out the output stream, non-null 390 * @return the number of bytes transferred 391 * @throws IOException if an I/O error occurs when reading or writing 392 * @throws NullPointerException if {@code out} is {@code null} 393 * 394 * @since 1.9 395 */ 396 public long transferTo(OutputStream out) throws IOException { 397 Objects.requireNonNull(out, "out"); 398 long transferred = 0; 399 byte[] buffer = new byte[TRANSFER_BUFFER_SIZE]; 400 int read; 401 while ((read = this.read(buffer, 0, TRANSFER_BUFFER_SIZE)) >= 0) { 402 out.write(buffer, 0, read); 403 transferred += read; 404 } 405 return transferred; 406 } 407 }