com.etymon.pjx
Class XrefTable

java.lang.Object
  extended by com.etymon.pjx.XrefTable

public class XrefTable
extends java.lang.Object

Represents the PDF cross-reference table and associated trailer dictionary. This class is immutable.


Field Summary
protected  int[] _generation
          The array of generation values.
protected  long[] _index
          The array of index values.
protected  long[] _index_sorted
          The array of sorted index values.
protected  java.util.List _startxrefs
          The list of startxref values associated with this cross-reference table.
protected  PdfDictionary _trailer
          The trailer dictionary associated with this cross-reference table.
protected  byte[] _usage
          The array of usage values.
static byte ENTRY_FREE
          This indicates that an entry is free.
static byte ENTRY_IN_USE
          This indicates that an entry is in-use.
static byte ENTRY_UNDEFINED
          This indicates that an entry is undefined.
 
Constructor Summary
protected XrefTable()
          A protected constructor intended to be called only from wrap(long[], int[], byte[], PdfDictionary).
  XrefTable(long[] index, int[] generation, byte[] usage, PdfDictionary trailerDictionary)
          Constructs a cross-reference table from a set of arrays and a trailer dictionary.
 
Method Summary
 java.lang.Object clone()
          Returns a shallow copy of this instance.
protected  void createSortedIndexArray()
           
 long estimateObjectEnd(int n)
          Returns an offset estimated to be relatively close to the end of the object (specified by object number).
 int getGeneration(int n)
          Returns the generation value for a specified object.
 int[] getGenerationArray()
          Returns the array of generation values.
 long getIndex(int n)
          Returns the index value for a specified object.
 long[] getIndexArray()
          Returns the array of index values.
protected  java.util.List getStartxrefList()
          Returns the list of startxref values associated with this cross-reference table.
 PdfDictionary getTrailerDictionary()
          Returns the trailer dictionary associated with this cross-reference table.
 byte getUsage(int n)
          Returns the usage value for a specified object.
 byte[] getUsageArray()
          Returns the array of usage values.
 int size()
          Returns the number of entries in this cross-reference table.
 java.lang.String toString()
          Returns the cross-reference table, associated trailer dictionary, and a complete PDF trailer as a string in PDF format.
protected  int[] unwrapGenerationArray()
          Returns the array of generation values.
protected  long[] unwrapIndexArray()
          Returns the array of index values.
protected  byte[] unwrapUsageArray()
          Returns the array of usage values.
protected static XrefTable wrap(long[] index, int[] generation, byte[] usage, PdfDictionary trailerDictionary)
          A factory for fast construction of this class.
protected  int writePdf(PdfWriter w, long startxref)
          Writes the cross-reference table, associated trailer dictionary, and a complete PDF trailer in PDF format.
protected  void xrefGenerateFreeList()
          Combines the free elements of this cross-reference table into a linked list as required by the PDF specification.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

_generation

protected int[] _generation
The array of generation values.


_index

protected long[] _index
The array of index values. Each value represents either a byte offset (if in-use) or the next free object number (if free).


_index_sorted

protected long[] _index_sorted
The array of sorted index values. This contains only in-use entries, to be used for binary searching and locating the offset of the next object in the file.


_startxrefs

protected java.util.List _startxrefs
The list of startxref values associated with this cross-reference table.


_trailer

protected PdfDictionary _trailer
The trailer dictionary associated with this cross-reference table.


_usage

protected byte[] _usage
The array of usage values. Each values is ENTRY_FREE, ENTRY_IN_USE, or ENTRY_UNDEFINED.


ENTRY_FREE

public static final byte ENTRY_FREE
This indicates that an entry is free.

See Also:
Constant Field Values

ENTRY_IN_USE

public static final byte ENTRY_IN_USE
This indicates that an entry is in-use.

See Also:
Constant Field Values

ENTRY_UNDEFINED

public static final byte ENTRY_UNDEFINED
This indicates that an entry is undefined.

See Also:
Constant Field Values
Constructor Detail

XrefTable

protected XrefTable()
A protected constructor intended to be called only from wrap(long[], int[], byte[], PdfDictionary).


XrefTable

public XrefTable(long[] index,
                 int[] generation,
                 byte[] usage,
                 PdfDictionary trailerDictionary)
          throws PdfFormatException
Constructs a cross-reference table from a set of arrays and a trailer dictionary.

