FileParserDataSource.php
6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Pdf
* @subpackage FileParser
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: FileParserDataSource.php 23775 2011-03-01 17:25:24Z ralph $
*/
/**
* Abstract helper class for {@link Zend_Pdf_FileParser} that provides the
* data source for parsing.
*
* Concrete subclasses allow for parsing of in-memory, filesystem, and other
* sources through a common API. These subclasses also take care of error
* handling and other mundane tasks.
*
* Subclasses must implement at minimum {@link __construct()},
* {@link __destruct()}, {@link readBytes()}, and {@link readAllBytes()}.
* Subclasses should also override {@link moveToOffset()} and
* {@link __toString()} as appropriate.
*
* @package Zend_Pdf
* @subpackage FileParser
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
abstract class Zend_Pdf_FileParserDataSource
{
/**** Instance Variables ****/
/**
* Total size in bytes of the data source.
* @var integer
*/
protected $_size = 0;
/**
* Byte offset of the current read position within the data source.
* @var integer
*/
protected $_offset = 0;
/**** Public Interface ****/
/* Abstract Methods */
/**
* Object constructor. Opens the data source for parsing.
*
* Must set $this->_size to the total size in bytes of the data source.
*
* Upon return the data source can be interrogated using the primitive
* methods described here.
*
* If the data source cannot be opened for any reason (such as insufficient
* permissions, missing file, etc.), will throw an appropriate exception.
*
* @throws Zend_Pdf_Exception
*/
abstract public function __construct();
/**
* Object destructor. Closes the data source.
*
* May also perform cleanup tasks such as deleting temporary files.
*/
abstract public function __destruct();
/**
* Returns the specified number of raw bytes from the data source at the
* byte offset of the current read position.
*
* Must advance the read position by the number of bytes read by updating
* $this->_offset.
*
* Throws an exception if there is insufficient data to completely fulfill
* the request or if an error occurs.
*
* @param integer $byteCount Number of bytes to read.
* @return string
* @throws Zend_Pdf_Exception
*/
abstract public function readBytes($byteCount);
/**
* Returns the entire contents of the data source as a string.
*
* This method may be called at any time and so must preserve the byte
* offset of the read position, both through $this->_offset and whatever
* other additional pointers (such as the seek position of a file pointer)
* that might be used.
*
* @return string
*/
abstract public function readAllBytes();
/* Object Magic Methods */
/**
* Returns a description of the object for debugging purposes.
*
* Subclasses should override this method to provide a more specific
* description of the actual object being represented.
*
* @return string
*/
public function __toString()
{
return get_class($this);
}
/* Accessors */
/**
* Returns the byte offset of the current read position within the data
* source.
*
* @return integer
*/
public function getOffset()
{
return $this->_offset;
}
/**
* Returns the total size in bytes of the data source.
*
* @return integer
*/
public function getSize()
{
return $this->_size;
}
/* Primitive Methods */
/**
* Moves the current read position to the specified byte offset.
*
* Throws an exception you attempt to move before the beginning or beyond
* the end of the data source.
*
* If a subclass needs to perform additional tasks (such as performing a
* fseek() on a filesystem source), it should do so after calling this
* parent method.
*
* @param integer $offset Destination byte offset.
* @throws Zend_Pdf_Exception
*/
public function moveToOffset($offset)
{
if ($this->_offset == $offset) {
return; // Not moving; do nothing.
}
if ($offset < 0) {
require_once 'Zend/Pdf/Exception.php';
throw new Zend_Pdf_Exception('Attempt to move before start of data source',
Zend_Pdf_Exception::MOVE_BEFORE_START_OF_FILE);
}
if ($offset >= $this->_size) { // Offsets are zero-based.
require_once 'Zend/Pdf/Exception.php';
throw new Zend_Pdf_Exception('Attempt to move beyond end of data source',
Zend_Pdf_Exception::MOVE_BEYOND_END_OF_FILE);
}
$this->_offset = $offset;
}
/**
* Shifts the current read position within the data source by the specified
* number of bytes.
*
* You may move forward (positive numbers) or backward (negative numbers).
* Throws an exception you attempt to move before the beginning or beyond
* the end of the data source.
*
* @param integer $byteCount Number of bytes to skip.
* @throws Zend_Pdf_Exception
*/
public function skipBytes($byteCount)
{
$this->moveToOffset($this->_offset + $byteCount);
}
}