Package mars.simulator
Class SystemIO
java.lang.Object
mars.simulator.SystemIO
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Virtual representation of a file for the file-related syscalls to use.static enum
Enumeration of locations whence to seek from in a file. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
Start writing data at the end of the file.static final int
Fail to open if the file already exists; in other words, prevent overwriting of an existing file.static final int
The first file descriptor that will be allocated to the user.static final int
Open the file for read access.static final int
Open the file for both reading and writing.static final int
File descriptor for standard error (System.err
).static final int
File descriptor for standard input (System.in
).static final int
File descriptor for standard output (System.out
).static final int
Open the file for write access. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
closeFile
(int descriptor) Close the file with specified file descriptor.getChannel
(int descriptor) Retrieve the byte channel corresponding to the given file descriptor.Get the string message indicating the result of the previous file operation.getOpenHandle
(int descriptor) Retrieve the file handle corresponding to the given file descriptor.getReadableChannel
(int descriptor) Retrieve the readable byte channel corresponding to the given file descriptor.Get the working directory which is used to calculate relative paths when usingopenFile(Path, int)
.getWritableChannel
(int descriptor) Retrieve the writable byte channel corresponding to the given file descriptor.int
Open a file for either reading or writing.void
printString
(String string) Implements syscall to print a string.int
readChar()
Implements syscall to read a char value.double
Implements syscall to read a double value.float
Implements syscall to read a float value.int
readFromFile
(int descriptor, ByteBuffer buffer) Read sequential bytes from a file into a buffer.int
Implements syscall to read an integer value.readString
(int maxLength) Implements syscall to read a string value.void
Close any open files, and reinitialize standard I/O just in case.long
seekFile
(int descriptor, long offset, SystemIO.SeekWhence whence) Change the current position where read/write operations will be done within a file.void
setWorkingDirectory
(Path workingDirectory) Set the working directory which is used to calculate relative paths when usingopenFile(Path, int)
.int
writeToFile
(int descriptor, ByteBuffer buffer) Write bytes to file.
-
Field Details
-
READ_ONLY_FLAGS
public static final int READ_ONLY_FLAGSOpen the file for read access. (Mutually exclusive withWRITE_ONLY_FLAG
andREAD_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_FLAGOpen the file for write access. (Mutually exclusive withREAD_ONLY_FLAGS
andREAD_WRITE_FLAG
.)- See Also:
-
READ_WRITE_FLAG
public static final int READ_WRITE_FLAGOpen the file for both reading and writing. (Mutually exclusive withREAD_ONLY_FLAGS
andWRITE_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 equalWRITE_ONLY_FLAG
, this mode had to be made distinct somehow.- See Also:
-
APPEND_FLAG
public static final int APPEND_FLAGStart writing data at the end of the file. (Can only be used withWRITE_ONLY_FLAG
.)- See Also:
-
CREATE_NEW_FLAG
public static final int CREATE_NEW_FLAGFail to open if the file already exists; in other words, prevent overwriting of an existing file. (Can be used with eitherWRITE_ONLY_FLAG
orREAD_WRITE_FLAG
.)- See Also:
-
STDIN_DESCRIPTOR
public static final int STDIN_DESCRIPTORFile descriptor for standard input (System.in
).- See Also:
-
STDOUT_DESCRIPTOR
public static final int STDOUT_DESCRIPTORFile descriptor for standard output (System.out
).- See Also:
-
STDERR_DESCRIPTOR
public static final int STDERR_DESCRIPTORFile descriptor for standard error (System.err
).- See Also:
-
FIRST_USER_DESCRIPTOR
public static final int FIRST_USER_DESCRIPTORThe 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
Get the working directory which is used to calculate relative paths when usingopenFile(Path, int)
.- Returns:
- The path representing the working directory, or
null
if the default working directory is in use (which is determined by theuser.dir
system property).
-
setWorkingDirectory
Set the working directory which is used to calculate relative paths when usingopenFile(Path, int)
.- Parameters:
workingDirectory
- The path representing the working directory, ornull
to use theuser.dir
system property (which is the default behavior).
-
getFileOperationMessage
Get the string message indicating the result of the previous file operation.- Returns:
- The operation message.
-
getOpenHandle
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
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
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
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
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 ofREAD_ONLY_FLAGS
,WRITE_ONLY_FLAG
,READ_WRITE_FLAG
,WRITE_ONLY_FLAG
|APPEND_FLAG
, or any of the aforementioned combined withCREATE_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
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
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
Change the current position where read/write operations will be done within a file.- Parameters:
descriptor
- Target file descriptor.offset
- Offset from position specified bywhence
. 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
Implements syscall to print a string.- Parameters:
string
- The string to print.
-
readInteger
Implements syscall to read an integer value.- Returns:
- Integer value entered by user.
- Throws:
NumberFormatException
- Thrown if invalid input is entered.InterruptedException
-
readFloat
Implements syscall to read a float value.- Returns:
- Float value entered by user.
- Throws:
NumberFormatException
- Thrown if invalid input is entered.InterruptedException
-
readDouble
Implements syscall to read a double value.- Returns:
- Double value entered by user.
- Throws:
NumberFormatException
- Thrown if invalid input is entered.InterruptedException
-
readString
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
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
-