[ZF-10089] Zend_Log
[zend/radio.git] / library / Zend / Pdf / FileParserDataSource.php
blob4371811cf2f5f4cc1c6aa020478bd7c59c86bea1
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
16 * @package Zend_Pdf
17 * @subpackage FileParser
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id$
23 /**
24 * Abstract helper class for {@link Zend_Pdf_FileParser} that provides the
25 * data source for parsing.
27 * Concrete subclasses allow for parsing of in-memory, filesystem, and other
28 * sources through a common API. These subclasses also take care of error
29 * handling and other mundane tasks.
31 * Subclasses must implement at minimum {@link __construct()},
32 * {@link __destruct()}, {@link readBytes()}, and {@link readAllBytes()}.
33 * Subclasses should also override {@link moveToOffset()} and
34 * {@link __toString()} as appropriate.
36 * @package Zend_Pdf
37 * @subpackage FileParser
38 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
39 * @license http://framework.zend.com/license/new-bsd New BSD License
41 abstract class Zend_Pdf_FileParserDataSource
43 /**** Instance Variables ****/
46 /**
47 * Total size in bytes of the data source.
48 * @var integer
50 protected $_size = 0;
52 /**
53 * Byte offset of the current read position within the data source.
54 * @var integer
56 protected $_offset = 0;
60 /**** Public Interface ****/
63 /* Abstract Methods */
65 /**
66 * Object constructor. Opens the data source for parsing.
68 * Must set $this->_size to the total size in bytes of the data source.
70 * Upon return the data source can be interrogated using the primitive
71 * methods described here.
73 * If the data source cannot be opened for any reason (such as insufficient
74 * permissions, missing file, etc.), will throw an appropriate exception.
76 * @throws Zend_Pdf_Exception
78 abstract public function __construct();
80 /**
81 * Object destructor. Closes the data source.
83 * May also perform cleanup tasks such as deleting temporary files.
85 abstract public function __destruct();
87 /**
88 * Returns the specified number of raw bytes from the data source at the
89 * byte offset of the current read position.
91 * Must advance the read position by the number of bytes read by updating
92 * $this->_offset.
94 * Throws an exception if there is insufficient data to completely fulfill
95 * the request or if an error occurs.
97 * @param integer $byteCount Number of bytes to read.
98 * @return string
99 * @throws Zend_Pdf_Exception
101 abstract public function readBytes($byteCount);
104 * Returns the entire contents of the data source as a string.
106 * This method may be called at any time and so must preserve the byte
107 * offset of the read position, both through $this->_offset and whatever
108 * other additional pointers (such as the seek position of a file pointer)
109 * that might be used.
111 * @return string
113 abstract public function readAllBytes();
116 /* Object Magic Methods */
119 * Returns a description of the object for debugging purposes.
121 * Subclasses should override this method to provide a more specific
122 * description of the actual object being represented.
124 * @return string
126 public function __toString()
128 return get_class($this);
132 /* Accessors */
135 * Returns the byte offset of the current read position within the data
136 * source.
138 * @return integer
140 public function getOffset()
142 return $this->_offset;
146 * Returns the total size in bytes of the data source.
148 * @return integer
150 public function getSize()
152 return $this->_size;
156 /* Primitive Methods */
159 * Moves the current read position to the specified byte offset.
161 * Throws an exception you attempt to move before the beginning or beyond
162 * the end of the data source.
164 * If a subclass needs to perform additional tasks (such as performing a
165 * fseek() on a filesystem source), it should do so after calling this
166 * parent method.
168 * @param integer $offset Destination byte offset.
169 * @throws Zend_Pdf_Exception
171 public function moveToOffset($offset)
173 if ($this->_offset == $offset) {
174 return; // Not moving; do nothing.
176 if ($offset < 0) {
177 require_once 'Zend/Pdf/Exception.php';
178 throw new Zend_Pdf_Exception('Attempt to move before start of data source',
179 Zend_Pdf_Exception::MOVE_BEFORE_START_OF_FILE);
181 if ($offset >= $this->_size) { // Offsets are zero-based.
182 require_once 'Zend/Pdf/Exception.php';
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
191 * number of bytes.
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);