package sparseviewer.mda; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; public class XdrInput { private final RandomAccessFile raf; public XdrInput(File file) throws IOException { this.raf = new RandomAccessFile(file, "r"); } public void close() throws IOException { raf.close(); } public long getFilePointer() throws IOException { return raf.getFilePointer(); } public void seek(long position) throws IOException { raf.seek(position); } public int readInt() throws IOException { return raf.readInt(); } public float readFloat() throws IOException { return raf.readFloat(); } public double readDouble() throws IOException { return raf.readDouble(); } public int[] readIntArray(int n) throws IOException { int[] values = new int[n]; for (int i = 0; i < n; i++) { values[i] = readInt(); } return values; } public String readXdrString() throws IOException { int length = readInt(); if (length <= 0) { return ""; } byte[] bytes = new byte[length]; raf.readFully(bytes); int pad = padding(length); if (pad > 0) { raf.skipBytes(pad); } return new String(bytes, "UTF-8"); } /** * MDA writes many strings as: * int n * if n > 0: xdr_string * * The xdr_string itself again contains length + bytes + padding. */ public String readStringWithLeadingLength() throws IOException { int n = readInt(); if (n <= 0) { return ""; } return readXdrString(); } private int padding(int length) { int r = length % 4; if (r == 0) { return 0; } return 4 - r; } }