広告

Java : DataInput - API使用例

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


概要

DataInputインタフェースは、バイナリ・ストリームからバイトを読み込み、そこからJavaプリミティブ型のいずれかでデータを再構築するために提供されています。 また、修正UTF-8形式のデータからStringを再作成する機能もあります。

クラス構成

DataInputは、バイトデータを プリミティブ型 として読み込むためのインタフェースです。
代表的な実装として DataInputStream があります。

final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
    os.writeByte(0xff);
    os.writeInt(0x12345678);
    os.writeUTF("abcd");
}

final var bytes = out.toByteArray();

// [ff, 12, 34, 56, 78, 00, 04, 61, 62, 63, 64]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");

try (final var is = new DataInputStream(new ByteArrayInputStream(bytes))) {
    final DataInput target = is;

    final var b = target.readByte();
    System.out.printf("%x%n", b); // ff

    final var i = target.readInt();
    System.out.printf("%x%n", i); // 12345678

    final var utf = target.readUTF();
    System.out.println(utf); // abcd
}

メソッド

boolean readBoolean ()

1バイトの入力データを読み込んで、そのバイトがゼロ以外の場合はtrue、そのバイトがゼロの場合はfalseを返します。

final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
    os.writeBoolean(true);
    os.writeBoolean(false);
}

final var bytes = out.toByteArray();

// [01, 00]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");

try (final var is = new DataInputStream(new ByteArrayInputStream(bytes))) {
    final DataInput target = is;

    System.out.println(target.readBoolean()); // true
    System.out.println(target.readBoolean()); // false
}

byte readByte ()

1バイトの入力データを読み込んで返します。

final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
    os.writeByte(0x10);
    os.writeByte(0x20);
    os.writeByte(0xff);
}

final var bytes = out.toByteArray();

// [10, 20, ff]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");

try (final var is = new DataInputStream(new ByteArrayInputStream(bytes))) {
    final DataInput target = is;

    System.out.printf("%x%n", target.readByte()); // 10
    System.out.printf("%x%n", target.readByte()); // 20
    System.out.printf("%x%n", target.readByte()); // ff
}

char readChar ()

2バイトの入力データを読み込んで、char値を返します。

final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
    os.writeChar('a');
    os.writeChar('b');
    os.writeChar('○');
    os.writeChar('△');
}

final var bytes = out.toByteArray();

// [00, 61, 00, 62, 25, cb, 25, b3]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");

try (final var is = new DataInputStream(new ByteArrayInputStream(bytes))) {
    final DataInput target = is;

    System.out.println(target.readChar()); // a
    System.out.println(target.readChar()); // b
    System.out.println(target.readChar()); // ○
    System.out.println(target.readChar()); // △
}

double readDouble ()

8バイトの入力データを読み込んで、double値を返します。

final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
    os.writeDouble(1.23);
    os.writeDouble(-4.56);
}

final var bytes = out.toByteArray();

// [3f, f3, ae, 14, 7a, e1, 47, ae, c0, 12, 3d, 70, a3, d7, 0a, 3d]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");

try (final var is = new DataInputStream(new ByteArrayInputStream(bytes))) {
    final DataInput target = is;

    System.out.println(target.readDouble()); // 1.23
    System.out.println(target.readDouble()); // -4.56
}

float readFloat ()

4バイトの入力データを読み込んで、float値を返します。

final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
    os.writeFloat(1.23f);
    os.writeFloat(-4.56f);
}

final var bytes = out.toByteArray();

// [3f, 9d, 70, a4, c0, 91, eb, 85]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");

try (final var is = new DataInputStream(new ByteArrayInputStream(bytes))) {
    final DataInput target = is;

    System.out.println(target.readFloat()); // 1.23
    System.out.println(target.readFloat()); // -4.56
}

void readFully (byte[] b)

入力ストリームからいくつかのバイトを読み込んで、バッファ配列bに格納します。

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

try (final var is = new DataInputStream(new ByteArrayInputStream(buf))) {
    final DataInput target = is;

    final var b1 = new byte[3];

    target.readFully(b1);
    System.out.println(Arrays.toString(b1)); // [10, 20, 30]

    final var b2 = new byte[3];

    target.readFully(b2);
    System.out.println(Arrays.toString(b2)); // [40, 50, 60]

    try {
        final var b3 = new byte[3];
        target.readFully(b3);
    } catch (EOFException e) {
        System.out.println("EOFException!");
    }

    // 結果
    // ↓
    //EOFException!
}

void readFully (byte[] b, int off, int len)

入力ストリームからlenバイトを読み込みます。

関連:readFully(byte[] b)

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

try (final var is = new DataInputStream(new ByteArrayInputStream(buf))) {
    final DataInput target = is;

    final var b1 = new byte[3];

    target.readFully(b1, 0, 1);
    System.out.println(Arrays.toString(b1)); // [10, 0, 0]

    final var b2 = new byte[3];

    target.readFully(b2, 0, 2);
    System.out.println(Arrays.toString(b2)); // [20, 30, 0]

    final var b3 = new byte[3];

    target.readFully(b3, 0, 3);
    System.out.println(Arrays.toString(b3)); // [40, 50, 60]
}

