de.waldheinz.fs.util
Class RamDisk

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

public final class RamDisk
extends Object
implements BlockDevice

A BlockDevice that lives entirely in heap memory. This is basically a RAM disk. A RamDisk is always writable.

Author:
Matthias Treydte <waldheinz at gmail.com>

Field Summary
static int DEFAULT_SECTOR_SIZE
          The default sector size for RamDisks.
 
Constructor Summary
RamDisk(int size)
          Creates a new instance of RamDisk of this specified size and using the DEFAULT_SECTOR_SIZE.
RamDisk(int size, int sectorSize)
          Creates a new instance of RamDisk of this specified size and sector size
 
Method Summary
 void close()
          Closes this BlockDevice.
 void flush()
          Flushes data in caches to the actual storage.
 ByteBuffer getBuffer()
          Returns a slice of the ByteBuffer that is used by this RamDisk as it's backing store.
 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()
          Returns always false, as a RamDisk is always writable.
 void read(long devOffset, ByteBuffer dest)
          Read a block of data from this device.
static RamDisk readGzipped(File f)
          Reads a GZIP compressed file into a new RamDisk instance.
static RamDisk readGzipped(InputStream in)
          Reads a GZIP compressed disk image from the specified input stream and returns a RamDisk holding the decompressed image.
 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

DEFAULT_SECTOR_SIZE

public static final int DEFAULT_SECTOR_SIZE
The default sector size for RamDisks.

See Also:
Constant Field Values
Constructor Detail

RamDisk

public RamDisk(int size)
Creates a new instance of RamDisk of this specified size and using the DEFAULT_SECTOR_SIZE.

Parameters:
size - the size of the new block device

RamDisk

public RamDisk(int size,
               int sectorSize)
Creates a new instance of RamDisk of this specified size and sector size

Parameters:
size - the size of the new block device
sectorSize - the sector size of the new block device
Method Detail

readGzipped

public static RamDisk readGzipped(InputStream in)
                           throws IOException
Reads a GZIP compressed disk image from the specified input stream and returns a RamDisk holding the decompressed image.

Parameters:
in - the stream to read the disk image from
Returns:
the decompressed RamDisk
Throws:
IOException - on read or decompression error

readGzipped

public static RamDisk readGzipped(File f)
                           throws FileNotFoundException,
                                  IOException
Reads a GZIP compressed file into a new RamDisk instance.

Parameters:
f - the file to read
Returns:
the new RamDisk with the file contents
Throws:
FileNotFoundException - if the specified file does not exist
IOException - on read error

getSize

public long getSize()
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

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()

getBuffer

public ByteBuffer getBuffer()
Returns a slice of the ByteBuffer that is used by this RamDisk as it's backing store. The returned buffer will be live (reflecting any changes made through the method, but read-only.

Returns:
a buffer holding the contents of this RamDisk

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()
Returns always false, as a RamDisk is always writable.

Specified by:
isReadOnly in interface BlockDevice
Returns:
always false


Copyright © 2014. All rights reserved.