@Nullable
  public static String readFully(final TypedInput typedInput) {
    InputStream inputStream = null;
    try {
      inputStream = typedInput.in();

      final ByteArrayOutputStream baos = new ByteArrayOutputStream();
      final byte[] buffer = new byte[1024];

      int n;
      while ((n = inputStream.read(buffer)) != -1) {
        baos.write(buffer, 0, n);
      }

      return new String(baos.toByteArray());

    } catch (final IOException ioe) {
      ioe.printStackTrace();
      return null;

    } finally {
      if (inputStream != null) {
        try {
          inputStream.close();
        } catch (final IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
Exemple #2
0
  public static String handleRetrofitErrorQuietly(final RetrofitError error) {
    error.printStackTrace();

    InputStream inputStream = null;
    try {
      if (error.isNetworkError()) {
        Log.e("XDA-ONE", "Network error happened.");
      } else {
        final TypedInput body = error.getResponse().getBody();
        if (body == null) {
          Log.e("XDA-ONE", "Unable to retrieve body");
          return null;
        }
        inputStream = body.in();

        final String result = IOUtils.toString(inputStream);
        Log.e("XDA-ONE", result);
        return result;
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      IOUtils.closeQuietly(inputStream);
    }
    return null;
  }
  @Test
  public void testCallbackWrapperSuccess_iOException() throws IOException {
    final Callback<OAuthResponse> callback =
        new Callback<OAuthResponse>() {
          @Override
          public void success(Result<OAuthResponse> result) {
            fail();
          }

          @Override
          public void failure(TwitterException exception) {
            assertNotNull(exception);
          }
        };
    final Callback<Response> callbackWrapper = service.getCallbackWrapper(callback);
    final TypedInput mockValue = mock(TypedInput.class);
    final Response mockResponse =
        new Response(
            "url",
            HttpURLConnection.HTTP_OK,
            "reason",
            new ArrayList<retrofit.client.Header>(),
            mockValue);
    when(mockValue.in()).thenThrow(mock(IOException.class));
    callbackWrapper.success(new Result<>(mockResponse, mockResponse));
  }
 @Override
 public Object fromBody(TypedInput body, Type type) throws ConversionException {
   try {
     return serializer.read((Class<?>) type, body.in());
   } catch (Exception e) {
     throw new ConversionException(e);
   }
 }
  @Test
  public void parseWithIOExceptionThrowsException() throws IOException, ConversionException {
    TypedInput input = mock(TypedInput.class);
    when(input.in()).thenThrow(new IOException());

    thrown.expect(ConversionException.class);

    TypedElement.parse(input);
  }
 @Override
 public Object fromBody(TypedInput typedInput, Type type) throws ConversionException {
   try {
     return StreamUtils.readInputStream(typedInput.in(), "utf-8");
   } catch (IOException exception) {
     exception.printStackTrace();
   }
   return null;
 }
 @Override
 public Object fromBody(TypedInput body, Type type) throws ConversionException {
   try {
     byte[] bytes = FileUtil.input2byte(body.in());
     return JSON.parseObject(bytes, type);
   } catch (IOException e) {
     throw new AssertionError(e);
   }
 }
Exemple #8
0
 @Override
 public Object fromBody(TypedInput body, final Type type) throws ConversionException {
   try {
     final JavaType javaType = TypeFactory.defaultInstance().constructType(type);
     return objectMapper.readValue(body.in(), javaType);
   } catch (final JsonParseException e) {
     throw new ConversionException(e);
   } catch (final JsonMappingException e) {
     throw new ConversionException(e);
   } catch (final IOException e) {
     throw new ConversionException(e);
   }
 }
  @Override
  public Object fromBody(TypedInput body, Type type) throws ConversionException {
    String charset = "UTF-8";

    if (body.mimeType() != null) {
      charset = MimeUtil.parseCharset(body.mimeType());
    }

    if (body.mimeType().equals("text/plain; charset=utf-8")) {
      BufferedReader reader = null;
      StringBuilder sb = new StringBuilder();

      try {
        reader = new BufferedReader(new InputStreamReader(body.in()));

        String line;
        while ((line = reader.readLine()) != null) {
          sb.append(line);
        }
      } catch (IOException e) {
        Timber.e(e, "Exception while reading XML");
      } finally {
        CloseableUtils.closeQuietly(reader);
      }

      return sb.toString();
    }

    InputStreamReader isr = null;
    try {
      isr = new InputStreamReader(body.in(), charset);
      return serializer.read((Class<?>) type, isr);
    } catch (Exception e) {
      throw new ConversionException(e);
    } finally {
      CloseableUtils.closeQuietly(isr);
    }
  }
 private void setupCallbackWrapperTest(
     String responseStr, Callback<OAuthResponse> authResponseCallback) throws IOException {
   final Callback<Response> callbackWrapper = service.getCallbackWrapper(authResponseCallback);
   final TypedInput mockValue = mock(TypedInput.class);
   final Response mockResponse =
       new Response(
           "url",
           HttpURLConnection.HTTP_OK,
           "reason",
           new ArrayList<retrofit.client.Header>(),
           mockValue);
   InputStream inputStream = null;
   try {
     inputStream = new ByteArrayInputStream(responseStr.getBytes("UTF-8"));
     when(mockValue.in()).thenReturn(inputStream);
     callbackWrapper.success(new Result<>(mockResponse, mockResponse));
   } finally {
     CommonUtils.closeQuietly(inputStream);
   }
 }
 @Override
 public Object fromBody(TypedInput body, Type type) throws ConversionException {
   InputStream inputStream = null;
   JSONObject jsonObject = null;
   try {
     inputStream = body.in();
     jsonObject = new JSONObject(IOUtils.toString(inputStream, UTF_8));
   } catch (IOException | JSONException e) {
     e.printStackTrace();
   } finally {
     try {
       if (inputStream != null) {
         inputStream.close();
       }
     } catch (IOException e) {
       e.printStackTrace();
     }
   }
   return jsonObject;
 }