@@ -163,32 +163,35 @@ public String read(int length) throws IOException {
163163 */
164164 public String readString () throws IOException {
165165 this .inputStream .mark (MARK_READ_LIMIT );
166- int index = 0 ;
167- while (index < MARK_READ_LIMIT ) {
168- byte b = this .inputStream .readByte ();
169- if (b == 0 ) {
170- break ;
166+ try {
167+ int index = 0 ;
168+ while (index < MARK_READ_LIMIT ) {
169+ byte b = this .inputStream .readByte ();
170+ if (b == 0 ) {
171+ break ;
172+ }
173+ index ++;
174+ if (index == MARK_READ_LIMIT ) {
175+ throw new IOException ("No null terminator found" );
176+ }
171177 }
172- index ++;
173- if ( index == MARK_READ_LIMIT ) {
174- throw new IOException ( "No null terminator found" ) ;
178+ if ( index == 0 ) {
179+ // Empty string, we don't have to ready anything.
180+ return "" ;
175181 }
176- }
177- // Reset the stream to the mark and read the name (if any).
178- this .inputStream .reset ();
179- if (index == 0 ) {
180- // No name, but we still need to skip the null-terminator.
182+
183+ // Reset the stream to the mark and read the string.
184+ this .inputStream .reset ();
185+ byte [] result = new byte [index ];
186+ this .inputStream .readFully (result );
187+ // Skip the null-terminator.
181188 //noinspection StatementWithEmptyBody
182189 while (this .inputStream .skip (1 ) < 1 ) {}
183- return "" ;
190+ return new String (result , StandardCharsets .UTF_8 );
191+ } finally {
192+ // Drop the mark.
193+ this .inputStream .mark (0 );
184194 }
185-
186- byte [] result = new byte [index ];
187- this .inputStream .readFully (result );
188- // Skip the null-terminator.
189- //noinspection StatementWithEmptyBody
190- while (this .inputStream .skip (1 ) < 1 ) {}
191- return new String (result , StandardCharsets .UTF_8 );
192195 }
193196
194197 /**
0 commit comments