@@ -330,14 +330,14 @@ protected JSONObject putRequest(String url, String obj) throws AlgoliaException
330
330
}
331
331
332
332
/**
333
- * Reads the answer and return it as a String
333
+ * Reads the InputStream as UTF-8
334
334
*
335
- * @param istream the InputStream to read
336
- * @return the stream's content
337
- * @throws IOException if the stream can't be read or closed
335
+ * @param stream the InputStream to read
336
+ * @return the stream's content as a String
337
+ * @throws IOException if the stream can't be read, decoded as UTF-8 or closed
338
338
*/
339
- private String _getAnswer (InputStream istream ) throws IOException {
340
- InputStreamReader is = new InputStreamReader (istream , "UTF-8" );
339
+ private String _toCharArray (InputStream stream ) throws IOException {
340
+ InputStreamReader is = new InputStreamReader (stream , "UTF-8" );
341
341
StringBuilder builder = new StringBuilder ();
342
342
char [] buf = new char [1000 ];
343
343
int l = 0 ;
@@ -349,6 +349,30 @@ private String _getAnswer(InputStream istream) throws IOException {
349
349
return builder .toString ();
350
350
}
351
351
352
+ /**
353
+ * Reads the InputStream into a byte array
354
+ * @param stream the InputStream to read
355
+ * @return the stream's content as a byte[]
356
+ * @throws AlgoliaException if the stream can't be read or flushed
357
+ */
358
+ private byte [] _toByteArray (InputStream stream ) throws AlgoliaException {
359
+ ByteArrayOutputStream out = new ByteArrayOutputStream ();
360
+ int read ;
361
+ byte [] buffer = new byte [1024 ];
362
+
363
+ try {
364
+ while ((read = stream .read (buffer , 0 , buffer .length )) != -1 ) {
365
+ out .write (buffer , 0 , read );
366
+ }
367
+
368
+ out .flush ();
369
+ return out .toByteArray ();
370
+ } catch (IOException e ) {
371
+ throw new AlgoliaException ("Error while reading stream: " + e .getMessage ());
372
+ }
373
+ }
374
+
375
+
352
376
private JSONObject _getJSONObject (String input ) throws JSONException {
353
377
return new JSONObject (new JSONTokener (input ));
354
378
}
@@ -358,7 +382,7 @@ private JSONObject _getJSONObject(byte[] array) throws JSONException {
358
382
}
359
383
360
384
private JSONObject _getAnswerJSONObject (InputStream istream ) throws IOException , JSONException {
361
- return _getJSONObject (_getAnswer (istream ));
385
+ return _getJSONObject (_toCharArray (istream ));
362
386
}
363
387
364
388
/**
@@ -499,7 +523,7 @@ private synchronized byte[] _requestRaw(Method m, String url, String json, List<
499
523
throw new AlgoliaException (message );
500
524
} else {
501
525
try {
502
- errors .put (host , _getAnswer (stream ));
526
+ errors .put (host , _toCharArray (stream ));
503
527
} catch (IOException e ) {
504
528
errors .put (host , String .valueOf (code ));
505
529
}
@@ -531,23 +555,6 @@ private synchronized byte[] _requestRaw(Method m, String url, String json, List<
531
555
throw new AlgoliaException (builder .toString ());
532
556
}
533
557
534
- private byte [] _toByteArray (InputStream stream ) throws AlgoliaException {
535
- ByteArrayOutputStream out = new ByteArrayOutputStream ();
536
- int read ;
537
- byte [] buffer = new byte [1024 ];
538
-
539
- try {
540
- while ((read = stream .read (buffer , 0 , buffer .length )) != -1 ) {
541
- out .write (buffer , 0 , read );
542
- }
543
-
544
- out .flush ();
545
- return out .toByteArray ();
546
- } catch (IOException e ) {
547
- throw new AlgoliaException ("Error while reading stream: " + e .getMessage ());
548
- }
549
- }
550
-
551
558
private void addError (HashMap <String , String > errors , String host , IOException e ) {
552
559
errors .put (host , String .format ("%s=%s" , e .getClass ().getName (), e .getMessage ()));
553
560
}
0 commit comments