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

import java.io.File;
import java.io.RandomAccessFile;
import java.nio.ByteOrder;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;


/**
 *  Reads the file written by <code>data_writer.c</code>, mapping it into the
 *  process' virtual address space. This is a trivial example, but contains
 *  all the code you need to work with a non-trivial example.
 */
public class MappedDataReader
{
    public static void main(String[] argv)
    throws Exception
    {
        File file = new File("/tmp/example.dat");
        FileChannel channel = new RandomAccessFile(file, "r").getChannel();
        
        MappedByteBuffer buf = channel.map(MapMode.READ_ONLY, 0L, file.length());
        buf.order(ByteOrder.LITTLE_ENDIAN);
        
        System.out.println(String.format("data = %x", buf.getInt(0)));
        
        // this next call will throw unless you open the file with "rw"
        buf.putInt(0, 0x87654321);
    }
}
