📄 javaio.doc11.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.doc10.html">Prev</a> | <a href="javaio.doc12.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<hr><br>
<a name="29220"></a>
<center><h1>22.13 The Class <code>java.io.PushbackInputStream</code></h1></center>
<a name="29221"></a>
A <code>PushbackInputStream</code> adds functionality to another input stream, namely the
ability to "push back" or "unread" one byte. This is useful in situations where it is
convenient for a fragment of code to read an indefinite number of data bytes that
are delimited by a particular byte value; after reading the terminating byte, the
code fragment can "unread" it, so that the next read operation on the input stream
will reread the byte that was pushed back. For example, bytes representing the
characters constituting an identifier might be terminated by a byte representing an
operator character; a method whose job is to read just an identifier can read until it
sees the operator and then push the operator back to be re-read.
<p><pre><a name="29222"></a>public class <code><b>PushbackInputStream</b></code> extends FilterInputStream {
<a name="29223"></a> protected int <code><b>pushBack</b></code> = -1;
<a name="29224"></a> public <code><b>PushbackInputStream</b></code>(InputStream in);
<a name="29225"></a> public int <code><b>read</b></code>() throws IOException;
<a name="29226"></a> public int <code><b>read</b></code>(byte[] bytes, int offset, int length)
<a name="29227"></a> throws IOException, NullPointerException,
<a name="32376"></a> IndexOutOfBoundsException;
<a name="29228"></a> public void <code><b>unread</b></code>(int ch) throws IOException;
<a name="29229"></a> public int <code><b>available</b></code>() throws IOException;
<a name="29230"></a> public boolean <code><b>markSupported</b></code>();
<a name="29231"></a>}
</pre><a name="29232"></a>
<p><font size=+1><strong>22.13.1 </strong> <code>protected int <code><b>pushBack</b></code> = -1;</code></font>
<p>
<a name="29233"></a>
If this field has a nonnegative value, it is a byte that was pushed back. If this field
is <code>-1</code>, there is currently no pushed-back byte.
<p><a name="29234"></a>
<p><font size=+1><strong>22.13.2 </strong> <code>public <code><b>PushbackInputStream</b></code>(InputStream in)</code></font>
<p>
<a name="29235"></a>
This constructor initializes a newly created <code>PushbackInputStream</code> by saving its
argument, the input stream <code>in</code>, for later use. Initially, there is no pushed-back byte
(the field <code>pushBack</code> is initialized to <code>-1</code>).
<p><a name="29239"></a>
<p><font size=+1><strong>22.13.3 </strong> <code>public int <code><b>read</b></code>() throws IOException</code></font>
<p>
<a name="29243"></a>
See the general contract of the <code>read</code> method of <code>InputStream</code> <a href="javaio.doc1.html#28142">(§22.3.1)</a>.
<p><a name="29244"></a>
If <code>pushBack</code> is not <code>-1</code>, the value of <code>pushBack</code> is returned and <code>pushBack</code> is set to <code>-1</code>. Otherwise, a byte is obtained from the contained input stream.<p>
<a name="29248"></a>
Overrides the <code>read</code> method of <code>FilterInputStream</code> <a href="javaio.doc7.html#28556">(§22.9.3)</a>.<p>
<a name="29259"></a>
<p><font size=+1><strong>22.13.4 </strong> <code>public int <code><b>read</b></code>(byte[] bytes, int offset, int length) throws IOException, NullPointerException,      IndexOutOfBoundsException</code></font>
<p>
<a name="29263"></a>
See the general contract of the <code>read</code> method of <code>InputStream</code> <a href="javaio.doc1.html#28164">(§22.3.3)</a>.
<p><a name="29264"></a>
If <code>pushBack</code> is not <code>-1</code>, it is used as an input byte (and <code>pushBack</code> is set to <code>-1</code>) before any bytes are read from the contained input stream.<p>
<a name="29268"></a>
Overrides the <code>read</code> method of <code>FilterInputStream</code> <a href="javaio.doc18.html#24613">(§22.9.5)</a>.<p>
<a name="29269"></a>
<p><font size=+1><strong>22.13.5 </strong> <code>public void <code><b>unread</b></code>(int b) throws IOException</code></font>
<p>
<a name="29270"></a>
If <code>pushBack</code> is not <code>-1</code>, an <code>IOException</code> is thrown (it is not permitted to push back
more than one byte). Otherwise, the byte value <code>b</code> is pushed back by assigning <code>b</code> to
<code>pushBack</code>.
<p><a name="29271"></a>
<p><font size=+1><strong>22.13.6 </strong> <code>public int <code><b>available</b></code>() throws IOException</code></font>
<p>
<a name="29275"></a>
See the general contract of the <code>available</code> method of <code>InputStream</code> <a href="javaio.doc1.html#28142">(§22.3.1)</a>.
<p><a name="32648"></a>
This method first calls the <code>available</code> method of the contained input stream. If <code>pushBack</code> is <code>-1</code>, the result is returned; otherwise, the result plus <code>1</code> is returned.<p>
<a name="29279"></a>
Overrides the <code>available</code> method of <code>FilterInputStream</code> <a href="javaio.doc7.html#28584">(§22.9.7)</a>.<p>
<a name="29280"></a>
<p><font size=+1><strong>22.13.7 </strong> <code>public boolean <code><b>markSupported</b></code>()</code></font>
<p>
<a name="29281"></a>
This method returns <code>false</code> (a <code>PushbackInputStream</code> does not support <code>mark</code>).
<p>
<hr>
<!-- This inserts footnotes--><p>
<a href="index.html">Contents</a> | <a href="javaio.doc10.html">Prev</a> | <a href="javaio.doc12.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 + -