org.blinkenbyte.io
Class BadBlockEmulator

java.lang.Object
  extended by org.blinkenbyte.io.BlockDeviceFilter
      extended by org.blinkenbyte.io.BadBlockEmulator
All Implemented Interfaces:
BlockDevice

public class BadBlockEmulator
extends BlockDeviceFilter

A BadBlockEmulator is a device filter that can filter out read/write requests to particular sectors, throwing a BadBlockException instead. The thrown exception details the filtered block that prevented the I/O from completing successfully.


Nested Class Summary
static class BadBlockEmulator.BadBlockException
          An exception class that is thrown when bad blocks are requested to be read or written by BadBlockEmulator.
 
Nested classes/interfaces inherited from interface org.blinkenbyte.io.BlockDevice
BlockDevice.DriveGeometry
 
Field Summary
 
Fields inherited from class org.blinkenbyte.io.BlockDeviceFilter
bd
 
Constructor Summary
BadBlockEmulator(BlockDevice _bd)
          Constructs a new BadBlockEmulator wrapped around an existing BlockDevice.
 
Method Summary
 void addBlock(long blockNumber, long blockLength)
          Adds a new range of blocks to this object.
 void clearList()
          Clears the list of blocks.
 boolean getAllowPartials()
          Returns whether reads and writes that overlap bad blocks but start with good blocks should be allowed.
 BlockRange getBlockRange(int index)
          Returns the BlockRange at index index within this object.
 java.util.Iterator<BlockRange> iterator()
          Returns an iterator for the blocks.
 void loadList(java.util.ArrayList<BlockRange> listToAdd)
          Appends a list of blocks to the current list.
 void loadList(java.io.Reader r)
          Loads a list of block ranges from a Reader, appending them to the existing list.
 int numBlocks()
          Returns the number of BlockRange objects contained in this object.
 int read()
          Reads a byte from the underlying block device, throwing an exception if the byte is in a bad block range.
 int read(byte[] b, int offset, int len)
          Reads up to len bytes from the underlying block device into a byte array, starting at b[offset].
 boolean removeBlock(long blockNumber, long blockLength)
          Removes a range of blocks from this object.
 void setAllowPartials(boolean _allowPartials)
          Sets whether partial reads and writes are allowed.
 void write(byte[] b, int offset, int len)
          Writes len bytes to the underlying block device from a byte array, starting at b[offset].
 void write(int b)
          Writes a byte to the underlying block device, throwing an exception if the byte is in a bad block range.
 
Methods inherited from class org.blinkenbyte.io.BlockDeviceFilter
close, getAutoSectorAlign, getDriveGeometry, getFilePointer, getSectorSize, getSectorSizeLog, length, open, read, seek, setAutoSectorAlign, setSectorSize, skipBytes, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BadBlockEmulator

public BadBlockEmulator(BlockDevice _bd)
Constructs a new BadBlockEmulator wrapped around an existing BlockDevice.

Method Detail

getAllowPartials

public boolean getAllowPartials()
Returns whether reads and writes that overlap bad blocks but start with good blocks should be allowed. If false, the entire read or write will fail before the underlying BlockDevice is called. If true (the default), the good blocks from the start of the read/write up to but not including the first bad block will be read or written. If a read is being performed, the read will end there. If a write is being performed, an exception will then be thrown.


setAllowPartials

public void setAllowPartials(boolean _allowPartials)
Sets whether partial reads and writes are allowed. See getAllowPartials() for details.


loadList

public void loadList(java.io.Reader r)
              throws java.lang.Exception
Loads a list of block ranges from a Reader, appending them to the existing list.

Throws:
java.lang.Exception

loadList

public void loadList(java.util.ArrayList<BlockRange> listToAdd)
Appends a list of blocks to the current list.


clearList

public void clearList()
Clears the list of blocks.


iterator

public java.util.Iterator<BlockRange> iterator()
Returns an iterator for the blocks.


numBlocks

public int numBlocks()
Returns the number of BlockRange objects contained in this object.


getBlockRange

public BlockRange getBlockRange(int index)
Returns the BlockRange at index index within this object.


addBlock

public void addBlock(long blockNumber,
                     long blockLength)
              throws java.lang.Exception
Adds a new range of blocks to this object.

Parameters:
blockNumber - The starting block number of the range.
blockLength - The length of the range.
Throws:
java.lang.Exception

removeBlock

public boolean removeBlock(long blockNumber,
                           long blockLength)
                    throws java.lang.Exception
Removes a range of blocks from this object.

Parameters:
blockNumber - The starting block number of the range.
blockLength - The length of the range.
Throws:
java.lang.Exception

read

public int read()
         throws java.io.IOException
Reads a byte from the underlying block device, throwing an exception if the byte is in a bad block range.

Specified by:
read in interface BlockDevice
Overrides:
read in class BlockDeviceFilter
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 underlying block device into a byte array, starting at b[offset]. If the bytes to be read do not overlap any bad blocks, the read proceeds normally. If the start of the read overlaps any bad blocks, the read fails immediately. If the bad blocks are not the first bytes to be read, the result depends on whether partial reads are allowed. If partial reads are allowed, a read request for the maximum possible number of bytes that could be read before reading a bad block is processed and returned. If partials are not allowed, the read fails immediately.

Specified by:
read in interface BlockDevice
Overrides:
read in class BlockDeviceFilter
Throws:
java.io.IOException

write

public void write(byte[] b,
                  int offset,
                  int len)
           throws java.io.IOException
Writes len bytes to the underlying block device from a byte array, starting at b[offset]. If the bytes to be written do not overlap any bad blocks, the write proceeds normally. If the start of the write overlaps any bad blocks, the write fails immediately. If the bad blocks are not the first bytes to be written, the result depends on whether partial writes are allowed. If partial writes are allowed, all bytes up to the start of the bad block range are written, after which an exception is thrown. If partials writes are not allowed, the write fails immediately.

Specified by:
write in interface BlockDevice
Overrides:
write in class BlockDeviceFilter
Throws:
java.io.IOException

write

public void write(int b)
           throws java.io.IOException
Writes a byte to the underlying block device, throwing an exception if the byte is in a bad block range.

Specified by:
write in interface BlockDevice
Overrides:
write in class BlockDeviceFilter
Throws:
java.io.IOException