CKTIS / Java
DataInputStream — reading typed binary data
DataInputStream reads primitive values and strings from a binary stream. You must know the field order and encoding; it does not parse numbers written as text.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
new DataInputStream(InputStream in)
byte readByte() throws IOException
int readInt() throws IOException
double readDouble() throws IOException
String readUTF() throws IOExceptionDataInputStream from java.io wraps an InputStream and reads binary data in a machine-independent format. Read types and order must match the data format, for example values written by DataOutputStream. The stream does not identify field types automatically.
DataInputStream.readByte() consumes one byte and returns a signed byte, from -128 to 127. DataInputStream.readInt() consumes four bytes and returns a signed 32-bit int. DataInputStream.readDouble() consumes eight bytes representing an IEEE 754 double value. Multibyte values are read in big-endian order: most significant byte first.
DataInputStream.readUTF() returns a String: it first reads a two-byte unsigned length, then that many bytes of modified UTF-8. The length counts bytes, not characters, and is at most 65535. This is not a reader for ordinary UTF-8 files; the format matches writeUTF().
These methods throw EOFException when there are not enough bytes to read a value, rather than returning an end marker. Other input/output failures cause IOException, and invalid string encoding can cause UTFDataFormatException. Concurrent access requires external synchronization.
Examples
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
byte[] data = {
-7,
0, 0, 0, 42,
0x40, 0x0C, 0, 0, 0, 0, 0, 0,
0, 2, 0x4F, 0x4B
};
try (DataInputStream in = new DataInputStream(
new ByteArrayInputStream(data))) {
System.out.println(in.readByte());
System.out.println(in.readInt());
System.out.println(in.readDouble());
System.out.println(in.readUTF());
}
}
}Result
-7 42 3.5 OK
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.