Class FileDescriptorByteChannel

java.lang.Object
org.jruby.ext.ffi.io.FileDescriptorByteChannel
All Implemented Interfaces:
Closeable, AutoCloseable, ByteChannel, Channel, ReadableByteChannel, WritableByteChannel

public class FileDescriptorByteChannel extends Object implements ByteChannel
An implementation of ByteChannel that reads from and writes to a native unix file descriptor.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final int
     
    private boolean
     
    private final jnr.posix.LibC
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    FileDescriptorByteChannel(Ruby runtime, int fd)
    Creates a new FileDescriptorByteChannel.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes the Channel.
    boolean
    Tests if the ByteChannel is open.
    private static jnr.posix.LibC
    libc(Ruby runtime)
     
    int
    Reads data from the native unix file descriptor.
    int
    Writes data to the native unix file descriptor.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • libc

      private final jnr.posix.LibC libc
    • fd

      private final int fd
    • isOpen

      private volatile boolean isOpen
  • Constructor Details

    • FileDescriptorByteChannel

      public FileDescriptorByteChannel(Ruby runtime, int fd)
      Creates a new FileDescriptorByteChannel.
      Parameters:
      fd - The native unix fd to read/write.
  • Method Details

    • libc

      private static jnr.posix.LibC libc(Ruby runtime)
    • read

      public int read(ByteBuffer dst) throws IOException
      Reads data from the native unix file descriptor.
      Specified by:
      read in interface ReadableByteChannel
      Parameters:
      dst - The destination ByteBuffer to place read bytes in.
      Returns:
      The number of bytes read.
      Throws:
      IOException - If an error occurred during reading.
    • write

      public int write(ByteBuffer src) throws IOException
      Writes data to the native unix file descriptor.
      Specified by:
      write in interface WritableByteChannel
      Parameters:
      src - The source ByteBuffer to write to the file descriptor.
      Returns:
      The number of bytes written.
      Throws:
      IOException - If an error occurred during writing.
    • isOpen

      public boolean isOpen()
      Tests if the ByteChannel is open.
      Specified by:
      isOpen in interface Channel
      Returns:
      true if the Channel is still open
    • close

      public void close() throws IOException
      Closes the Channel.

      This closes the underlying native file descriptor.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Channel
      Specified by:
      close in interface Closeable
      Throws:
      IOException