Completed
Push — master ( 2e5bdd...9e2471 )
by Ryuichi
05:26
created

InputStream   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
cbo 1
dl 0
loc 113
ccs 0
cts 39
cp 0
rs 10
c 0
b 0
f 0
lcom 1
1
<?php
2
namespace WebStream\IO;
3
4
use WebStream\Exception\Extend\IOException;
5
6
/**
7
 * InputStream
8
 * ftellの戻り値仕様により32ビット数値を返すため2GB以上のファイルを読み込むと動かなくなる
9
 * C言語のftell仕様依存
10
 * @author Ryuichi TANAKA.
11
 * @since 2016/02/05
12
 * @version 0.7
13
 */
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