Java : DataOutputStream - API使用例
DataOutputStream (Java SE 21 & JDK 21) の使い方まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
概要
DataOutputStream クラスは、プリミティブ型 をバイトデータとして書き込みます。
書き込んだバイトデータは DataInputStream で読み込むことができます。
DataOutputStream は使い終わったら close が必要です。
try-with-resources文 を使い、リソースリークが起きないようにしましょう。
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 var b = is.readByte();
System.out.printf("%x%n", b); // ff
final var i = is.readInt();
System.out.printf("%x%n", i); // 12345678
final var utf = is.readUTF();
System.out.println(utf); // abcd
}
フィールド
protected int written
class MyOutputStream extends DataOutputStream {
MyOutputStream(OutputStream out) {
super(out);
}
int getWritten() {
return written;
}
}
final var out = new ByteArrayOutputStream();
try (final var os = new MyOutputStream(out)) {
os.writeByte(0x10);
System.out.println(os.getWritten()); // 1
os.writeByte(0x20);
System.out.println(os.getWritten()); // 2
os.writeInt(0x12345678);
System.out.println(os.getWritten()); // 6
}
final var bytes = out.toByteArray();
// [10, 20, 12, 34, 56, 78]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");
FilterOutputStreamで宣言されたフィールド
out
「Java API 使用例 : FilterOutputStream」をご参照ください。
コンストラクタ
DataOutputStream (OutputStream out)
final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
os.writeByte(0x10);
os.writeByte(0x20);
os.writeInt(0x12345678);
}
final var bytes = out.toByteArray();
// [10, 20, 12, 34, 56, 78]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");
メソッド
void flush ()
final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(new BufferedOutputStream(out))) {
os.write(10);
System.out.println(Arrays.toString(out.toByteArray())); // []
os.flush();
System.out.println(Arrays.toString(out.toByteArray())); // [10]
os.write(20);
System.out.println(Arrays.toString(out.toByteArray())); // [10]
os.flush();
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20]
os.write(30);
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20]
}
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20, 30]
final int size ()
final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
os.writeByte(0x10);
System.out.println(os.size()); // 1
os.writeByte(0x20);
System.out.println(os.size()); // 2
os.writeInt(0x12345678);
System.out.println(os.size()); // 6
}
final var bytes = out.toByteArray();
// [10, 20, 12, 34, 56, 78]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");
void write (byte[] b, int off, int len)
final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
final byte[] b = {10, 20, 30, 40, 50};
os.write(b, 0, 2);
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20]
os.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)) {
os.write(10);
os.write(20);
os.write(30);
}
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20, 30]
final void writeBoolean (boolean v)
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))) {
System.out.println(is.readBoolean()); // true
System.out.println(is.readBoolean()); // false
}
final void writeByte (int v)
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))) {
System.out.printf("%x%n", is.readByte()); // 10
System.out.printf("%x%n", is.readByte()); // 20
System.out.printf("%x%n", is.readByte()); // ff
}
final void writeBytes (String s)
final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
os.writeBytes("abc");
os.writeBytes("○△×");
}
final var bytes = out.toByteArray();
// [61, 62, 63, cb, b3, d7]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");
try (final var is = new DataInputStream(new ByteArrayInputStream(bytes))) {
System.out.printf("%x%n", is.readByte()); // 61
System.out.printf("%x%n", is.readByte()); // 62
System.out.printf("%x%n", is.readByte()); // 63
System.out.printf("%x%n", is.readByte()); // cb
System.out.printf("%x%n", is.readByte()); // b3
System.out.printf("%x%n", is.readByte()); // d7
}
final void writeChar (int v)
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))) {
System.out.println(is.readChar()); // a
System.out.println(is.readChar()); // b
System.out.println(is.readChar()); // ○
System.out.println(is.readChar()); // △
}
final void writeChars (String s)
final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
os.writeChars("abc");
os.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) + "]");
try (final var is = new DataInputStream(new ByteArrayInputStream(bytes))) {
System.out.println(is.readChar()); // a
System.out.println(is.readChar()); // b
System.out.println(is.readChar()); // c
System.out.println(is.readChar()); // ○
System.out.println(is.readChar()); // △
System.out.println(is.readChar()); // ×
}
final void writeDouble (double v)
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))) {
System.out.println(is.readDouble()); // 1.23
System.out.println(is.readDouble()); // -4.56
}
final void writeFloat (float v)
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))) {
System.out.println(is.readFloat()); // 1.23
System.out.println(is.readFloat()); // -4.56
}
final void writeInt (int v)
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))) {
System.out.printf("%x%n", is.readInt()); // 12345678
System.out.printf("%x%n", is.readInt()); // ff
}
final void writeLong (long v)
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))) {
System.out.printf("%x%n", is.readLong()); // 1234567890abcdef
System.out.printf("%x%n", is.readLong()); // ff
}
final void writeShort (int v)
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))) {
System.out.printf("%x%n", is.readShort()); // 1234
System.out.printf("%x%n", is.readShort()); // ff
}
final void writeUTF (String str)
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))) {
System.out.println(is.readUTF()); // abc
System.out.println(is.readUTF()); // ○△×
}
FilterOutputStreamで宣言されたメソッド
close, write
「Java API 使用例 : FilterOutputStream」をご参照ください。
OutputStreamで宣言されたメソッド
nullOutputStream
「Java API 使用例 : OutputStream」をご参照ください。
DataOutputで宣言されたメソッド
write
「Java API 使用例 : DataOutput」をご参照ください。