File indexing completed on 2024-05-12 06:02:53

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Pdf
0017  * @subpackage FileParser
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * Abstract helper class for {@link Zend_Pdf_FileParser} that provides the
0025  * data source for parsing.
0026  *
0027  * Concrete subclasses allow for parsing of in-memory, filesystem, and other
0028  * sources through a common API. These subclasses also take care of error
0029  * handling and other mundane tasks.
0030  *
0031  * Subclasses must implement at minimum {@link __construct()},
0032  * {@link __destruct()}, {@link readBytes()}, and {@link readAllBytes()}.
0033  * Subclasses should also override {@link moveToOffset()} and
0034  * {@link __toString()} as appropriate.
0035  *
0036  * @package    Zend_Pdf
0037  * @subpackage FileParser
0038  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0039  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0040  */
0041 abstract class Zend_Pdf_FileParserDataSource
0042 {
0043   /**** Instance Variables ****/
0044 
0045 
0046     /**
0047      * Total size in bytes of the data source.
0048      * @var integer
0049      */
0050     protected $_size = 0;
0051 
0052     /**
0053      * Byte offset of the current read position within the data source.
0054      * @var integer
0055      */
0056     protected $_offset = 0;
0057 
0058 
0059 
0060   /**** Public Interface ****/
0061 
0062 
0063   /* Abstract Methods */
0064 
0065     /**
0066      * Object destructor. Closes the data source.
0067      *
0068      * May also perform cleanup tasks such as deleting temporary files.
0069      */
0070     abstract public function __destruct();
0071 
0072     /**
0073      * Returns the specified number of raw bytes from the data source at the
0074      * byte offset of the current read position.
0075      *
0076      * Must advance the read position by the number of bytes read by updating
0077      * $this->_offset.
0078      *
0079      * Throws an exception if there is insufficient data to completely fulfill
0080      * the request or if an error occurs.
0081      *
0082      * @param integer $byteCount Number of bytes to read.
0083      * @return string
0084      * @throws Zend_Pdf_Exception
0085      */
0086     abstract public function readBytes($byteCount);
0087 
0088     /**
0089      * Returns the entire contents of the data source as a string.
0090      *
0091      * This method may be called at any time and so must preserve the byte
0092      * offset of the read position, both through $this->_offset and whatever
0093      * other additional pointers (such as the seek position of a file pointer)
0094      * that might be used.
0095      *
0096      * @return string
0097      */
0098     abstract public function readAllBytes();
0099 
0100 
0101   /* Object Magic Methods */
0102 
0103     /**
0104      * Returns a description of the object for debugging purposes.
0105      *
0106      * Subclasses should override this method to provide a more specific
0107      * description of the actual object being represented.
0108      *
0109      * @return string
0110      */
0111     public function __toString()
0112     {
0113         return get_class($this);
0114     }
0115 
0116 
0117   /* Accessors */
0118 
0119     /**
0120      * Returns the byte offset of the current read position within the data
0121      * source.
0122      *
0123      * @return integer
0124      */
0125     public function getOffset()
0126     {
0127         return $this->_offset;
0128     }
0129 
0130     /**
0131      * Returns the total size in bytes of the data source.
0132      *
0133      * @return integer
0134      */
0135     public function getSize()
0136     {
0137         return $this->_size;
0138     }
0139 
0140 
0141   /* Primitive Methods */
0142 
0143     /**
0144      * Moves the current read position to the specified byte offset.
0145      *
0146      * Throws an exception you attempt to move before the beginning or beyond
0147      * the end of the data source.
0148      *
0149      * If a subclass needs to perform additional tasks (such as performing a
0150      * fseek() on a filesystem source), it should do so after calling this
0151      * parent method.
0152      *
0153      * @param integer $offset Destination byte offset.
0154      * @throws Zend_Pdf_Exception
0155      */
0156     public function moveToOffset($offset)
0157     {
0158         if ($this->_offset == $offset) {
0159             return;    // Not moving; do nothing.
0160         }
0161         if ($offset < 0) {
0162             // require_once 'Zend/Pdf/Exception.php';
0163             throw new Zend_Pdf_Exception('Attempt to move before start of data source',
0164                                          Zend_Pdf_Exception::MOVE_BEFORE_START_OF_FILE);
0165         }
0166         if ($offset >= $this->_size) {    // Offsets are zero-based.
0167             // require_once 'Zend/Pdf/Exception.php';
0168             throw new Zend_Pdf_Exception('Attempt to move beyond end of data source',
0169                                          Zend_Pdf_Exception::MOVE_BEYOND_END_OF_FILE);
0170         }
0171         $this->_offset = $offset;
0172     }
0173 
0174     /**
0175      * Shifts the current read position within the data source by the specified
0176      * number of bytes.
0177      *
0178      * You may move forward (positive numbers) or backward (negative numbers).
0179      * Throws an exception you attempt to move before the beginning or beyond
0180      * the end of the data source.
0181      *
0182      * @param integer $byteCount Number of bytes to skip.
0183      * @throws Zend_Pdf_Exception
0184      */
0185     public function skipBytes($byteCount)
0186     {
0187         $this->moveToOffset($this->_offset + $byteCount);
0188     }
0189 }