Skip to content

Commit

Permalink
[low-level HTTP] a way to use a part of byte[] as a response body
Browse files Browse the repository at this point in the history
Added `ok(Channel, boolean, byte[], int, int, MediaType)` and `writeBody(Channel, byte[], int, int, MediaType)` methods, so one could send a part of byte array (buffer) as a response body.
  • Loading branch information
Miha-x64 committed Jun 9, 2017
1 parent 48eacd6 commit 9a8d8f3
Showing 1 changed file with 12 additions and 4 deletions.
Expand Up @@ -155,14 +155,18 @@ private void writeContentTypeHeader(Channel ctx, MediaType contentType) {
ctx.write(contentType.getBytes());
ctx.write(CR_LF);
}

protected void writeBody(Channel ctx, byte[] body, MediaType contentType) {
writeBody(ctx, body, 0, body.length, contentType);
}

protected void writeBody(Channel ctx, byte[] body, int offset, int length, MediaType contentType) {
writeContentTypeHeader(ctx, contentType);
HttpIO.INSTANCE.writeContentLengthHeader(ctx, body.length);

ctx.write(CR_LF);

ctx.write(body);
ctx.write(body, offset, length);
}

protected void writeJsonBody(MaybeReq req, Channel ctx, Object value) {
Expand All @@ -181,10 +185,14 @@ protected HttpStatus serializeToJson(MaybeReq req, Channel ctx, boolean isKeepAl
writeJsonBody(req, ctx, value);
return HttpStatus.DONE;
}

protected HttpStatus ok(Channel ctx, boolean isKeepAlive, byte[] body, MediaType contentType) {
return ok(ctx, isKeepAlive, body, 0, body.length, contentType);
}

protected HttpStatus ok(Channel ctx, boolean isKeepAlive, byte[] body, int offset, int length, MediaType contentType) {
startResponse(ctx, isKeepAlive);
writeBody(ctx, body, contentType);
writeBody(ctx, body, offset, length, contentType);
return HttpStatus.DONE;
}

Expand Down

0 comments on commit 9a8d8f3

Please sign in to comment.