org.brains2b.thex.io
Class BinaryIOKit

java.lang.Object
  extended by org.brains2b.thex.io.BinaryIOKit

public class BinaryIOKit
extends java.lang.Object

Kit for handling IO for all BinaryDocument actions

This kit allows for reading from and writing to streams and handles read and writing files.

Files are read, written and altered in blocks determined by BLOCK_SIZE and divided over BinaryElement that can be read on a per block base and written per block. The later to a temporary file that is united with unchanged blocks when the file is saved.

Version:
$Revision: 1.5 $ $Date: 2010/01/15 03:53:05 $
Author:
dennis@brains2b.nl

Field Summary
static int BLOCK_SIZE
          The size of the blocks in which a file will be divided so blocks can be read and written independently
static int ELEMENT_ALTER
          Indicator for a block that is unaltered and on (a temporary) file
static int ELEMENT_MEMORY
          Indicator for a block that is in memory, either altered or unaltered
static int ELEMENT_OLD
          Indicator for a block that is unaltered and still on file
 
Constructor Summary
BinaryIOKit()
           
 
Method Summary
 java.io.File getFile()
          get the file that holds the content for the BinaryDocument
 void open(java.io.File f, BinaryDocument doc)
          open a file and set the content to the given document
 void read(java.io.InputStream is, BinaryDocument doc, int offset)
          read the content of an InputStream and insert it into a BinaryDocument starting at offset
 void readBlock(BinaryElement be)
          read a BinaryElement from the original file if BinaryElement.getState() is ELEMENT_OLD or a temporary file if BinaryElement.getState() is ELEMENT_ALTER.
protected  void releaseBlock(BinaryElement be)
          release the BinaryElement clearing it from memory and set BinaryElement.setState(int) to ELEMENT_OLD
 void save(java.io.File f, BinaryDocument doc)
          save a BinaryDocument to File
 void setBlockData(BinaryElement be)
          set the block data to the given block by reading it from file, removing the data for the oldest BinaryElement from cache, writing it to file if it was changed.
 void write(java.io.OutputStream os, BinaryDocument doc, int offset, int len)
          write the content of BinaryDocument from offset to length to the OutputStream
protected  void writeBlock(BinaryElement be)
          write this BinaryElement to the location within a temporary file
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BLOCK_SIZE

public static final int BLOCK_SIZE
The size of the blocks in which a file will be divided so blocks can be read and written independently

See Also:
Constant Field Values

ELEMENT_OLD

public static final int ELEMENT_OLD
Indicator for a block that is unaltered and still on file

See Also:
Constant Field Values

ELEMENT_ALTER

public static final int ELEMENT_ALTER
Indicator for a block that is unaltered and on (a temporary) file

See Also:
Constant Field Values

ELEMENT_MEMORY

public static final int ELEMENT_MEMORY
Indicator for a block that is in memory, either altered or unaltered

See Also:
Constant Field Values
Constructor Detail

BinaryIOKit

public BinaryIOKit()
Method Detail

write

public void write(java.io.OutputStream os,
                  BinaryDocument doc,
                  int offset,
                  int len)
           throws java.io.IOException,
                  javax.swing.text.BadLocationException
write the content of BinaryDocument from offset to length to the OutputStream

Parameters:
os - OutputStream, the stream to write to
doc - BinaryDocument, the document to read the content from
offset - int, the position to start reading from
len - int, the number of bytes to read from the document
Throws:
java.io.IOException - thrown if the content cannot be written to the stream
javax.swing.text.BadLocationException - thrown if the given offset or length cannot be read from the document

read

public void read(java.io.InputStream is,
                 BinaryDocument doc,
                 int offset)
          throws java.io.IOException,
                 javax.swing.text.BadLocationException
read the content of an InputStream and insert it into a BinaryDocument starting at offset

Parameters:
is - InputStream, the strea to read from
doc - BinaryDocument, the document to insert into
offset - int, the position to start writing to
Throws:
java.io.IOException - thrown if the content cannot be read from the stream
javax.swing.text.BadLocationException - thrown if the given offset cannot be inserted in the document

open

public void open(java.io.File f,
                 BinaryDocument doc)
          throws java.io.IOException,
                 javax.swing.text.BadLocationException
open a file and set the content to the given document

The document will be divided into blocks and only the first three blocks are read directly. Other blocks will be read if accessed trhough there offset.

Parameters:
f - File, the file to read
doc - BinaryDocument, the document to insert the content to
Throws:
java.io.IOException - thrown if the content cannot be read from the file
javax.swing.text.BadLocationException - thrown if the given offset cannot be inserted in the document

save

public void save(java.io.File f,
                 BinaryDocument doc)
          throws java.io.IOException,
                 javax.swing.text.BadLocationException
save a BinaryDocument to File

Parameters:
f - File, the file to save to
doc - BinaryDocument the document to save the content from
Throws:
java.io.IOException - thrown if the content cannot be written to the file
javax.swing.text.BadLocationException - thrown if the document cannot be read

writeBlock

protected void writeBlock(BinaryElement be)
                   throws java.io.IOException
write this BinaryElement to the location within a temporary file

This method is called from setBlockData(BinaryElement) if the BinaryElement.getState() equals ELEMENT_ALTER

Parameters:
be - BinaryElement containing the content to be written
Throws:
java.io.IOException - thrown if the content cannot be written to the file

releaseBlock

protected void releaseBlock(BinaryElement be)
release the BinaryElement clearing it from memory and set BinaryElement.setState(int) to ELEMENT_OLD

Parameters:
be - BinaryElement

readBlock

public void readBlock(BinaryElement be)
               throws java.io.IOException
read a BinaryElement from the original file if BinaryElement.getState() is ELEMENT_OLD or a temporary file if BinaryElement.getState() is ELEMENT_ALTER. The status of the Element will be changed to ELEMENT_MEMORY

Parameters:
be - BinaryElement
Throws:
java.io.IOException - thrown if the content cannot be read from the file

setBlockData

public void setBlockData(BinaryElement be)
                  throws java.io.IOException,
                         javax.swing.text.BadLocationException
set the block data to the given block by reading it from file, removing the data for the oldest BinaryElement from cache, writing it to file if it was changed.

Parameters:
be - BinaryElement
Throws:
java.io.IOException - thrown if the content cannot be read from the file or changed data cannot be written
javax.swing.text.BadLocationException

getFile

public java.io.File getFile()
get the file that holds the content for the BinaryDocument

Returns:
File