try (final var is = new DataInputStream(new ByteArrayInputStream(buf))) {
    final DataInput target = is;

    final var b1 = new byte[4];

    target.readFully(b1, 0, 2);
    System.out.println(Arrays.toString(b1)); // [10, 20, 0, 0]

    final var b2 = new byte[4];

    target.readFully(b2, 1, 2);
    System.out.println(Arrays.toString(b2)); // [0, 30, 40, 0]

    final var b3 = new byte[4];

    target.readFully(b3, 2, 2);
    System.out.println(Arrays.toString(b3)); // [0, 0, 50, 60]
}

int readInt ()

4バイトの入力データを読み込んで、int値を返します。

final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
    os.writeInt(0x12345678);
    os.writeInt(0xff);
}

final var bytes = out.toByteArray();

// [12, 34, 56, 78, 00, 00, 00, ff]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");

try (final var is = new DataInputStream(new ByteArrayInputStream(bytes))) {
    final DataInput target = is;

    System.out.printf("%x%n", target.readInt()); // 12345678
    System.out.printf("%x%n", target.readInt()); // ff
}

String readLine ()

入力ストリームから、次の行のテキストを読み込みます。

DataInputStream では非推奨となっているメソッドです。
代替方法などは DataInputStream (Java SE 18 & JDK 18) をご確認ください。

long readLong ()

8バイトの入力データを読み込んで、long値を返します。

final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
    os.writeLong(0x1234567890abcdefL);
    os.writeLong(0xff);
}

final var bytes = out.toByteArray();

// [12, 34, 56, 78, 90, ab, cd, ef, 00, 00, 00, 00, 00, 00, 00, ff]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");

try (final var is = new DataInputStream(new ByteArrayInputStream(bytes))) {
    final DataInput target = is;

    System.out.printf("%x%n", target.readLong()); // 1234567890abcdef
    System.out.printf("%x%n", target.readLong()); // ff
}

short readShort ()

2バイトの入力データを読み込んで、short値を返します。

final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
    os.writeShort(0x1234);
    os.writeShort(0xff);
}

final var bytes = out.toByteArray();

// [12, 34, 00, ff]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");

try (final var is = new DataInputStream(new ByteArrayInputStream(bytes))) {
    final DataInput target = is;

    System.out.printf("%x%n", target.readShort()); // 1234
    System.out.printf("%x%n", target.readShort()); // ff
}

int readUnsignedByte ()

1バイトの入力データを読み込み、int型にゼロ拡張して、結果を返します。結果はこのため、0 - 255の範囲内になります。

final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
    os.writeByte(10);
    os.writeByte(20);
    os.writeByte(-1);
}

final var bytes = out.toByteArray();

// [0a, 14, ff]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");

try (final var is = new DataInputStream(new ByteArrayInputStream(bytes))) {
    final DataInput target = is;

    System.out.println(target.readByte()); // 10
    System.out.println(target.readByte()); // 20
    System.out.println(target.readByte()); // -1
}

try (final var is = new DataInputStream(new ByteArrayInputStream(bytes))) {
    final DataInput target = is;

    System.out.println(target.readUnsignedByte()); // 10
    System.out.println(target.readUnsignedByte()); // 20
    System.out.println(target.readUnsignedByte()); // 255
}

int readUnsignedShort ()

2バイトの入力データを読み込んで、0 - 65535の範囲のint値を返します。

final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
    os.writeShort(10);
    os.writeShort(20);
    os.writeShort(-1);
}

final var bytes = out.toByteArray();

// [00, 0a, 00, 14, ff, ff]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");

try (final var is = new DataInputStream(new ByteArrayInputStream(bytes))) {
    final DataInput target = is;

    System.out.println(target.readShort()); // 10
    System.out.println(target.readShort()); // 20
    System.out.println(target.readShort()); // -1
}

try (final var is = new DataInputStream(new ByteArrayInputStream(bytes))) {
    final DataInput target = is;

    System.out.println(target.readUnsignedShort()); // 10
    System.out.println(target.readUnsignedShort()); // 20
    System.out.println(target.readUnsignedShort()); // 65535
}

String readUTF ()

修正UTF-8形式でエンコードされた文字列を読み込みます。

final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
    os.writeUTF("abc");
    os.writeUTF("○△×");
}

final var bytes = out.toByteArray();

// [00, 03, 61, 62, 63, 00, 08, e2, 97, 8b, e2, 96, b3, c3, 97]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");

try (final var is = new DataInputStream(new ByteArrayInputStream(bytes))) {
    final DataInput target = is;

    System.out.println(target.readUTF()); // abc
    System.out.println(target.readUTF()); // ○△×
}

int skipBytes (int n)

入力ストリームからnバイトをスキップするようにして、スキップしたバイトを破棄します。

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

try (final var is = new DataInputStream(new ByteArrayInputStream(buf))) {
    final DataInput target = is;

    System.out.println(target.readByte()); // 10

    System.out.println(target.skipBytes(1)); // 1
    System.out.println(target.readByte()); // 30

    System.out.println(target.skipBytes(2)); // 2
    System.out.println(target.readByte()); // 60

    System.out.println(target.skipBytes(3)); // 3
    System.out.println(target.readByte()); // 100

    System.out.println(target.skipBytes(1)); // 0
}

関連記事

ページの先頭へ