// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.*;

public class Process {
    private static void dump(int c) {
        if(c >= 0) {
            System.out.printf(" %02X", c);
        } else {
            System.out.print(" -1");
        }
    }
    public static void bytes(String fnm) throws IOException {
        System.out.printf("%s Java InputStream read:", fnm);
        InputStream is = new FileInputStream(fnm);
        int ix = 0;
        int c;
        while((c = is.read()) >= 0) {
            ix++;
            dump(c);
        }
        is.close();
        System.out.printf(" (%d bytes)\n", ix);
    }
    public static void positionbytes(String fnm, long siz) throws IOException {
        System.out.printf("%s Java RandomAccessFile seek and read:", fnm);
        RandomAccessFile raf = new RandomAccessFile(fnm, "r");
        for(int ix = 0; ix < siz; ix++) {
            try {
                raf.seek(ix);
                int c = raf.read();
                dump(c);
            } catch(IOException ex) {
                System.out.print(" -1");
            }
        }
        raf.close();
        System.out.printf(" (%d bytes)\n", siz);
    }
    public static void chars(String fnm) throws IOException {
        System.out.printf("%s Java InputStreamReader read:", fnm);
        InputStreamReader isr = new InputStreamReader(new FileInputStream(fnm));
        int ix = 0;
        int c;
        while((c = isr.read()) >= 0) {
            ix++;
            dump(c);
        }
        isr.close();
        System.out.printf(" (%d chars)\n", ix);
    }
    public static void lines(String fnm) throws IOException {
        System.out.printf("%s Java BufferedReader readLine:", fnm);
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fnm)));
        int ix = 0;
        String line;
        while((line = br.readLine()) != null) {
            for(int i = 0; i < line.length(); i++) {
                ix++;
                dump(line.charAt(i));
            }
            System.out.printf(" *");
        }
        br.close();
        System.out.printf(" (%d chars)\n", ix);
    }
    public static void main(String[] args) throws IOException {
        try {
            System.out.printf("%s Java size = %d bytes\n", args[0], (new File(args[0])).length());
            bytes(args[0]);
            positionbytes(args[0], (new File(args[0])).length());
            chars(args[0]);
            lines(args[0]);
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }
}
