[#1533] Introduce ByteBufHolder.duplicate() and make use of it in DefaultChannelGroup.write(...)

This commit is contained in:
Norman Maurer 2013-07-06 21:17:51 +02:00
parent 7dda4b9ce4
commit 086ae3536c
31 changed files with 199 additions and 1 deletions

View File

@ -32,6 +32,11 @@ public interface ByteBufHolder extends ReferenceCounted {
*/
ByteBufHolder copy();
/**
* Duplicate the {@link ByteBufHolder}. Be aware that this will not automatically call {@link #retain()}.
*/
ByteBufHolder duplicate();
@Override
ByteBufHolder retain();

View File

@ -45,6 +45,11 @@ public class DefaultByteBufHolder implements ByteBufHolder {
return new DefaultByteBufHolder(data.copy());
}
@Override
public ByteBufHolder duplicate() {
return new DefaultByteBufHolder(data.duplicate());
}
@Override
public int refCnt() {
return data.refCnt();

View File

@ -100,4 +100,13 @@ public class DefaultFullHttpRequest extends DefaultHttpRequest implements FullHt
copy.trailingHeaders().set(trailingHeaders());
return copy;
}
@Override
public FullHttpRequest duplicate() {
DefaultFullHttpRequest duplicate = new DefaultFullHttpRequest(
getProtocolVersion(), getMethod(), getUri(), content().duplicate());
duplicate.headers().set(headers());
duplicate.trailingHeaders().set(trailingHeaders());
return duplicate;
}
}

View File

@ -95,4 +95,13 @@ public class DefaultFullHttpResponse extends DefaultHttpResponse implements Full
copy.trailingHeaders().set(trailingHeaders());
return copy;
}
@Override
public FullHttpResponse duplicate() {
DefaultFullHttpResponse duplicate = new DefaultFullHttpResponse(getProtocolVersion(), getStatus(),
content().duplicate());
duplicate.headers().set(headers());
duplicate.trailingHeaders().set(trailingHeaders());
return duplicate;
}
}

View File

