-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileInputStream.php
182 lines (154 loc) · 4.42 KB
/
FileInputStream.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
namespace WebStream\IO;
use WebStream\Exception\Extend\InvalidArgumentException;
use WebStream\Exception\Extend\IOException;
/**
* FileInputStream
* @author Ryuichi TANAKA.
* @since 2016/02/05
* @version 0.7
*/
class FileInputStream extends InputStream
{
/**
* @var File ファイルオブジェクト
*/
protected File $file;
/**
* constructor
* @param mixed $file ファイルオブジェクトまたはファイルパス
* @throws InvalidArgumentException
* @throws IOException
*/
public function __construct($file)
{
if ($file instanceof File) {
$this->file = $file;
} elseif (is_string($file)) {
$this->file = new File($file);
} else {
throw new InvalidArgumentException("Unable to open file: " . $file);
}
// 読み込みはロックを掛けずダーティーリード
$stream = fopen($this->file->getAbsoluteFilePath(), 'r');
if (!is_resource($stream) || $stream === false) {
throw new IOException("Unable open " . $this->file->getAbsoluteFilePath());
}
parent::__construct($stream);
}
/**
* 入力ストリームを閉じる
*/
public function close()
{
if ($this->stream === null) {
return;
}
if (get_resource_type($this->stream) !== 'Unknown' && fclose($this->stream) === false) {
throw new IOException("Cannot close input stream.");
}
$this->stream = null;
}
/**
* {@inheritdoc}
*/
public function read(int $length = 0)
{
if ($this->stream === null) {
return null;
}
if ($this->eof()) {
return null;
}
$out = null;
if ($length === 0) {
if (($out = @fread($this->stream, 1)) === false) {
throw new IOException("Failed to read stream.");
}
} else {
// ポインタ位置が負になった場合、警告が出てfalseを返す
// ポインタの終端を越えた場合、読み込みを終了する
// すでに終端位置の場合、空文字を返す
if (($out = @fread($this->stream, $length)) === false) {
throw new IOException("Failed to read stream.");
}
}
return $out;
}
/**
* 入力ストリームから行単位でデータを読み込む
* 末尾に改行コードは含まない
* @return string 読み込みデータ
*/
public function readLine()
{
if ($this->stream === null) {
return null;
}
if ($this->eof()) {
return null;
}
$out = fgets($this->stream);
if ($out === false) {
return null;
}
$this->cursorPosition = ftell($this->stream);
return trim($out);
}
/**
* {@inheritdoc}
*/
public function skip(int $pos)
{
if ($this->stream === null) {
return -1;
}
// 現在のポインタ位置から$posだけ後方へ移動
// シークに対応していないファイルシステムの場合、-1を返す
if (fseek($this->stream, $pos, SEEK_CUR) === -1) {
return -1;
}
$start = $this->cursorPosition;
$this->cursorPosition = ftell($this->stream);
$skipNum = 0;
if ($start > $this->cursorPosition) {
// 後方へ移動
$skipNum = $start - $this->cursorPosition;
} else {
// 前方へ移動
$skipNum = $this->cursorPosition - $start;
}
return $skipNum;
}
/**
* {@inheritdoc}
*/
public function reset()
{
if (!$this->isMarkSupported()) {
throw new IOException(get_class($this) . " does not support mark and reset.");
}
if ($this->stream === null) {
return;
}
// ポインタ位置をmark位置に移動
fseek($this->stream, $this->markedPosition, SEEK_SET);
// mark位置を初期値に戻す
$this->cursorPosition = $this->markedPosition;
$this->markedPosition = 0;
}
/**
* {@inheritdoc}
*/
public function eof()
{
return feof($this->stream);
}
/**
* {@inheritdoc}
*/
public function isMarkSupported()
{
return true;
}
}