Source Home >> Java Source 1.5.0 >> java.io.InputStream V 0.09
  • 001/*
  • 002 * @(#)InputStream.java 1.45 04/02/19
  • 003 *
  • 004 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  • 005 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  • 006 */
  • 007
  • 008package java.io;
  • 009
  • 010/**
  • 011 * This abstract class is the superclass of all classes representing
  • 012 * an input stream of bytes.
  • 013 *
  • 014 * <p> Applications that need to define a subclass of <code>InputStream</code>
  • 015 * must always provide a method that returns the next byte of input.
  • 016 *
  • 017 * @author Arthur van Hoff
  • 018 * @version 1.45, 02/19/04
  • 019 * @see java.io.BufferedInputStream
  • 020 * @see java.io.ByteArrayInputStream
  • 021 * @see java.io.DataInputStream
  • 022 * @see java.io.FilterInputStream
  • 023 * @see java.io.InputStream#read()
  • 024 * @see java.io.OutputStream
  • 025 * @see java.io.PushbackInputStream
  • 026 * @since JDK1.0
  • 027 */
  • 028public abstract class InputStream implements Closeable {
  • 029
  • 030 // SKIP_BUFFER_SIZE is used to determine the size of skipBuffer
  • 031 private static final int SKIP_BUFFER_SIZE = 2048;
  • 032 // skipBuffer is initialized in skip(long), if needed.
  • 033 private static byte[] skipBuffer;
  • 034
  • 035 /**
  • 036 * Reads the next byte of data from the input stream. The value byte is
  • 037 * returned as an <code>int</code> in the range <code>0</code> to
  • 038 * <code>255</code>. If no byte is available because the end of the stream
  • 039 * has been reached, the value <code>-1</code> is returned. This method
  • 040 * blocks until input data is available, the end of the stream is detected,
  • 041 * or an exception is thrown.
  • 042 *
  • 043 * <p> A subclass must provide an implementation of this method.
  • 044 *
  • 045 * @return the next byte of data, or <code>-1</code> if the end of the
  • 046 * stream is reached.
  • 047 * @exception IOException if an I/O error occurs.
  • 048 */
  • 049 public abstract int read() throws IOException;
  • 050
  • 051 /**
  • 052 * Reads some number of bytes from the input stream and stores them into
  • 053 * the buffer array <code>b</code>. The number of bytes actually read is
  • 054 * returned as an integer. This method blocks until input data is
  • 055 * available, end of file is detected, or an exception is thrown.
  • 056 *
  • 057 * <p> If <code>b</code> is <code>null</code>, a
  • 058 * <code>NullPointerException</code> is thrown. If the length of
  • 059 * <code>b</code> is zero, then no bytes are read and <code>0</code> is
  • 060 * returned; otherwise, there is an attempt to read at least one byte. If
  • 061 * no byte is available because the stream is at end of file, the value
  • 062 * <code>-1</code> is returned; otherwise, at least one byte is read and
  • 063 * stored into <code>b</code>.
  • 064 *
  • 065 * <p> The first byte read is stored into element <code>b[0]</code>, the
  • 066 * next one into <code>b[1]</code>, and so on. The number of bytes read is,
  • 067 * at most, equal to the length of <code>b</code>. Let <i>k</i> be the
  • 068 * number of bytes actually read; these bytes will be stored in elements
  • 069 * <code>b[0]</code> through <code>b[</code><i>k</i><code>-1]</code>,
  • 070 * leaving elements <code>b[</code><i>k</i><code>]</code> through
  • 071 * <code>b[b.length-1]</code> unaffected.
  • 072 *
  • 073 * <p> If the first byte cannot be read for any reason other than end of
  • 074 * file, then an <code>IOException</code> is thrown. In particular, an
  • 075 * <code>IOException</code> is thrown if the input stream has been closed.
  • 076 *
  • 077 * <p> The <code>read(b)</code> method for class <code>InputStream</code>
  • 078 * has the same effect as: <pre><code> read(b, 0, b.length) </code></pre>
  • 079 *
  • 080 * @param b the buffer into which the data is read.
  • 081 * @return the total number of bytes read into the buffer, or
  • 082 * <code>-1</code> is there is no more data because the end of
  • 083 * the stream has been reached.
  • 084 * @exception IOException if an I/O error occurs.
  • 085 * @exception NullPointerException if <code>b</code> is <code>null</code>.
  • 086 * @see java.io.InputStream#read(byte[], int, int)
  • 087 */
  • 088 public int read(byte b[]) throws IOException {
  • 089 return read(b, 0, b.length);
  • 090 }
  • 091
  • 092 /**
  • 093 * Reads up to <code>len</code> bytes of data from the input stream into
  • 094 * an array of bytes. An attempt is made to read as many as
  • 095 * <code>len</code> bytes, but a smaller number may be read.
  • 096 * The number of bytes actually read is returned as an integer.
  • 097 *
  • 098 * <p> This method blocks until input data is available, end of file is
  • 099 * detected, or an exception is thrown.
  • 100 *
  • 101 * <p> If <code>b</code> is <code>null</code>, a
  • 102 * <code>NullPointerException</code> is thrown.
  • 103 *
  • 104 * <p> If <code>off</code> is negative, or <code>len</code> is negative, or
  • 105 * <code>off+len</code> is greater than the length of the array
  • 106 * <code>b</code>, then an <code>IndexOutOfBoundsException</code> is
  • 107 * thrown.
  • 108 *
  • 109 * <p> If <code>len</code> is zero, then no bytes are read and
  • 110 * <code>0</code> is returned; otherwise, there is an attempt to read at
  • 111 * least one byte. If no byte is available because the stream is at end of
  • 112 * file, the value <code>-1</code> is returned; otherwise, at least one
  • 113 * byte is read and stored into <code>b</code>.
  • 114 *
  • 115 * <p> The first byte read is stored into element <code>b[off]</code>, the
  • 116 * next one into <code>b[off+1]</code>, and so on. The number of bytes read
  • 117 * is, at most, equal to <code>len</code>. Let <i>k</i> be the number of
  • 118 * bytes actually read; these bytes will be stored in elements
  • 119 * <code>b[off]</code> through <code>b[off+</code><i>k</i><code>-1]</code>,
  • 120 * leaving elements <code>b[off+</code><i>k</i><code>]</code> through
  • 121 * <code>b[off+len-1]</code> unaffected.
  • 122 *
  • 123 * <p> In every case, elements <code>b[0]</code> through
  • 124 * <code>b[off]</code> and elements <code>b[off+len]</code> through
  • 125 * <code>b[b.length-1]</code> are unaffected.
  • 126 *
  • 127 * <p> If the first byte cannot be read for any reason other than end of
  • 128 * file, then an <code>IOException</code> is thrown. In particular, an
  • 129 * <code>IOException</code> is thrown if the input stream has been closed.
  • 130 *
  • 131 * <p> The <code>read(b,</code> <code>off,</code> <code>len)</code> method
  • 132 * for class <code>InputStream</code> simply calls the method
  • 133 * <code>read()</code> repeatedly. If the first such call results in an
  • 134 * <code>IOException</code>, that exception is returned from the call to
  • 135 * the <code>read(b,</code> <code>off,</code> <code>len)</code> method. If
  • 136 * any subsequent call to <code>read()</code> results in a
  • 137 * <code>IOException</code>, the exception is caught and treated as if it
  • 138 * were end of file; the bytes read up to that point are stored into
  • 139 * <code>b</code> and the number of bytes read before the exception
  • 140 * occurred is returned. Subclasses are encouraged to provide a more
  • 141 * efficient implementation of this method.
  • 142 *
  • 143 * @param b the buffer into which the data is read.
  • 144 * @param off the start offset in array <code>b</code>
  • 145 * at which the data is written.
  • 146 * @param len the maximum number of bytes to read.
  • 147 * @return the total number of bytes read into the buffer, or
  • 148 * <code>-1</code> if there is no more data because the end of
  • 149 * the stream has been reached.
  • 150 * @exception IOException if an I/O error occurs.
  • 151 * @exception NullPointerException if <code>b</code> is <code>null</code>.
  • 152 * @see java.io.InputStream#read()
  • 153 */
  • 154 public int read(byte b[], int off, int len) throws IOException {
  • 155 if (b == null) {
  • 156 throw new NullPointerException();
  • 157 } else if ((off < 0) || (off > b.length) || (len < 0) ||
  • 158 ((off + len) > b.length) || ((off + len) < 0)) {
  • 159 throw new IndexOutOfBoundsException();
  • 160 } else if (len == 0) {
  • 161 return 0;
  • 162 }
  • 163
  • 164 int c = read();
  • 165 if (c == -1) {
  • 166 return -1;
  • 167 }
  • 168 b[off] = (byte)c;
  • 169
  • 170 int i = 1;
  • 171 try {
  • 172 for (; i < len ; i++) {
  • 173 c = read();
  • 174 if (c == -1) {
  • 175 break;
  • 176 }
  • 177 if (b != null) {
  • 178 b[off + i] = (byte)c;
  • 179 }
  • 180 }
  • 181 } catch (IOException ee) {
  • 182 }
  • 183 return i;
  • 184 }
  • 185
  • 186 /**
  • 187 * Skips over and discards <code>n</code> bytes of data from this input
  • 188 * stream. The <code>skip</code> method may, for a variety of reasons, end
  • 189 * up skipping over some smaller number of bytes, possibly <code>0</code>.
  • 190 * This may result from any of a number of conditions; reaching end of file
  • 191 * before <code>n</code> bytes have been skipped is only one possibility.
  • 192 * The actual number of bytes skipped is returned. If <code>n</code> is
  • 193 * negative, no bytes are skipped.
  • 194 *
  • 195 * <p> The <code>skip</code> method of <code>InputStream</code> creates a
  • 196 * byte array and then repeatedly reads into it until <code>n</code> bytes
  • 197 * have been read or the end of the stream has been reached. Subclasses are
  • 198 * encouraged to provide a more efficient implementation of this method.
  • 199 *
  • 200 * @param n the number of bytes to be skipped.
  • 201 * @return the actual number of bytes skipped.
  • 202 * @exception IOException if an I/O error occurs.
  • 203 */
  • 204 public long skip(long n) throws IOException {
  • 205
  • 206 long remaining = n;
  • 207 int nr;
  • 208 if (skipBuffer == null)
  • 209 skipBuffer = new byte[SKIP_BUFFER_SIZE];
  • 210
  • 211 byte[] localSkipBuffer = skipBuffer;
  • 212
  • 213 if (n <= 0) {
  • 214 return 0;
  • 215 }
  • 216
  • 217 while (remaining > 0) {
  • 218 nr = read(localSkipBuffer, 0,
  • 219 (int) Math.min(SKIP_BUFFER_SIZE, remaining));
  • 220 if (nr < 0) {
  • 221 break;
  • 222 }
  • 223 remaining -= nr;
  • 224 }
  • 225
  • 226 return n - remaining;
  • 227 }
  • 228
  • 229 /**
  • 230 * Returns the number of bytes that can be read (or skipped over) from
  • 231 * this input stream without blocking by the next caller of a method for
  • 232 * this input stream. The next caller might be the same thread or
  • 233 * another thread.
  • 234 *
  • 235 * <p> The <code>available</code> method for class <code>InputStream</code>
  • 236 * always returns <code>0</code>.
  • 237 *
  • 238 * <p> This method should be overridden by subclasses.
  • 239 *
  • 240 * @return the number of bytes that can be read from this input stream
  • 241 * without blocking.
  • 242 * @exception IOException if an I/O error occurs.
  • 243 */
  • 244 public int available() throws IOException {
  • 245 return 0;
  • 246 }
  • 247
  • 248 /**
  • 249 * Closes this input stream and releases any system resources associated
  • 250 * with the stream.
  • 251 *
  • 252 * <p> The <code>close</code> method of <code>InputStream</code> does
  • 253 * nothing.
  • 254 *
  • 255 * @exception IOException if an I/O error occurs.
  • 256 */
  • 257 public void close() throws IOException {}
  • 258
  • 259 /**
  • 260 * Marks the current position in this input stream. A subsequent call to
  • 261 * the <code>reset</code> method repositions this stream at the last marked
  • 262 * position so that subsequent reads re-read the same bytes.
  • 263 *
  • 264 * <p> The <code>readlimit</code> arguments tells this input stream to
  • 265 * allow that many bytes to be read before the mark position gets
  • 266 * invalidated.
  • 267 *
  • 268 * <p> The general contract of <code>mark</code> is that, if the method
  • 269 * <code>markSupported</code> returns <code>true</code>, the stream somehow
  • 270 * remembers all the bytes read after the call to <code>mark</code> and
  • 271 * stands ready to supply those same bytes again if and whenever the method
  • 272 * <code>reset</code> is called. However, the stream is not required to
  • 273 * remember any data at all if more than <code>readlimit</code> bytes are
  • 274 * read from the stream before <code>reset</code> is called.
  • 275 *
  • 276 * <p> The <code>mark</code> method of <code>InputStream</code> does
  • 277 * nothing.
  • 278 *
  • 279 * @param readlimit the maximum limit of bytes that can be read before
  • 280 * the mark position becomes invalid.
  • 281 * @see java.io.InputStream#reset()
  • 282 */
  • 283 public synchronized void mark(int readlimit) {}
  • 284
  • 285 /**
  • 286 * Repositions this stream to the position at the time the
  • 287 * <code>mark</code> method was last called on this input stream.
  • 288 *
  • 289 * <p> The general contract of <code>reset</code> is:
  • 290 *
  • 291 * <p><ul>
  • 292 *
  • 293 * <li> If the method <code>markSupported</code> returns
  • 294 * <code>true</code>, then:
  • 295 *
  • 296 * <ul><li> If the method <code>mark</code> has not been called since
  • 297 * the stream was created, or the number of bytes read from the stream
  • 298 * since <code>mark</code> was last called is larger than the argument
  • 299 * to <code>mark</code> at that last call, then an
  • 300 * <code>IOException</code> might be thrown.
  • 301 *
  • 302 * <li> If such an <code>IOException</code> is not thrown, then the
  • 303 * stream is reset to a state such that all the bytes read since the
  • 304 * most recent call to <code>mark</code> (or since the start of the
  • 305 * file, if <code>mark</code> has not been called) will be resupplied
  • 306 * to subsequent callers of the <code>read</code> method, followed by
  • 307 * any bytes that otherwise would have been the next input data as of
  • 308 * the time of the call to <code>reset</code>. </ul>
  • 309 *
  • 310 * <li> If the method <code>markSupported</code> returns
  • 311 * <code>false</code>, then:
  • 312 *
  • 313 * <ul><li> The call to <code>reset</code> may throw an
  • 314 * <code>IOException</code>.
  • 315 *
  • 316 * <li> If an <code>IOException</code> is not thrown, then the stream
  • 317 * is reset to a fixed state that depends on the particular type of the
  • 318 * input stream and how it was created. The bytes that will be supplied
  • 319 * to subsequent callers of the <code>read</code> method depend on the
  • 320 * particular type of the input stream. </ul></ul>
  • 321 *
  • 322 * <p>The method <code>reset</code> for class <code>InputStream</code>
  • 323 * does nothing except throw an <code>IOException</code>.
  • 324 *
  • 325 * @exception IOException if this stream has not been marked or if the
  • 326 * mark has been invalidated.
  • 327 * @see java.io.InputStream#mark(int)
  • 328 * @see java.io.IOException
  • 329 */
  • 330 public synchronized void reset() throws IOException {
  • 331 throw new IOException("mark/reset not supported");
  • 332 }
  • 333
  • 334 /**
  • 335 * Tests if this input stream supports the <code>mark</code> and
  • 336 * <code>reset</code> methods. Whether or not <code>mark</code> and
  • 337 * <code>reset</code> are supported is an invariant property of a
  • 338 * particular input stream instance. The <code>markSupported</code> method
  • 339 * of <code>InputStream</code> returns <code>false</code>.
  • 340 *
  • 341 * @return <code>true</code> if this stream instance supports the mark
  • 342 * and reset methods; <code>false</code> otherwise.
  • 343 * @see java.io.InputStream#mark(int)
  • 344 * @see java.io.InputStream#reset()
  • 345 */
  • 346 public boolean markSupported() {
  • 347 return false;
  • 348 }
  • 349
  • 350}

文件:InputStream.java
包名:java.io
类名:InputStream
继承:
接口:[Closeable]