@ -44,6 +44,11 @@ public class DefaultHttpContent extends DefaultHttpObject implements HttpContent
return new DefaultHttpContent(content.copy());
}
@Override
public HttpContent duplicate() {
return new DefaultHttpContent(content.duplicate());
}
@Override
public int refCnt() {
return content.refCnt();

View File

@ -54,6 +54,13 @@ public class DefaultLastHttpContent extends DefaultHttpContent implements LastHt
return copy;
}
@Override
public LastHttpContent duplicate() {
DefaultLastHttpContent copy = new DefaultLastHttpContent(content().duplicate());
copy.trailingHeaders().set(trailingHeaders());
return copy;
}
@Override
public LastHttpContent retain(int increment) {
super.retain(increment);

View File

@ -30,6 +30,9 @@ public interface HttpContent extends HttpObject, ByteBufHolder {
@Override
HttpContent copy();
@Override
HttpContent duplicate();
@Override
HttpContent retain();

View File

@ -39,6 +39,11 @@ public interface LastHttpContent extends HttpContent {
return EMPTY_LAST_CONTENT;
}
@Override
public LastHttpContent duplicate() {
return this;
}
@Override
public HttpHeaders trailingHeaders() {
return HttpHeaders.EMPTY_HEADERS;

View File

@ -34,6 +34,9 @@ public interface Attribute extends HttpData {
@Override
Attribute copy();
@Override
Attribute duplicate();
@Override
Attribute retain();

View File

@ -155,6 +155,21 @@ public class DiskAttribute extends AbstractDiskHttpData implements Attribute {
return attr;
}
@Override
public Attribute duplicate() {
DiskAttribute attr = new DiskAttribute(getName());
attr.setCharset(getCharset());
ByteBuf content = content();
if (content != null) {
try {
attr.setContent(content.duplicate());
} catch (IOException e) {
throw new ChannelException(e);
}
}
return attr;
}
@Override
public Attribute retain(int increment) {
super.retain(increment);

View File

@ -178,6 +178,21 @@ public class DiskFileUpload extends AbstractDiskHttpData implements FileUpload {
return upload;
}
@Override
public FileUpload duplicate() {
DiskFileUpload upload = new DiskFileUpload(getName(),
getFilename(), getContentType(), getContentTransferEncoding(), getCharset(), size);
ByteBuf buf = content();
if (buf != null) {
try {
upload.setContent(buf.duplicate());
} catch (IOException e) {
throw new ChannelException(e);
}
}
return upload;
}
@Override
public FileUpload retain(int increment) {
super.retain(increment);

View File

@ -59,6 +59,9 @@ public interface FileUpload extends HttpData {
@Override
FileUpload copy();
@Override
FileUpload duplicate();
@Override
FileUpload retain();

View File

@ -180,6 +180,9 @@ public interface HttpData extends InterfaceHttpData, ByteBufHolder {
@Override
HttpData copy();
@Override
HttpData duplicate();
@Override
HttpData retain();

View File

@ -1145,6 +1145,15 @@ public class HttpPostRequestEncoder implements ChunkedMessageInput<HttpContent>
return copy;
}
@Override
public FullHttpRequest duplicate() {
DefaultFullHttpRequest duplicate = new DefaultFullHttpRequest(
getProtocolVersion(), getMethod(), getUri(), content().duplicate());
duplicate.headers().set(headers());
duplicate.trailingHeaders().set(trailingHeaders());
return duplicate;
}
@Override
public FullHttpRequest retain(int increment) {
content.retain(increment);

View File

@ -116,6 +116,21 @@ public class MemoryAttribute extends AbstractMemoryHttpData implements Attribute
return attr;
}
@Override
public Attribute duplicate() {
MemoryAttribute attr = new MemoryAttribute(getName());
attr.setCharset(getCharset());
ByteBuf content = content();
if (content != null) {
try {
attr.setContent(content.duplicate());
} catch (IOException e) {
throw new ChannelException(e);
}
}
return attr;
}
@Override
public Attribute retain() {
super.retain();

View File

@ -145,6 +145,21 @@ public class MemoryFileUpload extends AbstractMemoryHttpData implements FileUplo
return upload;
}
@Override
public FileUpload duplicate() {
MemoryFileUpload upload = new MemoryFileUpload(getName(), getFilename(), getContentType(),
getContentTransferEncoding(), getCharset(), size);
ByteBuf buf = content();
if (buf != null) {
try {
upload.setContent(buf.duplicate());
return upload;
} catch (IOException e) {
throw new ChannelException(e);
}
}
return upload;
}
@Override
public FileUpload retain() {
super.retain();

View File

@ -204,6 +204,11 @@ public class MixedAttribute implements Attribute {
return attribute.copy();
}
@Override
public Attribute duplicate() {
return attribute.duplicate();
}
@Override
public ByteBuf content() {
return attribute.content();

View File

@ -230,6 +230,11 @@ public class MixedFileUpload implements FileUpload {
return fileUpload.copy();
}
@Override
public FileUpload duplicate() {
return fileUpload.duplicate();
}
@Override
public ByteBuf content() {
return fileUpload.content();

View File

@ -59,6 +59,11 @@ public class BinaryWebSocketFrame extends WebSocketFrame {
return new BinaryWebSocketFrame(isFinalFragment(), rsv(), content().copy());
}
@Override
public BinaryWebSocketFrame duplicate() {
return new BinaryWebSocketFrame(isFinalFragment(), rsv(), content().duplicate());
}
@Override
public BinaryWebSocketFrame retain() {
super.retain();

View File

@ -143,6 +143,11 @@ public class CloseWebSocketFrame extends WebSocketFrame {
return new CloseWebSocketFrame(isFinalFragment(), rsv(), content().copy());
}
@Override
public CloseWebSocketFrame duplicate() {
return new CloseWebSocketFrame(isFinalFragment(), rsv(), content().duplicate());
}
@Override
public CloseWebSocketFrame retain() {
super.retain();

View File

@ -124,6 +124,11 @@ public class ContinuationWebSocketFrame extends WebSocketFrame {
return new ContinuationWebSocketFrame(isFinalFragment(), rsv(), content().copy(), aggregatedText());
}
@Override
public ContinuationWebSocketFrame duplicate() {
return new ContinuationWebSocketFrame(isFinalFragment(), rsv(), content().duplicate(), aggregatedText());
}
@Override
public ContinuationWebSocketFrame retain() {
super.retain();

View File

@ -59,6 +59,11 @@ public class PingWebSocketFrame extends WebSocketFrame {
return new PingWebSocketFrame(isFinalFragment(), rsv(), content().copy());
}
@Override
public PingWebSocketFrame duplicate() {
return new PingWebSocketFrame(isFinalFragment(), rsv(), content().duplicate());
}
@Override
public PingWebSocketFrame retain() {
super.retain();

View File

@ -59,6 +59,11 @@ public class PongWebSocketFrame extends WebSocketFrame {
return new PongWebSocketFrame(isFinalFragment(), rsv(), content().copy());
}
@Override
public PongWebSocketFrame duplicate() {
return new PongWebSocketFrame(isFinalFragment(), rsv(), content().duplicate());
}
@Override
public PongWebSocketFrame retain() {
super.retain();

View File

@ -99,6 +99,11 @@ public class TextWebSocketFrame extends WebSocketFrame {
return new TextWebSocketFrame(isFinalFragment(), rsv(), content().copy());
}
@Override
public TextWebSocketFrame duplicate() {
return new TextWebSocketFrame(isFinalFragment(), rsv(), content().duplicate());
}
@Override
public TextWebSocketFrame retain() {
super.retain();

View File

@ -62,6 +62,9 @@ public abstract class WebSocketFrame extends DefaultByteBufHolder {
@Override
public abstract WebSocketFrame copy();
@Override
public abstract WebSocketFrame duplicate();
@Override
public String toString() {
return getClass().getSimpleName() + "(data: " + content().toString() + ')';

View File

@ -85,6 +85,13 @@ public class DefaultSpdyDataFrame extends DefaultSpdyStreamFrame implements Spdy
return frame;
}
@Override
public SpdyDataFrame duplicate() {
SpdyDataFrame frame = new DefaultSpdyDataFrame(getStreamId(), content().duplicate());
frame.setLast(isLast());
return frame;
}
@Override
public int refCnt() {
return data.refCnt();

View File

@ -42,6 +42,9 @@ public interface SpdyDataFrame extends ByteBufHolder, SpdyStreamFrame {
@Override
SpdyDataFrame copy();
@Override
SpdyDataFrame duplicate();
@Override
SpdyDataFrame retain();

View File

@ -82,6 +82,11 @@ public class AbstractMemoryHttpDataTest {
throw new UnsupportedOperationException("Should never be called.");
}
@Override
public HttpData duplicate() {
throw new UnsupportedOperationException("Should never be called.");
}
@Override
public int compareTo(InterfaceHttpData o) {
throw new UnsupportedOperationException("Should never be called.");

View File

@ -135,6 +135,15 @@ public final class SctpMessage extends DefaultByteBufHolder {
}
}
@Override
public SctpMessage duplicate() {
if (msgInfo == null) {
return new SctpMessage(protocolIdentifier, streamIdentifier, content().duplicate());
} else {
return new SctpMessage(msgInfo, content().copy());
}
}
@Override
public SctpMessage retain() {
super.retain();

View File

@ -251,7 +251,7 @@ public class DefaultChannelGroup extends AbstractSet<Channel> implements Channel
if (message instanceof ByteBuf) {
return ((ByteBuf) message).duplicate().retain();
} else if (message instanceof ByteBufHolder) {
return ((ByteBufHolder) message).copy();
return ((ByteBufHolder) message).duplicate().retain();
} else {
return ReferenceCountUtil.retain(message);
}

View File

@ -47,6 +47,11 @@ public final class DatagramPacket
return new DatagramPacket(content().copy(), recipient(), sender());
}
@Override
public DatagramPacket duplicate() {
return new DatagramPacket(content().duplicate(), recipient(), sender());
}
@Override
public DatagramPacket retain() {
super.retain();