2424namespace pocketmine \nbt ;
2525
2626use pmmp \encoding \Byte ;
27- use pmmp \encoding \ByteBuffer ;
27+ use pmmp \encoding \ByteBufferReader ;
28+ use pmmp \encoding \ByteBufferWriter ;
2829use pmmp \encoding \DataDecodeException ;
2930use pocketmine \nbt \tag \Tag ;
3031use function strlen ;
3334 * Base Named Binary Tag encoder/decoder
3435 */
3536abstract class BaseNbtSerializer implements NbtStreamReader, NbtStreamWriter{
36- protected ByteBuffer $ buffer ;
37-
38- public function __construct (){
39- $ this ->buffer = new ByteBuffer ();
40- }
37+ protected ByteBufferReader $ reader ;
38+ protected ByteBufferWriter $ writer ;
4139
4240 /**
4341 * @throws DataDecodeException
4442 * @throws NbtDataException
4543 */
4644 private function readRoot (int $ maxDepth ) : TreeRoot {
47- $ type = Byte::readUnsigned ($ this ->buffer );
45+ $ type = Byte::readUnsigned ($ this ->reader );
4846 if ($ type === NBT ::TAG_End){
4947 throw new NbtDataException ("Found TAG_End at the start of buffer " );
5048 }
@@ -61,15 +59,15 @@ private function readRoot(int $maxDepth) : TreeRoot{
6159 * @throws NbtDataException
6260 */
6361 public function read (string $ buffer , int &$ offset = 0 , int $ maxDepth = 0 ) : TreeRoot {
64- $ this ->buffer = new ByteBuffer ($ buffer );
65- $ this ->buffer -> setReadOffset ($ offset );
62+ $ this ->reader = new ByteBufferReader ($ buffer );
63+ $ this ->reader -> setOffset ($ offset );
6664
6765 try {
6866 $ data = $ this ->readRoot ($ maxDepth );
6967 }catch (DataDecodeException $ e ){
7068 throw new NbtDataException ($ e ->getMessage (), 0 , $ e );
7169 }
72- $ offset = $ this ->buffer -> getReadOffset ();
70+ $ offset = $ this ->reader -> getOffset ();
7371
7472 return $ data ;
7573 }
@@ -85,11 +83,11 @@ public function read(string $buffer, int &$offset = 0, int $maxDepth = 0) : Tree
8583 * @throws NbtDataException
8684 */
8785 public function readHeadless (string $ buffer , int $ rootType , int &$ offset = 0 , int $ maxDepth = 0 ) : Tag {
88- $ this ->buffer = new ByteBuffer ($ buffer );
89- $ this ->buffer -> setReadOffset ($ offset );
86+ $ this ->reader = new ByteBufferReader ($ buffer );
87+ $ this ->reader -> setOffset ($ offset );
9088
9189 $ data = NBT ::createTag ($ rootType , $ this , new ReaderTracker ($ maxDepth ));
92- $ offset = $ this ->buffer -> getReadOffset ();
90+ $ offset = $ this ->reader -> getOffset ();
9391
9492 return $ data ;
9593 }
@@ -104,11 +102,12 @@ public function readHeadless(string $buffer, int $rootType, int &$offset = 0, in
104102 * @throws NbtDataException
105103 */
106104 public function readMultiple (string $ buffer , int $ maxDepth = 0 ) : array {
107- $ this ->buffer = new ByteBuffer ($ buffer );
105+ $ this ->reader = new ByteBufferReader ($ buffer );
108106
109107 $ retval = [];
110108
111- while ($ this ->buffer ->getReadOffset () < $ this ->buffer ->getUsedLength ()){
109+ $ length = strlen ($ this ->reader ->getData ());
110+ while ($ this ->reader ->getOffset () < $ length ){
112111 try {
113112 $ retval [] = $ this ->readRoot ($ maxDepth );
114113 }catch (DataDecodeException $ e ){
@@ -120,17 +119,17 @@ public function readMultiple(string $buffer, int $maxDepth = 0) : array{
120119 }
121120
122121 private function writeRoot (TreeRoot $ root ) : void {
123- Byte::writeUnsigned ($ this ->buffer , $ root ->getTag ()->getType ());
122+ Byte::writeUnsigned ($ this ->writer , $ root ->getTag ()->getType ());
124123 $ this ->writeString ($ root ->getName ());
125124 $ root ->getTag ()->write ($ this );
126125 }
127126
128127 public function write (TreeRoot $ data ) : string {
129- $ this ->buffer = new ByteBuffer ();
128+ $ this ->writer = new ByteBufferWriter ();
130129
131130 $ this ->writeRoot ($ data );
132131
133- return $ this ->buffer -> toString ();
132+ return $ this ->writer -> getData ();
134133 }
135134
136135 /**
@@ -140,45 +139,45 @@ public function write(TreeRoot $data) : string{
140139 * @see BaseNbtSerializer::readHeadless()
141140 */
142141 public function writeHeadless (Tag $ data ) : string {
143- $ this ->buffer = new ByteBuffer ();
142+ $ this ->writer = new ByteBufferWriter ();
144143 $ data ->write ($ this );
145- return $ this ->buffer -> toString ();
144+ return $ this ->writer -> getData ();
146145 }
147146
148147 /**
149148 * @param TreeRoot[] $data
150149 */
151150 public function writeMultiple (array $ data ) : string {
152- $ this ->buffer = new ByteBuffer ();
151+ $ this ->writer = new ByteBufferWriter ();
153152 foreach ($ data as $ root ){
154153 $ this ->writeRoot ($ root );
155154 }
156- return $ this ->buffer -> toString ();
155+ return $ this ->writer -> getData ();
157156 }
158157
159158 public function readByte () : int {
160- return Byte::readUnsigned ($ this ->buffer );
159+ return Byte::readUnsigned ($ this ->reader );
161160 }
162161
163162 public function readSignedByte () : int {
164- return Byte::readSigned ($ this ->buffer );
163+ return Byte::readSigned ($ this ->reader );
165164 }
166165
167166 public function writeByte (int $ v ) : void {
168- Byte::writeUnsigned ($ this ->buffer , $ v );
167+ Byte::writeUnsigned ($ this ->writer , $ v );
169168 }
170169
171170 public function readByteArray () : string {
172171 $ length = $ this ->readInt ();
173172 if ($ length < 0 ){
174173 throw new NbtDataException ("Array length cannot be less than zero ( $ length < 0) " );
175174 }
176- return $ this ->buffer ->readByteArray ($ length );
175+ return $ this ->reader ->readByteArray ($ length );
177176 }
178177
179178 public function writeByteArray (string $ v ) : void {
180179 $ this ->writeInt (strlen ($ v )); //TODO: overflow
181- $ this ->buffer ->writeByteArray ($ v );
180+ $ this ->writer ->writeByteArray ($ v );
182181 }
183182
184183 /**
@@ -202,14 +201,14 @@ protected static function checkWriteStringLength(int $len) : int{
202201 }
203202
204203 public function readString () : string {
205- return $ this ->buffer ->readByteArray (self ::checkReadStringLength ($ this ->readShort ()));
204+ return $ this ->reader ->readByteArray (self ::checkReadStringLength ($ this ->readShort ()));
206205 }
207206
208207 /**
209208 * @throws \InvalidArgumentException if the string is too long
210209 */
211210 public function writeString (string $ v ) : void {
212211 $ this ->writeShort (self ::checkWriteStringLength (strlen ($ v )));
213- $ this ->buffer ->writeByteArray ($ v );
212+ $ this ->writer ->writeByteArray ($ v );
214213 }
215214}
0 commit comments