修改HttpResponse.finish(byte[])

This commit is contained in:
Redkale
2018-08-27 11:55:36 +08:00
parent 827b404a57
commit 2ca1e6305c

View File

@@ -614,15 +614,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
*/ */
@Override @Override
public void finish(final byte[] bs) { public void finish(final byte[] bs) {
if (isClosed()) return; //避免重复关闭 this.finish(this.contentType, bs);
if (this.context.getBufferCapacity() >= bs.length) {
ByteBuffer buffer = getBodyBufferSupplier().get();
buffer.put(bs);
buffer.flip();
this.finish(false, buffer);
} else {
this.finish(false, ByteBuffer.wrap(bs));
}
} }
/** /**
@@ -633,15 +625,30 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
*/ */
public void finish(final String contentType, final byte[] bs) { public void finish(final String contentType, final byte[] bs) {
if (isClosed()) return; //避免重复关闭 if (isClosed()) return; //避免重复关闭
this.contentType = contentType; final byte[] content = bs == null ? new byte[0] : bs;
if (this.context.getBufferCapacity() >= bs.length) { if (!this.headsended) {
ByteBuffer buffer = getBodyBufferSupplier().get(); this.contentType = contentType;
buffer.put(bs); this.contentLength = content.length;
buffer.flip(); ByteBuffer headbuf = createHeader();
this.finish(false, buffer); if (headbuf.remaining() >= content.length) {
headbuf.put(content);
headbuf.flip();
super.finish(false, headbuf);
} else {
headbuf.flip();
super.finish(false, new ByteBuffer[]{headbuf, ByteBuffer.wrap(content)});
}
} else { } else {
this.finish(false, ByteBuffer.wrap(bs)); if (this.context.getBufferCapacity() >= content.length) {
ByteBuffer buffer = getBodyBufferSupplier().get();
buffer.put(content);
buffer.flip();
this.finish(false, buffer);
} else {
this.finish(false, ByteBuffer.wrap(content));
}
} }
} }
/** /**