~chroot64bit/zivios/gentoo-experimental

« back to all changes in this revision

Viewing changes to application/library/Zend/Pdf/FileParserDataSource.php

  • Committer: Mustafa A. Hashmi
  • Date: 2008-12-04 13:32:21 UTC
  • Revision ID: mhashmi@zivios.org-20081204133221-0nd1trunwevijj38
Inclusion of new installation framework with ties to zend layout and dojo layout

Show diffs side-by-side

added added

removed removed

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