1 | <?php |
||
14 | abstract class InputStream |
||
15 | { |
||
16 | /** |
||
17 | * @var mixed 入力ストリーム |
||
18 | */ |
||
19 | protected $stream; |
||
20 | |||
21 | /** |
||
22 | * @var int 現在のポインタ位置 |
||
23 | */ |
||
24 | protected $cursorPosition; |
||
25 | |||
26 | /** |
||
27 | * @var int markしたポインタ位置 |
||
28 | */ |
||
29 | protected $markedPosition; |
||
30 | |||
31 | /** |
||
32 | * constructor |
||
33 | * @param mixed $stream 入力ストリーム |
||
34 | */ |
||
35 | public function __construct($stream) |
||
36 | { |
||
37 | $this->stream = $stream; |
||
38 | $this->cursorPosition = 0; |
||
39 | $this->markedPosition = 0; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * destructor |
||
44 | */ |
||
45 | public function __destruct() |
||
46 | { |
||
47 | $this->close(); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * 入力ストリームを閉じる |
||
52 | */ |
||
53 | abstract public function close(); |
||
54 | |||
55 | /** |
||
56 | * 入力ストリームからデータを読み込む |
||
57 | * 引数に数値を指定した場合、指定数値バイトだけ読み込む |
||
58 | * @param int length 読み込みバイト数 |
||
59 | * @return string 読み込みデータ |
||
60 | * @throws InvalidArgumentException |
||
61 | * @throws IOException |
||
62 | */ |
||
63 | abstract public function read($length = null); |
||
64 | |||
65 | /** |
||
66 | * 入力ストリームから指定バイト数後方へポインタを移動する |
||
67 | * @param int $pos 後方への移動バイト数(負数の場合は前方へ移動) |
||
68 | * @return int $skipNum 移動したバイト数、移動に失敗した場合-1 |
||
69 | */ |
||
70 | public function skip(int $pos) |
||
71 | { |
||
72 | return -1; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * EOFかどうか返却する |
||
77 | * @return bool EOFならtrue、EOF以外またはリードエラーの場合はfalse |
||
78 | */ |
||
79 | abstract public function eof(); |
||
80 | |||
81 | /** |
||
82 | * 入力ストリームの現在位置にmarkを設定する |
||
83 | * @param int マークするバイト位置 |
||
84 | * @throws IOException |
||
85 | */ |
||
86 | public function mark() |
||
87 | { |
||
88 | if (!$this->isMarkSupported()) { |
||
89 | throw new IOException(get_class($this) . " does not support mark."); |
||
90 | } |
||
91 | |||
92 | if ($this->stream === null) { |
||
93 | return null; |
||
94 | } |
||
95 | |||
96 | $this->markedPosition = $this->cursorPosition; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * 最後にmarkされた位置に再配置する |
||
101 | * @throws IOException |
||
102 | */ |
||
103 | public function reset() |
||
104 | { |
||
105 | if (!$this->isMarkSupported()) { |
||
106 | throw new IOException(get_class($this) . " does not support mark and reset."); |
||
107 | } |
||
108 | |||
109 | if ($this->stream === null) { |
||
110 | return null; |
||
111 | } |
||
112 | |||
113 | // mark位置を初期値に戻す |
||
114 | $this->cursorPosition = $this->markedPosition; |
||
115 | $this->markedPosition = 0; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * mark機能をサポートしているかどうか |
||
120 | * @return boolean マークをサポートしていればtrue |
||
121 | */ |
||
122 | public function isMarkSupported() |
||
123 | { |
||
124 | return false; |
||
125 | } |
||
126 | } |
||
127 |