import java.io.RandomAccessFile; import java.io.IOException; public class QuickUHSReader { private RandomAccessFile input = null; private static int recordLength = 4 + 4 + 4 + 4 + 4 + (7 * 4); public static void main(String [] args) throws Exception { String fileName = ""; try { fileName = args[0]; } catch (ArrayIndexOutOfBoundsException aix) { System.err.println("Usage: java QuickUHSReader FILE [N|all]"); System.err.println(" FILE\n " + "The qualified name of the random access file to read" ); System.err.println(" N\n " + "The number of records to read or \"all\" to read the entire " + "file" ); System.exit(1); } QuickUHSReader reader = null; try { reader = new QuickUHSReader(fileName); } catch (IOException iox) { System.err.println("QuickUHSReader:: \"" + fileName + "\" no such file."); System.exit(1); } int numRecords = 10; if (args.length > 0) { try { numRecords = Integer.parseInt(args[1]); } catch (NumberFormatException nfx) { if ( "all".equalsIgnoreCase(args[1])) { numRecords = reader.getRecordCount(); } else { numRecords = 10; } } } reader.parseFile(numRecords); } public QuickUHSReader(String fileName) throws Exception { input = new RandomAccessFile(fileName, "r"); } public int getRecordCount() throws Exception { return ((int) input.length()) / recordLength; } public void parseFile(int recordCount) throws Exception { for (int i = 0; i < recordCount; ++i) { System.out.println(getNextRecord()); } } private String getNextRecord() throws Exception { StringBuffer buff = new StringBuffer(); buff.append( String.format("%d,%4.2f,%4.2f,%f", swapInt(input.readInt()), /* Record number */ swapIntToFloat(input.readInt()), /* Latitude */ swapIntToFloat(input.readInt()), /* Longitude */ swapIntToFloat(input.readInt()) /* FEX */ ) ); /* Number of values */ short numValues = swapShort(input.readShort()); buff.append(String.format(",%d", numValues)); short i; for(i = 0; i < numValues; ++i) { buff.append(String.format(",%f", swapIntToFloat(input.readInt())/100.0)); } // Get back to the record boundary for(;i < 7; ++i) { input.readInt(); /* Toss it */ } input.skipBytes(2); // Bet back to the boundary return buff.toString(); } private static int swapInt(int value) { int b1 = (value >> 0) & 0xff; int b2 = (value >> 8) & 0xff; int b3 = (value >> 16) & 0xff; int b4 = (value >> 24) & 0xff; return b1 << 24 | b2 << 16 | b3 << 8 | b4 << 0; } private static float swapIntToFloat(int value) { int b1 = (value >> 0) & 0xff; int b2 = (value >> 8) & 0xff; int b3 = (value >> 16) & 0xff; int b4 = (value >> 24) & 0xff; return Float.intBitsToFloat(b1 << 24 | b2 << 16 | b3 << 8 | b4 << 0); } private static short swapShort(short value) { int b1 = value & 0xff; int b2 = (value >> 8) & 0xff; return (short) (b1 << 8 | b2 << 0); } }