📄 javaio.doc16.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.doc15.html">Prev</a> | <a href="javaio.doc17.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<hr><br>
<a name="29558"></a>
<center><h1>22.18 The Class <code>java.io.ByteArrayOutputStream</code></h1></center>
<a name="29559"></a>
A <code>ByteArrayOutputStream</code> contains an internal buffer that accumulates all the
bytes written to the stream since its creation or the most recent call to the <code>reset</code>
method. At any point, the bytes written to the stream so far may be retrieved in the
form of an array of bytes or a <code>String</code>. The bytes written so far may also be copied
to some other output stream. The <code>size</code> method returns the number of characters
written so far.
<p><pre><a name="29560"></a>public class <code><b>ByteArrayOutputStream</b></code> extends OutputStream {
<a name="29561"></a> protected byte[] <code><b>buf</b></code>;
<a name="29562"></a> protected int <code><b>count</b></code>;
<a name="29563"></a> public <code><b>ByteArrayOutputStream</b></code>();
<a name="29564"></a> public <code><b>ByteArrayOutputStream</b></code>(int size);
<a name="29565"></a> public void <code><b>write</b></code>(int b);
<a name="29566"></a> public void <code><b>write</b></code>(byte[] b, int off, int len)
<a name="32417"></a> throws NullPointerException, IndexOutOfBoundsException;
<a name="29567"></a> public int <code><b>size</b></code>();
<a name="29568"></a> public void <code><b>reset</b></code>();
<a name="29569"></a> public byte[] <code><b>toByteArray</b></code>();
<a name="29570"></a> public String <code><b>toString</b></code>();
<a name="29571"></a> public String <code><b>toString</b></code>(int hibyte);
<a name="29572"></a> public void <code><b>writeTo</b></code>(OutputStream out) throws IOException;
<a name="29573"></a>}
</pre><a name="29574"></a>
<p><font size=+1><strong>22.18.1 </strong> <code>protected byte[] <code><b>buf</b></code>;</code></font>
<p>
<a name="29575"></a>
An internal array of bytes. Elements <code>buf[0]</code> through <code>buf[count-1]</code> are the bytes
that have been written to the stream since its creation or the last <code>reset</code> <a href="javaio.doc21.html#27817">(§22.18.8)</a>
operation.
<p><a name="29579"></a>
<p><font size=+1><strong>22.18.2 </strong> <code>protected int <code><b>count</b></code>;</code></font>
<p>
<a name="29580"></a>
This value should always be nonnegative. It is the number of bytes that have been
written to the stream since its creation or the last <code>reset</code> <a href="javaio.doc21.html#27817">(§22.18.8)</a> operation.
<p><a name="29584"></a>
<p><font size=+1><strong>22.18.3 </strong> <code>public <code><b>ByteArrayOutputStream</b></code>()</code></font>
<p>
<a name="29585"></a>
This constructor initializes a newly created <code>ByteArrayOutputStream</code> so that its
internal buffer array has length 32.
<p><a name="29586"></a>
<p><font size=+1><strong>22.18.4 </strong> <code>public <code><b>ByteArrayOutputStream</b></code>(int size)</code></font>
<p>
<a name="29587"></a>
This constructor initializes a newly created <code>ByteArrayOutputStream</code> so that its
internal buffer array has length <code>size</code>. This matters only for reasons of efficiency;
the buffer array is replaced by a larger one whenever necessary to accommodate
additional bytes written to the stream.
<p><a name="29588"></a>
<p><font size=+1><strong>22.18.5 </strong> <code>public void <code><b>write</b></code>(int b)</code></font>
<p>
<a name="29589"></a>
One byte is added on the internal buffer. The byte to be added is the eight low-
order bits of the argument <code>n</code>. The 24 high-order bits of <code>n</code> are ignored.
<p><a name="29593"></a>
Implements the <code>write</code> method of <code>OutputStream</code> <a href="javaio.doc13.html#29411">(§22.15.1)</a>.<p>
<a name="29594"></a>
<p><font size=+1><strong>22.18.6 </strong> <code>public void <code><b>write</b></code>(byte[] b, int off, int len)<br>throws NullPointerException,      IndexOutOfBoundsException</code></font>
<p>
<a name="29595"></a>
Elements <code>b[off]</code> through <code>b[off+len-1]</code> are appended to the internal buffer.
<p><a name="29596"></a>
If <code>b</code> is <code>null</code>, a <code>NullPointerException</code> is thrown.<p>
<a name="29597"></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="29601"></a>
Overrides the <code>write</code> method of <code>OutputStream</code> <a href="javaio.doc13.html#29426">(§22.15.3)</a>.<p>
<a name="29602"></a>
<p><font size=+1><strong>22.18.7 </strong> <code>public int <code><b>size</b></code>()</code></font>
<p>
<a name="29603"></a>
The current value of <code>count</code> is returned.
<p><a name="29605"></a>
<p><font size=+1><strong>22.18.8 </strong> <code>public void <code><b>reset</b></code>()</code></font>
<p>
<a name="29606"></a>
The internal variable <code>count</code> is reset to zero, thereby logically discarding all bytes
written to the stream so far. However, the internal buffer array, which may be quite
large, remains as it is.
<p><a name="29607"></a>
<p><font size=+1><strong>22.18.9 </strong> <code>public byte[] <code><b>toByteArray</b></code>()</code></font>
<p>
<a name="29608"></a>
A new array of bytes is created and returned. Its length is equal to the current
value of <code>count</code>. Its initial contents are copies of the bytes written to the stream so
far-that is, elements <code>0</code> through <code>count-1</code> of <code>buf</code>.
<p><a name="29609"></a>
<p><font size=+1><strong>22.18.10 </strong> <code>public String <code><b>toString</b></code>()</code></font>
<p>
<a name="29610"></a>
A new <code>String</code> is created and returned. Its length is equal to the current value of
<code>count</code>. Its initial contents are copies of the bytes written to the stream so far-that
is, elements <code>0</code> through <code>count-1</code> of <code>buf</code>, zero-extended to produce characters.
Thus, <code>tostring()</code> has the same effect as <code>toString(0)</code> <a href="javaio.doc16.html#29615">(§22.18.11)</a>.
<p><a name="29614"></a>
Overrides the <code>toString</code> method of <code>Object</code> <a href="javalang.doc1.html#1152">(§20.1.2)</a>.<p>
<a name="29615"></a>
<p><font size=+1><strong>22.18.11 </strong> <code>public String <code><b>toString</b></code>(int hibyte)</code></font>
<p>
<a name="29616"></a>
A new array of bytes is created and returned. Its length is equal to the current
value of <code>count</code>. Its initial contents are copies of the bytes written to the stream so
far-that is, elements <code>0</code> through <code>count-1</code> of <code>buf</code>-with <code>hibyte</code> supplying the
high-order eight bits of each character. Thus, character <code>k</code> of the result is equal to:
<p><pre><a name="29617"></a>((hibyte & 0xff) << 8) | (buf[k] & 0xff)
</pre><a name="29621"></a>
See the <code>String</code> constructor that accepts a <code>hibyte</code> argument <a href="javalang.doc11.html#4041">(§20.12.6)</a>.<p>
<a name="29622"></a>
<p><font size=+1><strong>22.18.12 </strong> <code>public void <code><b>writeTo</b></code>(OutputStream out) throws IOException</code></font>
<p>
<a name="29623"></a>
The current contents of the internal buffer are written to the output stream <code>out</code> by
the call:
<p><pre><a name="29624"></a>out.write(buf, 0, count)
</pre><a name="29625"></a>
Note that if <code>out</code> is the same as <code>this</code>, the effect is simply to append to the buffer a copy of its current contents, thereby doubling the number of buffered bytes. This may not be a particularly useful effect; the point is merely that the operation does terminate, having had a sensible effect, rather than running off into an endless loop.<p>
<hr>
<!-- This inserts footnotes--><p>
<a href="index.html">Contents</a> | <a href="javaio.doc15.html">Prev</a> | <a href="javaio.doc17.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 + -