広告

Java : InputStream - API使用例

InputStream (Java SE 17 & JDK 17) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様のおともにどうぞ。


概要

この抽象クラスは、バイト入力ストリームを表現するすべてのクラスのスーパー・クラスです。

クラス構成

final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data

Files.write(file, new byte[]{10, 20, 30});

try (final var is = Files.newInputStream(file)) {

    final var ret = is.readAllBytes();
    System.out.println(Arrays.toString(ret)); // [10, 20, 30]
}

InputStream は使い終わったら close が必要です。
try-with-resources文 を使い、リソースリークが起きないようにしましょう。

※ByteArrayInputStream など、一部 close しなくても問題ないクラスもあります。


コンストラクタ

InputStream ()

サブクラスが呼び出すためのコンストラクタ。

独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。

メソッド

int available ()

ブロックせずにこの入力ストリームから(またはスキップ)を読み取ることができるバイト数の見積りを返します(0またはストリームの終わりが検出された場合は0)。

あくまで見積りのサイズを返します。
正確な合計サイズを返す、というわけではないのでご注意ください。

final byte[] bytes = {10, 20, 30, 40, 50};

try (final InputStream is = new ByteArrayInputStream(bytes)) {

    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
}

合計サイズを返さない例です。

final byte[] bytes1 = {10, 20, 30};
final byte[] bytes2 = {40, 50};

try (final InputStream is = new SequenceInputStream(
        new ByteArrayInputStream(bytes1),
        new ByteArrayInputStream(bytes2))) {

    System.out.println(is.available()); // 3
    System.out.println(is.read()); // 10

    System.out.println(is.available()); // 2
    System.out.println(is.read()); // 20

    System.out.println(is.available()); // 1
    System.out.println(is.read()); // 30

    System.out.println(is.available()); // 0
    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 ()

この入力ストリームを閉じて、そのストリームに関連するすべてのシステム・リソースを解放します。

可能であれば try-with-resources文 を使うことをおすすめします。

final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data

Files.write(file, new byte[]{1, 2, 3});

try (final var is = Files.newInputStream(file)) {
    final var ret = is.readAllBytes();
    System.out.println(Arrays.toString(ret)); // [1, 2, 3]
}
// try-with-resources文を使わない例です。
final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data

Files.write(file, new byte[]{1, 2, 3});

final var is = Files.newInputStream(file);
try {
    final var ret = is.readAllBytes();
    System.out.println(Arrays.toString(ret)); // [1, 2, 3]
} finally {
    is.close();
}

void mark (int readlimit)

この入力ストリームの現在位置にマークを設定します。

final byte[] buf = {10, 20, 30, 40, 50};

try (final InputStream is = new BufferedInputStream(new ByteArrayInputStream(buf), 2)) {

    System.out.println(is.read()); // 10
    System.out.println(is.read()); // 20

    is.mark(4);

    System.out.println(is.read()); // 30
    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()); // 30
    System.out.println(is.read()); // 40
    System.out.println(is.read()); // 50
    System.out.println(is.read()); // -1
}
final byte[] buf = {10, 20, 30, 40, 50};

try (final InputStream is = new BufferedInputStream(new ByteArrayInputStream(buf), 2)) {

    System.out.println(is.read()); // 10
    System.out.println(is.read()); // 20

    is.mark(3);

    System.out.println(is.read()); // 30
    System.out.println(is.read()); // 40
    System.out.println(is.read()); // 50
    System.out.println(is.read()); // -1

    try {
        is.reset();
    } catch (IOException e) {
        System.out.println(e);
    }

    // 結果
    // ↓
    //java.io.IOException: Mark invalid
}

boolean markSupported ()

この入力ストリームがmarkおよびresetメソッドをサポートしているかどうかを判定します。

final byte[] bytes = {10, 20, 30, 40, 50};

try (final InputStream is = new ByteArrayInputStream(bytes)) {
    System.out.println(is.markSupported()); // true
}
final var file = Path.of("R:", "java-work", "test.data");

