📄 javaio.doc4.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.doc3.html">Prev</a> | <a href="javaio.doc5.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<hr><br>
<a name="28355"></a>
<center><h1>22.6 The Class <code>java.io.ByteArrayInputStream</code></h1></center>
<a name="28356"></a>
A <code>ByteArrayInputStream</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>StringBufferInputStream</code> <a href="javaio.doc5.html#28427">(§22.7)</a>.
<p><pre><a name="28360"></a>public class <code><b>ByteArrayInputStream</b></code> extends InputStream {
<a name="28361"></a> protected byte[] <code><b>buf</b></code>;
<a name="28362"></a> protected int <code><b>pos</b></code>;
<a name="28363"></a> protected int <code><b>count</b></code>;
<a name="28364"></a> public <code><b>ByteArrayInputStream</b></code>(byte[] buf);
<a name="28365"></a> public <code><b>ByteArrayInputStream</b></code>(byte[] buf,
<a name="28366"></a> int offset, int length);
<a name="28367"></a> public int <code><b>read</b></code>()
<a name="32314"></a> throws NullPointerException, IndexOutOfBoundsException;
<a name="32305"></a> public int <code><b>read</b></code>(byte[] b, int off, int len)
<a name="32307"></a> throws NullPointerException, IndexOutOfBoundsException;
<a name="28369"></a> public long <code><b>skip</b></code>(long n);
<a name="28370"></a> public int <code><b>available</b></code>();
<a name="28371"></a> public void <code><b>reset</b></code>();
<a name="28372"></a>}
</pre><a name="28373"></a>
<p><font size=+1><strong>22.6.1 </strong> <code>protected byte[] <code><b>buf</b></code>;</code></font>
<p>
<a name="28374"></a>
An array of bytes that was provided by the creator of the stream. Elements <code>buf[0]</code>
through <code>buf[count-1]</code> are the only bytes that can ever be read from the stream;
element <code>buf[pos]</code> is the next byte to be read.
<p><a name="28375"></a>
<p><font size=+1><strong>22.6.2 </strong> <code>protected int <code><b>pos</b></code>;</code></font>
<p>
<a name="28376"></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>buf[pos]</code>.
<p><a name="28377"></a>
<p><font size=+1><strong>22.6.3 </strong> <code>protected int <code><b>count</b></code>;</code></font>
<p>
<a name="28378"></a>
This value should always be nonnegative and not larger than the length of <code>buf</code>. It
is one greater than the position of the last byte within <code>buf</code> that can ever be read
from this stream.
<p><a name="28379"></a>
<p><font size=+1><strong>22.6.4 </strong> <code>public <code><b>ByteArrayInputStream</b></code>(byte[] buf)</code></font>
<p>
<a name="28380"></a>
This constructor initializes a newly created <code>ByteArrayInputStream</code> so that it
uses <code>buf</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>buf</code>.
<p><a name="28381"></a>
<p><font size=+1><strong>22.6.5 </strong> <code>public <code><b>ByteArrayInputStream</b></code>(byte[] buf,<br>int offset, int length)</code></font>
<p>
<a name="28382"></a>
This constructor initializes a newly created <code>ByteArrayInputStream</code> so that it
uses <code>buf</code> as its buffer array. The initial value of <code>pos</code> is <code>offset</code> and the initial value
of <code>count</code> is <code>offset+len</code>.
<p><a name="28383"></a>
Note that if bytes are simply read from the resulting input stream, elements <code>buf[pos]</code> through <code>buf[pos+len-1]</code> will be read; however, if a <code>reset</code> operation <a href="javaio.doc4.html#28415">(§22.6.10)</a> is performed, then bytes <code>buf[0]</code> through b<code>uf[pos-1]</code> will then become available for input.<p>
<a name="28387"></a>
<p><font size=+1><strong>22.6.6 </strong> <code>public int <code><b>read</b></code>()<br>throws NullPointerException,      IndexOutOfBoundsException</code></font>
<p>
<a name="28388"></a>
If <code>pos</code> equals <code>count</code>, then <code>-1</code> is returned to indicate end of file. Otherwise, the
value <code>buf[pos]&0xff</code> is returned; just before the return, <code>pos</code> is incremented by <code>1</code>.
<p><a name="28392"></a>
Implements the <code>read</code> method of <code>InputStream</code> <a href="javaio.doc1.html#28142">(§22.3.1)</a>.<p>
<a name="28393"></a>
<p><font size=+1><strong>22.6.7 </strong> <code>public int <code><b>read</b></code>(byte[] b, int off, int len)<br>throws NullPointerException,      IndexOutOfBoundsException</code></font>
<p>
<a name="28394"></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>buf[pos]</code> through <code>buf[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="28401"></a>
Overrides the <code>read</code> method of <code>InputStream</code> <a href="javaio.doc1.html#28164">(§22.3.3)</a>.<p>
<a name="28402"></a>
<p><font size=+1><strong>22.6.8 </strong> <code>public long <code><b>skip</b></code>(long n)</code></font>
<p>
<a name="28403"></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="28407"></a>
Overrides the <code>skip</code> method of <code>InputStream</code> <a href="javaio.doc1.html#28178">(§22.3.4)</a>.<p>
<a name="28408"></a>
<p><font size=+1><strong>22.6.9 </strong> <code>public int <code><b>available</b></code>()</code></font>
<p>
<a name="28409"></a>
The quantity <code>count-pos</code> is returned.
<p><a name="28413"></a>
Overrides the <code>available</code> method of <code>InputStream</code> <a href="javaio.doc1.html#28182">(§22.3.5)</a>.<p>
<a name="28415"></a>
<p><font size=+1><strong>22.6.10 </strong> <code>public void <code><b>reset</b></code>()</code></font>
<p>
<a name="28416"></a>
The value of <code>pos</code> is set to <code>0</code>.
<p><a name="28420"></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.doc3.html">Prev</a> | <a href="javaio.doc5.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 + -