webstream-framework /
IO
| 1 | <?php |
||
| 2 | |||
| 3 | namespace WebStream\IO; |
||
| 4 | |||
| 5 | use WebStream\Exception\Extend\IOException; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * InputStream |
||
| 9 | * ftellの戻り値仕様により32ビット数値を返すため2GB以上のファイルを読み込むと動かなくなる |
||
| 10 | * C言語のftell仕様依存 |
||
| 11 | * @author Ryuichi TANAKA. |
||
| 12 | * @since 2016/02/05 |
||
| 13 | * @version 0.7 |
||
| 14 | */ |
||
| 15 | abstract class InputStream |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var mixed 入力ストリーム |
||
| 19 | */ |
||
| 20 | protected $stream; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var int 現在のポインタ位置 |
||
| 24 | */ |
||
| 25 | protected int $cursorPosition; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var int markしたポインタ位置 |
||
| 29 | */ |
||
| 30 | protected int $markedPosition; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * constructor |
||
| 34 | * @param mixed $stream 入力ストリーム |
||
| 35 | */ |
||
| 36 | 11 | public function __construct($stream) |
|
| 37 | { |
||
| 38 | 11 | $this->stream = $stream; |
|
| 39 | 11 | $this->cursorPosition = 0; |
|
| 40 | 11 | $this->markedPosition = 0; |
|
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * destructor |
||
| 45 | */ |
||
| 46 | 11 | public function __destruct() |
|
| 47 | { |
||
| 48 | 11 | $this->close(); |
|
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * 入力ストリームを閉じる |
||
| 53 | */ |
||
| 54 | abstract public function close(); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * 入力ストリームからデータを読み込む |
||
| 58 | * 引数に数値を指定した場合、指定数値バイトだけ読み込む |
||
| 59 | * @param int length 読み込みバイト数 |
||
| 60 | * @return string 読み込みデータ |
||
| 61 | * @throws IOException |
||
| 62 | */ |
||
| 63 | abstract public function read(int $length = 0); |
||
| 64 | |||
| 65 | /** |
||
| 66 | * 入力ストリームから行単位でデータを読み込む |
||
| 67 | * 末尾に改行コードは含まない |
||
| 68 | * @return string 読み込みデータ |
||
| 69 | */ |
||
| 70 | abstract public function readLine(); |
||
| 71 | |||
| 72 | /** |
||
| 73 | * 入力ストリームから指定バイト数後方へポインタを移動する |
||
| 74 | * @param int $pos 後方への移動バイト数(負数の場合は前方へ移動) |
||
| 75 | * @return int $skipNum 移動したバイト数、移動に失敗した場合-1 |
||
| 76 | */ |
||
| 77 | public function skip(int $pos) |
||
| 78 | { |
||
| 79 | return -1; |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * EOFかどうか返却する |
||
| 84 | * @return bool EOFならtrue、EOF以外またはリードエラーの場合はfalse |
||
| 85 | */ |
||
| 86 | abstract public function eof(); |
||
| 87 | |||
| 88 | /** |
||
| 89 | * 入力ストリームの現在位置にmarkを設定する |
||
| 90 | * @param int マークするバイト位置 |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 91 | * @throws IOException |
||
| 92 | */ |
||
| 93 | 2 | public function mark() |
|
| 94 | { |
||
| 95 | 2 | if (!$this->isMarkSupported()) { |
|
| 96 | throw new IOException(get_class($this) . " does not support mark."); |
||
| 97 | } |
||
| 98 | |||
| 99 | 2 | if ($this->stream === null) { |
|
| 100 | return; |
||
| 101 | } |
||
| 102 | |||
| 103 | 2 | $this->markedPosition = $this->cursorPosition; |
|
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * 最後にmarkされた位置に再配置する |
||
| 108 | * @throws IOException |
||
| 109 | */ |
||
| 110 | 2 | public function reset() |
|
| 111 | { |
||
| 112 | 2 | if (!$this->isMarkSupported()) { |
|
| 113 | throw new IOException(get_class($this) . " does not support mark and reset."); |
||
| 114 | } |
||
| 115 | |||
| 116 | 2 | if ($this->stream === null) { |
|
| 117 | return null; |
||
| 118 | } |
||
| 119 | |||
| 120 | // mark位置を初期値に戻す |
||
| 121 | 2 | $this->cursorPosition = $this->markedPosition; |
|
| 122 | 2 | $this->markedPosition = 0; |
|
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * mark機能をサポートしているかどうか |
||
| 127 | * @return bool マークをサポートしていればtrue |
||
| 128 | */ |
||
| 129 | public function isMarkSupported() |
||
| 130 | { |
||
| 131 | return false; |
||
| 132 | } |
||
| 133 | } |
||
| 134 |