-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputStream.php
133 lines (114 loc) · 3.22 KB
/
InputStream.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
<?php
namespace WebStream\IO;
use WebStream\Exception\Extend\IOException;
/**
* InputStream
* ftellの戻り値仕様により32ビット数値を返すため2GB以上のファイルを読み込むと動かなくなる
* C言語のftell仕様依存
* @author Ryuichi TANAKA.
* @since 2016/02/05
* @version 0.7
*/
abstract class InputStream
{
/**
* @var mixed 入力ストリーム
*/
protected $stream;
/**
* @var int 現在のポインタ位置
*/
protected int $cursorPosition;
/**
* @var int markしたポインタ位置
*/
protected int $markedPosition;
/**
* constructor
* @param mixed $stream 入力ストリーム
*/
public function __construct($stream)
{
$this->stream = $stream;
$this->cursorPosition = 0;
$this->markedPosition = 0;
}
/**
* destructor
*/
public function __destruct()
{
$this->close();
}
/**
* 入力ストリームを閉じる
*/
abstract public function close();
/**
* 入力ストリームからデータを読み込む
* 引数に数値を指定した場合、指定数値バイトだけ読み込む
* @param int length 読み込みバイト数
* @return string 読み込みデータ
* @throws IOException
*/
abstract public function read(int $length = 0);
/**
* 入力ストリームから行単位でデータを読み込む
* 末尾に改行コードは含まない
* @return string 読み込みデータ
*/
abstract public function readLine();
/**
* 入力ストリームから指定バイト数後方へポインタを移動する
* @param int $pos 後方への移動バイト数(負数の場合は前方へ移動)
* @return int $skipNum 移動したバイト数、移動に失敗した場合-1
*/
public function skip(int $pos)
{
return -1;
}
/**
* EOFかどうか返却する
* @return bool EOFならtrue、EOF以外またはリードエラーの場合はfalse
*/
abstract public function eof();
/**
* 入力ストリームの現在位置にmarkを設定する
* @param int マークするバイト位置
* @throws IOException
*/
public function mark()
{
if (!$this->isMarkSupported()) {
throw new IOException(get_class($this) . " does not support mark.");
}
if ($this->stream === null) {
return;
}
$this->markedPosition = $this->cursorPosition;
}
/**
* 最後にmarkされた位置に再配置する
* @throws IOException
*/
public function reset()
{
if (!$this->isMarkSupported()) {
throw new IOException(get_class($this) . " does not support mark and reset.");
}
if ($this->stream === null) {
return null;
}
// mark位置を初期値に戻す
$this->cursorPosition = $this->markedPosition;
$this->markedPosition = 0;
}
/**
* mark機能をサポートしているかどうか
* @return bool マークをサポートしていればtrue
*/
public function isMarkSupported()
{
return false;
}
}