Java : ByteArrayInputStream - API使用例
ByteArrayInputStream (Java SE 21 & JDK 21) の使い方まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
概要
ByteArrayInputStream は Closeable を実装しますが、close を呼び出さなくても影響はありません。
(詳細は API仕様書をご参照ください)
本記事でも close の呼び出しは基本的に省いています。
final byte[] buf = {10, 20, 30, 40, 50};
final var is = new ByteArrayInputStream(buf);
System.out.println(is.read()); // 10
System.out.println(is.read()); // 20
System.out.println(Arrays.toString(is.readAllBytes())); // [30, 40, 50]
フィールド
protected byte[] buf
protectedです。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。
protected int count
protectedです。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。
protected int mark
protectedです。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。
protected int pos
protectedです。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。
コンストラクタ
ByteArrayInputStream (byte[] buf)
final byte[] buf = {10, 20, 30, 40, 50};
final var is = new ByteArrayInputStream(buf);
System.out.println(is.read()); // 10
System.out.println(is.read()); // 20
System.out.println(Arrays.toString(is.readAllBytes())); // [30, 40, 50]
ByteArrayInputStream (byte[] buf, int offset, int length)
final byte[] buf = {10, 20, 30, 40};
final var is1 = new ByteArrayInputStream(buf, 0, 0);
System.out.println(Arrays.toString(is1.readAllBytes())); // []
final var is2 = new ByteArrayInputStream(buf, 0, 1);
System.out.println(Arrays.toString(is2.readAllBytes())); // [10]
final var is3 = new ByteArrayInputStream(buf, 0, 2);
System.out.println(Arrays.toString(is3.readAllBytes())); // [10, 20]
final var is4 = new ByteArrayInputStream(buf, 0, 3);
System.out.println(Arrays.toString(is4.readAllBytes())); // [10, 20, 30]
final var is5 = new ByteArrayInputStream(buf, 0, 4);
System.out.println(Arrays.toString(is5.readAllBytes())); // [10, 20, 30, 40]
final var is6 = new ByteArrayInputStream(buf, 0, 5);
System.out.println(Arrays.toString(is6.readAllBytes())); // [10, 20, 30, 40]
final byte[] buf = {10, 20, 30, 40};
final var is1 = new ByteArrayInputStream(buf, 0, 4);
System.out.println(Arrays.toString(is1.readAllBytes())); // [10, 20, 30, 40]
final var is2 = new ByteArrayInputStream(buf, 1, 3);
System.out.println(Arrays.toString(is2.readAllBytes())); // [20, 30, 40]
final var is3 = new ByteArrayInputStream(buf, 2, 2);
System.out.println(Arrays.toString(is3.readAllBytes())); // [30, 40]
final var is4 = new ByteArrayInputStream(buf, 3, 1);
System.out.println(Arrays.toString(is4.readAllBytes())); // [40]
final var is5 = new ByteArrayInputStream(buf, 4, 0);
System.out.println(Arrays.toString(is5.readAllBytes())); // []
メソッド
int available ()
final byte[] buf = {10, 20, 30, 40, 50};
final var is = new ByteArrayInputStream(buf);
System.out.println(is.available()); // 5
System.out.println(is.read()); // 10
System.out.println(is.available()); // 4
System.out.println(is.read()); // 20
System.out.println(is.available()); // 3
System.out.println(is.read()); // 30
System.out.println(is.available()); // 2
System.out.println(is.read()); // 40
System.out.println(is.available()); // 1
System.out.println(is.read()); // 50
System.out.println(is.available()); // 0
System.out.println(is.read()); // -1
void close ()
final byte[] buf = {10, 20, 30, 40, 50};
final var is = new ByteArrayInputStream(buf);
// なんの影響もありません。
is.close();
// close後も各メソッドを呼び出せます。
System.out.println(Arrays.toString(is.readAllBytes())); // [10, 20, 30, 40, 50]
void mark (int readAheadLimit)
final byte[] buf = {10, 20, 30, 40, 50};
final var is = new ByteArrayInputStream(buf);
System.out.println(is.read()); // 10
System.out.println(is.read()); // 20
System.out.println(is.read()); // 30
// ByteArrayInputStream の readAheadLimit には意味はありません。
is.mark(0);
System.out.println(is.read()); // 40
System.out.println(is.read()); // 50
System.out.println(is.read()); // -1
is.reset();
System.out.println(is.read()); // 40
System.out.println(is.read()); // 50
System.out.println(is.read()); // -1
boolean markSupported ()
final byte[] buf = {10, 20, 30, 40, 50};
final var is = new ByteArrayInputStream(buf);
System.out.println(is.markSupported()); // true
int read ()
final byte[] buf = {10, 20, 30, 40, 50};
final var is = new ByteArrayInputStream(buf);
System.out.println(is.read()); // 10
System.out.println(is.read()); // 20
System.out.println(is.read()); // 30
System.out.println(is.read()); // 40
System.out.println(is.read()); // 50
System.out.println(is.read()); // -1
int read (byte[] b, int off, int len)
final byte[] buf = {10, 20, 30, 40, 50};
final byte[] b = new byte[10];
final var is = new ByteArrayInputStream(buf);
System.out.println(Arrays.toString(b)); // [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
System.out.println(is.read(b, 0, 10)); // 5
System.out.println(Arrays.toString(b)); // [10, 20, 30, 40, 50, 0, 0, 0, 0, 0]
final byte[] buf = {10, 20, 30, 40, 50, 60, 70};
final byte[] b = new byte[5];
final var is = new ByteArrayInputStream(buf);
System.out.println(Arrays.toString(b)); // [0, 0, 0, 0, 0]
System.out.println(is.read(b, 0, 1)); // 1
System.out.println(Arrays.toString(b)); // [10, 0, 0, 0, 0]
System.out.println(is.read(b, 0, 2)); // 2
System.out.println(Arrays.toString(b)); // [20, 30, 0, 0, 0]
System.out.println(is.read(b, 0, 3)); // 3
System.out.println(Arrays.toString(b)); // [40, 50, 60, 0, 0]
System.out.println(is.read(b, 0, 4)); // 1
System.out.println(Arrays.toString(b)); // [70, 50, 60, 0, 0]
System.out.println(is.read(b, 0, 5)); // -1
System.out.println(Arrays.toString(b)); // [70, 50, 60, 0, 0]
final byte[] buf = {10, 20, 30, 40, 50};
final byte[] b = new byte[5];
final var is = new ByteArrayInputStream(buf);
System.out.println(Arrays.toString(b)); // [0, 0, 0, 0, 0]
System.out.println(is.read(b, 4, 1)); // 1
System.out.println(Arrays.toString(b)); // [0, 0, 0, 0, 10]
System.out.println(is.read(b, 3, 1)); // 1
System.out.println(Arrays.toString(b)); // [0, 0, 0, 20, 10]
System.out.println(is.read(b, 2, 1)); // 1
System.out.println(Arrays.toString(b)); // [0, 0, 30, 20, 10]
System.out.println(is.read(b, 1, 1)); // 1
System.out.println(Arrays.toString(b)); // [0, 40, 30, 20, 10]
System.out.println(is.read(b, 0, 1)); // 1
System.out.println(Arrays.toString(b)); // [50, 40, 30, 20, 10]
final byte[] buf = {10, 20, 30, 40, 50, 60, 70};
final byte[] b = new byte[3];
final var is = new ByteArrayInputStream(buf);
System.out.println(Arrays.toString(b)); // [0, 0, 0]
System.out.println(is.read(b, 0, 3)); // 3
System.out.println(Arrays.toString(b)); // [10, 20, 30]
//is.read(b, 0, 4); // IndexOutOfBoundsException
//is.read(b, 3, 1); // IndexOutOfBoundsException
byte[] readAllBytes ()
final byte[] buf = {10, 20, 30, 40, 50};
final var is = new ByteArrayInputStream(buf);
final var ret = is.readAllBytes();
System.out.println(Arrays.toString(ret)); // [10, 20, 30, 40, 50]
int readNBytes (byte[] b, int off, int len)
final byte[] buf = {10, 20, 30, 40, 50};
final byte[] b = new byte[10];
final var is = new ByteArrayInputStream(buf);
System.out.println(Arrays.toString(b)); // [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
System.out.println(is.readNBytes(b, 0, 10)); // 5
System.out.println(Arrays.toString(b)); // [10, 20, 30, 40, 50, 0, 0, 0, 0, 0]
final byte[] buf = {10, 20, 30, 40, 50, 60, 70};
final byte[] b = new byte[5];
final var is = new ByteArrayInputStream(buf);
System.out.println(Arrays.toString(b)); // [0, 0, 0, 0, 0]
System.out.println(is.readNBytes(b, 0, 1)); // 1
System.out.println(Arrays.toString(b)); // [10, 0, 0, 0, 0]
System.out.println(is.readNBytes(b, 0, 2)); // 2
System.out.println(Arrays.toString(b)); // [20, 30, 0, 0, 0]
System.out.println(is.readNBytes(b, 0, 3)); // 3
System.out.println(Arrays.toString(b)); // [40, 50, 60, 0, 0]
System.out.println(is.readNBytes(b, 0, 4)); // 1
System.out.println(Arrays.toString(b)); // [70, 50, 60, 0, 0]
System.out.println(is.readNBytes(b, 0, 5)); // 0
System.out.println(Arrays.toString(b)); // [70, 50, 60, 0, 0]
final byte[] buf = {10, 20, 30, 40, 50};
final byte[] b = new byte[5];
final var is = new ByteArrayInputStream(buf);
System.out.println(Arrays.toString(b)); // [0, 0, 0, 0, 0]
System.out.println(is.readNBytes(b, 4, 1)); // 1
System.out.println(Arrays.toString(b)); // [0, 0, 0, 0, 10]
System.out.println(is.readNBytes(b, 3, 1)); // 1
System.out.println(Arrays.toString(b)); // [0, 0, 0, 20, 10]
System.out.println(is.readNBytes(b, 2, 1)); // 1
System.out.println(Arrays.toString(b)); // [0, 0, 30, 20, 10]
System.out.println(is.readNBytes(b, 1, 1)); // 1
System.out.println(Arrays.toString(b)); // [0, 40, 30, 20, 10]
System.out.println(is.readNBytes(b, 0, 1)); // 1
System.out.println(Arrays.toString(b)); // [50, 40, 30, 20, 10]
final byte[] buf = {10, 20, 30, 40, 50, 60, 70};
final byte[] b = new byte[3];
final var is = new ByteArrayInputStream(buf);
System.out.println(Arrays.toString(b)); // [0, 0, 0]
System.out.println(is.readNBytes(b, 0, 3)); // 3
System.out.println(Arrays.toString(b)); // [10, 20, 30]
//is.readNBytes(b, 0, 4); // IndexOutOfBoundsException
//is.readNBytes(b, 3, 1); // IndexOutOfBoundsException
void reset ()
final byte[] buf = {10, 20, 30, 40, 50};
final var is = new ByteArrayInputStream(buf);
System.out.println(is.read()); // 10
System.out.println(is.read()); // 20
System.out.println(is.read()); // 30
// ByteArrayInputStream の readAheadLimit には意味はありません。
is.mark(0);
System.out.println(is.read()); // 40
System.out.println(is.read()); // 50
System.out.println(is.read()); // -1
is.reset();
System.out.println(is.read()); // 40
System.out.println(is.read()); // 50
System.out.println(is.read()); // -1
long skip (long n)
final byte[] buf = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
final var is = new ByteArrayInputStream(buf);
System.out.println(is.read()); // 10
System.out.println(is.skip(1)); // 1
System.out.println(is.read()); // 30
System.out.println(is.skip(2)); // 2
System.out.println(is.read()); // 60
System.out.println(is.skip(3)); // 3
System.out.println(is.read()); // 100
System.out.println(is.skip(1)); // 0
System.out.println(is.read()); // -1
final byte[] buf = {10, 20, 30, 40, 50};
final var is = new ByteArrayInputStream(buf);
System.out.println(is.read()); // 10
System.out.println(is.skip(10)); // 4
System.out.println(is.read()); // -1
System.out.println(is.skip(1)); // 0
System.out.println(is.read()); // -1
InputStreamで宣言されたメソッド
nullInputStream, read, readNBytes, skipNBytes, transferTo
「Java API 使用例 : InputStream」をご参照ください。