/**
  * Write a single byte.
  *
  * @param b The byte to write.
  * @throws IOException
  */
 public void write(int b) throws IOException {
   // write(nfd.getNativeFD(), b);
   int result = FileSystem.writeByte(nfd.getNativeFD(), b);
   if (result < 0) {
     throw new IOException("Error code " + Integer.toString(result));
   }
 }
 /**
  * Read a single byte.
  *
  * @return The byte read, or -1 on end of file.
  * @throws IOException
  */
 public int read() throws IOException {
   // return read(nfd.getNativeFD());
   int result = FileSystem.readByte(nfd.getNativeFD());
   if (result < -1) {
     throw new IOException("Error code " + Integer.toString(result));
   }
   return result;
 }
 /**
  * Use JikesRVM's internal read function - the fast way.
  *
  * @param fd File descriptor
  * @param dst Destination buffer
  * @param position Starting offset in the buffer
  * @param len Number of bytes to read
  * @return Number of bytes read, or -1 for end of file.
  * @throws IOException
  */
 private static int read(int fd, byte[] dst, int position, int len) throws IOException {
   if (VM.VerifyAssertions) VM._assert(MemoryManager.willNeverMove(dst));
   int bytes = FileSystem.readBytes(fd, dst, position, len);
   if (bytes < 0) {
     throw new IOException("Error code " + Integer.toString(bytes));
   }
   if (bytes == 0) {
     bytes = -1;
   }
   return bytes;
 }
 public int write(ByteBuffer src, int pos, int len) throws IOException {
   int bytes;
   if (len == 1) {
     bytes = FileSystem.writeByte(nfd.getNativeFD(), src.get(pos));
   } else if (src.hasArray()) {
     bytes = write(src.array(), pos, len);
   } else {
     // Use classpath version, which does buffer housekeeping
     return write(nfd.getNativeFD(), src);
   }
   if (bytes > 0) src.position(src.position() + bytes);
   return bytes;
 }
 /**
  * Read a byte buffer, given a starting position and length. Looks at the type of buffer and
  * decides which is the fastest way to perform the write. If the buffer is backed by a byte array,
  * use the internal method, otherwise push it out to classpath's native function (the slow way).
  *
  * @param dst
  * @param pos
  * @param len
  * @return the number of bytes actually read
  * @throws IOException
  */
 private int read(ByteBuffer dst, int pos, int len) throws IOException {
   int bytes;
   if (len == 1) {
     int b = FileSystem.readByte(nfd.getNativeFD());
     if (b >= 0) {
       dst.put((byte) (b & 0xFF));
       dst.position(pos + 1);
       return 1;
     } else bytes = b;
   } else if (dst.hasArray()) {
     bytes = read(dst.array(), pos, len);
   } else {
     return read(nfd.getNativeFD(), dst);
   }
   if (bytes > 0) dst.position(pos + bytes);
   return bytes;
 }
 /**
  * Use JikesRVM's internal read function - the fast way.
  *
  * @param fd File descriptor
  * @param src SOurce buffer
  * @param pos Starting offset in the buffer
  * @param len Number of bytes to write
  * @return Number of bytes written.
  * @throws IOException
  */
 private static int write(int fd, byte[] src, int pos, int len) throws IOException {
   int bytes = FileSystem.writeBytes(fd, src, pos, len);
   if (bytes < 0) throw new IOException("Error code " + Integer.toString(bytes));
   return bytes;
 }