1 | <?php
|
---|
2 |
|
---|
3 | class MemoryStream
|
---|
4 | {
|
---|
5 | public $Data;
|
---|
6 | public $Position;
|
---|
7 |
|
---|
8 | function __construct()
|
---|
9 | {
|
---|
10 | $this->Data = '';
|
---|
11 | $this->Position = 0;
|
---|
12 | }
|
---|
13 |
|
---|
14 | function __destruct()
|
---|
15 | {
|
---|
16 | }
|
---|
17 |
|
---|
18 | public function ReadBlock($Count)
|
---|
19 | {
|
---|
20 | $Result = substr($this->Data, $this->Position, $Count);
|
---|
21 | $this->Position = $this->Position + $Count;
|
---|
22 | return $Result;
|
---|
23 | }
|
---|
24 |
|
---|
25 | public function ReadByte()
|
---|
26 | {
|
---|
27 | $Result = $this->Data[$this->Position];
|
---|
28 | $this->Position = $this->Position + 1;
|
---|
29 | return $Result;
|
---|
30 | }
|
---|
31 |
|
---|
32 | public function ReadUint()
|
---|
33 | {
|
---|
34 | $val = unpack('V*', substr($this->Data, $this->Position, 4));
|
---|
35 | $this->Position = $this->Position + 4;
|
---|
36 | return $val[1];
|
---|
37 | }
|
---|
38 |
|
---|
39 | public function ReadInt()
|
---|
40 | {
|
---|
41 | $val = unpack('I*', substr($this->Data, $this->Position, 4));
|
---|
42 | $this->Position = $this->Position + 4;
|
---|
43 | return $val[1];
|
---|
44 | }
|
---|
45 |
|
---|
46 | public function ReadFloat()
|
---|
47 | {
|
---|
48 | $val = unpack('f*', substr($this->Data, $this->Position, 4));
|
---|
49 | $this->Position = $this->Position + 4;
|
---|
50 | return $val[1];
|
---|
51 | }
|
---|
52 |
|
---|
53 | public function ReadChar()
|
---|
54 | {
|
---|
55 | $Result = $this->Data[$this->Position];
|
---|
56 | $this->Position = $this->Position + 1;
|
---|
57 | return $Result;
|
---|
58 | }
|
---|
59 |
|
---|
60 | public function ReadLine($EndSymbol = "\r")
|
---|
61 | {
|
---|
62 | $Length = 0;
|
---|
63 | $StartPosition = $this->Position;
|
---|
64 | while (!$this->EOF())
|
---|
65 | {
|
---|
66 | $Char = $this->ReadChar();
|
---|
67 | if ($Char == $EndSymbol) break;
|
---|
68 | }
|
---|
69 | $Result = substr($this->Data, $StartPosition, $this->Position - $StartPosition);
|
---|
70 | return $Result;
|
---|
71 | }
|
---|
72 |
|
---|
73 | public function WriteBlock($Value, $Count)
|
---|
74 | {
|
---|
75 | $this->Data = substr_replace($this->Data, $Value, $this->Position, $Count);
|
---|
76 | $this->Position = $this->Position + $Count;
|
---|
77 | }
|
---|
78 |
|
---|
79 | public function WriteByte($Value)
|
---|
80 | {
|
---|
81 | $this->Data[$this->Position] = pack('C*', $Value);
|
---|
82 | $this->Position++;
|
---|
83 | }
|
---|
84 |
|
---|
85 | public function WriteUint($Value)
|
---|
86 | {
|
---|
87 | $this->WriteBlock(pack('V*', $Value), 4);
|
---|
88 | }
|
---|
89 |
|
---|
90 | public function WriteInt($Value)
|
---|
91 | {
|
---|
92 | $this->WriteBlock(pack('I*', $Value), 4);
|
---|
93 | }
|
---|
94 |
|
---|
95 | public function WriteFloat($Value)
|
---|
96 | {
|
---|
97 | $this->WriteBlock(pack('f*', $Value), 4);
|
---|
98 | }
|
---|
99 |
|
---|
100 | public function WriteChar($Value)
|
---|
101 | {
|
---|
102 | $this->Data[$this->Position] = pack('C*', $Value);
|
---|
103 | $this->Position++;
|
---|
104 | }
|
---|
105 |
|
---|
106 | public function WriteLine($Value)
|
---|
107 | {
|
---|
108 | $this->WriteBlock($Value."\r\n", strlen($Value) + 2);
|
---|
109 | }
|
---|
110 |
|
---|
111 | public function SetPosition($Position)
|
---|
112 | {
|
---|
113 | $this->Position = $Position;
|
---|
114 | }
|
---|
115 |
|
---|
116 | public function GetPosition()
|
---|
117 | {
|
---|
118 | return $this->Position;
|
---|
119 | }
|
---|
120 |
|
---|
121 | public function GetSize()
|
---|
122 | {
|
---|
123 | return strlen($this->Data);
|
---|
124 | }
|
---|
125 |
|
---|
126 | public function SetSize($Size)
|
---|
127 | {
|
---|
128 | return $this->Data = substr($this->Data, 0, $Size);
|
---|
129 | }
|
---|
130 |
|
---|
131 | public function EOF()
|
---|
132 | {
|
---|
133 | return $this->Position >= strlen($this->Data);
|
---|
134 | }
|
---|
135 | }
|
---|