try (final var is = Files.newInputStream(file)) {
    System.out.println(is.markSupported()); // false
}
final var file = Path.of("R:", "java-work", "test.data");

try (final InputStream is = new BufferedInputStream(Files.newInputStream(file))) {
    System.out.println(is.markSupported()); // true
}
final byte[] bytes1 = {10, 20, 30};
final byte[] bytes2 = {40, 50};

try (final InputStream is = new SequenceInputStream(
        new ByteArrayInputStream(bytes1),
        new ByteArrayInputStream(bytes2))) {

    System.out.println(is.markSupported()); // false
}

static InputStream nullInputStream ()

バイトを読み取らない新しいInputStreamを返します。

final var is = InputStream.nullInputStream();

// なにも起きません。
System.out.println(is.read()); // -1
System.out.println(Arrays.toString(is.readAllBytes())); // []

is.close();

// closeの後は例外発生
//is.read(); // IOException: Stream closed

abstract int read ()

入力ストリームからデータの次のバイトを読み込みます。

final byte[] bytes = {10, 20, 30, 40, 50};

try (final InputStream is = new ByteArrayInputStream(bytes)) {
    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)

入力ストリームから数バイトを読み込み、それをバッファ配列bに格納します。

final byte[] bytes = {10, 20, 30, 40, 50};
final byte[] b = new byte[10];

try (final InputStream is = new ByteArrayInputStream(bytes)) {

    System.out.println(Arrays.toString(b)); // [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

    System.out.println(is.read(b)); // 5
    System.out.println(Arrays.toString(b)); // [10, 20, 30, 40, 50, 0, 0, 0, 0, 0]
}
final byte[] bytes = {10, 20, 30, 40, 50};
final byte[] b = new byte[5];

try (final InputStream is = new ByteArrayInputStream(bytes)) {

    System.out.println(Arrays.toString(b)); // [0, 0, 0, 0, 0]

    System.out.println(is.read(b)); // 5
    System.out.println(Arrays.toString(b)); // [10, 20, 30, 40, 50]
}
final byte[] bytes = {10, 20, 30, 40, 50};
final byte[] b = new byte[3];

try (final InputStream is = new ByteArrayInputStream(bytes)) {

    System.out.println(Arrays.toString(b)); // [0, 0, 0]

    System.out.println(is.read(b)); // 3
    System.out.println(Arrays.toString(b)); // [10, 20, 30]

    System.out.println(is.read(b)); // 2
    System.out.println(Arrays.toString(b)); // [40, 50, 30]
}

int read (byte[] b, int off, int len)

最大lenバイトのデータを、入力ストリームからバイト配列に読み込みます。

final byte[] bytes = {10, 20, 30, 40, 50};
final byte[] b = new byte[10];

