Class SystemIO

java.lang.Object
mars.simulator.SystemIO

public class SystemIO extends Object
Provides standard input/output services needed to simulate the MIPS syscall routines. These methods will detect whether the simulator is being run from the command line or through the GUI, then do I/O to System.in and System.out in the former situation, and interact with the GUI in the latter.
Author:
Pete Sanderson and Ken Vollmar, August 2003-2005
  • Field Details

    • READ_ONLY_FLAGS

      public static final int READ_ONLY_FLAGS
      Open the file for read access. (Mutually exclusive with WRITE_ONLY_FLAG and READ_WRITE_FLAG.)

      This constant is named differently from all the others due to it representing the absence of any flags. So, to check for this state, check for equality instead of performing a bitwise AND.

      See Also:
    • WRITE_ONLY_FLAG

      public static final int WRITE_ONLY_FLAG
      Open the file for write access. (Mutually exclusive with READ_ONLY_FLAGS and READ_WRITE_FLAG.)
      See Also:
    • READ_WRITE_FLAG

      public static final int READ_WRITE_FLAG
      Open the file for both reading and writing. (Mutually exclusive with READ_ONLY_FLAGS and WRITE_ONLY_FLAG.)

      This is a separate flag to preserve backwards compatibility with older versions of MARS, as well as SPIM, which use the absence of any flags to represent read-only mode. Since READ_ONLY_FLAGS | WRITE_ONLY_FLAG would equal WRITE_ONLY_FLAG, this mode had to be made distinct somehow.

      See Also:
    • APPEND_FLAG

      public static final int APPEND_FLAG
      Start writing data at the end of the file. (Can only be used with WRITE_ONLY_FLAG.)
      See Also:
    • CREATE_NEW_FLAG

      public static final int CREATE_NEW_FLAG
      Fail to open if the file already exists; in other words, prevent overwriting of an existing file. (Can be used with either WRITE_ONLY_FLAG or READ_WRITE_FLAG.)
      See Also:
    • STDIN_DESCRIPTOR

      public static final int STDIN_DESCRIPTOR
      File descriptor for standard input (System.in).
      See Also:
    • STDOUT_DESCRIPTOR

      public static final int STDOUT_DESCRIPTOR
      File descriptor for standard output (System.out).
      See Also:
    • STDERR_DESCRIPTOR

      public static final int STDERR_DESCRIPTOR
      File descriptor for standard error (System.err).
      See Also:
    • FIRST_USER_DESCRIPTOR

      public static final int FIRST_USER_DESCRIPTOR
      The first file descriptor that will be allocated to the user.
      See Also:
  • Constructor Details

    • SystemIO

      public SystemIO()
      Set up I/O functionality.
  • Method Details

    • resetFiles

      public void resetFiles()
      Close any open files, and reinitialize standard I/O just in case.
    • getWorkingDirectory

      public Path getWorkingDirectory()
      Get the working directory which is used to calculate relative paths when using openFile(Path, int).
      Returns:
      The path representing the working directory, or null if the default working directory is in use (which is determined by the user.dir system property).
    • setWorkingDirectory

      public void setWorkingDirectory(Path workingDirectory)
      Set the working directory which is used to calculate relative paths when using openFile(Path, int).
      Parameters:
      workingDirectory - The path representing the working directory, or null to use the user.dir system property (which is the default behavior).
    • getFileOperationMessage

      public String getFileOperationMessage()
      Get the string message indicating the result of the previous file operation.
      Returns:
      The operation message.
    • getOpenHandle

      public SystemIO.FileHandle getOpenHandle(int descriptor)
      Retrieve the file handle corresponding to the given file descriptor.
      Parameters:
      descriptor - The file descriptor for the desired handle.
      Returns:
      The handle, if it exists and is currently in use, or null otherwise.
    • getChannel

      public Channel getChannel(int descriptor)
      Retrieve the byte channel corresponding to the given file descriptor.
      Parameters:
      descriptor - The file descriptor for the desired byte channel.
      Returns:
      The byte channel, if the descriptor corresponds to a valid file handle, or null otherwise.
    • getReadableChannel

      public ReadableByteChannel getReadableChannel(int descriptor)
      Retrieve the readable byte channel corresponding to the given file descriptor.
      Parameters:
      descriptor - The file descriptor for the desired readable byte channel.
      Returns:
      The readable byte channel, if the descriptor corresponds to a valid readable file handle, or null otherwise.
    • getWritableChannel

      public WritableByteChannel getWritableChannel(int descriptor)
      Retrieve the writable byte channel corresponding to the given file descriptor.
      Parameters:
      descriptor - The file descriptor for the desired writable byte channel.
      Returns:
      The writable byte channel, if the descriptor corresponds to a valid writable file handle, or null otherwise.
    • openFile

      public int openFile(Path filename, int flags)
      Open a file for either reading or writing. Also note that file permission modes are also NOT IMPLEMENTED.
      Parameters:
      filename - The path of the file to open.
      flags - One of READ_ONLY_FLAGS, WRITE_ONLY_FLAG, READ_WRITE_FLAG, WRITE_ONLY_FLAG | APPEND_FLAG, or any of the aforementioned combined with CREATE_NEW_FLAG.
      Returns:
      File descriptor for the file opened, or -1 if an error occurred.
    • closeFile

      public void closeFile(int descriptor)
      Close the file with specified file descriptor. Sets the file operation message accordingly.
      Parameters:
      descriptor - The descriptor of the file to close.
    • writeToFile

      public int writeToFile(int descriptor, ByteBuffer buffer)
      Write bytes to file.
      Parameters:
      descriptor - Target file descriptor.
      buffer - Byte buffer containing characters to write. All characters from the current position to the limit will be written.
      Returns:
      Number of bytes written, or -1 on error.
    • readFromFile

      public int readFromFile(int descriptor, ByteBuffer buffer) throws InterruptedException
      Read sequential bytes from a file into a buffer.
      Parameters:
      descriptor - Target file descriptor.
      buffer - Byte buffer to contain bytes read. The remaining capacity is the maximum number of characters.
      Returns:
      Number of bytes read, 0 on EOF, or -1 on error.
      Throws:
      InterruptedException
    • seekFile

      public long seekFile(int descriptor, long offset, SystemIO.SeekWhence whence)
      Change the current position where read/write operations will be done within a file.
      Parameters:
      descriptor - Target file descriptor.
      offset - Offset from position specified by whence. May be negative if applicable.
      whence - Directive indicating where in the file to start the offset from.
      Returns:
      New position in the file, or -1 if an error occurred.
    • printString

      public void printString(String string)
      Implements syscall to print a string.
      Parameters:
      string - The string to print.
    • readInteger

      public int readInteger() throws NumberFormatException, InterruptedException
      Implements syscall to read an integer value.
      Returns:
      Integer value entered by user.
      Throws:
      NumberFormatException - Thrown if invalid input is entered.
      InterruptedException
    • readFloat

      public float readFloat() throws NumberFormatException, InterruptedException
      Implements syscall to read a float value.
      Returns:
      Float value entered by user.
      Throws:
      NumberFormatException - Thrown if invalid input is entered.
      InterruptedException
    • readDouble

      public double readDouble() throws NumberFormatException, InterruptedException
      Implements syscall to read a double value.
      Returns:
      Double value entered by user.
      Throws:
      NumberFormatException - Thrown if invalid input is entered.
      InterruptedException
    • readString

      public String readString(int maxLength) throws InterruptedException
      Implements syscall to read a string value.
      Parameters:
      maxLength - The maximum string length.
      Returns:
      String value entered by user, truncated to maximum length if necessary.
      Throws:
      InterruptedException
    • readChar

      public int readChar() throws IndexOutOfBoundsException, InterruptedException
      Implements syscall to read a char value.
      Returns:
      Integer value with least significant byte corresponding to user input.
      Throws:
      IndexOutOfBoundsException - Thrown if invalid input is entered.
      InterruptedException