@@ 18-65 (lines=48) @@ | ||
15 | * |
|
16 | * @package GravityMedia\Stream\Reader |
|
17 | */ |
|
18 | class Integer16Reader extends AbstractIntegerReader |
|
19 | { |
|
20 | /** |
|
21 | * Use byte order aware trait |
|
22 | */ |
|
23 | use ByteOrderAwareTrait; |
|
24 | ||
25 | /** |
|
26 | * Read unsigned 16-bit integer (short) data from the stream |
|
27 | * |
|
28 | * @return int |
|
29 | */ |
|
30 | protected function readUnsigned() |
|
31 | { |
|
32 | switch ($this->getByteOrder()) { |
|
33 | case ByteOrder::BIG_ENDIAN: |
|
34 | $format = 'n*'; |
|
35 | break; |
|
36 | case ByteOrder::LITTLE_ENDIAN: |
|
37 | $format = 'v*'; |
|
38 | break; |
|
39 | default: |
|
40 | $format = 'S*'; |
|
41 | } |
|
42 | ||
43 | list(, $value) = unpack($format, $this->getStream()->read(2)); |
|
44 | return $value; |
|
45 | } |
|
46 | ||
47 | /** |
|
48 | * Read signed 16-bit integer (short) data from the stream |
|
49 | * |
|
50 | * @return int |
|
51 | */ |
|
52 | protected function readSigned() |
|
53 | { |
|
54 | $data = $this->getStream()->read(2); |
|
55 | ||
56 | if (ByteOrder::MACHINE_ENDIAN !== $this->getByteOrder() |
|
57 | && $this->getMachineByteOrder() !== $this->getByteOrder() |
|
58 | ) { |
|
59 | $data = strrev($data); |
|
60 | } |
|
61 | ||
62 | list(, $value) = unpack('s*', $data); |
|
63 | return $value; |
|
64 | } |
|
65 | } |
|
66 |
@@ 18-65 (lines=48) @@ | ||
15 | * |
|
16 | * @package GravityMedia\Stream\Reader |
|
17 | */ |
|
18 | class Integer32Reader extends AbstractIntegerReader |
|
19 | { |
|
20 | /** |
|
21 | * Use byte order aware trait |
|
22 | */ |
|
23 | use ByteOrderAwareTrait; |
|
24 | ||
25 | /** |
|
26 | * Read unsigned 32-bit integer (long) data from the stream |
|
27 | * |
|
28 | * @return int |
|
29 | */ |
|
30 | protected function readUnsigned() |
|
31 | { |
|
32 | switch ($this->getByteOrder()) { |
|
33 | case ByteOrder::BIG_ENDIAN: |
|
34 | $format = 'N*'; |
|
35 | break; |
|
36 | case ByteOrder::LITTLE_ENDIAN: |
|
37 | $format = 'V*'; |
|
38 | break; |
|
39 | default: |
|
40 | $format = 'L*'; |
|
41 | } |
|
42 | ||
43 | list(, $value) = unpack($format, $this->getStream()->read(4)); |
|
44 | return $value; |
|
45 | } |
|
46 | ||
47 | /** |
|
48 | * Read signed 32-bit integer (long) data from the stream |
|
49 | * |
|
50 | * @return int |
|
51 | */ |
|
52 | protected function readSigned() |
|
53 | { |
|
54 | $data = $this->stream->read(4); |
|
55 | ||
56 | if (ByteOrder::MACHINE_ENDIAN !== $this->getByteOrder() |
|
57 | && $this->getMachineByteOrder() !== $this->getByteOrder() |
|
58 | ) { |
|
59 | $data = strrev($data); |
|
60 | } |
|
61 | ||
62 | list(, $value) = unpack('l*', $data); |
|
63 | return $value; |
|
64 | } |
|
65 | } |
|
66 |