/** * Closes the <tt>Channel</tt>. * <p> * This closes the underlying native file descriptor. * </p> * @throws java.io.IOException */ public void close() throws IOException { if (!isOpen) { throw new IllegalStateException("file already closed"); } isOpen = false; libc.close(fd); } }
/** * Reads data from the native unix file descriptor. * @param dst The destination <tt>ByteBuffer</tt> to place read bytes in. * @return The number of bytes read. * * @throws java.io.IOException If an error occurred during reading. */ public int read(ByteBuffer dst) throws IOException { if (!isOpen) { throw new IOException("Not open"); } int n = libc.read(fd, dst, dst.remaining()); if (n > 0) { dst.position(dst.position() + n); } else if (n == 0) { return -1; // EOF } return n; }
/** * Writes data to the native unix file descriptor. * * @param src The source <tt>ByteBuffer</tt> to write to the file descriptor. * @return The number of bytes written. * * @throws java.io.IOException If an error occurred during writing. */ public int write(ByteBuffer src) throws IOException { if (!isOpen) { throw new IOException("Not open"); } int n = libc.write(fd, src, src.remaining()); if (n > 0) { src.position(src.position() + n); } return n; }
public int truncate(CharSequence path, long length) { // Windows doesn't have a native truncate() equivalent, but it does have a native ftruncate() equivalent. // In order to call the ftruncate() equivalent, we must convert a path to a FD. We do that by wrapping the // ftruncate() call with open() and close(). // Permissions are ignored since we're not using O_CREAT. int fd = libc().open(path, OpenFlags.O_WRONLY.intValue(), 0); if (fd == -1) { return -1; } if (libc().ftruncate(fd, length) == -1) { return -1; } if (libc().close(fd) == -1) { return -1; } // truncate() returns 0 on success. return 0; }
public int isatty(int fd) { return libc().isatty(fd); }
public int endgrent() { return libc().endgrent(); }
public int execv(String path, String[] args) { return libc().execv(path, args); }
public int endpwent() { return libc().endpwent(); }
public int chdir(String path) { return libc().chdir(path); }
public int chmod(String filename, int mode) { return libc().chmod(filename, mode); }
public int chown(String filename, int user, int group) { return libc().chown(filename, user, group); }
public int truncate(CharSequence path, long length) { // Windows doesn't have a native truncate() equivalent, but it does have a native ftruncate() equivalent. // In order to call the ftruncate() equivalent, we must convert a path to a FD. We do that by wrapping the // ftruncate() call with open() and close(). // Permissions are ignored since we're not using O_CREAT. int fd = libc().open(path, OpenFlags.O_WRONLY.intValue(), 0); if (fd == -1) { return -1; } if (libc().ftruncate(fd, length) == -1) { return -1; } if (libc().close(fd) == -1) { return -1; } // truncate() returns 0 on success. return 0; }
public boolean isatty(FileDescriptor fd) { return libc().isatty(helper.getfd(fd)) != 0; }
public int endgrent() { return libc().endgrent(); }
public int execv(String path, String[] args) { return libc().execv(path, args); }
public int endpwent() { return libc().endpwent(); }
public int chdir(String path) { return libc().chdir(path); }
public int chmod(String filename, int mode) { return libc().chmod(filename, mode); }
public int chown(String filename, int user, int group) { return libc().chown(filename, user, group); }
/** * Reads data from the native unix file descriptor. * @param dst The destination <tt>ByteBuffer</tt> to place read bytes in. * @return The number of bytes read. * * @throws java.io.IOException If an error occurred during reading. */ public int read(ByteBuffer dst) throws IOException { if (!isOpen) { throw new IOException("Not open"); } int n = libc.read(fd, dst, dst.remaining()); if (n > 0) { dst.position(dst.position() + n); } else if (n == 0) { return -1; // EOF } return n; }