com.etymon.pjx
Class PdfInputBuffer

java.lang.Object
  extended by com.etymon.pjx.PdfInputBuffer
All Implemented Interfaces:
PdfInput

public class PdfInputBuffer
extends java.lang.Object
implements PdfInput

Provides low-level methods for reading portions of a PDF document. PdfReader accesses PDF documents through the PdfInput interface, which is implemented by this class. The entire buffer is stored in memory (except in the case of PdfInputBuffer(File, true)); having the buffer in memory generally improves processing speed as compared to PdfInputFile, but of course it requires the memory to be available and therefore puts greater stress on system resources. For large PDF documents, it is normally better to use PdfInputFile instead.

This class is synchronized; however, note that since it acts as a wrapper around a buffer (in the case of PdfInputBuffer(ByteBuffer, String)), it is the calling method's responsibility to ensure that the buffer is not modified externally to this class. In the case of PdfInputBuffer(File) or PdfInputBuffer(File, false), the constructor reads the entire file into a buffer and that buffer is not externally accessible; so there is no such danger. However, PdfInputBuffer(File, true) leaves the file open, and therefore the calling method must ensure that the file is not modified externally to this class.


Field Summary
protected  java.nio.ByteBuffer _bbuf
          The byte buffer containing the PDF raw data.
protected  java.nio.CharBuffer _cbuf
          The char buffer associated with _bbuf.
protected  java.nio.channels.FileChannel _fileChannel
          The file channel associated with the PDF document.
protected  java.io.FileInputStream _fileInputStream
          The file input stream associated with the PDF document.
protected  java.lang.String _name
          The file name or assigned name of this buffer.
 
Constructor Summary
PdfInputBuffer(java.nio.ByteBuffer pdfBuffer, java.lang.String name)
          Constructs a PDF input source based on a specified ByteBuffer.
PdfInputBuffer(java.io.File pdfFile)
          Constructs a PDF input source based on a specified file.
PdfInputBuffer(java.io.File pdfFile, boolean memoryMapped)
          Constructs a PDF input source based on a specified file, with optional memory-mapping.
 
Method Summary
 void close()
          Closes the PDF document and releases any system resources associated with it.
 long getLength()
          Returns the length of the PDF document.
 java.lang.String getName()
          Returns a name string associated of the PDF document.
protected  void init()
          Performs initialization common to multiple constructors of this class.
protected  void initBuffer()
          Performs initialization common to multiple constructors of this class.
protected  void initFile(java.io.File pdfFile, boolean memoryMapped)
          Performs initialization common to multiple constructors of this class.
 java.nio.ByteBuffer readBytes(long start, long end)
          Returns a specified portion of a PDF document as a ByteBuffer.
 java.nio.CharBuffer readChars(long start, long end)
          Returns a specified portion of a PDF document as a CharBuffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_bbuf

protected java.nio.ByteBuffer _bbuf
The byte buffer containing the PDF raw data.


_cbuf

protected java.nio.CharBuffer _cbuf
The char buffer associated with _bbuf.


_fileChannel

protected java.nio.channels.FileChannel _fileChannel
The file channel associated with the PDF document.


_fileInputStream

protected java.io.FileInputStream _fileInputStream
The file input stream associated with the PDF document.


_name

protected java.lang.String _name
The file name or assigned name of this buffer.

Constructor Detail

PdfInputBuffer

public PdfInputBuffer(java.nio.ByteBuffer pdfBuffer,
                      java.lang.String name)
               throws java.io.IOException
Constructs a PDF input source based on a specified ByteBuffer. The buffer is read starting at its current position until no more bytes are remaining. It is the calling method's responsibility to ensure that the buffer is not modified externally to this class. A name can optionally be assigned to this document for identification.

Parameters:
pdfBuffer - the source buffer.
name - the name to assign to the document (or null if no name is to be assigned).
Throws:
java.io.IOException

PdfInputBuffer

public PdfInputBuffer(java.io.File pdfFile)
               throws java.io.IOException
Constructs a PDF input source based on a specified file. This is equivalent to PdfInputBuffer(File, false); i.e. this constructor reads the entire file into memory and closes the file, and the file is not memory-mapped. The size of the file must not be greater than 231-1; use PdfInputFile for huge files.

Parameters:
pdfFile - the source file.
Throws:
java.io.IOException

PdfInputBuffer

public PdfInputBuffer(java.io.File pdfFile,
                      boolean memoryMapped)
               throws java.io.IOException
Constructs a PDF input source based on a specified file, with optional memory-mapping. If memory-mapping is disabled, this constructor reads the entire file into memory and closes the file; if it is enabled, the file is kept open, and it is the calling method's responsibility to ensure that the file is not modified externally to this class. The memory-mapping feature may be useful for very large files, but in such cases PdfInputFile makes more sense in order to minimize stress on system resources; therefore using the memory-mapping feature is not generally recommended. The size of the file must not be greater than 231-1; use PdfInputFile for huge files.

Parameters:
pdfFile - the source file.
memoryMapped - specifies whether the file is to be memory-mapped. A value of true enables memory-mapping.
Throws:
java.io.IOException
Method Detail

close

public void close()
           throws java.io.IOException
Closes the PDF document and releases any system resources associated with it.

Throws:
java.io.IOException

init

protected void init()
Performs initialization common to multiple constructors of this class. This method is only intended to be called from the constructors.


initBuffer

protected void initBuffer()
Performs initialization common to multiple constructors of this class. This method is only intended to be called from the constructors.


initFile

protected void initFile(java.io.File pdfFile,
                        boolean memoryMapped)
                 throws java.io.IOException
Performs initialization common to multiple constructors of this class. This method is only intended to be called from the constructors.

Throws:
java.io.IOException

readBytes

public java.nio.ByteBuffer readBytes(long start,
                                     long end)
                              throws java.io.IOException
Description copied from interface: PdfInput
Returns a specified portion of a PDF document as a ByteBuffer.

Specified by:
readBytes in interface PdfInput
Parameters:
start - the offset position of the first byte to read.
end - the offset position at which to stop reading. (The byte at this offset is not included.)
Returns:
the requested portion of the PDF document.
Throws:
java.io.IOException

readChars

public java.nio.CharBuffer readChars(long start,
                                     long end)
                              throws java.io.IOException
Description copied from interface: PdfInput
Returns a specified portion of a PDF document as a CharBuffer.

Specified by:
readChars in interface PdfInput
Parameters:
start - the offset position of the first byte to read.
end - the offset position at which to stop reading. (The byte at this offset is not included.)
Returns:
the requested portion of the PDF document.
Throws:
java.io.IOException

getLength

public long getLength()
Description copied from interface: PdfInput
Returns the length of the PDF document.

Specified by:
getLength in interface PdfInput
Returns:
the length (in bytes) of the PDF document.

getName

public java.lang.String getName()
Description copied from interface: PdfInput
Returns a name string associated of the PDF document.

Specified by:
getName in interface PdfInput
Returns:
the name of the PDF document.