Class SystemIO.FileHandle

java.lang.Object
mars.simulator.SystemIO.FileHandle
Enclosing class:
SystemIO

public static class SystemIO.FileHandle extends Object
Virtual representation of a file for the file-related syscalls to use. An instance of this class can be in one of two states: open or closed.

In the open state, the instance contains a currently active input/output stream, as well as the name of the stream and which flags it was opened with.

In the closed state, the instance instead stores one integer for the index of the next free file descriptor in the list of handles. This allows the list of handles to always know the next closed file descriptor it can open.

Author:
Sean Clarke 04/2024
  • Constructor Summary

    Constructors
    Constructor
    Description
    FileHandle(int nextDescriptor)
    Create a new closed handle with the previous head of the closed handle list.
    FileHandle(String name, Channel channel, int flags)
    Create a new open handle with the given byte channel information.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    close(int nextDescriptor)
    Close this handle with the previous head of the closed handle list.
     
    int
    Get the flags the byte channel was opened with.
    Get the name of the byte channel used by this handle.
    int
    Get the descriptor of the next closed handle following this one.
    boolean
    Determine whether this handle is open or closed.
    void
    open(String name, Channel channel, int flags)
    Open this handle with the given byte channel information.
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • FileHandle

      public FileHandle(int nextDescriptor)
      Create a new closed handle with the previous head of the closed handle list.
      Parameters:
      nextDescriptor - The descriptor of the next closed handle following this one.
    • FileHandle

      public FileHandle(String name, Channel channel, int flags)
      Create a new open handle with the given byte channel information.
      Parameters:
      name - The name of the byte channel.
      channel - The corresponding byte channel.
      flags - The opening flags for this byte channel.
  • Method Details

    • open

      public void open(String name, Channel channel, int flags)
      Open this handle with the given byte channel information.

      Note: this does not attempt to close an existing byte channel. Ensure close(int) is called beforehand if this handle is already open.

      Parameters:
      name - The name of the byte channel.
      channel - The corresponding byte channel.
      flags - The opening flags for this byte channel.
    • close

      public void close(int nextDescriptor)
      Close this handle with the previous head of the closed handle list.
      Parameters:
      nextDescriptor - The descriptor of the next closed handle following this one.
    • isOpen

      public boolean isOpen()
      Determine whether this handle is open or closed.
      Returns:
      true if this handle is open, or false otherwise.
    • getName

      public String getName()
      Get the name of the byte channel used by this handle.

      Note: only call this method if this handle is open.

      Returns:
      The name of the byte channel.
    • getFlags

      public int getFlags()
      Get the flags the byte channel was opened with.

      Note: only call this method if this handle is open.

      Returns:
      The integer flags.
    • getNextDescriptor

      public int getNextDescriptor()
      Get the descriptor of the next closed handle following this one.

      Note: only call this method if this handle is closed.

      Returns:
      The descriptor of the next handle.
    • getChannel

      public Channel getChannel()
    • toString

      public String toString()
      Overrides:
      toString in class Object