FileParserDataSource.php 6.0 KB

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