001: /*
002: * Copyright 1994-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package java.io;
027:
028: /**
029: * This abstract class is the superclass of all classes representing
030: * an input stream of bytes.
031: *
032: * <p> Applications that need to define a subclass of <code>InputStream</code>
033: * must always provide a method that returns the next byte of input.
034: *
035: * @author Arthur van Hoff
036: * @version 1.58, 05/05/07
037: * @see java.io.BufferedInputStream
038: * @see java.io.ByteArrayInputStream
039: * @see java.io.DataInputStream
040: * @see java.io.FilterInputStream
041: * @see java.io.InputStream#read()
042: * @see java.io.OutputStream
043: * @see java.io.PushbackInputStream
044: * @since JDK1.0
045: */
046: public abstract class InputStream implements Closeable {
047:
048: // SKIP_BUFFER_SIZE is used to determine the size of skipBuffer
049: private static final int SKIP_BUFFER_SIZE = 2048;
050: // skipBuffer is initialized in skip(long), if needed.
051: private static byte[] skipBuffer;
052:
053: /**
054: * Reads the next byte of data from the input stream. The value byte is
055: * returned as an <code>int</code> in the range <code>0</code> to
056: * <code>255</code>. If no byte is available because the end of the stream
057: * has been reached, the value <code>-1</code> is returned. This method
058: * blocks until input data is available, the end of the stream is detected,
059: * or an exception is thrown.
060: *
061: * <p> A subclass must provide an implementation of this method.
062: *
063: * @return the next byte of data, or <code>-1</code> if the end of the
064: * stream is reached.
065: * @exception IOException if an I/O error occurs.
066: */
067: public abstract int read() throws IOException;
068:
069: /**
070: * Reads some number of bytes from the input stream and stores them into
071: * the buffer array <code>b</code>. The number of bytes actually read is
072: * returned as an integer. This method blocks until input data is
073: * available, end of file is detected, or an exception is thrown.
074: *
075: * <p> If the length of <code>b</code> is zero, then no bytes are read and
076: * <code>0</code> is returned; otherwise, there is an attempt to read at
077: * least one byte. If no byte is available because the stream is at the
078: * end of the file, the value <code>-1</code> is returned; otherwise, at
079: * least one byte is read and stored into <code>b</code>.
080: *
081: * <p> The first byte read is stored into element <code>b[0]</code>, the
082: * next one into <code>b[1]</code>, and so on. The number of bytes read is,
083: * at most, equal to the length of <code>b</code>. Let <i>k</i> be the
084: * number of bytes actually read; these bytes will be stored in elements
085: * <code>b[0]</code> through <code>b[</code><i>k</i><code>-1]</code>,
086: * leaving elements <code>b[</code><i>k</i><code>]</code> through
087: * <code>b[b.length-1]</code> unaffected.
088: *
089: * <p> The <code>read(b)</code> method for class <code>InputStream</code>
090: * has the same effect as: <pre><code> read(b, 0, b.length) </code></pre>
091: *
092: * @param b the buffer into which the data is read.
093: * @return the total number of bytes read into the buffer, or
094: * <code>-1</code> is there is no more data because the end of
095: * the stream has been reached.
096: * @exception IOException If the first byte cannot be read for any reason
097: * other than the end of the file, if the input stream has been closed, or
098: * if some other I/O error occurs.
099: * @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</code> is
199: * negative, no bytes are skipped.
200: *
201: * <p> The <code>skip</code> method of this class creates a
202: * byte array and then repeatedly reads into it until <code>n</code> bytes
203: * have been read or the end of the stream has been reached. Subclasses are
204: * encouraged to provide a more efficient implementation of this method.
205: * For instance, the implementation may depend on the ability to seek.
206: *
207: * @param n the number of bytes to be skipped.
208: * @return the actual number of bytes skipped.
209: * @exception IOException if the stream does not support seek,
210: * or if some other I/O error occurs.
211: */
212: public long skip(long n) throws IOException {
213:
214: long remaining = n;
215: int nr;
216: if (skipBuffer == null)
217: skipBuffer = new byte[SKIP_BUFFER_SIZE];
218:
219: byte[] localSkipBuffer = skipBuffer;
220:
221: if (n <= 0) {
222: return 0;
223: }
224:
225: while (remaining > 0) {
226: nr = read(localSkipBuffer, 0, (int) Math.min(
227: SKIP_BUFFER_SIZE, remaining));
228: if (nr < 0) {
229: break;
230: }
231: remaining -= nr;
232: }
233:
234: return n - remaining;
235: }
236:
237: /**
238: * Returns an estimate of the number of bytes that can be read (or
239: * skipped over) from this input stream without blocking by the next
240: * invocation of a method for this input stream. The next invocation
241: * might be the same thread or another thread. A single read or skip of this
242: * many bytes will not block, but may read or skip fewer bytes.
243: *
244: * <p> Note that while some implementations of {@code InputStream} will return
245: * the total number of bytes in the stream, many will not. It is
246: * never correct to use the return value of this method to allocate
247: * a buffer intended to hold all data in this stream.
248: *
249: * <p> A subclass' implementation of this method may choose to throw an
250: * {@link IOException} if this input stream has been closed by
251: * invoking the {@link #close()} method.
252: *
253: * <p> The {@code available} method for class {@code InputStream} always
254: * returns {@code 0}.
255: *
256: * <p> This method should be overridden by subclasses.
257: *
258: * @return an estimate of the number of bytes that can be read (or skipped
259: * over) from this input stream without blocking or {@code 0} when
260: * it reaches the end of the input stream.
261: * @exception IOException if an I/O error occurs.
262: */
263: public int available() throws IOException {
264: return 0;
265: }
266:
267: /**
268: * Closes this input stream and releases any system resources associated
269: * with the stream.
270: *
271: * <p> The <code>close</code> method of <code>InputStream</code> does
272: * nothing.
273: *
274: * @exception IOException if an I/O error occurs.
275: */
276: public void close() throws IOException {
277: }
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: /**
309: * Repositions this stream to the position at the time the
310: * <code>mark</code> method was last called on this input stream.
311: *
312: * <p> The general contract of <code>reset</code> is:
313: *
314: * <p><ul>
315: *
316: * <li> If the method <code>markSupported</code> returns
317: * <code>true</code>, then:
318: *
319: * <ul><li> If the method <code>mark</code> has not been called since
320: * the stream was created, or the number of bytes read from the stream
321: * since <code>mark</code> was last called is larger than the argument
322: * to <code>mark</code> at that last call, then an
323: * <code>IOException</code> might be thrown.
324: *
325: * <li> If such an <code>IOException</code> is not thrown, then the
326: * stream is reset to a state such that all the bytes read since the
327: * most recent call to <code>mark</code> (or since the start of the
328: * file, if <code>mark</code> has not been called) will be resupplied
329: * to subsequent callers of the <code>read</code> method, followed by
330: * any bytes that otherwise would have been the next input data as of
331: * the time of the call to <code>reset</code>. </ul>
332: *
333: * <li> If the method <code>markSupported</code> returns
334: * <code>false</code>, then:
335: *
336: * <ul><li> The call to <code>reset</code> may throw an
337: * <code>IOException</code>.
338: *
339: * <li> If an <code>IOException</code> is not thrown, then the stream
340: * is reset to a fixed state that depends on the particular type of the
341: * input stream and how it was created. The bytes that will be supplied
342: * to subsequent callers of the <code>read</code> method depend on the
343: * particular type of the input stream. </ul></ul>
344: *
345: * <p>The method <code>reset</code> for class <code>InputStream</code>
346: * does nothing except throw an <code>IOException</code>.
347: *
348: * @exception IOException if this stream has not been marked or if the
349: * mark has been invalidated.
350: * @see java.io.InputStream#mark(int)
351: * @see java.io.IOException
352: */
353: public synchronized void reset() throws IOException {
354: throw new IOException("mark/reset not supported");
355: }
356:
357: /**
358: * Tests if this input stream supports the <code>mark</code> and
359: * <code>reset</code> methods. Whether or not <code>mark</code> and
360: * <code>reset</code> are supported is an invariant property of a
361: * particular input stream instance. The <code>markSupported</code> method
362: * of <code>InputStream</code> returns <code>false</code>.
363: *
364: * @return <code>true</code> if this stream instance supports the mark
365: * and reset methods; <code>false</code> otherwise.
366: * @see java.io.InputStream#mark(int)
367: * @see java.io.InputStream#reset()
368: */
369: public boolean markSupported() {
370: return false;
371: }
372:
373: }
|