FileParserDataSource.php 5.5 KB

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