7
* This source file is subject to the new BSD license that is bundled
8
* with this package in the file LICENSE.txt.
9
* It is also available through the world-wide-web at this URL:
10
* http://framework.zend.com/license/new-bsd
11
* If you did not receive a copy of the license and are unable to
12
* obtain it through the world-wide-web, please send an email
13
* to license@zend.com so we can send you a copy immediately.
16
* @subpackage FileParser
17
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
18
* @license http://framework.zend.com/license/new-bsd New BSD License
21
/** Zend_Pdf_Exception */
22
require_once 'Zend/Pdf/Exception.php';
26
* Abstract helper class for {@link Zend_Pdf_FileParser} that provides the
27
* data source for parsing.
29
* Concrete subclasses allow for parsing of in-memory, filesystem, and other
30
* sources through a common API. These subclasses also take care of error
31
* handling and other mundane tasks.
33
* Subclasses must implement at minimum {@link __construct()},
34
* {@link __destruct()}, {@link readBytes()}, and {@link readAllBytes()}.
35
* Subclasses should also override {@link moveToOffset()} and
36
* {@link __toString()} as appropriate.
39
* @subpackage FileParser
40
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
41
* @license http://framework.zend.com/license/new-bsd New BSD License
43
abstract class Zend_Pdf_FileParserDataSource
45
/**** Instance Variables ****/
49
* Total size in bytes of the data source.
55
* Byte offset of the current read position within the data source.
58
protected $_offset = 0;
62
/**** Public Interface ****/
65
/* Abstract Methods */
68
* Object constructor. Opens the data source for parsing.
70
* Must set $this->_size to the total size in bytes of the data source.
72
* Upon return the data source can be interrogated using the primitive
73
* methods described here.
75
* If the data source cannot be opened for any reason (such as insufficient
76
* permissions, missing file, etc.), will throw an appropriate exception.
78
* @throws Zend_Pdf_Exception
80
abstract public function __construct();
83
* Object destructor. Closes the data source.
85
* May also perform cleanup tasks such as deleting temporary files.
87
abstract public function __destruct();
90
* Returns the specified number of raw bytes from the data source at the
91
* byte offset of the current read position.
93
* Must advance the read position by the number of bytes read by updating
96
* Throws an exception if there is insufficient data to completely fulfill
97
* the request or if an error occurs.
99
* @param integer $byteCount Number of bytes to read.
101
* @throws Zend_Pdf_Exception
103
abstract public function readBytes($byteCount);
106
* Returns the entire contents of the data source as a string.
108
* This method may be called at any time and so must preserve the byte
109
* offset of the read position, both through $this->_offset and whatever
110
* other additional pointers (such as the seek position of a file pointer)
111
* that might be used.
115
abstract public function readAllBytes();
118
/* Object Magic Methods */
121
* Returns a description of the object for debugging purposes.
123
* Subclasses should override this method to provide a more specific
124
* description of the actual object being represented.
128
public function __toString()
130
return get_class($this);
137
* Returns the byte offset of the current read position within the data
142
public function getOffset()
144
return $this->_offset;
148
* Returns the total size in bytes of the data source.
152
public function getSize()
158
/* Primitive Methods */
161
* Moves the current read position to the specified byte offset.
163
* Throws an exception you attempt to move before the beginning or beyond
164
* the end of the data source.
166
* If a subclass needs to perform additional tasks (such as performing a
167
* fseek() on a filesystem source), it should do so after calling this
170
* @param integer $offset Destination byte offset.
171
* @throws Zend_Pdf_Exception
173
public function moveToOffset($offset)
175
if ($this->_offset == $offset) {
176
return; // Not moving; do nothing.
179
throw new Zend_Pdf_Exception('Attempt to move before start of data source',
180
Zend_Pdf_Exception::MOVE_BEFORE_START_OF_FILE);
182
if ($offset >= $this->_size) { // Offsets are zero-based.
183
throw new Zend_Pdf_Exception('Attempt to move beyond end of data source',
184
Zend_Pdf_Exception::MOVE_BEYOND_END_OF_FILE);
186
$this->_offset = $offset;
190
* Shifts the current read position within the data source by the specified
193
* You may move forward (positive numbers) or backward (negative numbers).
194
* Throws an exception you attempt to move before the beginning or beyond
195
* the end of the data source.
197
* @param integer $byteCount Number of bytes to skip.
198
* @throws Zend_Pdf_Exception
200
public function skipBytes($byteCount)
202
$this->moveToOffset($this->_offset + $byteCount);