MM.java

import java.io.*;
import java.nio.*;
import java.nio.channels.*; // for FileChannel

/**
Simple test case for memory-mapped reading of a file.
Echoes all but the first and last 10 bytes of the file.
*/
public class MM
{
    public static void main(String argv[])
    {
        try {
            FileInputStream in = new FileInputStream("MM.java");
            FileChannel channel = in.getChannel();
            MappedByteBuffer buf = 
                channel.map(
                    FileChannel.MapMode.READ_ONLY,
                    10,
                    (int)channel.size()-20);

            while (buf.hasRemaining()) {
                byte b = buf.get();
                System.out.print((char)b);
                }

            in.close();
            }
        catch(IOException e) {
            System.err.println("I/O error: " + e);
            }
    }
}