ByteArrayOutputStreamクラスはbyte配列のストリームへ書きこむ出力ストリームで,OutputStreamクラスを継承して実装されている。バッファとしてbyte[]型のインスタンス変数buf,そして,バッファに書き込まれたサイズを示すint型のインスタンス変数countが宣言されている。
コンストラクタはByteArrayOutputStream()と,バッファサイズを指定するByteArrayOutputStream(int size)が実装されている。ディフォルトのバッファサイズは32である。バッファサイズが不足すると,grow(int minCapacity)メソッドでバッファを拡張する。新しいバッファサイズはint newCapacity = oldCapacity << 1という式で算出されているので,1bitの左シフト,すなわち,2倍である。
書き込むためのメソッドとしては,write(int b),write(byte b[], int off, int len),writeBytes(byte b[])が実装されている。
write(int b) メソッドは引数のint値をbyte型にキャストして書き込む。 write(byte b[], int off, int len) メソッドは引数で渡されたbyte[]型の配列を指定のオフセットから指定の長さだけ書き込む。このメソッド内部はSystem.arraycopyメソッドを用いて配列をコピーしている。writeBytes(byte b[])メソッドは引数で与えられたbyte[]型の配列を先頭からすべて書き込むようにwrite()メソッドを呼び出している。
writeTo(OutputStream out)メソッドでは,引数で与えられたOutputStreamに対して,バッファの内容を書き出す。 toByteArray()メソッドではバッファの内容をbyte[]型配列として返す。
size()メソッドでは,バッファ内に書き込まれているサイズを返す。また,toString()メソッドではバッファ内のbyte[]配列からStringのインスタンスを作成して返す。toString(String charsetName)メソッドでは,指定の文字セットを使ってバッファ内のbyte[]配列をStringに変換して返す。
* Copyright (c) 1994, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.io;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Objects;
public class ByteArrayOutputStream extends OutputStream {
protected byte buf[];
protected int count;
public ByteArrayOutputStream() {
this(32);
}
public ByteArrayOutputStream(int size) {
if (size < 0) {
throw new IllegalArgumentException("Negative initial size: "
+ size);
}
buf = new byte[size];
}
private void ensureCapacity(int minCapacity) {
// overflow-conscious code
if (minCapacity – buf.length > 0)
grow(minCapacity);
}
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE – 8;
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = buf.length;
int newCapacity = oldCapacity << 1;
if (newCapacity – minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity – MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
buf = Arrays.copyOf(buf, newCapacity);
}
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
public synchronized void write(int b) {
ensureCapacity(count + 1);
buf[count] = (byte) b;
count += 1;
}
public synchronized void write(byte b[], int off, int len) {
Objects.checkFromIndexSize(off, len, b.length);
ensureCapacity(count + len);
System.arraycopy(b, off, buf, count, len);
count += len;
}
public void writeBytes(byte b[]) {
write(b, 0, b.length);
}
public synchronized void writeTo(OutputStream out) throws IOException {
out.write(buf, 0, count);
}
public synchronized void reset() {
count = 0;
}
public synchronized byte[] toByteArray() {
return Arrays.copyOf(buf, count);
}
public synchronized int size() {
return count;
}
public synchronized String toString() {
return new String(buf, 0, count);
}
public synchronized String toString(String charsetName)
throws UnsupportedEncodingException
{
return new String(buf, 0, count, charsetName);
}
public synchronized String toString(Charset charset) {
return new String(buf, 0, count, charset);
}
@Deprecated
public synchronized String toString(int hibyte) {
return new String(buf, hibyte, 0, count);
}
public void close() throws IOException {
}
}