|
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
|
32 |
|
public function __construct($stream, $readLength = self::READ_LENGTH, $minSize = -1) |
|
33
|
|
|
{ |
|
34
|
32 |
|
$this->stream = $stream; |
|
35
|
32 |
|
$this->readLength = $readLength; |
|
36
|
32 |
|
$this->position = ftell($stream); |
|
37
|
32 |
|
$this->minSize = $minSize; |
|
38
|
32 |
|
$this->length = 0; |
|
39
|
32 |
|
$this->eof = false; |
|
40
|
32 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
30 |
|
public function getContents() |
|
46
|
|
|
{ |
|
47
|
30 |
|
return $this->contents; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return int |
|
52
|
|
|
*/ |
|
53
|
27 |
|
public function getLength() |
|
54
|
|
|
{ |
|
55
|
27 |
|
return $this->length; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return bool |
|
60
|
|
|
*/ |
|
61
|
29 |
|
public function isEof() |
|
62
|
|
|
{ |
|
63
|
29 |
|
return $this->length === 0; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return bool |
|
68
|
|
|
*/ |
|
69
|
32 |
|
public function read() |
|
70
|
|
|
{ |
|
71
|
32 |
|
if ($this->canRead()) { |
|
72
|
32 |
|
$next = fread($this->stream, $this->readLength); |
|
73
|
32 |
|
if (strlen($next) > 0) { |
|
74
|
30 |
|
$this->contents .= $next; |
|
75
|
30 |
|
$this->length += strlen($next); |
|
76
|
30 |
|
return true; |
|
77
|
|
|
} else { |
|
78
|
29 |
|
$this->eof = true; |
|
79
|
|
|
} |
|
80
|
29 |
|
} |
|
81
|
30 |
|
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
|
27 |
|
public function move($length) |
|
92
|
|
|
{ |
|
93
|
27 |
|
$this->contents = substr($this->contents, $length); |
|
94
|
27 |
|
$newLen = max(0, $this->length - $length); |
|
95
|
27 |
|
$this->position += $this->length - $newLen; |
|
96
|
27 |
|
$this->length = $newLen; |
|
97
|
27 |
|
return true; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Determine if we can read more from the stream |
|
102
|
|
|
* |
|
103
|
|
|
* @return bool |
|
104
|
|
|
*/ |
|
105
|
32 |
|
private function canRead() |
|
106
|
|
|
{ |
|
107
|
32 |
|
return ((!$this->eof) |
|
108
|
32 |
|
&& ($this->minSize < 0 || $this->length <= $this->minSize)); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return bool |
|
113
|
|
|
*/ |
|
114
|
1 |
|
public function isSourceEof() |
|
115
|
|
|
{ |
|
116
|
1 |
|
return $this->eof; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @return int |
|
121
|
|
|
*/ |
|
122
|
27 |
|
public function getPosition() |
|
123
|
|
|
{ |
|
124
|
27 |
|
return $this->position; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return int |
|
129
|
|
|
*/ |
|
130
|
1 |
|
public function getMinBufferSize() |
|
131
|
|
|
{ |
|
132
|
1 |
|
return $this->minSize; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param int $size |
|
137
|
|
|
* |
|
138
|
|
|
* @return $this |
|
139
|
|
|
*/ |
|
140
|
4 |
|
public function setMinBufferSize($size) |
|
141
|
|
|
{ |
|
142
|
4 |
|
$this->minSize = $size; |
|
143
|
4 |
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|