package io.netty.handler.codec.http;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.IllegalReferenceCountException;
import static io.netty.util.internal.ObjectUtil.checkNotNull;
public class DefaultFullHttpResponse extends DefaultHttpResponse implements FullHttpResponse {
private final ByteBuf content;
private final HttpHeaders trailingHeaders;
private int hash;
public DefaultFullHttpResponse(HttpVersion version, HttpResponseStatus status) {
this(version, status, Unpooled.buffer(0));
}
public DefaultFullHttpResponse(HttpVersion version, HttpResponseStatus status, ByteBuf content) {
this(version, status, content, true);
}
public DefaultFullHttpResponse(HttpVersion version, HttpResponseStatus status, boolean validateHeaders) {
this(version, status, Unpooled.buffer(0), validateHeaders, false);
}
public DefaultFullHttpResponse(HttpVersion version, HttpResponseStatus status, boolean validateHeaders,
boolean singleFieldHeaders) {
this(version, status, Unpooled.buffer(0), validateHeaders, singleFieldHeaders);
}
public DefaultFullHttpResponse(HttpVersion version, HttpResponseStatus status,
ByteBuf content, boolean validateHeaders) {
this(version, status, content, validateHeaders, false);
}
public DefaultFullHttpResponse(HttpVersion version, HttpResponseStatus status,
ByteBuf content, boolean validateHeaders, boolean singleFieldHeaders) {
super(version, status, validateHeaders, singleFieldHeaders);
this.content = checkNotNull(content, "content");
this.trailingHeaders = singleFieldHeaders ? new CombinedHttpHeaders(validateHeaders)
: new DefaultHttpHeaders(validateHeaders);
}
public DefaultFullHttpResponse(HttpVersion version, HttpResponseStatus status,
ByteBuf content, HttpHeaders headers, HttpHeaders trailingHeaders) {
super(version, status, headers);
this.content = checkNotNull(content, "content");
this.trailingHeaders = checkNotNull(trailingHeaders, "trailingHeaders");
}
@Override
public HttpHeaders trailingHeaders() {
return trailingHeaders;
}
@Override
public ByteBuf content() {
return content;
}
@Override
public int refCnt() {
return content.refCnt();
}
@Override
public FullHttpResponse retain() {
content.retain();
return this;
}
@Override
public FullHttpResponse retain(int increment) {
content.retain(increment);
return this;
}
@Override
public FullHttpResponse touch() {
content.touch();
return this;
}
@Override
public FullHttpResponse touch(Object hint) {
content.touch(hint);
return this;
}
@Override
public boolean release() {
return content.release();
}
@Override
public boolean release(int decrement) {
return content.release(decrement);
}
@Override
public FullHttpResponse setProtocolVersion(HttpVersion version) {
super.setProtocolVersion(version);
return this;
}
@Override
public FullHttpResponse setStatus(HttpResponseStatus status) {
super.setStatus(status);
return this;
}
@Override
public FullHttpResponse copy() {
return replace(content().copy());
}
@Override
public FullHttpResponse duplicate() {
return replace(content().duplicate());
}
@Override
public FullHttpResponse retainedDuplicate() {
return replace(content().retainedDuplicate());
}
@Override
public FullHttpResponse replace(ByteBuf content) {
FullHttpResponse response = new DefaultFullHttpResponse(protocolVersion(), status(), content,
headers().copy(), trailingHeaders().copy());
response.setDecoderResult(decoderResult());
return response;
}
@Override
public int hashCode() {
int hash = this.hash;
if (hash == 0) {
if (content().refCnt() != 0) {
try {
hash = 31 + content().hashCode();
} catch (IllegalReferenceCountException ignored) {
hash = 31;
}
} else {
hash = 31;
}
hash = 31 * hash + trailingHeaders().hashCode();
hash = 31 * hash + super.hashCode();
this.hash = hash;
}
return hash;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof DefaultFullHttpResponse)) {
return false;
}
DefaultFullHttpResponse other = (DefaultFullHttpResponse) o;
return super.equals(other) &&
content().equals(other.content()) &&
trailingHeaders().equals(other.trailingHeaders());
}
@Override
public String toString() {
return HttpMessageUtil.appendFullResponse(new StringBuilder(256), this).toString();
}
}