// Copyright (c) Keith D Gregory, all rights reserved
package com.kdgregory.example.bytebuffer;

import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;


/**
 *  Reads the file written by <code>data_writer.c</code>, using a ByteBuffer to
 *  extract the 4-byte integer from the file's contents.
 */
public class DataReader
{
    public static void main(String[] argv)
    throws Exception
    {
        byte[] data = new byte[4];
        FileInputStream in = new FileInputStream("/tmp/example.dat");
        if (in.read(data) < 4)
            throw new Exception("unable to read file contents");

        ByteBuffer buf = ByteBuffer.wrap(data);
        System.out.println(String.format("data = %x", buf.getInt(0)));

        buf.order(ByteOrder.LITTLE_ENDIAN);
        System.out.println(String.format("data = %x", buf.getInt(0)));
    }
}
