Introduce new utility class calles ReferenceCountUtil and move utility methods from ByteBufUtil to it.

The ones in ByteBufUtil were marked as @deprecated
This commit is contained in:
Norman Maurer 2013-06-14 07:07:33 +02:00
parent 4bf5003f76
commit 6a9f965f9b
9 changed files with 120 additions and 48 deletions

View File

@ -16,6 +16,7 @@
package io.netty.buffer;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.ReferenceCounted;
import java.nio.ByteBuffer;
@ -45,47 +46,47 @@ public final class ByteBufUtil {
/**
* Try to call {@link ReferenceCounted#retain()} if the specified message implements {@link ReferenceCounted}.
* If the specified message doesn't implement {@link ReferenceCounted}, this method does nothing.
*
* @deprecated use {@link ReferenceCountUtil#retain(Object)}
*/
@SuppressWarnings("unchecked")
@Deprecated
public static <T> T retain(T msg) {
if (msg instanceof ReferenceCounted) {
return (T) ((ReferenceCounted) msg).retain();
}
return msg;
return ReferenceCountUtil.retain(msg);
}
/**
* Try to call {@link ReferenceCounted#retain()} if the specified message implements {@link ReferenceCounted}.
* If the specified message doesn't implement {@link ReferenceCounted}, this method does nothing.
*
* @deprecated use {@link ReferenceCountUtil#retain(Object, int)}
*/
@SuppressWarnings("unchecked")
@Deprecated
public static <T> T retain(T msg, int increment) {
if (msg instanceof ReferenceCounted) {
return (T) ((ReferenceCounted) msg).retain(increment);
}
return msg;
return ReferenceCountUtil.retain(msg, increment);
}
/**
* Try to call {@link ReferenceCounted#release()} if the specified message implements {@link ReferenceCounted}.
* If the specified message doesn't implement {@link ReferenceCounted}, this method does nothing.
*
* @deprecated use {@link ReferenceCountUtil#release(Object)}
*/
@Deprecated
public static boolean release(Object msg) {
if (msg instanceof ReferenceCounted) {
return ((ReferenceCounted) msg).release();
}
return false;
return ReferenceCountUtil.release(msg);
}
/**
* Try to call {@link ReferenceCounted#release()} if the specified message implements {@link ReferenceCounted}.
* If the specified message doesn't implement {@link ReferenceCounted}, this method does nothing.
*
* @deprecated use {@link ReferenceCountUtil#release(Object, int)}
*/
@Deprecated
public static boolean release(Object msg, int decrement) {
if (msg instanceof ReferenceCounted) {
return ((ReferenceCounted) msg).release(decrement);
}
return false;
return ReferenceCountUtil.release(msg, decrement);
}
/**

View File

@ -17,12 +17,12 @@ package io.netty.handler.codec.http;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufHolder;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.MessageList;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.util.ReferenceCountUtil;
/**
* Decodes the content of the received {@link HttpRequest} and {@link HttpContent}.
@ -58,7 +58,7 @@ public abstract class HttpContentDecoder extends MessageToMessageDecoder<HttpObj
continueResponse = true;
}
// 100-continue response must be passed through.
out.add(ByteBufUtil.retain(msg));
out.add(ReferenceCountUtil.retain(msg));
return;
}
@ -67,7 +67,7 @@ public abstract class HttpContentDecoder extends MessageToMessageDecoder<HttpObj
continueResponse = false;
}
// 100-continue response must be passed through.
out.add(ByteBufUtil.retain(msg));
out.add(ReferenceCountUtil.retain(msg));
return;
}

View File

@ -17,7 +17,6 @@ package io.netty.handler.codec.http;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufHolder;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.MessageList;
@ -25,6 +24,7 @@ import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.MessageToMessageCodec;
import io.netty.handler.codec.http.HttpHeaders.Names;
import io.netty.handler.codec.http.HttpHeaders.Values;
import io.netty.util.ReferenceCountUtil;
import java.util.ArrayDeque;
import java.util.Queue;
@ -77,7 +77,7 @@ public abstract class HttpContentEncoder extends MessageToMessageCodec<HttpReque
acceptedEncoding = HttpHeaders.Values.IDENTITY;
}
acceptEncodingQueue.add(acceptedEncoding);
out.add(ByteBufUtil.retain(msg));
out.add(ReferenceCountUtil.retain(msg));
}
@Override
@ -92,7 +92,7 @@ public abstract class HttpContentEncoder extends MessageToMessageCodec<HttpReque
if (res.getStatus().code() == 100) {
if (isFull) {
out.add(ByteBufUtil.retain(res));
out.add(ReferenceCountUtil.retain(res));
} else {
out.add(res);
// Pass through all following contents.
@ -113,7 +113,7 @@ public abstract class HttpContentEncoder extends MessageToMessageCodec<HttpReque
// Set the content length to 0.
res.headers().remove(Names.TRANSFER_ENCODING);
res.headers().set(Names.CONTENT_LENGTH, "0");
out.add(ByteBufUtil.retain(res));
out.add(ReferenceCountUtil.retain(res));
break;
}
}
@ -127,7 +127,7 @@ public abstract class HttpContentEncoder extends MessageToMessageCodec<HttpReque
// As an unchunked response
res.headers().remove(Names.TRANSFER_ENCODING);
res.headers().set(Names.CONTENT_LENGTH, ((ByteBufHolder) res).content().readableBytes());
out.add(ByteBufUtil.retain(res));
out.add(ReferenceCountUtil.retain(res));
} else {
// As a chunked response
res.headers().remove(Names.CONTENT_LENGTH);
@ -172,7 +172,7 @@ public abstract class HttpContentEncoder extends MessageToMessageCodec<HttpReque
}
case PASS_THROUGH: {
ensureContent(msg);
out.add(ByteBufUtil.retain(msg));
out.add(ReferenceCountUtil.retain(msg));
// Passed through all following contents of the current response.
if (msg instanceof LastHttpContent) {
state = State.AWAIT_HEADERS;

View File

@ -16,7 +16,6 @@
package io.netty.handler.codec.http;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.CompositeByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
@ -27,6 +26,7 @@ import io.netty.handler.codec.DecoderResult;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.handler.codec.TooLongFrameException;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;
import static io.netty.handler.codec.http.HttpHeaders.*;
@ -129,7 +129,7 @@ public class HttpObjectAggregator extends MessageToMessageDecoder<HttpObject> {
if (!m.getDecoderResult().isSuccess()) {
removeTransferEncodingChunked(m);
this.currentMessage = null;
out.add(ByteBufUtil.retain(m));
out.add(ReferenceCountUtil.retain(m));
return;
}
if (msg instanceof HttpRequest) {

View File

@ -15,11 +15,11 @@
*/
package io.netty.handler.codec.spdy;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.MessageList;
import io.netty.handler.codec.MessageToMessageCodec;
import io.netty.handler.codec.http.HttpMessage;
import io.netty.util.ReferenceCountUtil;
import java.util.LinkedList;
import java.util.Queue;
@ -46,7 +46,7 @@ public class SpdyHttpResponseStreamIdHandler extends
SpdyHttpHeaders.setStreamId(msg, id);
}
out.add(ByteBufUtil.retain(msg));
out.add(ReferenceCountUtil.retain(msg));
}
@Override
@ -62,6 +62,6 @@ public class SpdyHttpResponseStreamIdHandler extends
ids.remove(((SpdyRstStreamFrame) msg).getStreamId());
}
out.add(ByteBufUtil.retain(msg));
out.add(ReferenceCountUtil.retain(msg));
}
}

View File

@ -0,0 +1,70 @@
/*
* Copyright 2013 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.util;
/**
* Collection of method to handle objects that may implement {@link ReferenceCounted}.
*/
public final class ReferenceCountUtil {
/**
* Try to call {@link ReferenceCounted#retain()} if the specified message implements {@link ReferenceCounted}.
* If the specified message doesn't implement {@link ReferenceCounted}, this method does nothing.
*/
@SuppressWarnings("unchecked")
public static <T> T retain(T msg) {
if (msg instanceof ReferenceCounted) {
return (T) ((ReferenceCounted) msg).retain();
}
return msg;
}
/**
* Try to call {@link ReferenceCounted#retain()} if the specified message implements {@link ReferenceCounted}.
* If the specified message doesn't implement {@link ReferenceCounted}, this method does nothing.
*/
@SuppressWarnings("unchecked")
public static <T> T retain(T msg, int increment) {
if (msg instanceof ReferenceCounted) {
return (T) ((ReferenceCounted) msg).retain(increment);
}
return msg;
}
/**
* Try to call {@link ReferenceCounted#release()} if the specified message implements {@link ReferenceCounted}.
* If the specified message doesn't implement {@link ReferenceCounted}, this method does nothing.
*/
public static boolean release(Object msg) {
if (msg instanceof ReferenceCounted) {
return ((ReferenceCounted) msg).release();
}
return false;
}
/**
* Try to call {@link ReferenceCounted#release()} if the specified message implements {@link ReferenceCounted}.
* If the specified message doesn't implement {@link ReferenceCounted}, this method does nothing.
*/
public static boolean release(Object msg, int decrement) {
if (msg instanceof ReferenceCounted) {
return ((ReferenceCounted) msg).release(decrement);
}
return false;
}
private ReferenceCountUtil() { }
}

View File

@ -16,7 +16,7 @@
package io.netty.channel;
import io.netty.buffer.ByteBufUtil;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.ReferenceCounted;
import io.netty.util.internal.StringUtil;
@ -82,24 +82,24 @@ public class DefaultAddressedEnvelope<M, A extends SocketAddress> implements Add
@Override
public AddressedEnvelope<M, A> retain() {
ByteBufUtil.retain(message);
ReferenceCountUtil.retain(message);
return this;
}
@Override
public AddressedEnvelope<M, A> retain(int increment) {
ByteBufUtil.retain(message, increment);
ReferenceCountUtil.retain(message, increment);
return this;
}
@Override
public boolean release() {
return ByteBufUtil.release(message);
return ReferenceCountUtil.release(message);
}
@Override
public boolean release(int decrement) {
return ByteBufUtil.release(message, decrement);
return ReferenceCountUtil.release(message, decrement);
}
@Override

View File

@ -17,9 +17,9 @@
package io.netty.channel;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.util.Recycler;
import io.netty.util.Recycler.Handle;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.Signal;
import io.netty.util.internal.PlatformDependent;
@ -74,49 +74,50 @@ public final class MessageList<T> implements Iterable<T> {
}
/**
* Call {@link ByteBufUtil#retain(Object)} on all messages in this {@link MessageList} and return itself.
* Call {@link ReferenceCountUtil#retain(Object)} on all messages in this {@link MessageList} and return itself.
*/
public MessageList<T> retainAll() {
int size = this.size;
for (int i = 0; i < size; i ++) {
ByteBufUtil.retain(elements[i]);
ReferenceCountUtil.retain(elements[i]);
}
return this;
}
/**
* Call {@link ByteBufUtil#retain(Object), int} on all messages in this {@link MessageList} and return itself.
* Call {@link ReferenceCountUtil#retain(Object), int} on all messages in this {@link MessageList} and return
* itself.
*/
public MessageList<T> retainAll(int increment) {
int size = this.size;
for (int i = 0; i < size; i ++) {
ByteBufUtil.retain(elements[i], increment);
ReferenceCountUtil.retain(elements[i], increment);
}
return this;
}
/**
* Call {@link ByteBufUtil#release(Object)} on all messages in this {@link MessageList} and return {@code true}
* if all messages were released.
* Call {@link ReferenceCountUtil#release(Object)} on all messages in this {@link MessageList} and return
* {@code true} if all messages were released.
*/
public boolean releaseAll() {
boolean releasedAll = true;
int size = this.size;
for (int i = 0; i < size; i++) {
releasedAll &= ByteBufUtil.release(elements[i]);
releasedAll &= ReferenceCountUtil.release(elements[i]);
}
return releasedAll;
}
/**
* Call {@link ByteBufUtil#release(Object, int)} on all messages in this {@link MessageList} and return
* Call {@link ReferenceCountUtil#release(Object, int)} on all messages in this {@link MessageList} and return
* {@code true} if all messages were released.
*/
public boolean releaseAll(int decrement) {
boolean releasedAll = true;
int size = this.size;
for (int i = 0; i < size; i++) {
releasedAll &= ByteBufUtil.release(elements[i], decrement);
releasedAll &= ReferenceCountUtil.release(elements[i], decrement);
}
return releasedAll;
}

View File

@ -15,12 +15,12 @@
*/
package io.netty.channel.group;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.MessageList;
import io.netty.channel.ServerChannel;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.concurrent.EventExecutor;
import io.netty.util.internal.PlatformDependent;
@ -216,11 +216,11 @@ public class DefaultChannelGroup extends AbstractSet<Channel> implements Channel
Map<Integer, ChannelFuture> futures = new LinkedHashMap<Integer, ChannelFuture>(size());
for (Channel c: nonServerChannels.values()) {
ByteBufUtil.retain(message);
ReferenceCountUtil.retain(message);
futures.put(c.id(), c.write(message));
}
ByteBufUtil.release(message);
ReferenceCountUtil.release(message);
return new DefaultChannelGroupFuture(this, futures, executor);
}