PocketMine-MP 5.15.1 git-fb9a74e8799c71ed8292cfa53abe7a4c9204629d
BinaryStream.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20*/
21
22declare(strict_types=1);
23
24namespace pocketmine\utils;
25
26use function chr;
27use function ord;
28use function strlen;
29use function substr;
30
32 //TODO: use typed properties when https://bugs.php.net/bug.php?id=81090 is fixed
33
35 protected $offset;
37 protected $buffer;
38
39 public function __construct(string $buffer = "", int $offset = 0){
40 $this->buffer = $buffer;
41 $this->offset = $offset;
42 }
43
47 public function rewind() : void{
48 $this->offset = 0;
49 }
50
51 public function setOffset(int $offset) : void{
52 $this->offset = $offset;
53 }
54
55 public function getOffset() : int{
56 return $this->offset;
57 }
58
59 public function getBuffer() : string{
60 return $this->buffer;
61 }
62
67 public function get(int $len) : string{
68 if($len === 0){
69 return "";
70 }
71 if($len < 0){
72 throw new \InvalidArgumentException("Length must be positive");
73 }
74
75 $remaining = strlen($this->buffer) - $this->offset;
76 if($remaining < $len){
77 throw new BinaryDataException("Not enough bytes left in buffer: need $len, have $remaining");
78 }
79
80 return $len === 1 ? $this->buffer[$this->offset++] : substr($this->buffer, ($this->offset += $len) - $len, $len);
81 }
82
87 public function getRemaining() : string{
88 $buflen = strlen($this->buffer);
89 if($this->offset >= $buflen){
90 throw new BinaryDataException("No bytes left to read");
91 }
92 $str = substr($this->buffer, $this->offset);
93 $this->offset = $buflen;
94 return $str;
95 }
96
97 public function put(string $str) : void{
98 $this->buffer .= $str;
99 }
100
105 public function getBool() : bool{
106 return $this->get(1) !== "\x00";
107 }
108
109 public function putBool(bool $v) : void{
110 $this->buffer .= ($v ? "\x01" : "\x00");
111 }
112
117 public function getByte() : int{
118 return ord($this->get(1));
119 }
120
121 public function putByte(int $v) : void{
122 $this->buffer .= chr($v);
123 }
124
129 public function getShort() : int{
130 return Binary::readShort($this->get(2));
131 }
132
137 public function getSignedShort() : int{
138 return Binary::readSignedShort($this->get(2));
139 }
140
141 public function putShort(int $v) : void{
142 $this->buffer .= Binary::writeShort($v);
143 }
144
149 public function getLShort() : int{
150 return Binary::readLShort($this->get(2));
151 }
152
157 public function getSignedLShort() : int{
158 return Binary::readSignedLShort($this->get(2));
159 }
160
161 public function putLShort(int $v) : void{
162 $this->buffer .= Binary::writeLShort($v);
163 }
164
169 public function getTriad() : int{
170 return Binary::readTriad($this->get(3));
171 }
172
173 public function putTriad(int $v) : void{
174 $this->buffer .= Binary::writeTriad($v);
175 }
176
181 public function getLTriad() : int{
182 return Binary::readLTriad($this->get(3));
183 }
184
185 public function putLTriad(int $v) : void{
186 $this->buffer .= Binary::writeLTriad($v);
187 }
188
193 public function getInt() : int{
194 return Binary::readInt($this->get(4));
195 }
196
197 public function putInt(int $v) : void{
198 $this->buffer .= Binary::writeInt($v);
199 }
200
205 public function getLInt() : int{
206 return Binary::readLInt($this->get(4));
207 }
208
209 public function putLInt(int $v) : void{
210 $this->buffer .= Binary::writeLInt($v);
211 }
212
217 public function getFloat() : float{
218 return Binary::readFloat($this->get(4));
219 }
220
225 public function getRoundedFloat(int $accuracy) : float{
226 return Binary::readRoundedFloat($this->get(4), $accuracy);
227 }
228
229 public function putFloat(float $v) : void{
230 $this->buffer .= Binary::writeFloat($v);
231 }
232
237 public function getLFloat() : float{
238 return Binary::readLFloat($this->get(4));
239 }
240
245 public function getRoundedLFloat(int $accuracy) : float{
246 return Binary::readRoundedLFloat($this->get(4), $accuracy);
247 }
248
249 public function putLFloat(float $v) : void{
250 $this->buffer .= Binary::writeLFloat($v);
251 }
252
257 public function getDouble() : float{
258 return Binary::readDouble($this->get(8));
259 }
260
261 public function putDouble(float $v) : void{
262 $this->buffer .= Binary::writeDouble($v);
263 }
264
269 public function getLDouble() : float{
270 return Binary::readLDouble($this->get(8));
271 }
272
273 public function putLDouble(float $v) : void{
274 $this->buffer .= Binary::writeLDouble($v);
275 }
276
281 public function getLong() : int{
282 return Binary::readLong($this->get(8));
283 }
284
285 public function putLong(int $v) : void{
286 $this->buffer .= Binary::writeLong($v);
287 }
288
293 public function getLLong() : int{
294 return Binary::readLLong($this->get(8));
295 }
296
297 public function putLLong(int $v) : void{
298 $this->buffer .= Binary::writeLLong($v);
299 }
300
307 public function getUnsignedVarInt() : int{
308 return Binary::readUnsignedVarInt($this->buffer, $this->offset);
309 }
310
314 public function putUnsignedVarInt(int $v) : void{
315 $this->put(Binary::writeUnsignedVarInt($v));
316 }
317
324 public function getVarInt() : int{
325 return Binary::readVarInt($this->buffer, $this->offset);
326 }
327
331 public function putVarInt(int $v) : void{
332 $this->put(Binary::writeVarInt($v));
333 }
334
341 public function getUnsignedVarLong() : int{
342 return Binary::readUnsignedVarLong($this->buffer, $this->offset);
343 }
344
348 public function putUnsignedVarLong(int $v) : void{
349 $this->buffer .= Binary::writeUnsignedVarLong($v);
350 }
351
358 public function getVarLong() : int{
359 return Binary::readVarLong($this->buffer, $this->offset);
360 }
361
365 public function putVarLong(int $v) : void{
366 $this->buffer .= Binary::writeVarLong($v);
367 }
368
372 public function feof() : bool{
373 return !isset($this->buffer[$this->offset]);
374 }
375}