1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\CsvToken\Buffer; |
4
|
|
|
|
5
|
|
|
class StreamBuffer implements BufferInterface |
6
|
|
|
{ |
7
|
|
|
const READ_LENGTH = 128; |
8
|
|
|
|
9
|
|
|
/** @var string */ |
10
|
|
|
protected $contents; |
11
|
|
|
/** @var int */ |
12
|
|
|
protected $length; |
13
|
|
|
/** @var int */ |
14
|
|
|
protected $position; |
15
|
|
|
/** @var bool */ |
16
|
|
|
protected $eof = false; |
17
|
|
|
/** @var int */ |
18
|
|
|
protected $minSize; |
19
|
|
|
|
20
|
|
|
/** @var resource */ |
21
|
|
|
private $stream; |
22
|
|
|
/** @var int */ |
23
|
|
|
private $readLength; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Create a buffer around a stream |
27
|
|
|
* |
28
|
|
|
* @param resource $stream |
29
|
|
|
* @param int $readLength |
30
|
|
|
* @param int $minSize |
31
|
|
|
*/ |
32
|
|
|
public function __construct($stream, $readLength = self::READ_LENGTH, $minSize = -1) |
33
|
|
|
{ |
34
|
|
|
$this->stream = $stream; |
35
|
|
|
$this->readLength = $readLength; |
36
|
|
|
$this->position = ftell($stream); |
37
|
|
|
$this->minSize = $minSize; |
38
|
|
|
$this->length = 0; |
39
|
|
|
$this->eof = false; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
public function getContents() |
46
|
|
|
{ |
47
|
|
|
return $this->contents; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return int |
52
|
|
|
*/ |
53
|
|
|
public function getLength() |
54
|
|
|
{ |
55
|
|
|
return $this->length; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
public function isEof() |
62
|
|
|
{ |
63
|
|
|
return $this->length === 0; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
|
|
public function read() |
70
|
|
|
{ |
71
|
|
|
if ($this->canRead()) { |
72
|
|
|
$next = fread($this->stream, $this->readLength); |
73
|
|
|
if (strlen($next) > 0) { |
74
|
|
|
$this->contents .= $next; |
75
|
|
|
$this->length += strlen($next); |
76
|
|
|
return true; |
77
|
|
|
} else { |
78
|
|
|
$this->eof = true; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
return false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Remove the first $length characters from the buffer |
86
|
|
|
* |
87
|
|
|
* @param int $length |
88
|
|
|
* |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
public function move($length) |
92
|
|
|
{ |
93
|
|
|
$this->contents = substr($this->contents, $length); |
94
|
|
|
$newLen = max(0, $this->length - $length); |
95
|
|
|
$this->position += $this->length - $newLen; |
96
|
|
|
$this->length = $newLen; |
97
|
|
|
return true; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Determine if we can read more from the stream |
102
|
|
|
* |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
|
|
private function canRead() |
106
|
|
|
{ |
107
|
|
|
return ((!$this->eof) |
108
|
|
|
&& ($this->minSize < 0 || $this->length <= $this->minSize)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
|
|
public function isSourceEof() |
115
|
|
|
{ |
116
|
|
|
return $this->eof; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return int |
121
|
|
|
*/ |
122
|
|
|
public function getPosition() |
123
|
|
|
{ |
124
|
|
|
return $this->position; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return int |
129
|
|
|
*/ |
130
|
|
|
public function getMinBufferSize() |
131
|
|
|
{ |
132
|
|
|
return $this->minSize; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param int $size |
137
|
|
|
* |
138
|
|
|
* @return $this |
139
|
|
|
*/ |
140
|
|
|
public function setMinBufferSize($size) |
141
|
|
|
{ |
142
|
|
|
$this->minSize = $size; |
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|