Parameters:
index - the array of index values. Each value represents either a byte offset (if in-use) or the next free object number (if free).
generation - the array of generation values.
usage - the array of usage values. Each value is ENTRY_FREE, ENTRY_IN_USE, or ENTRY_UNDEFINED.
trailerDictionary - the trailer dictionary.
Throws:
PdfFormatException
Method Detail

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Returns a shallow copy of this instance.

Overrides:
clone in class java.lang.Object
Returns:
a clone of this instance.
Throws:
java.lang.CloneNotSupportedException

createSortedIndexArray

protected void createSortedIndexArray()

estimateObjectEnd

public long estimateObjectEnd(int n)
Returns an offset estimated to be relatively close to the end of the object (specified by object number). The offset will be no earlier than the end of the object.

Parameters:
n - the specified object number.
Returns:
the index value, or -1 if the specified object number corresponds to the last object in the document. If the object number does not correspond to an in-use entry, then -1 is returned.

getGeneration

public int getGeneration(int n)
Returns the generation value for a specified object.

Parameters:
n - the object number.
Returns:
the generation value.

getGenerationArray

public int[] getGenerationArray()
Returns the array of generation values. The calling method must not modify this array unless it guarantees that this object exists in no other thread, in order to comply with the immutability requirement of this class.

Returns:
the array of generation values.

getIndex

public long getIndex(int n)
Returns the index value for a specified object.

Parameters:
n - the object number.
Returns:
the index value.

getIndexArray

public long[] getIndexArray()
Returns the array of index values. The calling method must not modify this array unless it guarantees that this object exists in no other thread, in order to comply with the immutability requirement of this class.

Returns:
the array of index values.

getStartxrefList

protected java.util.List getStartxrefList()
Returns the list of startxref values associated with this cross-reference table. The calling method must not modify this list unless it guarantees that this object exists in no other thread, in order to comply with the immutability requirement of this class.

Returns:
the list of startxref values (as Long objects.

getTrailerDictionary

public PdfDictionary getTrailerDictionary()
Returns the trailer dictionary associated with this cross-reference table.

Returns:
the trailer dictionary.

getUsage

public byte getUsage(int n)
Returns the usage value for a specified object.

Parameters:
n - the object number.
Returns:
the usage value.

getUsageArray

public byte[] getUsageArray()
Returns the array of usage values. The calling method must not modify this array unless it guarantees that this object exists in no other thread, in order to comply with the immutability requirement of this class.

Returns:
the array of usage values.

size

public int size()
Returns the number of entries in this cross-reference table.

Returns:
the number of entries.

toString

public java.lang.String toString()
Returns the cross-reference table, associated trailer dictionary, and a complete PDF trailer as a string in PDF format.

Overrides:
toString in class java.lang.Object
Returns:
the PDF string.

unwrapGenerationArray

protected int[] unwrapGenerationArray()
Returns the array of generation values. The calling method must ensure that the array is never externally modified, in order to meet the immutability requirement of this class.

Returns:
the array of generation values.

unwrapIndexArray

protected long[] unwrapIndexArray()
Returns the array of index values. The calling method must ensure that the array is never externally modified, in order to meet the immutability requirement of this class.

Returns:
the array of index values.

unwrapUsageArray

protected byte[] unwrapUsageArray()
Returns the array of usage values. The calling method must ensure that the array is never externally modified, in order to meet the immutability requirement of this class.

Returns:
the array of usage values.

wrap

protected static XrefTable wrap(long[] index,
                                int[] generation,
                                byte[] usage,
                                PdfDictionary trailerDictionary)
A factory for fast construction of this class. The constructed object will be a wrapper around the specified objects. The calling method must ensure that the arrays are never externally modified, in order to meet the immutability requirement of this class. It must also ensure that all three arrays have the same length. It is also the calling method's responsibility to call createSortedIndexArray() before the instance will be used.

Parameters:
index - the array of index values.
generation - the array of generation values.
usage - the array of usage values.
trailerDictionary - the trailer dictionary.
Returns:
the constructed object.

writePdf

protected int writePdf(PdfWriter w,
                       long startxref)
                throws java.io.IOException
Writes the cross-reference table, associated trailer dictionary, and a complete PDF trailer in PDF format. Before writing the cross-reference table, the free entries are combined into a linked list as required by the PDF specification.

Parameters:
w - the PdfWriter to write to.
startxref - the byte offset within the output file where the cross-reference table begins.
Returns:
the number of bytes written by this method.
Throws:
java.io.IOException

xrefGenerateFreeList

protected void xrefGenerateFreeList()
Combines the free elements of this cross-reference table into a linked list as required by the PDF specification.