|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the PHP Generics package. |
|
5
|
|
|
* |
|
6
|
|
|
* @package Generics |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace Generics\Streams; |
|
9
|
|
|
|
|
10
|
|
|
use Generics\FileNotFoundException; |
|
11
|
|
|
use Generics\LockException; |
|
12
|
|
|
use Generics\Lockable; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* This class provides an input stream for files. |
|
16
|
|
|
* |
|
17
|
|
|
* @author Maik Greubel <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class FileInputStream implements InputStream, Lockable |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* The file handle |
|
24
|
|
|
* |
|
25
|
|
|
* @var resource |
|
26
|
|
|
*/ |
|
27
|
|
|
private $handle; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The absolute file path and name |
|
31
|
|
|
* |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
private $fileName; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Whether the access is locked |
|
38
|
|
|
* |
|
39
|
|
|
* @var bool |
|
40
|
|
|
*/ |
|
41
|
|
|
private $locked; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Create a new FileInputStream |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $file |
|
47
|
|
|
* The absolute (or relative) path to the file to open |
|
48
|
|
|
* @throws FileNotFoundException |
|
49
|
|
|
*/ |
|
50
|
25 |
|
public function __construct($file) |
|
51
|
|
|
{ |
|
52
|
25 |
|
if (! file_exists($file)) { |
|
53
|
1 |
|
throw new FileNotFoundException("File {file} could not be found", array( |
|
54
|
1 |
|
'file' => $file |
|
55
|
|
|
)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
24 |
|
$this->handle = fopen($file, "rb"); |
|
59
|
|
|
|
|
60
|
24 |
|
if (! $this->ready()) { |
|
61
|
|
|
throw new StreamException("Could not open {file} for reading", array( |
|
62
|
|
|
'file' => $file |
|
63
|
|
|
)); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
24 |
|
$this->fileName = $file; |
|
67
|
24 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Cleanup (e.g. |
|
71
|
|
|
* release lock) |
|
72
|
|
|
*/ |
|
73
|
24 |
|
public function __destruct() |
|
74
|
|
|
{ |
|
75
|
|
|
try { |
|
76
|
24 |
|
if ($this->locked) { |
|
77
|
24 |
|
$this->unlock(); |
|
78
|
|
|
} |
|
79
|
|
|
} catch (\Generics\GenericsException $ex) { |
|
80
|
|
|
// Do nothing |
|
81
|
|
|
} |
|
82
|
24 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* |
|
86
|
|
|
* {@inheritdoc} |
|
87
|
|
|
* @see \Generics\Streams\Stream::close() |
|
88
|
|
|
*/ |
|
89
|
18 |
|
public function close() |
|
90
|
|
|
{ |
|
91
|
18 |
|
if ($this->handle != null) { |
|
92
|
18 |
|
fclose($this->handle); |
|
93
|
18 |
|
$this->handle = null; |
|
94
|
|
|
} |
|
95
|
18 |
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* |
|
99
|
|
|
* {@inheritdoc} |
|
100
|
|
|
* @see \Generics\Streams\Stream::ready() |
|
101
|
|
|
*/ |
|
102
|
24 |
|
public function ready(): bool |
|
103
|
|
|
{ |
|
104
|
24 |
|
return is_resource($this->handle) && ! feof($this->handle); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* |
|
109
|
|
|
* {@inheritdoc} |
|
110
|
|
|
* @see \Generics\Streams\InputStream::read() |
|
111
|
|
|
*/ |
|
112
|
20 |
|
public function read($length = 1, $offset = null): string |
|
113
|
|
|
{ |
|
114
|
20 |
|
if (! $this->ready()) { |
|
115
|
1 |
|
throw new StreamException("Stream is not ready!"); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
19 |
|
if ($offset !== null && intval($offset) > 0) { |
|
119
|
|
|
if (fseek($this->handle, $offset, SEEK_SET) != 0) { |
|
120
|
|
|
throw new StreamException("Could not set offset!"); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
19 |
|
return fread($this->handle, $length); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* |
|
129
|
|
|
* {@inheritdoc} |
|
130
|
|
|
* @see \Countable::count() |
|
131
|
|
|
*/ |
|
132
|
3 |
|
public function count(): int |
|
133
|
|
|
{ |
|
134
|
3 |
|
$stat = fstat($this->handle); |
|
135
|
3 |
|
return $stat['size']; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* |
|
140
|
|
|
* {@inheritdoc} |
|
141
|
|
|
* @see \Generics\Streams\InputStream::reset() |
|
142
|
|
|
*/ |
|
143
|
3 |
|
public function reset() |
|
144
|
|
|
{ |
|
145
|
3 |
|
fseek($this->handle, 0, SEEK_SET); |
|
146
|
3 |
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* |
|
150
|
|
|
* {@inheritdoc} |
|
151
|
|
|
* @see \Generics\Lockable::lock() |
|
152
|
|
|
*/ |
|
153
|
2 |
View Code Duplication |
public function lock() |
|
154
|
|
|
{ |
|
155
|
2 |
|
if ($this->locked || flock($this->handle, LOCK_SH) === false) { |
|
156
|
1 |
|
throw new LockException("Could not acquire lock"); |
|
157
|
|
|
} |
|
158
|
2 |
|
$this->locked = true; |
|
159
|
2 |
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* |
|
163
|
|
|
* {@inheritdoc} |
|
164
|
|
|
* @see \Generics\Lockable::unlock() |
|
165
|
|
|
*/ |
|
166
|
2 |
View Code Duplication |
public function unlock() |
|
167
|
|
|
{ |
|
168
|
2 |
|
if (! $this->locked || flock($this->handle, LOCK_UN) === false) { |
|
169
|
|
|
throw new LockException("Could not release lock"); |
|
170
|
|
|
} |
|
171
|
2 |
|
$this->locked = false; |
|
172
|
2 |
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* |
|
176
|
|
|
* {@inheritdoc} |
|
177
|
|
|
* @see \Generics\Lockable::isLocked() |
|
178
|
|
|
*/ |
|
179
|
1 |
|
public function isLocked(): bool |
|
180
|
|
|
{ |
|
181
|
1 |
|
return $this->locked; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Retrieve the file path and name |
|
186
|
|
|
* |
|
187
|
|
|
* @return string |
|
188
|
|
|
*/ |
|
189
|
|
|
public function __toString(): string |
|
190
|
|
|
{ |
|
191
|
|
|
return realpath($this->fileName); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* |
|
196
|
|
|
* {@inheritdoc} |
|
197
|
|
|
* @see \Generics\Streams\Stream::isOpen() |
|
198
|
|
|
*/ |
|
199
|
|
|
public function isOpen(): bool |
|
200
|
|
|
{ |
|
201
|
|
|
return is_resource($this->handle); |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|