org.blinkenbyte.io
Class ReadWriteFile

java.lang.Object
  extended by org.blinkenbyte.io.ReadWriteFile
All Implemented Interfaces:
java.io.Closeable, ReadWriteDevice

public class ReadWriteFile
extends java.lang.Object
implements ReadWriteDevice

Class to present a file as a ReadWriteDevice. Essentially an extension of RandomAccessFile


Nested Class Summary
 
Nested classes/interfaces inherited from interface org.blinkenbyte.io.ReadWriteDevice
ReadWriteDevice.RWDInputStream, ReadWriteDevice.RWDOutputStream
 
Field Summary
protected  boolean isClosed
          Flag indicating whether this ReadWriteFile has been closed.
protected  long markPtr
          The current mark.
protected  java.io.RandomAccessFile raf
          The RandomAccessFile being wrapped
 
Constructor Summary
ReadWriteFile(java.io.File file, java.lang.String mode)
          Constructs a new ReadWriteFile using the specified File and mode.
ReadWriteFile(java.io.RandomAccessFile _raf)
          Constructs a new ReadWriteFile around the specified RandomAccessFile
ReadWriteFile(java.lang.String filename, java.lang.String mode)
          Constructs a new ReadWriteFile using the specified filename and mode.
 
Method Summary
 int available()
          Returns 0.
 void close()
          Closes this object and the underlying RandomAccessFile.
 void flush()
          Flushes the underlying RandomAccessFile.
 ReadWriteDevice.RWDInputStream getInputStream()
          Returns a new RWDInputStream wrapped around this object.
 ReadWriteDevice.RWDOutputStream getOutputStream()
          Returns a new RWDOutputStream wrapped around this object.
 long getPointer()
          Returns the current file pointer.
 long length()
          Returns the length of the underlying file.
 void mark(long markLimit)
          Sets the mark with the specified mark limit.
 boolean markSupported()
          Returns true.
 int read()
          Reads a byte from the underlying ReadWriteDevice.
 int read(byte[] b)
          Reads up to b.length bytes from the device into array b.
 int read(byte[] b, int offset, int len)
          Reads up to len bytes from the device into array b at b[offset].
 void reset()
          Resets the pointer to the last mark.
 void seek(long newPointer)
          Seeks the underlying file to the specified position.
 long skip(long n)
          Advances the file pointer by the specified number of bytes.
protected  void throwIfClosed()
          Convenience method to throw an exception if this object has already been closed.
 void write(byte[] b)
          Writes b.length bytes from array b to the stream.
 void write(byte[] b, int offset, int len)
          Writes len bytes from array b at offset b[offset] to the stream.
 void write(int b)
          Writes a single byte to the stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

raf

protected java.io.RandomAccessFile raf
The RandomAccessFile being wrapped


markPtr

protected long markPtr
The current mark.


isClosed

protected boolean isClosed
Flag indicating whether this ReadWriteFile has been closed.

Constructor Detail

ReadWriteFile

public ReadWriteFile(java.io.RandomAccessFile _raf)
              throws java.io.IOException
Constructs a new ReadWriteFile around the specified RandomAccessFile

Throws:
java.io.IOException

ReadWriteFile

public ReadWriteFile(java.io.File file,
                     java.lang.String mode)
              throws java.io.FileNotFoundException
Constructs a new ReadWriteFile using the specified File and mode. The mode is the same format as used in RandomAccessFile.

Throws:
java.io.FileNotFoundException

ReadWriteFile

public ReadWriteFile(java.lang.String filename,
                     java.lang.String mode)
              throws java.io.FileNotFoundException
Constructs a new ReadWriteFile using the specified filename and mode. The mode is the same format as used in RandomAccessFile.

Throws:
java.io.FileNotFoundException
Method Detail

available

public int available()
              throws java.io.IOException
Returns 0.

Specified by:
available in interface ReadWriteDevice
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Closes this object and the underlying RandomAccessFile.

Specified by:
close in interface java.io.Closeable
Specified by:
close in interface ReadWriteDevice
Throws:
java.io.IOException

flush

public void flush()
           throws java.io.IOException
Flushes the underlying RandomAccessFile.

Specified by:
flush in interface ReadWriteDevice
Throws:
java.io.IOException

throwIfClosed

protected void throwIfClosed()
                      throws java.io.IOException
Convenience method to throw an exception if this object has already been closed.

Throws:
java.io.IOException

length

public long length()
            throws java.io.IOException
Returns the length of the underlying file.

Specified by:
length in interface ReadWriteDevice
Throws:
java.io.IOException

getPointer

public long getPointer()
                throws java.io.IOException
Returns the current file pointer.

Specified by:
getPointer in interface ReadWriteDevice
Throws:
java.io.IOException

seek

public void seek(long newPointer)
          throws java.io.IOException
Seeks the underlying file to the specified position.

Specified by:
seek in interface ReadWriteDevice
Throws:
java.io.IOException

skip

public long skip(long n)
          throws java.io.IOException
Advances the file pointer by the specified number of bytes.

Specified by:
skip in interface ReadWriteDevice
Throws:
java.io.IOException

read

public int read()
         throws java.io.IOException
Reads a byte from the underlying ReadWriteDevice. Returns -1 if end-of-stream.

Specified by:
read in interface ReadWriteDevice
Throws:
java.io.IOException

read

public int read(byte[] b)
         throws java.io.IOException
Reads up to b.length bytes from the device into array b. Returns the number of bytes read, -1 if currently at end-of-stream.

Specified by:
read in interface ReadWriteDevice
Throws:
java.io.IOException

read

public int read(byte[] b,
                int offset,
                int len)
         throws java.io.IOException
Reads up to len bytes from the device into array b at b[offset]. Returns the number of bytes read, -1 if currently at end-of-stream.

Specified by:
read in interface ReadWriteDevice
Throws:
java.io.IOException

write

public void write(int b)
           throws java.io.IOException
Writes a single byte to the stream.

Specified by:
write in interface ReadWriteDevice
Throws:
java.io.IOException

write

public void write(byte[] b)
           throws java.io.IOException
Writes b.length bytes from array b to the stream.

Specified by:
write in interface ReadWriteDevice
Throws:
java.io.IOException

write

public void write(byte[] b,
                  int offset,
                  int len)
           throws java.io.IOException
Writes len bytes from array b at offset b[offset] to the stream.

Specified by:
write in interface ReadWriteDevice
Throws:
java.io.IOException

mark

public void mark(long markLimit)
          throws java.io.IOException
Sets the mark with the specified mark limit.

Specified by:
mark in interface ReadWriteDevice
Throws:
java.io.IOException

markSupported

public boolean markSupported()
Returns true.

Specified by:
markSupported in interface ReadWriteDevice

reset

public void reset()
           throws java.io.IOException
Resets the pointer to the last mark.

Specified by:
reset in interface ReadWriteDevice
Throws:
java.io.IOException

getInputStream

public ReadWriteDevice.RWDInputStream getInputStream()
Returns a new RWDInputStream wrapped around this object.

Specified by:
getInputStream in interface ReadWriteDevice

getOutputStream

public ReadWriteDevice.RWDOutputStream getOutputStream()
Returns a new RWDOutputStream wrapped around this object.

Specified by:
getOutputStream in interface ReadWriteDevice