Browse - Programming Tips - Java: How do I read in "network" order?

Date: 2019mar15
Language: Java

Q.  Java: How do I read in "network" order?
(Big Endian)

A.  Use DataInputSteam

import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; // Helper function private static byte[] readChunk(DataInputStream ds, int len) throws IOException { byte[] buf = new byte[len]; ds.read(buf, 0, buf.length); return buf; } exampleUse(String absFile) throws IOException { DataInputStream ds = new DataInputStream(new FileInputStream(absFile)); byte b = ds.readByte(); // Read 1 byte number short bu = ds.readUnsignedByte(); // Read unsigned 1 byte number short s = ds.readShort(); // Read 2 byte number int su = ds.readUnsignedShort(); // Read unsigned 2 byte number int i = ds.readInt(); // Read 4 byte number long iu = ds.readUnsignedInt(); // Read unsigned 4 byte number long l = ds.readLong(); // Read 8 byte number byte[] chunk = readChunk(ds, 10); // Read 10 byte buffer }