Java : DataInputStream with Examples

DataInputStream (Java SE 18 & JDK 18) API Examples.
You will find code examples on most DataInputStream methods.


Summary

A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way. An application uses a data output stream to write data that can later be read by a data input stream.

Class diagram

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
}

Fields declared in FilterInputStream

in

Please see the link below.

Constructors

DataInputStream (InputStream in)

Creates a DataInputStream that uses the specified underlying InputStream.

final byte[] buf = {0x10, 0x20, 0x30};

try (final var is = new DataInputStream(new ByteArrayInputStream(buf))) {
    final var b = is.readByte();
    System.out.printf("%x%n", b); // 10

    final var s = is.readShort();
    System.out.printf("%x%n", s); // 2030
}

Methods

final int read (byte[] b)

Reads some number of bytes from the contained input stream and stores them into the buffer array b.

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

try (final var is = new DataInputStream(new ByteArrayInputStream(buf))) {
    final var b1 = new byte[3];

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

    final var b2 = new byte[3];

    System.out.println(is.read(b2)); // 2
    System.out.println(Arrays.toString(b2)); // [40, 50, 0]

    final var b3 = new byte[3];

    System.out.println(is.read(b3)); // -1
    System.out.println(Arrays.toString(b3)); // [0, 0, 0]
}

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

Reads up to len bytes of data from the contained input stream into an array of bytes.

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

try (final var is = new DataInputStream(new ByteArrayInputStream(buf))) {
    final var b1 = new byte[3];

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

    final var b2 = new byte[3];

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

    final var b3 = new byte[3];

    System.out.println(is.read(b3, 0, 3)); // -1
    System.out.println(Arrays.toString(b3)); // [0, 0, 0]
}

try (final var is = new DataInputStream(new ByteArrayInputStream(buf))) {
    final var b1 = new byte[3];

    System.out.println(is.read(b1, 2, 1)); // 1
    System.out.println(Arrays.toString(b1)); // [0, 0, 10]

    final var b2 = new byte[3];

    System.out.println(is.read(b2, 1, 2)); // 2
    System.out.println(Arrays.toString(b2)); // [0, 20, 30]

    final var b3 = new byte[3];

    System.out.println(is.read(b3, 0, 3)); // -1
    System.out.println(Arrays.toString(b3)); // [0, 0, 0]
}

final boolean readBoolean ()

See the general contract of the readBoolean method of DataInput.

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 byte readByte ()

See the general contract of the readByte method of DataInput.

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 char readChar ()

See the general contract of the readChar method of DataInput.

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 double readDouble ()

See the general contract of the readDouble method of DataInput.

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 float readFloat ()

See the general contract of the readFloat method of DataInput.

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 readFully (byte[] b)

See the general contract of the readFully method of DataInput.

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

try (final var is = new DataInputStream(new ByteArrayInputStream(buf))) {
    final var b1 = new byte[3];

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

    final var b2 = new byte[3];

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

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

    // Result
    // ↓
    //EOFException!
}

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

See the general contract of the readFully method of DataInput.

Please see also : readFully(byte[] b)

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

try (final var is = new DataInputStream(new ByteArrayInputStream(buf))) {
    final var b1 = new byte[3];

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

    final var b2 = new byte[3];

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

    final var b3 = new byte[3];

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

try (final var is = new DataInputStream(new ByteArrayInputStream(buf))) {
    final var b1 = new byte[4];

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

    final var b2 = new byte[4];

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

    final var b3 = new byte[4];

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

final int readInt ()

See the general contract of the readInt method of DataInput.

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 String readLine ()

Deprecated. This method does not properly convert bytes to characters.

Deprecated.

final long readLong ()

See the general contract of the readLong method of DataInput.

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 short readShort ()

See the general contract of the readShort method of DataInput.

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 int readUnsignedByte ()

See the general contract of the readUnsignedByte method of DataInput.

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))) {
    System.out.println(is.readByte()); // 10
    System.out.println(is.readByte()); // 20
    System.out.println(is.readByte()); // -1
}

try (final var is = new DataInputStream(new ByteArrayInputStream(bytes))) {
    System.out.println(is.readUnsignedByte()); // 10
    System.out.println(is.readUnsignedByte()); // 20
    System.out.println(is.readUnsignedByte()); // 255
}

final int readUnsignedShort ()

See the general contract of the readUnsignedShort method of DataInput.

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))) {
    System.out.println(is.readShort()); // 10
    System.out.println(is.readShort()); // 20
    System.out.println(is.readShort()); // -1
}

try (final var is = new DataInputStream(new ByteArrayInputStream(bytes))) {
    System.out.println(is.readUnsignedShort()); // 10
    System.out.println(is.readUnsignedShort()); // 20
    System.out.println(is.readUnsignedShort()); // 65535
}

final String readUTF ()

See the general contract of the readUTF method of DataInput.

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()); // ○△×
}

static final String readUTF (DataInput in)

Reads from the stream in a representation of a Unicode character string encoded in modified UTF-8 format; this string of characters is then returned as a String.

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(DataInputStream.readUTF(is)); // abc
    System.out.println(DataInputStream.readUTF(is)); // ○△×
}

final int skipBytes (int n)

See the general contract of the skipBytes method of DataInput.

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

try (final var is = new DataInputStream(new ByteArrayInputStream(buf))) {
    System.out.println(is.readByte()); // 10

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

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

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

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

Methods declared in FilterInputStream

available, close, mark, markSupported, read, reset, skip

Please see the link below.

Methods declared in InputStream

nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo

Please see the link below.


Related posts

To top of page