1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19

20
/**
21
 * Convenience class for reading files.
22
 *
23
 * @author <a href="mailto:yl@seasonfive.com">Yannick Lecaillez</a>
24
 *
25
 * @see FilterReader
26
 *
27
 * @package phing.system.io
28
 */
29
class BufferedReader extends Reader
30
{
31
    private $bufferSize = 0;
32
    private $buffer = null;
33
    private $bufferPos = 0;
34

35
    /**
36
     * The Reader we are buffering for.
37
     *
38
     * @var InputStreamReader
39
     */
40
    private $in;
41

42
    /**
43
     *
44
     * @param InputStreamReader $reader The reader (e.g. FileReader).
45
     * @param integer $buffsize The size of the buffer we should use for reading files.
46
     *                                    A large buffer ensures that most files (all scripts?)
47
     *                                    are parsed in 1 buffer.
48
     */
49 1
    public function __construct(InputStreamReader $reader, $buffsize = 65536)
50
    {
51 1
        $this->in = $reader;
52 1
        $this->bufferSize = $buffsize;
53
    }
54

55
    /**
56
     * Reads and returns a chunk of data.
57
     *
58
     * @param  int $len Number of bytes to read.  Default is to read configured buffer size number of bytes.
59
     * @return mixed buffer or -1 if EOF.
60
     */
61 1
    public function read($len = null)
62
    {
63

64
        // if $len is specified, we'll use that; otherwise, use the configured buffer size.
65 1
        if ($len === null) {
66 1
            $len = $this->bufferSize;
67
        }
68

69 1
        if (($data = $this->in->read($len)) !== -1) {
70
            // not all files end with a newline character, so we also need to check EOF
71 1
            if (!$this->in->eof()) {
72 1
                $notValidPart = strrchr($data, "\n");
73 1
                $notValidPartSize = strlen($notValidPart);
74

75 1
                if ($notValidPartSize > 1) {
76
                    // Block doesn't finish on a EOL
77
                    // Find the last EOL and forget all following stuff
78 0
                    $dataSize = strlen($data);
79 0
                    $validSize = $dataSize - $notValidPartSize + 1;
80

81 0
                    $data = substr($data, 0, $validSize);
82

83
                    // Rewind to the beginning of the forgotten stuff.
84 0
                    $this->in->skip(-$notValidPartSize + 1);
85
                }
86
            } // if !EOF
87
        }
88

89 1
        return $data;
90
    }
91

92
    /**
93
     * @param int $n
94
     * @return int
95
     */
96 0
    public function skip($n)
97
    {
98 0
        return $this->in->skip($n);
99
    }
100

101 0
    public function reset()
102
    {
103 0
        $this->in->reset();
104
    }
105

106 1
    public function close()
107
    {
108 1
        $this->in->close();
109
    }
110

111
    /**
112
     * Read a line from input stream.
113
     */
114 1
    public function readLine()
115
    {
116 1
        $line = null;
117 1
        while (($ch = $this->readChar()) !== -1) {
118 1
            if ($ch === "\n") {
119 1
                break;
120
            }
121 1
            $line .= $ch;
122
        }
123

124
        // Warning : Not considering an empty line as an EOF
125 1
        if ($line === null && $ch !== -1) {
126 1
            return "";
127
        }
128

129 1
        return $line;
130
    }
131

132
    /**
133
     * Reads a single char from the reader.
134
     *
135
     * @return string single char or -1 if EOF.
136
     */
137 1
    public function readChar()
138
    {
139 1
        if ($this->buffer === null) {
140
            // Buffer is empty, fill it ...
141 1
            $read = $this->in->read($this->bufferSize);
142 1
            if ($read === -1) {
143 1
                $ch = -1;
144
            } else {
145 1
                $this->buffer = $read;
146

147 1
                return $this->readChar(); // recurse
148
            }
149
        } else {
150
            // Get next buffered char ...
151
            // handle case where buffer is read-in, but is empty.  The next readChar() will return -1 EOF,
152
            // so we just return empty string (char) at this point.  (Probably could also return -1 ...?)
153 1
            $ch = ($this->buffer !== "") ? $this->buffer[$this->bufferPos] : '';
154 1
            $this->bufferPos++;
155 1
            if ($this->bufferPos >= strlen($this->buffer)) {
156 1
                $this->buffer = null;
157 1
                $this->bufferPos = 0;
158
            }
159
        }
160

161 1
        return $ch;
162
    }
163

164
    /**
165
     * Returns whether eof has been reached in stream.
166
     * This is important, because filters may want to know if the end of the file (and not just buffer)
167
     * has been reached.
168
     *
169
     * @return boolean
170
     */
171 1
    public function eof()
172
    {
173 1
        return $this->in->eof();
174
    }
175

176
    /**
177
     * @return string
178
     */
179 1
    public function getResource()
180
    {
181 1
        return $this->in->getResource();
182
    }
183
}

Read our documentation on viewing source code .

Loading