avoid copying bytes

This commit is contained in:
fjy 2014-04-16 10:14:15 -07:00
parent 51e2da9d8d
commit 6306e87a96
1 changed files with 29 additions and 43 deletions

View File

@ -113,16 +113,15 @@ public class AsyncQueryForwardingServlet extends HttpServlet
resp.setStatus(response.getStatus().getCode()); resp.setStatus(response.getStatus().getCode());
resp.setContentType("application/json"); resp.setContentType("application/json");
byte[] bytes = getContentBytes(response.getContent());
if (bytes.length > 0) {
try { try {
outputStream.write(bytes); ChannelBuffer buf = response.getContent();
buf.readBytes(outputStream, buf.readableBytes());
} }
catch (Exception e) { catch (Exception e) {
asyncContext.complete(); asyncContext.complete();
throw Throwables.propagate(e); throw Throwables.propagate(e);
} }
}
return ClientResponse.finished(outputStream); return ClientResponse.finished(outputStream);
} }
@ -131,16 +130,14 @@ public class AsyncQueryForwardingServlet extends HttpServlet
ClientResponse<OutputStream> clientResponse, HttpChunk chunk ClientResponse<OutputStream> clientResponse, HttpChunk chunk
) )
{ {
byte[] bytes = getContentBytes(chunk.getContent());
if (bytes.length > 0) {
try { try {
clientResponse.getObj().write(bytes); ChannelBuffer buf = chunk.getContent();
buf.readBytes(outputStream, buf.readableBytes());
} }
catch (Exception e) { catch (Exception e) {
asyncContext.complete(); asyncContext.complete();
throw Throwables.propagate(e); throw Throwables.propagate(e);
} }
}
return clientResponse; return clientResponse;
} }
@ -249,16 +246,14 @@ public class AsyncQueryForwardingServlet extends HttpServlet
resp.setStatus(response.getStatus().getCode()); resp.setStatus(response.getStatus().getCode());
resp.setContentType("application/x-javascript"); resp.setContentType("application/x-javascript");
byte[] bytes = getContentBytes(response.getContent());
if (bytes.length > 0) {
try { try {
outputStream.write(bytes); ChannelBuffer buf = response.getContent();
buf.readBytes(outputStream, buf.readableBytes());
} }
catch (Exception e) { catch (Exception e) {
asyncContext.complete(); asyncContext.complete();
throw Throwables.propagate(e); throw Throwables.propagate(e);
} }
}
return ClientResponse.finished(outputStream); return ClientResponse.finished(outputStream);
} }
@ -267,16 +262,14 @@ public class AsyncQueryForwardingServlet extends HttpServlet
ClientResponse<OutputStream> clientResponse, HttpChunk chunk ClientResponse<OutputStream> clientResponse, HttpChunk chunk
) )
{ {
byte[] bytes = getContentBytes(chunk.getContent());
if (bytes.length > 0) {
try { try {
clientResponse.getObj().write(bytes); ChannelBuffer buf = chunk.getContent();
buf.readBytes(outputStream, buf.readableBytes());
} }
catch (Exception e) { catch (Exception e) {
asyncContext.complete(); asyncContext.complete();
throw Throwables.propagate(e); throw Throwables.propagate(e);
} }
}
return clientResponse; return clientResponse;
} }
@ -387,11 +380,4 @@ public class AsyncQueryForwardingServlet extends HttpServlet
} }
return String.format("http://%s%s?%s", host, req.getRequestURI(), req.getQueryString()); return String.format("http://%s%s?%s", host, req.getRequestURI(), req.getQueryString());
} }
private byte[] getContentBytes(ChannelBuffer content)
{
byte[] contentBytes = new byte[content.readableBytes()];
content.readBytes(contentBytes);
return contentBytes;
}
} }