Package mars.simulator
Class SystemIO.FileHandle
java.lang.Object
mars.simulator.SystemIO.FileHandle
- Enclosing class:
- SystemIO
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
ConstructorsConstructorDescriptionFileHandle
(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 TypeMethodDescriptionvoid
close
(int nextDescriptor) Close this handle with the previous head of the closed handle list.int
getFlags()
Get the flags the byte channel was opened with.getName()
Get the name of the byte channel used by this handle.int
Get the descriptor of the next closed handle following this one.boolean
isOpen()
Determine whether this handle is open or closed.void
Open this handle with the given byte channel information.toString()
-
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
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
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, orfalse
otherwise.
-
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
-
toString
-