try (final InputStream is = new ByteArrayInputStream(bytes)) {

    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[] bytes = {10, 20, 30, 40, 50, 60, 70};
final byte[] b = new byte[5];

try (final InputStream is = new ByteArrayInputStream(bytes)) {

    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[] bytes = {10, 20, 30, 40, 50};
final byte[] b = new byte[5];

try (final InputStream is = new ByteArrayInputStream(bytes)) {

    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[] bytes = {10, 20, 30, 40, 50, 60, 70};
final byte[] b = new byte[3];

try (final InputStream is = new ByteArrayInputStream(bytes)) {

    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[] bytes = {10, 20, 30, 40, 50};

try (final InputStream is = new ByteArrayInputStream(bytes)) {
    final var ret = is.readAllBytes();
    System.out.println(Arrays.toString(ret)); // [10, 20, 30, 40, 50]
}
final byte[] bytes1 = {10, 20, 30};
final byte[] bytes2 = {40, 50};

try (final InputStream is = new SequenceInputStream(
        new ByteArrayInputStream(bytes1),
        new ByteArrayInputStream(bytes2))) {

    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[] bytes = {10, 20, 30, 40, 50};
final byte[] b = new byte[10];

try (final InputStream is = new ByteArrayInputStream(bytes)) {

    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[] bytes = {10, 20, 30, 40, 50, 60, 70};
final byte[] b = new byte[5];

try (final InputStream is = new ByteArrayInputStream(bytes)) {

    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[] bytes = {10, 20, 30, 40, 50};
final byte[] b = new byte[5];

try (final InputStream is = new ByteArrayInputStream(bytes)) {

    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[] bytes = {10, 20, 30, 40, 50, 60, 70};
final byte[] b = new byte[3];

try (final InputStream is = new ByteArrayInputStream(bytes)) {

    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
}

byte[] readNBytes (int len)

入力ストリームから指定のバイト数まで読み取ります。

final byte[] bytes = {10, 20, 30, 40, 50, 60, 70, 80};

try (final InputStream is = new ByteArrayInputStream(bytes)) {

    System.out.println(Arrays.toString(is.readNBytes(0))); // []
    System.out.println(Arrays.toString(is.readNBytes(1))); // [10]
    System.out.println(Arrays.toString(is.readNBytes(2))); // [20, 30]
    System.out.println(Arrays.toString(is.readNBytes(3))); // [40, 50, 60]

    System.out.println(Arrays.toString(is.readNBytes(1))); // [70]
    System.out.println(Arrays.toString(is.readNBytes(1))); // [80]
    System.out.println(Arrays.toString(is.readNBytes(1))); // []
}
final byte[] bytes = {10, 20, 30, 40, 50};

try (final InputStream is = new ByteArrayInputStream(bytes)) {

    System.out.println(Arrays.toString(is.readNBytes(100))); // [10, 20, 30, 40, 50]
}

void reset ()

このストリームを、この入力ストリームで最後にmarkメソッドが呼び出されたときの位置に再配置します。

このメソッドの使用例は、mark(int readlimit) にまとめて記載しました。
そちらのAPI使用例をご参照ください。

long skip (long n)

この入力ストリームからnバイトのデータをスキップして破棄します。

final byte[] bytes = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};

try (final InputStream is = new ByteArrayInputStream(bytes)) {

    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[] bytes = {10, 20, 30, 40, 50};

try (final InputStream is = new ByteArrayInputStream(bytes)) {

    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
}
final byte[] bytes = {10, 20, 30, 40, 50};
try (final InputStream is = new BufferedInputStream(new ByteArrayInputStream(bytes), 2)) {

    is.mark(1);

    // 指定したスキップ数より小さくなる例
    System.out.println(is.skip(4)); // 2

    System.out.println(Arrays.toString(is.readAllBytes())); // [30, 40, 50]
}

void skipNBytes (long n)

この入力ストリームからのデータのnバイトに正確にスキップし、破棄します。

final byte[] bytes = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};

try (final InputStream is = new ByteArrayInputStream(bytes)) {

    System.out.println(is.read()); // 10

    is.skipNBytes(1);
    System.out.println(is.read()); // 30

    is.skipNBytes(2);
    System.out.println(is.read()); // 60

    is.skipNBytes(3);
    System.out.println(is.read()); // 100

    //is.skipNBytes(1); // EOFException
}
final byte[] bytes = {10, 20, 30, 40, 50};

try (final InputStream is = new ByteArrayInputStream(bytes)) {

    //is.skipNBytes(10); // EOFException
}
final byte[] bytes = {10, 20, 30, 40, 50};
try (final InputStream is = new BufferedInputStream(new ByteArrayInputStream(bytes), 2)) {

    is.mark(1);

    is.skipNBytes(4);

    System.out.println(Arrays.toString(is.readAllBytes())); // [50]
}

long transferTo (OutputStream out)

この入力ストリームからすべてのバイトを読み込んで、指定された出力ストリームに読み込まれた順序でバイトを書き込みます。

final byte[] bytes = {10, 20, 30, 40, 50};

try (final InputStream is = new ByteArrayInputStream(bytes)) {

    try (final var os = new ByteArrayOutputStream()) {

        System.out.println(Arrays.toString(os.toByteArray())); // []

        System.out.println(is.transferTo(os)); // 5
        System.out.println(Arrays.toString(os.toByteArray())); // [10, 20, 30, 40, 50]
    }
}

関連記事

ページの先頭へ