PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/vendor/zend/Zend/Pdf/FileParserDataSource.php

http://zoop.googlecode.com/
PHP | 204 lines | 43 code | 24 blank | 137 comment | 4 complexity | 03b99ad8c0b890f9ba6e13a1e703e3c1 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: FileParserDataSource.php 20096 2010-01-06 02:05:09Z bkarwin $
  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-2010 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 constructor. Opens the data source for parsing.
  57. *
  58. * Must set $this->_size to the total size in bytes of the data source.
  59. *
  60. * Upon return the data source can be interrogated using the primitive
  61. * methods described here.
  62. *
  63. * If the data source cannot be opened for any reason (such as insufficient
  64. * permissions, missing file, etc.), will throw an appropriate exception.
  65. *
  66. * @throws Zend_Pdf_Exception
  67. */
  68. abstract public function __construct();
  69. /**
  70. * Object destructor. Closes the data source.
  71. *
  72. * May also perform cleanup tasks such as deleting temporary files.
  73. */
  74. abstract public function __destruct();
  75. /**
  76. * Returns the specified number of raw bytes from the data source at the
  77. * byte offset of the current read position.
  78. *
  79. * Must advance the read position by the number of bytes read by updating
  80. * $this->_offset.
  81. *
  82. * Throws an exception if there is insufficient data to completely fulfill
  83. * the request or if an error occurs.
  84. *
  85. * @param integer $byteCount Number of bytes to read.
  86. * @return string
  87. * @throws Zend_Pdf_Exception
  88. */
  89. abstract public function readBytes($byteCount);
  90. /**
  91. * Returns the entire contents of the data source as a string.
  92. *
  93. * This method may be called at any time and so must preserve the byte
  94. * offset of the read position, both through $this->_offset and whatever
  95. * other additional pointers (such as the seek position of a file pointer)
  96. * that might be used.
  97. *
  98. * @return string
  99. */
  100. abstract public function readAllBytes();
  101. /* Object Magic Methods */
  102. /**
  103. * Returns a description of the object for debugging purposes.
  104. *
  105. * Subclasses should override this method to provide a more specific
  106. * description of the actual object being represented.
  107. *
  108. * @return string
  109. */
  110. public function __toString()
  111. {
  112. return get_class($this);
  113. }
  114. /* Accessors */
  115. /**
  116. * Returns the byte offset of the current read position within the data
  117. * source.
  118. *
  119. * @return integer
  120. */
  121. public function getOffset()
  122. {
  123. return $this->_offset;
  124. }
  125. /**
  126. * Returns the total size in bytes of the data source.
  127. *
  128. * @return integer
  129. */
  130. public function getSize()
  131. {
  132. return $this->_size;
  133. }
  134. /* Primitive Methods */
  135. /**
  136. * Moves the current read position to the specified byte offset.
  137. *
  138. * Throws an exception you attempt to move before the beginning or beyond
  139. * the end of the data source.
  140. *
  141. * If a subclass needs to perform additional tasks (such as performing a
  142. * fseek() on a filesystem source), it should do so after calling this
  143. * parent method.
  144. *
  145. * @param integer $offset Destination byte offset.
  146. * @throws Zend_Pdf_Exception
  147. */
  148. public function moveToOffset($offset)
  149. {
  150. if ($this->_offset == $offset) {
  151. return; // Not moving; do nothing.
  152. }
  153. if ($offset < 0) {
  154. require_once 'Zend/Pdf/Exception.php';
  155. throw new Zend_Pdf_Exception('Attempt to move before start of data source',
  156. Zend_Pdf_Exception::MOVE_BEFORE_START_OF_FILE);
  157. }
  158. if ($offset >= $this->_size) { // Offsets are zero-based.
  159. require_once 'Zend/Pdf/Exception.php';
  160. throw new Zend_Pdf_Exception('Attempt to move beyond end of data source',
  161. Zend_Pdf_Exception::MOVE_BEYOND_END_OF_FILE);
  162. }
  163. $this->_offset = $offset;
  164. }
  165. /**
  166. * Shifts the current read position within the data source by the specified
  167. * number of bytes.
  168. *
  169. * You may move forward (positive numbers) or backward (negative numbers).
  170. * Throws an exception you attempt to move before the beginning or beyond
  171. * the end of the data source.
  172. *
  173. * @param integer $byteCount Number of bytes to skip.
  174. * @throws Zend_Pdf_Exception
  175. */
  176. public function skipBytes($byteCount)
  177. {
  178. $this->moveToOffset($this->_offset + $byteCount);
  179. }
  180. }