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