Remove deprecated ByteBufUtil.release(..) and ByteBufUtil.retain(..) methods and its usage. Also fix a problem where an object would have been released two times.

* The problem with the release(..) calls here was that it would have called release on an unsupported message and then throw an exception. This exception will trigger ChannelOutboundBuffer.fail(..), which will also try to release the message again.
* Also use the same exception type for unsupported messages as in other channel impls.
This commit is contained in:
Norman Maurer 2013-07-03 10:00:13 +02:00
parent 328969485c
commit 7ec12d327f
3 changed files with 18 additions and 71 deletions

View File

@ -16,8 +16,6 @@
package io.netty.buffer;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.ReferenceCounted;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
@ -43,52 +41,6 @@ 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) {
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) {
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) {
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) {
return ReferenceCountUtil.release(msg, decrement);
}
/**
* Returns a <a href="http://en.wikipedia.org/wiki/Hex_dump">hex dump</a>
* of the specified buffer's readable bytes.

View File

@ -17,7 +17,6 @@ package io.netty.channel.socket.nio;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufHolder;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.AddressedEnvelope;
import io.netty.channel.Channel;
import io.netty.channel.ChannelException;
@ -30,6 +29,7 @@ import io.netty.channel.nio.AbstractNioMessageChannel;
import io.netty.channel.socket.DatagramChannelConfig;
import io.netty.channel.socket.DatagramPacket;
import io.netty.channel.socket.InternetProtocolFamily;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.StringUtil;
@ -255,8 +255,7 @@ public final class NioDatagramChannel
} else if (m instanceof ByteBuf) {
data = (ByteBuf) m;
} else {
ByteBufUtil.release(o);
throw new ChannelException("unsupported message type: " + StringUtil.simpleClassName(o));
throw new UnsupportedOperationException("unsupported message type: " + StringUtil.simpleClassName(0));
}
int dataLen = data.readableBytes();
@ -293,7 +292,7 @@ public final class NioDatagramChannel
}
// Wrote a packet - free the message.
ByteBufUtil.release(o);
ReferenceCountUtil.release(o);
if (index + 1 == msgs.size()) {
// Wrote the outbound buffer completely - clear OP_WRITE.

View File

@ -17,7 +17,6 @@ package io.netty.channel.socket.oio;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufHolder;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.AddressedEnvelope;
import io.netty.channel.Channel;
import io.netty.channel.ChannelException;
@ -31,6 +30,7 @@ import io.netty.channel.socket.DatagramChannel;
import io.netty.channel.socket.DatagramChannelConfig;
import io.netty.channel.socket.DatagramPacket;
import io.netty.channel.socket.DefaultDatagramChannelConfig;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.internal.EmptyArrays;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.StringUtil;
@ -263,27 +263,23 @@ public class OioDatagramChannel extends AbstractOioMessageChannel
} else if (m instanceof ByteBuf) {
data = (ByteBuf) m;
} else {
ByteBufUtil.release(o);
throw new ChannelException("unsupported message type: " + StringUtil.simpleClassName(o));
throw new UnsupportedOperationException("unsupported message type: " + StringUtil.simpleClassName(o));
}
try {
int length = data.readableBytes();
if (remoteAddress != null) {
tmpPacket.setSocketAddress(remoteAddress);
}
if (data.hasArray()) {
tmpPacket.setData(data.array(), data.arrayOffset() + data.readerIndex(), length);
} else {
byte[] tmp = new byte[length];
data.getBytes(data.readerIndex(), tmp);
tmpPacket.setData(tmp);
}
socket.send(tmpPacket);
return 1;
} finally {
ByteBufUtil.release(o);
int length = data.readableBytes();
if (remoteAddress != null) {
tmpPacket.setSocketAddress(remoteAddress);
}
if (data.hasArray()) {
tmpPacket.setData(data.array(), data.arrayOffset() + data.readerIndex(), length);
} else {
byte[] tmp = new byte[length];
data.getBytes(data.readerIndex(), tmp);
tmpPacket.setData(tmp);
}
socket.send(tmpPacket);
ReferenceCountUtil.release(o);
return 1;
}
@Override