44
55use PhpBinaryReader \Exception \InvalidDataException ;
66use PhpBinaryReader \Type \Bit ;
7+ use PhpBinaryReader \Type \Byte ;
78use PhpBinaryReader \Type \Int8 ;
89use PhpBinaryReader \Type \Int16 ;
910use PhpBinaryReader \Type \Int32 ;
@@ -46,6 +47,36 @@ class BinaryReader
4647 */
4748 private $ endian ;
4849
50+ /**
51+ * @var \PhpBinaryReader\Type\Byte
52+ */
53+ private $ byteReader ;
54+
55+ /**
56+ * @var \PhpBinaryReader\Type\Bit
57+ */
58+ private $ bitReader ;
59+
60+ /**
61+ * @var \PhpBinaryReader\Type\String
62+ */
63+ private $ stringReader ;
64+
65+ /**
66+ * @var \PhpBinaryReader\Type\Int8
67+ */
68+ private $ int8Reader ;
69+
70+ /**
71+ * @var \PhpBinaryReader\Type\Int16
72+ */
73+ private $ int16Reader ;
74+
75+ /**
76+ * @var \PhpBinaryReader\Type\Int32
77+ */
78+ private $ int32Reader ;
79+
4980 /**
5081 * @param string $str
5182 * @param int|string $endian
@@ -89,7 +120,7 @@ public function align()
89120 */
90121 public function readBits ($ count )
91122 {
92- return Bit:: readSigned ($ this , $ count );
123+ return $ this -> getBitReader ()-> readSigned ($ this , $ count );
93124 }
94125
95126 /**
@@ -98,55 +129,64 @@ public function readBits($count)
98129 */
99130 public function readUBits ($ count )
100131 {
101- return Bit::read ($ this , $ count );
132+ return $ this ->getBitReader ()->read ($ this , $ count );
133+ }
134+
135+ /**
136+ * @param int $count
137+ * @return int
138+ */
139+ public function readBytes ($ count )
140+ {
141+ return $ this ->getByteReader ()->read ($ this , $ count );
102142 }
103143
104144 /**
105145 * @return int
106146 */
107147 public function readInt8 ()
108148 {
109- return Int8:: readSigned ($ this );
149+ return $ this -> getInt8Reader ()-> readSigned ($ this );
110150 }
111151
112152 /**
113153 * @return int
114154 */
115155 public function readUInt8 ()
116156 {
117- return Int8:: read ($ this );
157+ return $ this -> getInt8Reader ()-> read ($ this );
118158 }
119159
120160 /**
121161 * @return int
122162 */
123163 public function readInt16 ()
124164 {
125- return Int16:: readSigned ($ this );
165+ return $ this -> getInt16Reader ()-> readSigned ($ this );
126166 }
127167
128168 /**
129169 * @return string
130170 */
131171 public function readUInt16 ()
132172 {
133- return Int16:: read ($ this );
173+ return $ this -> getInt16Reader ()-> read ($ this );
134174 }
135175
136176 /**
137177 * @return int
138178 */
139179 public function readInt32 ()
140180 {
141- return Int32:: readSigned ($ this );
181+ return $ this -> getInt32Reader ()-> readSigned ($ this );
142182 }
143183
144184 /**
145185 * @return int
146186 */
147187 public function readUInt32 ()
148188 {
149- return Int32:: read ($ this );
189+ return $ this -> getInt32Reader ()-> read ($ this );
150190 }
151191
152192 /**
@@ -155,7 +195,7 @@ public function readUInt32()
155195 */
156196 public function readString ($ length )
157197 {
158- return String:: read ($ this , $ length );
198+ return $ this -> getStringReader ()-> read ($ this , $ length );
159199 }
160200
161201 /**
@@ -164,7 +204,7 @@ public function readString($length)
164204 */
165205 public function readAlignedString ($ length )
166206 {
167- return String:: readAligned ($ this , $ length );
207+ return $ this -> getStringReader ()-> readAligned ($ this , $ length );
168208 }
169209
170210 /**
@@ -295,4 +335,76 @@ public function getCurrentBit()
295335 {
296336 return $ this ->currentBit ;
297337 }
338+
339+ /**
340+ * @return \PhpBinaryReader\Type\Bit
341+ */
342+ public function getBitReader ()
343+ {
344+ if (!$ this ->bitReader instanceof Bit) {
345+ $ this ->bitReader = new Bit ();
346+ }
347+
348+ return $ this ->bitReader ;
349+ }
350+
351+ /**
352+ * @return \PhpBinaryReader\Type\Byte
353+ */
354+ public function getByteReader ()
355+ {
356+ if (!$ this ->byteReader instanceof Byte) {
357+ $ this ->byteReader = new Byte ();
358+ }
359+
360+ return $ this ->byteReader ;
361+ }
362+
363+ /**
364+ * @return \PhpBinaryReader\Type\Int8
365+ */
366+ public function getInt8Reader ()
367+ {
368+ if (!$ this ->int8Reader instanceof Int8) {
369+ $ this ->int8Reader = new Int8 ();
370+ }
371+
372+ return $ this ->int8Reader ;
373+ }
374+
375+ /**
376+ * @return \PhpBinaryReader\Type\Int16
377+ */
378+ public function getInt16Reader ()
379+ {
380+ if (!$ this ->int16Reader instanceof Int16) {
381+ $ this ->int16Reader = new Int16 ();
382+ }
383+
384+ return $ this ->int16Reader ;
385+ }
386+
387+ /**
388+ * @return \PhpBinaryReader\Type\Int32
389+ */
390+ public function getInt32Reader ()
391+ {
392+ if (!$ this ->int32Reader instanceof Int32) {
393+ $ this ->int32Reader = new Int32 ();
394+ }
395+
396+ return $ this ->int32Reader ;
397+ }
398+
399+ /**
400+ * @return \PhpBinaryReader\Type\String
401+ */
402+ public function getStringReader ()
403+ {
404+ if (!$ this ->stringReader instanceof String) {
405+ $ this ->stringReader = new String ();
406+ }
407+
408+ return $ this ->stringReader ;
409+ }
298410}
0 commit comments