📄 javaio.doc1.html
字号:
<html>
<head>
<title>The Java Language Specification The Package java.io </title>
</head>
<body BGCOLOR=#eeeeff text=#000000 LINK=#0000ff VLINK=#000077 ALINK=#ff0000>
<a href="index.html">Contents</a> | <a href="javaio.doc.html">Prev</a> | <a href="javaio.doc2.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<hr><br>
<a name="28126"></a>
<center><h1>22.3 The Class <code>java.io.InputStream</code></h1></center>
<a name="33838"></a>
An input stream makes input bytes available from some source.
<p><pre><a name="28127"></a>public abstract class <code><b>InputStream</b></code> {
<a name="28128"></a> public abstract int <code><b>read</b></code>() throws IOException;
<a name="28129"></a> public int <code><b>read</b></code>(byte[] b)
<a name="28130"></a> throws IOException, NullPointerException;
<a name="28131"></a> public int <code><b>read</b></code>(byte[] b, int off, int len)
<a name="28132"></a> throws IOException, NullPointerException,
<a name="28133"></a> IndexOutOfBoundsException;
<a name="28134"></a> public long <code><b>skip</b></code>(long n) throws IOException;
<a name="28135"></a> public int <code><b>available</b></code>() throws IOException;
<a name="28136"></a> public void <code><b>close</b></code>() throws IOException;
<a name="28137"></a> public void <code><b>mark</b></code>(int readlimit);
<a name="28138"></a> public void <code><b>reset</b></code>() throws IOException;
<a name="28139"></a> public boolean <code><b>markSupported</b></code>();
<a name="28140"></a>}
</pre><a name="28142"></a>
<p><font size=+1><strong>22.3.1 </strong> <code>public abstract int <code><b>read</b></code>() throws IOException</code></font>
<p>
<a name="28143"></a>
The general contract of <code>read</code> is that it reads one byte from the input stream. The
byte is returned as an integer in the range <code>0</code> to <code>255</code> (<code>0x00</code>-<code>0xff</code>). If no byte is
available because the stream is at end of file, the value <code>-1</code> is returned.
<p><a name="28144"></a>
This method blocks until input data is available, end of file is detected, or an exception is thrown.<p>
<a name="28145"></a>
If the byte cannot be read for any reason other than end of file, an <code>IOException</code> is thrown. In particular, an <code>IOException</code> is thrown if the input stream has been closed <a href="javaio.doc1.html#28187">(§22.3.6)</a>.<p>
<a name="28150"></a>
<p><font size=+1><strong>22.3.2 </strong> <code>public int <code><b>read</b></code>(byte[] b)<br>throws IOException, NullPointerException</code></font>
<p>
<a name="28151"></a>
The general contract of <code>read(b)</code> is that it reads some number of bytes from the
input stream and stores them into the buffer array <code>b</code>. The number of bytes actually
read is returned as an integer.
<p><a name="28152"></a>
This method blocks until input data is available, end of file is detected, or an exception is thrown.<p>
<a name="28153"></a>
If <code>b</code> is <code>null</code>, a <code>NullPointerException</code> is thrown.<p>
<a name="28154"></a>
If the length of <code>b</code> is zero, then no bytes are read and <code>0</code> is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at end of file, the value <code>-1</code> is returned; otherwise, at least one byte is read and stored into <code>b</code>.<p>
<a name="28155"></a>
The first byte read is stored into element <code>b[0]</code>, the next one into <code>b[1]</code>, and so on. The number of bytes read is, at most, equal to the length of <code>b</code>. Let <i>k</i> be the number of bytes actually read; these bytes will be stored in elements <code>b[0]</code> through <code>b[</code><i>k</i><code>-1]</code>, leaving elements <code>b[</code><i>k</i><code>]</code> through <code>b[b.length-1]</code> unaffected.<p>
<a name="28157"></a>
If the first byte cannot be read for any reason other than end of file, then an <code>IOException</code> is thrown. In particular, an <code>IOException</code> is thrown if the input stream has been closed <a href="javaio.doc13.html#29445">(§22.15.5)</a>.<p>
<a name="28161"></a>
The <code>read(b)</code> method for class <code>InputStream</code> has the same effect as:<p>
<pre><a name="28162"></a>read(b, 0, b.length)
</pre><a name="28164"></a>
<p><font size=+1><strong>22.3.3 </strong> <code>public int <code><b>read</b></code>(byte[] b, int off, int len)<br>throws IOException, <code>NullPointerException</code>,<br>      IndexOutOfBoundsException</code></font>
<p>
<a name="28165"></a>
The general contract of <code>read(b, off, len)</code> is that it reads some number of
bytes from the input stream and stores them into the buffer array <code>b</code>. An attempt is
made to read as many as <code>len</code> bytes, but a smaller number may be read, possibly
zero. The number of bytes actually read is returned as an integer.
<p><a name="28166"></a>
This method blocks until input data is available, end of file is detected, or an exception is thrown.<p>
<a name="28167"></a>
If <code>b</code> is <code>null</code>, a <code>NullPointerException</code> is thrown.<p>
<a name="28168"></a>
If <code>off</code> is negative, or <code>len</code> is negative, or <code>off+len</code> is greater than the length of the array <code>b</code>, then an <code>IndexOutOfBoundsException</code> is thrown.<p>
<a name="28169"></a>
If <code>len</code> is zero, then no bytes are read and <code>0</code> is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at end of file, the value <code>-1</code> is returned; otherwise, at least one byte is read and stored into <code>b</code>.<p>
<a name="28170"></a>
The first byte read is stored into element <code>b[off]</code>, the next one into <code>b[off+1]</code>, and so on. The number of bytes read is, at most, equal to <code>len</code>. Let <i>k</i> be the number of bytes actually read; these bytes will be stored in elements <code>b[off]</code> through <code>b[off+</code><i>k</i><code>-1]</code>, leaving elements <code>b[off+</code><i>k</i><code>]</code> through <code>b[off+len-1]</code> unaffected.<p>
<a name="28171"></a>
In every case, elements <code>b[0]</code> through <code>b[off]</code> and elements <code>b[off+len]</code> through <code>b[b.length-1]</code> are unaffected. <p>
<a name="28172"></a>
If the first byte cannot be read for any reason other than end of file, then an <code>IOException</code> is thrown. In particular, an <code>IOException</code> is thrown if the input stream has been closed <a href="javaio.doc13.html#29445">(§22.15.5)</a>.<p>
<a name="28176"></a>
The <code>read(b,</code> <code>off,</code> <code>len)</code> method for class <code>InputStream</code> simple calls the method <code>read()</code> repeatedly. If the first such call results in an <code>IOException</code>, that exception is returned from the call to the <code>read(b,</code> <code>off,</code> <code>len)</code> method. If any subsequent call to <code>read()</code> results in a <code>IOException</code>, the exception is caught and treated as if it were end of file; the bytes read up to that point are stored into <code>b</code> and the number of bytes read before the exception occurred is returned.<p>
<a name="28178"></a>
<p><font size=+1><strong>22.3.4 </strong> <code>public long <code><b>skip</b></code>(long n) throws IOException</code></font>
<p>
<a name="28179"></a>
The general contract of <code>skip</code> is that it makes an attempt to skip over <code>n</code> bytes of
data from the input stream, discarding the skipped bytes. However, it may skip
over some smaller number of bytes, possibly zero. This may result from any of a
number of conditions; reaching end of file before <code>n</code> bytes have been skipped is
only one possibility. The actual number of bytes skipped is returned.
<p><a name="28182"></a>
<p><font size=+1><strong>22.3.5 </strong> <code>public int <code><b>available</b></code>() throws IOException</code></font>
<p>
<a name="28183"></a>
The general contract of <code>available</code> is that it returns an integer <code>k</code>; the next caller of
a method for this input stream, which might be the same thread or another thread,
can then expect to be able to read or skip up to <code>k</code> bytes without blocking (waiting
for input data to arrive).
<p><a name="28184"></a>
The <code>available</code> method for class <code>InputStream</code> always returns <code>0</code>.<p>
<a name="28187"></a>
<p><font size=+1><strong>22.3.6 </strong> <code>public int <code><b>close</b></code>() throws IOException</code></font>
<p>
<a name="28188"></a>
The general contract of <code>close</code> is that it closes the input stream. A closed stream
cannot perform input operations and cannot be reopened.
<p><a name="28189"></a>
The <code>close</code> method for class <code>InputStream</code> does nothing and simply returns.<p>
<a name="28192"></a>
<p><font size=+1><strong>22.3.7 </strong> <code>public void <code><b>mark</b></code>(int readlimit)</code></font>
<p>
<a name="28193"></a>
The general contract of <code>mark</code> is that, if the method <code>markSupported</code> returns <code>true</code>,
the stream somehow remembers all the bytes read after the call to <code>mark</code> and stands
ready to supply those same bytes again if and whenever the method <code>reset</code> is
called. However, the stream is not required to remember any data at all if more
than <code>readlimit</code> bytes are read from the stream before <code>reset</code> is called.
<p><a name="28194"></a>
The <code>mark</code> method for class <code>InputStream</code> does nothing.<p>
<a name="28197"></a>
<p><font size=+1><strong>22.3.8 </strong> <code>public void <code><b>reset</b></code>() throws IOException</code></font>
<p>
<a name="28198"></a>
The general contract of <code>reset</code> is:
<p><ul><a name="28199"></a>
<li>If the method <code>markSupported</code> returns <code>true</code>, then:
<ul>
<a name="28200"></a>
<li>If the method <code>mark</code> has not been called since the stream was created, or the number of bytes read from the stream since <code>mark</code> was last called is larger than the argument to <code>mark</code> at that last call, then an <code>IOException</code> might be thrown.
<a name="28201"></a>
<li>If such an <code>IOException</code> is not thrown, then the stream is reset to a state such that all the bytes read since the most recent call to <code>mark</code> (or since the start of the file, if <code>mark</code> has not been called) will be resupplied to subsequent callers of the <code>read</code> method, followed by any bytes that otherwise would have been the next input data as of the time of the call to <code>reset</code>.
</ul>
<a name="28202"></a>
<li>If the method <code>markSupported</code> returns <code>false</code>, then:
<ul>
<a name="28203"></a>
<li>The call to <code>reset</code> may throw an <code>IOException</code>.
<a name="28204"></a>
<li>If an <code>IOException</code> is not thrown, then the stream is reset to a fixed state that depends on the particular type of the input stream and how it was created. The bytes that will be supplied to subsequent callers of the <code>read</code> method depend on the particular type of the input stream.
</ul>
</ul><a name="28205"></a>
The method <code>reset</code> for class <code>InputStream</code> always throws an <code>IOException</code>.<p>
<a name="28208"></a>
<p><font size=+1><strong>22.3.9 </strong> <code>public boolean <code><b>markSupported</b></code>()</code></font>
<p>
<a name="28209"></a>
The general contract of <code>markSupported</code> is that if it returns <code>true</code>, then the stream
supports the <code>mark</code> <a href="javaio.doc1.html#28192">(§22.3.7)</a> and <code>reset</code> <a href="javaio.doc1.html#28197">(§22.3.8)</a> operations. For any given
instance of <code>InputStream</code>, this method should consistently return the same truth
value whenever it is called.
<p><a name="28216"></a>
The <code>markSupported</code> method for class <code>InputStream</code> returns <code>false</code>.<p>
<hr>
<!-- This inserts footnotes--><p>
<a href="index.html">Contents</a> | <a href="javaio.doc.html">Prev</a> | <a href="javaio.doc2.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<p>
<font size=-1>Java Language Specification (HTML generated by Suzette Pelouch on February 24, 1998)<br>
<i><a href="jcopyright.doc.html">Copyright © 1996 Sun Microsystems, Inc.</a>
All rights reserved</i>
<br>
Please send any comments or corrections to <a href="mailto:doug.kramer@sun.com">doug.kramer@sun.com</a>
</font>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -