Java : DataOutput - API使用例
DataOutput (Java SE 21 & JDK 21) の使い方まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
概要
DataOutputは、プリミティブ型 をバイトデータへと変換するためのインタフェースです。
代表的な実装として DataOutputStream があります。
final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
final DataOutput target = os;
target.writeByte(0xff);
target.writeInt(0x12345678);
target.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) + "]");
メソッド
void write (byte[] b)
final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
final DataOutput target = os;
final byte[] b1 = {10, 20};
target.write(b1);
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20]
final byte[] b2 = {30, 40, 50};
target.write(b2);
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20, 30, 40, 50]
}
void write (byte[] b, int off, int len)
final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
final DataOutput target = os;
final byte[] b = {10, 20, 30, 40, 50};
target.write(b, 0, 2);
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20]
target.write(b, 2, 3);
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20, 30, 40, 50]
}
void write (int b)
final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
final DataOutput target = os;
target.write(10);
target.write(20);
target.write(30);
}
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20, 30]
void writeBoolean (boolean v)
final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
final DataOutput target = os;
target.writeBoolean(true);
target.writeBoolean(false);
}
final var bytes = out.toByteArray();
// [01, 00]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");
void writeByte (int v)
final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
final DataOutput target = os;
target.writeByte(0x10);
target.writeByte(0x20);
target.writeByte(0xff);
}
final var bytes = out.toByteArray();
// [10, 20, ff]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");
void writeBytes (String s)
final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
final DataOutput target = os;
target.writeBytes("abc");
target.writeBytes("○△×");
}
final var bytes = out.toByteArray();
// [61, 62, 63, cb, b3, d7]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");
void writeChar (int v)
final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
final DataOutput target = os;
target.writeChar('a');
target.writeChar('b');
target.writeChar('○');
target.writeChar('△');
}
final var bytes = out.toByteArray();
// [00, 61, 00, 62, 25, cb, 25, b3]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");
void writeChars (String s)
final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
final DataOutput target = os;
target.writeChars("abc");
target.writeChars("○△×");
}
final var bytes = out.toByteArray();
// [00, 61, 00, 62, 00, 63, 25, cb, 25, b3, 00, d7]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");
void writeDouble (double v)
final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
final DataOutput target = os;
target.writeDouble(1.23);
target.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) + "]");
void writeFloat (float v)
final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
final DataOutput target = os;
target.writeFloat(1.23f);
target.writeFloat(-4.56f);
}
final var bytes = out.toByteArray();
// [3f, 9d, 70, a4, c0, 91, eb, 85]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");
void writeInt (int v)
final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
final DataOutput target = os;
target.writeInt(0x12345678);
target.writeInt(0xff);
}
final var bytes = out.toByteArray();
// [12, 34, 56, 78, 00, 00, 00, ff]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");
void writeLong (long v)
final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
final DataOutput target = os;
target.writeLong(0x1234567890abcdefL);
target.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) + "]");
void writeShort (int v)
final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
final DataOutput target = os;
target.writeShort(0x1234);
target.writeShort(0xff);
}
final var bytes = out.toByteArray();
// [12, 34, 00, ff]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");
void writeUTF (String s)
final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
final DataOutput target = os;
target.writeUTF("abc");
target.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) + "]");