uk.me.parabola.imgfmt.sys
Class FileNode

java.lang.Object
  extended by uk.me.parabola.imgfmt.sys.FileNode
All Implemented Interfaces:
java.io.Closeable, java.nio.channels.ByteChannel, java.nio.channels.Channel, java.nio.channels.ReadableByteChannel, java.nio.channels.WritableByteChannel, ImgChannel

public class FileNode
extends java.lang.Object
implements ImgChannel

The internal representation of a file in the file system. In use it should only be referred to by the ImgChannel interface.

Author:
Steve Ratcliffe

Constructor Summary
FileNode(java.nio.channels.FileChannel file, uk.me.parabola.imgfmt.sys.Dirent dir, java.lang.String mode)
          Creates a new file in the file system.
 
Method Summary
 void close()
          Closes this channel.
 boolean isOpen()
          Tells whether or not this channel is open.
 long position()
          Get the file position.
 void position(long pos)
          Set the position within the file.
 int read(java.nio.ByteBuffer dst)
          Reads a sequence of bytes from this channel into the given buffer.
 void setXorByte(byte xorByte)
           
 int write(java.nio.ByteBuffer src)
          Writes a sequence of bytes to this channel from the given buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileNode

public FileNode(java.nio.channels.FileChannel file,
                uk.me.parabola.imgfmt.sys.Dirent dir,
                java.lang.String mode)
Creates a new file in the file system. You can treat this just like a regular file and write or read from it. Operations to two different files may not be interleaved although it may be possible to implement this.

Parameters:
file - The handle to the underlying file.
dir - The directory entry associated with this file.
mode - The mode "rw" for read and write etc.
Method Detail

close

public void close()
           throws java.io.IOException
Closes this channel.

After a channel is closed, any further attempt to invoke I/O operations upon it will cause a ClosedChannelException to be thrown.

If this channel is already closed then invoking this method has no effect.

This method may be invoked at any time. If some other thread has already invoked it, however, then another invocation will block until the first invocation is complete, after which it will return without effect.

Specified by:
close in interface java.io.Closeable
Specified by:
close in interface java.nio.channels.Channel
Throws:
java.io.IOException - If an I/O error occurs

isOpen

public boolean isOpen()
Tells whether or not this channel is open.

Specified by:
isOpen in interface java.nio.channels.Channel
Returns:
true if, and only if, this channel is open

read

public int read(java.nio.ByteBuffer dst)
         throws java.io.IOException
Reads a sequence of bytes from this channel into the given buffer.

Specified by:
read in interface java.nio.channels.ReadableByteChannel
Parameters:
dst - The buffer into which bytes are to be transferred
Returns:
The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream
Throws:
java.nio.channels.NonReadableChannelException - If this channel was not opened for reading
java.nio.channels.ClosedChannelException - If this channel is closed
java.nio.channels.AsynchronousCloseException - If another thread closes this channel while the read operation is in progress
java.nio.channels.ClosedByInterruptException - If another thread interrupts the current thread while the read operation is in progress, thereby closing the channel and setting the current thread's interrupt status
java.io.IOException - If some other I/O error occurs

write

public int write(java.nio.ByteBuffer src)
          throws java.io.IOException
Writes a sequence of bytes to this channel from the given buffer.

An attempt is made to write up to r bytes to the channel, where r is the number of bytes remaining in the buffer, that is, dst.remaining(), at the moment this method is invoked.

The logical block has to be converted to a physical block in the underlying file.

Specified by:
write in interface java.nio.channels.WritableByteChannel
Parameters:
src - The buffer from which bytes are to be retrieved
Returns:
The number of bytes written, possibly zero
Throws:
java.nio.channels.NonWritableChannelException - If this channel was not opened for writing
java.nio.channels.ClosedChannelException - If this channel is closed
java.io.IOException - If some other I/O error occurs

position

public long position()
Description copied from interface: ImgChannel
Get the file position. Note that this is a logical position relative to the beginning of the file (the file within the .img file, not the beginning of the .img file itself).

Specified by:
position in interface ImgChannel
Returns:
The offset in bytes from the beginning of the file.

position

public void position(long pos)
Description copied from interface: ImgChannel
Set the position within the file.

Specified by:
position in interface ImgChannel
Parameters:
pos - The position to set.

setXorByte

public void setXorByte(byte xorByte)