org.freecompany.redline.header
Class AbstractHeader

java.lang.Object
  extended by org.freecompany.redline.header.AbstractHeader
Direct Known Subclasses:
Header, Signature

public abstract class AbstractHeader
extends java.lang.Object


Nested Class Summary
 class AbstractHeader.AbstractEntry<T>
           
static interface AbstractHeader.Entry<T>
           
static interface AbstractHeader.Tag
           
 
Field Summary
protected  java.util.Map<java.lang.Integer,AbstractHeader.Entry<?>> entries
           
protected static int ENTRY_SIZE
           
protected static int HEADER_HEADER_SIZE
           
protected static int MAGIC_WORD
           
protected  java.util.Map<AbstractHeader.Entry<?>,java.lang.Integer> pending
           
protected  java.util.Map<java.lang.Integer,AbstractHeader.Tag> tags
           
 
Constructor Summary
AbstractHeader()
           
 
Method Summary
 AbstractHeader.Entry<?> addEntry(AbstractHeader.Tag tag, int count)
          Adds a pending entry to this header.
 int count()
           
 AbstractHeader.Entry<java.lang.String[]> createEntry(AbstractHeader.Tag tag, java.lang.CharSequence value)
           
 AbstractHeader.Entry<int[]> createEntry(AbstractHeader.Tag tag, int value)
           
<T> AbstractHeader.Entry<T>
createEntry(AbstractHeader.Tag tag, int type, T values)
           
<T> AbstractHeader.Entry<T>
createEntry(AbstractHeader.Tag tag, T values)
           
protected  AbstractHeader.Entry<?> createEntry(int type)
           
 AbstractHeader.Entry<?> createEntry(int tag, int type, int count)
           
<T> AbstractHeader.Entry<T>
createEntry(int tag, int type, T values)
           
protected  java.nio.ByteBuffer getData(java.nio.ByteBuffer index)
          Writes the data section of the file, starting at the current position which must be immediately after the header section.
 AbstractHeader.Entry<?> getEntry(AbstractHeader.Tag tag)
           
 AbstractHeader.Entry<?> getEntry(int tag)
           
protected  java.nio.ByteBuffer getHeader()
          Memory maps the portion of the destination file that will contain the header structure header and advances the file channels position.
protected  java.nio.ByteBuffer getIndex()
          Memory maps the portion of the destination file that will contain the index structure header and advances the file channels position.
 java.util.Map<AbstractHeader.Entry<?>,java.lang.Integer> getPending()
           
protected abstract  boolean pad()
           
 int read(java.nio.channels.ReadableByteChannel in)
          Reads the entire header contents for this channel and returns the number of entries found.
 AbstractHeader.Entry<?> readEntry(int tag, int type, int offset, int count, java.nio.ByteBuffer data)
           
 void removeEntry(AbstractHeader.Entry<?> entry)
           
 java.lang.String toString()
           
 int write(java.nio.channels.WritableByteChannel out)
          Writes this header section to the provided file at the current position and returns the required padding.
protected  int writeData(java.util.Collection<java.nio.ByteBuffer> buffers, java.nio.ByteBuffer index, AbstractHeader.Entry<?> entry, int offset)
           
 void writePending(java.nio.channels.FileChannel channel)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

HEADER_HEADER_SIZE

protected static final int HEADER_HEADER_SIZE
See Also:
Constant Field Values

ENTRY_SIZE

protected static final int ENTRY_SIZE
See Also:
Constant Field Values

MAGIC_WORD

protected static final int MAGIC_WORD
See Also:
Constant Field Values

tags

protected final java.util.Map<java.lang.Integer,AbstractHeader.Tag> tags

entries

protected final java.util.Map<java.lang.Integer,AbstractHeader.Entry<?>> entries

pending

protected final java.util.Map<AbstractHeader.Entry<?>,java.lang.Integer> pending
Constructor Detail

AbstractHeader

public AbstractHeader()
Method Detail

pad

protected abstract boolean pad()

read

public int read(java.nio.channels.ReadableByteChannel in)
         throws java.io.IOException
Reads the entire header contents for this channel and returns the number of entries found.

Throws:
java.io.IOException

write

public int write(java.nio.channels.WritableByteChannel out)
          throws java.io.IOException
Writes this header section to the provided file at the current position and returns the required padding. The caller is responsible for adding the padding immediately after this data.

Throws:
java.io.IOException

count

public int count()

getHeader

protected java.nio.ByteBuffer getHeader()
                                 throws java.io.IOException
Memory maps the portion of the destination file that will contain the header structure header and advances the file channels position. The resulting buffer will be prefilled with the necesssary magic data and the correct index count, but will require an integer value to be written with the total data section size once data writing is complete.

This method must be invoked before mapping the index or data sections.

Throws:
java.io.IOException

getIndex

protected java.nio.ByteBuffer getIndex()
                                throws java.io.IOException
Memory maps the portion of the destination file that will contain the index structure header and advances the file channels position. The resulting buffer will be ready for writing of the entry indexes.

This method must be invoked before mapping the data section, but after mapping the header.

Throws:
java.io.IOException

getData

protected java.nio.ByteBuffer getData(java.nio.ByteBuffer index)
                               throws java.io.IOException
Writes the data section of the file, starting at the current position which must be immediately after the header section. Each entry writes its corresponding index into the provided index buffer and then writes its data to the file channel.

Returns:
the total number of bytes written to the data section of the file.
Throws:
java.io.IOException

writeData

protected int writeData(java.util.Collection<java.nio.ByteBuffer> buffers,
                        java.nio.ByteBuffer index,
                        AbstractHeader.Entry<?> entry,
                        int offset)

writePending

public void writePending(java.nio.channels.FileChannel channel)

getPending

public java.util.Map<AbstractHeader.Entry<?>,java.lang.Integer> getPending()

removeEntry

public void removeEntry(AbstractHeader.Entry<?> entry)

getEntry

public AbstractHeader.Entry<?> getEntry(AbstractHeader.Tag tag)

getEntry

public AbstractHeader.Entry<?> getEntry(int tag)

createEntry

public AbstractHeader.Entry<java.lang.String[]> createEntry(AbstractHeader.Tag tag,
                                                            java.lang.CharSequence value)

createEntry

public AbstractHeader.Entry<int[]> createEntry(AbstractHeader.Tag tag,
                                               int value)

createEntry

public <T> AbstractHeader.Entry<T> createEntry(AbstractHeader.Tag tag,
                                               T values)

createEntry

public <T> AbstractHeader.Entry<T> createEntry(AbstractHeader.Tag tag,
                                               int type,
                                               T values)

createEntry

public <T> AbstractHeader.Entry<T> createEntry(int tag,
                                               int type,
                                               T values)

addEntry

public AbstractHeader.Entry<?> addEntry(AbstractHeader.Tag tag,
                                        int count)
Adds a pending entry to this header. This entry will have the correctly sized buffer allocated, but will not be written until the caller writes a value and then invokes writePending on this object.


readEntry

public AbstractHeader.Entry<?> readEntry(int tag,
                                         int type,
                                         int offset,
                                         int count,
                                         java.nio.ByteBuffer data)

createEntry

public AbstractHeader.Entry<?> createEntry(int tag,
                                           int type,
                                           int count)

createEntry

protected AbstractHeader.Entry<?> createEntry(int type)

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object