📄 javaio.doc5.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.doc4.html">Prev</a> | <a href="javaio.doc6.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<hr><br>
<a name="28427"></a>
<center><h1>22.7 The Class <code>java.io.StringBufferInputStream</code></h1></center>
<a name="28428"></a>
A <code>StringBufferInputStream</code> contains an internal buffer that contains bytes that
may be read from the stream. An internal counter keeps track of the next byte to
be supplied by the <code>read</code> method. See also <code>ByteArrayInputStream</code> <a href="javaio.doc4.html#28355">(§22.6)</a>.
<p><pre><a name="28432"></a>public class <code><b>StringBufferInputStream</b></code> extends InputStream {
<a name="28433"></a> protected String <code><b>buffer</b></code>;
<a name="28434"></a> protected int <code><b>pos</b></code>;
<a name="28435"></a> protected int <code><b>count</b></code>;
<a name="28436"></a> public <code><b>StringBufferInputStream</b></code>(String s)
<a name="32317"></a> throws NullPointerException;
<a name="28437"></a> public int <code><b>read</b></code>();
<a name="28438"></a> public int <code><b>read</b></code>(byte[] b, int off, int len)
<a name="32321"></a> throws NullPointerException, IndexOutOfBoundsException;
<a name="28439"></a> public long <code><b>skip</b></code>(long n);
<a name="28440"></a> public int <code><b>available</b></code>();
<a name="28441"></a> public void <code><b>reset</b></code>();
<a name="28442"></a>}
</pre><a name="28443"></a>
Note that bytes read from a <code>StringBufferInputStream</code> are the low-order eight bits of each character in the string; the high-order eight bits of each character are ignored.<p>
<a name="28444"></a>
<p><font size=+1><strong>22.7.1 </strong> <code>protected String <code><b>buffer</b></code>;</code></font>
<p>
<a name="28445"></a>
A <code>String</code> that was provided by the creator of the stream. Elements <code>buffer[0]</code>
through <code>buffer[count-1]</code> are the only bytes that can ever be read from this
stream; element <code>buffer[pos]</code> is the next byte to be read.
<p><a name="28446"></a>
<p><font size=+1><strong>22.7.2 </strong> <code>protected int <code><b>pos</b></code>;</code></font>
<p>
<a name="28447"></a>
This value should always be nonnegative and not larger than the value of <code>count</code>.
The next byte to be read from this stream will be <code>buffer[pos]</code>.
<p><a name="28448"></a>
<p><font size=+1><strong>22.7.3 </strong> <code>protected int <code><b>count</b></code>;</code></font>
<p>
<a name="28449"></a>
This value equals the length of <code>buffer</code>. It is the number of bytes of data in
<code>buffer</code> that can ever be read from this stream.
<p><a name="28450"></a>
<p><font size=+1><strong>22.7.4 </strong> <code>public <code><b>StringBufferInputStream</b></code>(String s)<br>throws NullPointerException</code></font>
<p>
<a name="28451"></a>
This constructor initializes a newly created <code>StringBufferInputStream</code> so that it
uses <code>s</code> as its buffer array. The initial value of <code>pos</code> is <code>0</code> and the initial value of
<code>count</code> is the length of <code>buffer</code>.
<p><a name="28452"></a>
<p><font size=+1><strong>22.7.5 </strong> <code>public int <code><b>read</b></code>()</code></font>
<p>
<a name="28453"></a>
If <code>pos</code> equals <code>count</code>, then <code>-1</code> is returned to indicate end of file. Otherwise, the
value <code>buffer[pos]&0xff</code> is returned; just before the return, <code>1</code> is added to <code>pos</code>.
<p><a name="28457"></a>
Implements the <code>read</code> method of <code>InputStream</code> <a href="javaio.doc1.html#28142">(§22.3.1)</a>.<p>
<a name="28458"></a>
<p><font size=+1><strong>22.7.6 </strong> <code>public int <code><b>read</b></code>(byte[] b, int off, int len)<br>throws NullPointerException,      IndexOutOfBoundsException</code></font>
<p>
<a name="28459"></a>
If <code>pos</code> equals <code>count</code>, then <code>-1</code> is returned to indicate end of file. Otherwise, the
number <code>k</code> of bytes read is equal to the smaller of <code>len</code> and <code>count-pos</code>. If <code>k</code> is positive,
then bytes <code>buffer[pos]</code> through <code>buffer[pos+k-1]</code> are copied into <code>b[off]</code>
through <code>b[off+k-1]</code> in the manner performed by <code>System.arraycopy</code>
<a href="javalang.doc17.html#3211">(§20.18.16)</a>. The value <code>k</code> is added into <code>pos</code> and <code>k</code> is returned.
<p><a name="28466"></a>
Overrides the <code>read</code> method of <code>InputStream</code> <a href="javaio.doc1.html#28164">(§22.3.3)</a>.<p>
<a name="28467"></a>
<p><font size=+1><strong>22.7.7 </strong> <code>public long <code><b>skip</b></code>(long n)</code></font>
<p>
<a name="28468"></a>
The actual number <code>k</code> of bytes to be skipped is equal to the smaller of <code>n</code> and
<code>count-pos</code>. The value <code>k</code> is added into <code>pos</code> and <code>k</code> is returned.
<p><a name="28472"></a>
Overrides the <code>skip</code> method of <code>InputStream</code> <a href="javaio.doc1.html#28178">(§22.3.4)</a>.<p>
<a name="28473"></a>
<p><font size=+1><strong>22.7.8 </strong> <code>public int <code><b>available</b></code>()</code></font>
<p>
<a name="28474"></a>
The quantity <code>count-pos</code> is returned.
<p><a name="28478"></a>
Overrides the <code>available</code> method of <code>InputStream</code> <a href="javaio.doc1.html#28182">(§22.3.5)</a>.<p>
<a name="28479"></a>
<p><font size=+1><strong>22.7.9 </strong> <code>public void <code><b>reset</b></code>()</code></font>
<p>
<a name="28480"></a>
The value of <code>pos</code> is set to <code>0</code>.
<p><a name="28484"></a>
Overrides the <code>reset</code> method of <code>InputStream</code> <a href="javaio.doc1.html#28197">(§22.3.8)</a>.<p>
<hr>
<!-- This inserts footnotes--><p>
<a href="index.html">Contents</a> | <a href="javaio.doc4.html">Prev</a> | <a href="javaio.doc6.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 + -