Java : DataOutput with Examples

DataOutput (Java SE 21 & JDK 21) with Examples.
You will find code examples on most DataOutput methods.


Summary

The DataOutput interface provides for converting data from any of the Java primitive types to a series of bytes and writing these bytes to a binary stream. There is also a facility for converting a String into modified UTF-8 format and writing the resulting series of bytes.

Class diagram

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) + "]");

Methods

void write (byte[] b)

Writes to the output stream all the bytes in array 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)

Writes len bytes from array b, in order, to the output stream.

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)

Writes to the output stream the eight low-order bits of the argument 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)

Writes a boolean value to this output stream.

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)

Writes to the output stream the eight low-order bits of the argument 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)

Writes a string to the output stream.

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)

Writes a char value, which is comprised of two bytes, to the output stream.

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)

Writes every character in the string s, to the output stream, in order, two bytes per character.

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)

Writes a double value, which is comprised of eight bytes, to the output stream.

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)

Writes a float value, which is comprised of four bytes, to the output stream.

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)

Writes an int value, which is comprised of four bytes, to the output stream.

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)

Writes a long value, which is comprised of eight bytes, to the output stream.

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)

Writes two bytes to the output stream to represent the value of the argument.

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)

Writes two bytes of length information to the output stream, followed by the modified UTF-8 representation of every character in the 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) + "]");

Related posts

To top of page