// Check whether a file is sorted. // Records are assumed to be 4 bytes with a 2-byte key and 2-byte data field import java.io.*; import java.nio.*; public class SortCheck { static final int NumRec = 1024; static final int RecSize = 4; public static void main(String args[]) throws IOException { if (args.length != 1) { System.out.println("Usage: SortCheck "); return; } File f = new File(args[0]); int filelength = (int)f.length(); if (((filelength/(RecSize*NumRec))*(RecSize*NumRec)) != filelength) { System.out.println("The file size must be a multiple of " + RecSize*NumRec); return; } int blocks = filelength/(RecSize * NumRec); DataInputStream theFile = new DataInputStream( new BufferedInputStream(new FileInputStream(args[0]))); byte[] rec1b = new byte[RecSize]; ByteBuffer rec1bb = ByteBuffer.wrap(rec1b); ShortBuffer rec1sb = rec1bb.asShortBuffer(); byte[] rec2b = new byte[RecSize]; ByteBuffer rec2bb = ByteBuffer.wrap(rec2b); ShortBuffer rec2sb = rec2bb.asShortBuffer(); int i; theFile.read(rec2b, 0, 4); // Priming read for (i=1; i rec2sb.get(0)) { System.out.println("Record " + i + ": Unsorted output file"); return; } } System.out.println(); theFile.close(); System.out.println(i + " records processed"); } }