de.waldheinz.fs.util
Class FileDisk

java.lang.Object
  extended by de.waldheinz.fs.util.FileDisk
All Implemented Interfaces:
BlockDevice

public final class FileDisk
extends Object
implements BlockDevice

This is a BlockDevice that uses a File as it's backing store.

Author:
Matthias Treydte <matthias.treydte at meetwise.com>

Field Summary
static int BYTES_PER_SECTOR
          The number of bytes per sector for all FileDisk instances.
 
Constructor Summary
FileDisk(File file, boolean readOnly)
          Creates a new instance of FileDisk for the specified File.
 
Method Summary
 void close()
          Closes this BlockDevice.
static FileDisk create(File file, long size)
          Creates a new FileDisk of the specified size.
 void flush()
          Flushes data in caches to the actual storage.
 int getSectorSize()
          Returns the size of a sector on this device.
 long getSize()
          Gets the total length of this device in bytes.
 boolean isClosed()
          Checks if this device was already closed.
 boolean isReadOnly()
          Checks if this BlockDevice is read-only.
 void read(long devOffset, ByteBuffer dest)
          Read a block of data from this device.
 void write(long devOffset, ByteBuffer src)
          Writes a block of data to this device.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BYTES_PER_SECTOR

public static final int BYTES_PER_SECTOR
The number of bytes per sector for all FileDisk instances.

See Also:
Constant Field Values
Constructor Detail

FileDisk

public FileDisk(File file,
                boolean readOnly)
         throws FileNotFoundException
Creates a new instance of FileDisk for the specified File.

Parameters:
file - the file that holds the disk contents
readOnly - if the file should be opened in read-only mode, which will result in a read-only FileDisk instance
Throws:
FileNotFoundException - if the specified file does not exist
See Also:
isReadOnly()
Method Detail

create

public static FileDisk create(File file,
                              long size)
                       throws IOException
Creates a new FileDisk of the specified size. The FileDisk returned by this method will be writable.

Parameters:
file - the file to hold the FileDisk contents
size - the size of the new FileDisk
Returns:
the created FileDisk instance
Throws:
IOException - on error creating the FileDisk

getSize

public long getSize()
             throws IOException
Description copied from interface: BlockDevice
Gets the total length of this device in bytes.

Specified by:
getSize in interface BlockDevice
Returns:
the total number of bytes on this device
Throws:
IOException - on error getting the size of this device

read

public void read(long devOffset,
                 ByteBuffer dest)
          throws IOException
Description copied from interface: BlockDevice
Read a block of data from this device.

Specified by:
read in interface BlockDevice
Parameters:
devOffset - the byte offset where to read the data from
dest - the destination buffer where to store the data read
Throws:
IOException - on read error

write

public void write(long devOffset,
                  ByteBuffer src)
           throws IOException
Description copied from interface: BlockDevice
Writes a block of data to this device.

Specified by:
write in interface BlockDevice
Parameters:
devOffset - the byte offset where to store the data
src - the source ByteBuffer to write to the device
Throws:
IOException - on write error
See Also:
BlockDevice.isReadOnly()

flush

public void flush()
           throws IOException
Description copied from interface: BlockDevice
Flushes data in caches to the actual storage.

Specified by:
flush in interface BlockDevice
Throws:
IOException - on write error

getSectorSize

public int getSectorSize()
Description copied from interface: BlockDevice
Returns the size of a sector on this device.

Specified by:
getSectorSize in interface BlockDevice
Returns:
the sector size in bytes

close

public void close()
           throws IOException
Description copied from interface: BlockDevice
Closes this BlockDevice. No methods of this device may be accesses after this method was called.

Specified by:
close in interface BlockDevice
Throws:
IOException - on error closing this device
See Also:
BlockDevice.isClosed()

isClosed

public boolean isClosed()
Description copied from interface: BlockDevice
Checks if this device was already closed. No methods may be called on a closed device (except this method).

Specified by:
isClosed in interface BlockDevice
Returns:
if this device is closed

isReadOnly

public boolean isReadOnly()
Description copied from interface: BlockDevice
Checks if this BlockDevice is read-only.

Specified by:
isReadOnly in interface BlockDevice
Returns:
if this BlockDevice is read-only


Copyright © 2014. All rights reserved.