Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Commit

Permalink
Merge pull request #137 from matthiasblaesing/utf8-stream
Browse files Browse the repository at this point in the history
Read Activity Data directly from InputStream
  • Loading branch information
tracyboehrer authored Oct 28, 2019
2 parents fdcb731 + 7d5ee88 commit ad8eb63
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,9 @@ public void handle(HttpExchange httpExchange) throws IOException {
}
}

private String getRequestBody(HttpExchange httpExchange) throws IOException {
StringBuilder buffer = new StringBuilder();
InputStream stream = httpExchange.getRequestBody();
int rByte;
while ((rByte = stream.read()) != -1) {
buffer.append((char)rByte);
}
stream.close();
if (buffer.length() > 0) {
return URLDecoder.decode(buffer.toString(), "UTF-8");
}
return "";
}

private Activity getActivity(HttpExchange httpExchange) {
try {
String body = getRequestBody(httpExchange);
LOGGER.log(Level.INFO, body);
return objectMapper.readValue(body, Activity.class);
try (InputStream is = httpExchange.getRequestBody()) {
return objectMapper.readValue(is, Activity.class);
} catch (Exception ex) {
LOGGER.log(Level.WARNING, "Failed to get activity", ex);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,8 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)

// Creates an Activity object from the request
private Activity getActivity(HttpServletRequest request) throws IOException {
String body = getRequestBody(request);
return objectMapper.readValue(body, Activity.class);
}

private String getRequestBody(HttpServletRequest request) throws IOException {
StringBuilder buffer = new StringBuilder();
try (InputStream stream = request.getInputStream()) {
int rByte;
while ((rByte = stream.read()) != -1) {
buffer.append((char) rByte);
}
stream.close();
if (buffer.length() > 0) {
return buffer.toString();
}
return "";
try(InputStream is = request.getInputStream()) {
return objectMapper.readValue(is, Activity.class);
}
}
}

0 comments on commit ad8eb63

Please sign in to comment.