/* Copyright (c) 2008-2013, Avian Contributors

   Permission to use, copy, modify, and/or distribute this software
   for any purpose with or without fee is hereby granted, provided
   that the above copyright notice and this permission notice appear
   in all copies.

   There is NO WARRANTY for this software.  See license.txt for
   details. */

package java.io;

import java.lang.IllegalArgumentException;

public class RandomAccessFile {
  private long peer;
  private File file;
  private long position = 0;
  private long length;
  private boolean allowWrite;

  public RandomAccessFile(String name, String mode)
    throws FileNotFoundException
  {
    this(new File(name), mode);
  }

  public RandomAccessFile(File file, String mode)
    throws FileNotFoundException
  {
    if (file == null) throw new NullPointerException();
    if (mode.equals("rw")) allowWrite = true;
    else if (! mode.equals("r")) throw new IllegalArgumentException();
    this.file = file;
    open();
  }

  private void open() throws FileNotFoundException {
    long[] result = new long[2];
    open(file.getPath(), allowWrite, result);
    peer = result[0];
    length = result[1];
  }

  private static native void open(String name, boolean allowWrite, long[] result)
    throws FileNotFoundException;

  private void refresh() throws IOException {
    if (file.length() != length) {
      close();
      open();
    }
  }

  public long length() throws IOException {
    refresh();
    return length;
  }

  public long getFilePointer() throws IOException {
    return position;
  }

  public void seek(long position) throws IOException {
    if (position < 0 || (!allowWrite && position > length())) throw new IOException();

    this.position = position;
  }

  public int skipBytes(int count) throws IOException {
    if (position + count > length()) throw new IOException();
    this.position = position + count;
    return count;
  }
  
  public int read(byte b[], int off, int len) throws IOException {
    if(b == null)
	  throw new IllegalArgumentException();
    if (peer == 0)
	  throw new IOException();
	if(len == 0)
	  return 0;
	if (position + len > this.length)
      throw new EOFException();
	if (off < 0 || off + len > b.length)
      throw new ArrayIndexOutOfBoundsException();
    int bytesRead = readBytes(peer, position, b, off, len);
	position += bytesRead;
	return bytesRead;
  }
  
  public int read(byte b[]) throws IOException {
    if(b == null)
	  throw new IllegalArgumentException();
    if (peer == 0)
	  throw new IOException();
	if(b.length == 0)
	  return 0;
	if (position + b.length > this.length)
      throw new EOFException();
    int bytesRead = readBytes(peer, position, b, 0, b.length);
	position += bytesRead;
	return bytesRead;
  }

  public void readFully(byte b[], int off, int len) throws IOException {
    if(b == null)
	  throw new IllegalArgumentException();
    if (peer == 0)
	  throw new IOException();
	if(len == 0)
	  return;
	if (position + len > this.length)
      throw new EOFException();
	if (off < 0 || off + len > b.length)
      throw new ArrayIndexOutOfBoundsException();
    int n = 0;
    do {
      int count = readBytes(peer, position, b, off + n, len - n);
      position += count;
      if (count == 0)
        throw new EOFException();
      n += count;
    } while (n < len);
  }
  
  public void readFully(byte b[]) throws IOException {
    readFully(b, 0, b.length);
  }

  private static native int readBytes(long peer, long position, byte[] buffer,
                                  int offset, int length);

  public void write(int b) throws IOException {
    int count = writeBytes(peer, position, new byte[] { (byte)b }, 0, 1);
    if (count > 0) position += count;
  }

  private static native int writeBytes(long peer, long position, byte[] buffer,
                                  int offset, int length);

  public void close() throws IOException {
    if (peer != 0) {
      close(peer);
      peer = 0;
    }
  }

  private static native void close(long peer);
}

@@ -404,16 +404,82 @@
 ception;
+%0Aimport java.nio.ByteBuffer;%0Aimport java.nio.channels.FileChannel;
 %0A%0Apublic
@@ -4098,11 +4098,1447 @@
 g peer);
+%0A%0A  public FileChannel getChannel() %7B%0A    return new FileChannel() %7B%0A      public void close() %7B%0A        if (peer != 0) RandomAccessFile.close(peer);%0A      %7D%0A%0A      public boolean isOpen() %7B%0A        return peer != 0;%0A      %7D%0A%0A      public int read(ByteBuffer dst, long position) throws IOException %7B%0A        if (!dst.hasArray()) throw new IOException(%22Cannot handle %22 + dst.getClass());%0A%09// TODO: this needs to be synchronized on the Buffer, no?%0A        byte%5B%5D array = dst.array();%0A        return readBytes(peer, position, array, dst.position(), dst.remaining());%0A      %7D%0A%0A      public int read(ByteBuffer dst) throws IOException %7B%0A        int count = read(dst, position);%0A        if (count %3E 0) position += count;%0A        return count;%0A      %7D%0A%0A      public int write(ByteBuffer src, long position) throws IOException %7B%0A        if (!src.hasArray()) throw new IOException(%22Cannot handle %22 + src.getClass());%0A        byte%5B%5D array = src.array();%0A        return writeBytes(peer, position, array, src.position(), src.remaining());%0A      %7D%0A%0A      public int write(ByteBuffer src) throws IOException %7B%0A        int count = write(src, position);%0A        if (count %3E 0) position += count;%0A        return count;%0A      %7D%0A%0A      public long position() throws IOException %7B%0A        return getFilePointer();%0A      %7D%0A%0A      public FileChannel position(long position) throws IOException %7B%0A        seek(position);%0A        return this;%0A      %7D%0A    %7D;%0A  %7D
 %0A%7D%0A

Implement a rudimentary RandomAccessFile#getChannel