nachos.machine
Class OpenFile

java.lang.Object
  extended by nachos.machine.OpenFile
Direct Known Subclasses:
OpenFileWithPosition

public class OpenFile
extends Object

A file that supports reading, writing, and seeking.


Constructor Summary
OpenFile()
          Allocate a new unnamed OpenFile that is not associated with any file system.
OpenFile(FileSystem fileSystem, String name)
          Allocate a new OpenFile object with the specified name on the specified file system.
 
Method Summary
 void close()
          Close this file and release any associated system resources.
 FileSystem getFileSystem()
          Get the file system to which this file belongs.
 String getName()
          Get the name of this open file.
 int length()
          Get the length of this file.
 int read(byte[] buf, int offset, int length)
          Read this file starting at the current file pointer and return the number of bytes successfully read.
 int read(int pos, byte[] buf, int offset, int length)
          Read this file starting at the specified position and return the number of bytes successfully read.
 void seek(int pos)
          Set the value of the current file pointer.
 int tell()
          Get the value of the current file pointer, or -1 if this file has no pointer.
 int write(byte[] buf, int offset, int length)
          Write this file starting at the current file pointer and return the number of bytes successfully written.
 int write(int pos, byte[] buf, int offset, int length)
          Write this file starting at the specified position and return the number of bytes successfully written.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

OpenFile

public OpenFile(FileSystem fileSystem,
                String name)
Allocate a new OpenFile object with the specified name on the specified file system.

Parameters:
fileSystem - the file system to which this file belongs.
name - the name of the file, on that file system.

OpenFile

public OpenFile()
Allocate a new unnamed OpenFile that is not associated with any file system.

Method Detail

getFileSystem

public FileSystem getFileSystem()
Get the file system to which this file belongs.

Returns:
the file system to which this file belongs.

getName

public String getName()
Get the name of this open file.

Returns:
the name of this open file.

read

public int read(int pos,
                byte[] buf,
                int offset,
                int length)
Read this file starting at the specified position and return the number of bytes successfully read. If no bytes were read because of a fatal error, returns -1

Parameters:
pos - the offset in the file at which to start reading.
buf - the buffer to store the bytes in.
offset - the offset in the buffer to start storing bytes.
length - the number of bytes to read.
Returns:
the actual number of bytes successfully read, or -1 on failure.

write

public int write(int pos,
                 byte[] buf,
                 int offset,
                 int length)
Write this file starting at the specified position and return the number of bytes successfully written. If no bytes were written because of a fatal error, returns -1.

Parameters:
pos - the offset in the file at which to start writing.
buf - the buffer to get the bytes from.
offset - the offset in the buffer to start getting.
length - the number of bytes to write.
Returns:
the actual number of bytes successfully written, or -1 on failure.

length

public int length()
Get the length of this file.

Returns:
the length of this file, or -1 if this file has no length.

close

public void close()
Close this file and release any associated system resources.


seek

public void seek(int pos)
Set the value of the current file pointer.


tell

public int tell()
Get the value of the current file pointer, or -1 if this file has no pointer.


read

public int read(byte[] buf,
                int offset,
                int length)
Read this file starting at the current file pointer and return the number of bytes successfully read. Advances the file pointer by this amount. If no bytes could be* read because of a fatal error, returns -1.

Parameters:
buf - the buffer to store the bytes in.
offset - the offset in the buffer to start storing bytes.
length - the number of bytes to read.
Returns:
the actual number of bytes successfully read, or -1 on failure.

write

public int write(byte[] buf,
                 int offset,
                 int length)
Write this file starting at the current file pointer and return the number of bytes successfully written. Advances the file pointer by this amount. If no bytes could be written because of a fatal error, returns -1.

Parameters:
buf - the buffer to get the bytes from.
offset - the offset in the buffer to start getting.
length - the number of bytes to write.
Returns:
the actual number of bytes successfully written, or -1 on failure.