Fixed incorrect usage of ByteBuffer.arrayOffset()

* NETTY-368 Wrappedbuffer does not honour posiiton()
* and similar mistakes found during a review
This commit is contained in:
Trustin Lee 2011-01-12 19:23:50 +09:00
parent 55f9597c0f
commit 5af93c2753
3 changed files with 7 additions and 5 deletions

View File

@ -313,7 +313,7 @@ public class ChannelBuffers {
return EMPTY_BUFFER; return EMPTY_BUFFER;
} }
if (buffer.hasArray()) { if (buffer.hasArray()) {
return wrappedBuffer(buffer.order(), buffer.array(), buffer.arrayOffset(),buffer.remaining()); return wrappedBuffer(buffer.order(), buffer.array(), buffer.arrayOffset() + buffer.position(),buffer.remaining());
} else { } else {
return new ByteBufferBackedChannelBuffer(buffer); return new ByteBufferBackedChannelBuffer(buffer);
} }

View File

@ -100,13 +100,14 @@ class OioDatagramWorker implements Runnable {
Object message, SocketAddress remoteAddress) { Object message, SocketAddress remoteAddress) {
try { try {
ChannelBuffer buf = (ChannelBuffer) message; ChannelBuffer buf = (ChannelBuffer) message;
int offset = buf.readerIndex();
int length = buf.readableBytes(); int length = buf.readableBytes();
ByteBuffer nioBuf = buf.toByteBuffer(); ByteBuffer nioBuf = buf.toByteBuffer();
DatagramPacket packet; DatagramPacket packet;
if (nioBuf.hasArray()) { if (nioBuf.hasArray()) {
// Avoid copy if the buffer is backed by an array. // Avoid copy if the buffer is backed by an array.
packet = new DatagramPacket( packet = new DatagramPacket(
nioBuf.array(), nioBuf.arrayOffset(), length); nioBuf.array(), nioBuf.arrayOffset() + offset, length);
} else { } else {
// Otherwise it will be expensive. // Otherwise it will be expensive.
byte[] arrayBuf = new byte[length]; byte[] arrayBuf = new byte[length];

View File

@ -18,10 +18,10 @@ package org.jboss.netty.handler.codec.protobuf;
import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBufferInputStream; import org.jboss.netty.buffer.ChannelBufferInputStream;
import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandler.Sharable;
import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline; import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.MessageEvent; import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.ChannelHandler.Sharable;
import org.jboss.netty.handler.codec.frame.FrameDecoder; import org.jboss.netty.handler.codec.frame.FrameDecoder;
import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder; import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder;
import org.jboss.netty.handler.codec.frame.LengthFieldPrepender; import org.jboss.netty.handler.codec.frame.LengthFieldPrepender;
@ -99,12 +99,13 @@ public class ProtobufDecoder extends OneToOneDecoder {
ChannelBuffer buf = (ChannelBuffer) msg; ChannelBuffer buf = (ChannelBuffer) msg;
if (buf.hasArray()) { if (buf.hasArray()) {
final int offset = buf.readerIndex();
if(extensionRegistry == null) { if(extensionRegistry == null) {
return prototype.newBuilderForType().mergeFrom( return prototype.newBuilderForType().mergeFrom(
buf.array(),buf.arrayOffset(), buf.readableBytes()).build(); buf.array(), buf.arrayOffset() + offset, buf.readableBytes()).build();
} else { } else {
return prototype.newBuilderForType().mergeFrom( return prototype.newBuilderForType().mergeFrom(
buf.array(), buf.arrayOffset(), buf.readableBytes(), extensionRegistry).build(); buf.array(), buf.arrayOffset() + offset, buf.readableBytes(), extensionRegistry).build();
} }
} else { } else {
if (extensionRegistry == null) { if (extensionRegistry == null) {