@@ -33,6 +33,7 @@ public function __construct(BufferInterface $input = null)
3333 if ($ input instanceof BufferInterface) {
3434 $ this ->string = $ input ->getBinary ();
3535 $ this ->size = $ input ->getSize ();
36+ assert (strlen ($ this ->string ) === $ this ->size );
3637 }
3738 }
3839
@@ -85,64 +86,6 @@ public function readBytes(int $numBytes, bool $flipBytes = false): BufferInterfa
8586 return new Buffer ($ string , $ length );
8687 }
8788
88- /**
89- * Write $data as $bytes bytes. Can be flipped if needed.
90- *
91- * @param integer $numBytes - number of bytes to write
92- * @param SerializableInterface|BufferInterface|string $data - buffer, serializable or hex
93- * @param bool $flipBytes
94- * @return Parser
95- */
96- public function writeBytes (int $ numBytes , $ data , bool $ flipBytes = false ): Parser
97- {
98- // Treat $data to ensure it's a buffer, with the correct size
99- if ($ data instanceof SerializableInterface) {
100- $ data = $ data ->getBuffer ();
101- }
102-
103- if (is_string ($ data )) {
104- // Convert to a buffer
105- $ data = Buffer::hex ($ data , $ numBytes );
106- } else if (!($ data instanceof BufferInterface)) {
107- throw new \RuntimeException ('Invalid data passed to Parser::writeBytes ' );
108- }
109-
110- $ this ->writeBuffer ($ numBytes , $ data , $ flipBytes );
111-
112- return $ this ;
113- }
114-
115- /**
116- * Write $data as $bytes bytes. Can be flipped if needed.
117- *
118- * @param integer $numBytes
119- * @param string $data
120- * @param bool $flipBytes
121- * @return Parser
122- */
123- public function writeRawBinary (int $ numBytes , string $ data , bool $ flipBytes = false ): Parser
124- {
125- return $ this ->writeBuffer ($ numBytes , new Buffer ($ data , $ numBytes ), $ flipBytes );
126- }
127-
128- /**
129- * @param BufferInterface $buffer
130- * @param bool $flipBytes
131- * @param int $numBytes
132- * @return Parser
133- */
134- public function writeBuffer (int $ numBytes , BufferInterface $ buffer , bool $ flipBytes = false ): Parser
135- {
136- // only create a new buffer if the size does not match
137- if ($ buffer ->getSize () != $ numBytes ) {
138- $ buffer = new Buffer ($ buffer ->getBinary (), $ numBytes );
139- }
140-
141- $ this ->appendBuffer ($ buffer , $ flipBytes );
142-
143- return $ this ;
144- }
145-
14689 /**
14790 * @param BufferInterface $buffer
14891 * @param bool $flipBytes
@@ -184,7 +127,7 @@ public function writeArray(array $serializable): Parser
184127 }
185128
186129 if ($ object instanceof BufferInterface) {
187- $ parser ->writeBytes ($ object ->getSize (), $ object );
130+ $ parser ->appendBinary ($ object ->getBinary () );
188131 } else {
189132 throw new \RuntimeException ('Input to writeArray must be Buffer[], or SerializableInterface[] ' );
190133 }
@@ -203,6 +146,6 @@ public function writeArray(array $serializable): Parser
203146 */
204147 public function getBuffer (): BufferInterface
205148 {
206- return new Buffer ($ this ->string , null );
149+ return new Buffer ($ this ->string , $ this -> size );
207150 }
208151}
0 commit comments