19
19
import java .io .ByteArrayOutputStream ;
20
20
import java .io .IOException ;
21
21
import java .io .InputStream ;
22
+ import java .io .InputStreamReader ;
22
23
import java .nio .ByteBuffer ;
23
24
import java .nio .charset .Charset ;
24
25
import org .json .JSONException ;
@@ -38,6 +39,7 @@ public class BundleReader {
38
39
39
40
private final BundleSerializer serializer ;
40
41
private final InputStream bundleInputStream ;
42
+ private final InputStreamReader dataReader ;
41
43
42
44
@ Nullable BundleMetadata metadata ;
43
45
private ByteBuffer buffer ;
@@ -46,6 +48,7 @@ public class BundleReader {
46
48
public BundleReader (BundleSerializer serializer , InputStream bundleInputStream ) {
47
49
this .serializer = serializer ;
48
50
this .bundleInputStream = bundleInputStream ;
51
+ this .dataReader = new InputStreamReader (this .bundleInputStream );
49
52
buffer = ByteBuffer .allocate (BUFFER_CAPACITY );
50
53
51
54
buffer .flip (); // Start the buffer in "reading mode"
@@ -135,9 +138,9 @@ private BundleElement readNextElement() throws IOException, JSONException {
135
138
throw abort ("Reached the end of bundle when a length string is expected." );
136
139
}
137
140
138
- byte [] c = new byte [nextOpenBracket ];
139
- buffer .get (c );
140
- return UTF8_CHARSET .decode (ByteBuffer .wrap (c )).toString ();
141
+ byte [] b = new byte [nextOpenBracket ];
142
+ buffer .get (b );
143
+ return UTF8_CHARSET .decode (ByteBuffer .wrap (b )).toString ();
141
144
}
142
145
143
146
/** Returns the index of the first open bracket, or -1 if none is found. */
@@ -193,16 +196,9 @@ private String readJsonString(int bytesToRead) throws IOException {
193
196
private boolean pullMoreData () throws IOException {
194
197
buffer .compact ();
195
198
196
- int available = bundleInputStream .available ();
197
- int bytesToRead = Math .min (available , buffer .remaining ());
198
- // `available` is an estimation, we still try to read if the estimation is 0, to move things
199
- // forward.
200
- if (available == 0 ) {
201
- bytesToRead = buffer .remaining ();
202
- }
203
199
int bytesRead =
204
200
bundleInputStream .read (
205
- buffer .array (), buffer .arrayOffset () + buffer .position (), bytesToRead );
201
+ buffer .array (), buffer .arrayOffset () + buffer .position (), buffer . remaining () );
206
202
boolean readSuccess = bytesRead > 0 ;
207
203
if (readSuccess ) {
208
204
buffer .position (buffer .position () + bytesRead );
0 commit comments