Implement the ByteBuf methods that should have been overridden

Motivation:

Some ByteBuf implementations do not override all necessary methods,
which can lead to potentially sub-optimal behavior.

Also, SlicedByteBuf does not perform the range check correctly due to
missing overrides.

Modifications:

- Add missing overrides
- Use unwrap() instead of direct member access in derived buffers for
  consistency
- Merge unwrap0() into unwrap() using covariant return type
- Deprecate AbstractDerivedByteBuf and its subtypes, because they were
  not meant to be public

Result:

Correctness
This commit is contained in:
Trustin Lee 2016-04-13 20:34:39 +09:00
parent 6fc05772d0
commit bb3bf5dc2f
9 changed files with 252 additions and 155 deletions

View File

@ -21,7 +21,10 @@ import java.nio.ByteBuffer;
/** /**
* Abstract base class for {@link ByteBuf} implementations that wrap another * Abstract base class for {@link ByteBuf} implementations that wrap another
* {@link ByteBuf}. * {@link ByteBuf}.
*
* @deprecated Do not use.
*/ */
@Deprecated
public abstract class AbstractDerivedByteBuf extends AbstractByteBuf { public abstract class AbstractDerivedByteBuf extends AbstractByteBuf {
protected AbstractDerivedByteBuf(int maxCapacity) { protected AbstractDerivedByteBuf(int maxCapacity) {

View File

@ -20,61 +20,63 @@ package io.netty.buffer;
* is of type {@link AbstractByteBuf}. * is of type {@link AbstractByteBuf}.
*/ */
final class DuplicatedAbstractByteBuf extends DuplicatedByteBuf { final class DuplicatedAbstractByteBuf extends DuplicatedByteBuf {
public DuplicatedAbstractByteBuf(AbstractByteBuf buffer) { DuplicatedAbstractByteBuf(AbstractByteBuf buffer) {
super(buffer); super(buffer);
} }
@Override
public AbstractByteBuf unwrap() {
return (AbstractByteBuf) super.unwrap();
}
@Override @Override
protected byte _getByte(int index) { protected byte _getByte(int index) {
return unwrap0()._getByte(index); return unwrap()._getByte(index);
} }
@Override @Override
protected short _getShort(int index) { protected short _getShort(int index) {
return unwrap0()._getShort(index); return unwrap()._getShort(index);
} }
@Override @Override
protected int _getUnsignedMedium(int index) { protected int _getUnsignedMedium(int index) {
return unwrap0()._getUnsignedMedium(index); return unwrap()._getUnsignedMedium(index);
} }
@Override @Override
protected int _getInt(int index) { protected int _getInt(int index) {
return unwrap0()._getInt(index); return unwrap()._getInt(index);
} }
@Override @Override
protected long _getLong(int index) { protected long _getLong(int index) {
return unwrap0()._getLong(index); return unwrap()._getLong(index);
} }
@Override @Override
protected void _setByte(int index, int value) { protected void _setByte(int index, int value) {
unwrap0()._setByte(index, value); unwrap()._setByte(index, value);
} }
@Override @Override
protected void _setShort(int index, int value) { protected void _setShort(int index, int value) {
unwrap0()._setShort(index, value); unwrap()._setShort(index, value);
} }
@Override @Override
protected void _setMedium(int index, int value) { protected void _setMedium(int index, int value) {
unwrap0()._setMedium(index, value); unwrap()._setMedium(index, value);
} }
@Override @Override
protected void _setInt(int index, int value) { protected void _setInt(int index, int value) {
unwrap0()._setInt(index, value); unwrap()._setInt(index, value);
} }
@Override @Override
protected void _setLong(int index, long value) { protected void _setLong(int index, long value) {
unwrap0()._setLong(index, value); unwrap()._setLong(index, value);
} }
private AbstractByteBuf unwrap0() {
return (AbstractByteBuf) unwrap();
}
} }

View File

@ -23,12 +23,14 @@ import java.nio.ByteOrder;
import java.nio.channels.GatheringByteChannel; import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel; import java.nio.channels.ScatteringByteChannel;
/** /**
* A derived buffer which simply forwards all data access requests to its * A derived buffer which simply forwards all data access requests to its
* parent. It is recommended to use {@link ByteBuf#duplicate()} instead * parent. It is recommended to use {@link ByteBuf#duplicate()} instead
* of calling the constructor explicitly. * of calling the constructor explicitly.
*
* @deprecated Do not use.
*/ */
@Deprecated
public class DuplicatedByteBuf extends AbstractDerivedByteBuf { public class DuplicatedByteBuf extends AbstractDerivedByteBuf {
private final ByteBuf buffer; private final ByteBuf buffer;
@ -54,254 +56,250 @@ public class DuplicatedByteBuf extends AbstractDerivedByteBuf {
@Override @Override
public ByteBufAllocator alloc() { public ByteBufAllocator alloc() {
return buffer.alloc(); return unwrap().alloc();
} }
@Override @Override
@Deprecated
public ByteOrder order() { public ByteOrder order() {
return buffer.order(); return unwrap().order();
} }
@Override @Override
public boolean isDirect() { public boolean isDirect() {
return buffer.isDirect(); return unwrap().isDirect();
} }
@Override @Override
public int capacity() { public int capacity() {
return buffer.capacity(); return unwrap().capacity();
} }
@Override @Override
public ByteBuf capacity(int newCapacity) { public ByteBuf capacity(int newCapacity) {
buffer.capacity(newCapacity); unwrap().capacity(newCapacity);
return this; return this;
} }
@Override @Override
public boolean hasArray() { public boolean hasArray() {
return buffer.hasArray(); return unwrap().hasArray();
} }
@Override @Override
public byte[] array() { public byte[] array() {
return buffer.array(); return unwrap().array();
} }
@Override @Override
public int arrayOffset() { public int arrayOffset() {
return buffer.arrayOffset(); return unwrap().arrayOffset();
} }
@Override @Override
public boolean hasMemoryAddress() { public boolean hasMemoryAddress() {
return buffer.hasMemoryAddress(); return unwrap().hasMemoryAddress();
} }
@Override @Override
public long memoryAddress() { public long memoryAddress() {
return buffer.memoryAddress(); return unwrap().memoryAddress();
} }
@Override @Override
public byte getByte(int index) { public byte getByte(int index) {
return buffer.getByte(index); return unwrap().getByte(index);
} }
@Override @Override
protected byte _getByte(int index) { protected byte _getByte(int index) {
return buffer.getByte(index); return unwrap().getByte(index);
} }
@Override @Override
public short getShort(int index) { public short getShort(int index) {
return buffer.getShort(index); return unwrap().getShort(index);
} }
@Override @Override
protected short _getShort(int index) { protected short _getShort(int index) {
return buffer.getShort(index); return unwrap().getShort(index);
} }
@Override @Override
public int getUnsignedMedium(int index) { public int getUnsignedMedium(int index) {
return buffer.getUnsignedMedium(index); return unwrap().getUnsignedMedium(index);
} }
@Override @Override
protected int _getUnsignedMedium(int index) { protected int _getUnsignedMedium(int index) {
return buffer.getUnsignedMedium(index); return unwrap().getUnsignedMedium(index);
} }
@Override @Override
public int getInt(int index) { public int getInt(int index) {
return buffer.getInt(index); return unwrap().getInt(index);
} }
@Override @Override
protected int _getInt(int index) { protected int _getInt(int index) {
return buffer.getInt(index); return unwrap().getInt(index);
} }
@Override @Override
public long getLong(int index) { public long getLong(int index) {
return buffer.getLong(index); return unwrap().getLong(index);
} }
@Override @Override
protected long _getLong(int index) { protected long _getLong(int index) {
return buffer.getLong(index); return unwrap().getLong(index);
} }
@Override @Override
public ByteBuf copy(int index, int length) { public ByteBuf copy(int index, int length) {
return buffer.copy(index, length); return unwrap().copy(index, length);
} }
@Override @Override
public ByteBuf slice(int index, int length) { public ByteBuf slice(int index, int length) {
return buffer.slice(index, length); return unwrap().slice(index, length);
} }
@Override @Override
public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
buffer.getBytes(index, dst, dstIndex, length); unwrap().getBytes(index, dst, dstIndex, length);
return this; return this;
} }
@Override @Override
public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) { public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
buffer.getBytes(index, dst, dstIndex, length); unwrap().getBytes(index, dst, dstIndex, length);
return this; return this;
} }
@Override @Override
public ByteBuf getBytes(int index, ByteBuffer dst) { public ByteBuf getBytes(int index, ByteBuffer dst) {
buffer.getBytes(index, dst); unwrap().getBytes(index, dst);
return this; return this;
} }
@Override @Override
public ByteBuf setByte(int index, int value) { public ByteBuf setByte(int index, int value) {
buffer.setByte(index, value); unwrap().setByte(index, value);
return this; return this;
} }
@Override @Override
protected void _setByte(int index, int value) { protected void _setByte(int index, int value) {
buffer.setByte(index, value); unwrap().setByte(index, value);
} }
@Override @Override
public ByteBuf setShort(int index, int value) { public ByteBuf setShort(int index, int value) {
buffer.setShort(index, value); unwrap().setShort(index, value);
return this; return this;
} }
@Override @Override
protected void _setShort(int index, int value) { protected void _setShort(int index, int value) {
buffer.setShort(index, value); unwrap().setShort(index, value);
} }
@Override @Override
public ByteBuf setMedium(int index, int value) { public ByteBuf setMedium(int index, int value) {
buffer.setMedium(index, value); unwrap().setMedium(index, value);
return this; return this;
} }
@Override @Override
protected void _setMedium(int index, int value) { protected void _setMedium(int index, int value) {
buffer.setMedium(index, value); unwrap().setMedium(index, value);
} }
@Override @Override
public ByteBuf setInt(int index, int value) { public ByteBuf setInt(int index, int value) {
buffer.setInt(index, value); unwrap().setInt(index, value);
return this; return this;
} }
@Override @Override
protected void _setInt(int index, int value) { protected void _setInt(int index, int value) {
buffer.setInt(index, value); unwrap().setInt(index, value);
} }
@Override @Override
public ByteBuf setLong(int index, long value) { public ByteBuf setLong(int index, long value) {
buffer.setLong(index, value); unwrap().setLong(index, value);
return this; return this;
} }
@Override @Override
protected void _setLong(int index, long value) { protected void _setLong(int index, long value) {
buffer.setLong(index, value); unwrap().setLong(index, value);
} }
@Override @Override
public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) { public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
buffer.setBytes(index, src, srcIndex, length); unwrap().setBytes(index, src, srcIndex, length);
return this; return this;
} }
@Override @Override
public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) { public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
buffer.setBytes(index, src, srcIndex, length); unwrap().setBytes(index, src, srcIndex, length);
return this; return this;
} }
@Override @Override
public ByteBuf setBytes(int index, ByteBuffer src) { public ByteBuf setBytes(int index, ByteBuffer src) {
buffer.setBytes(index, src); unwrap().setBytes(index, src);
return this; return this;
} }
@Override @Override
public ByteBuf getBytes(int index, OutputStream out, int length) public ByteBuf getBytes(int index, OutputStream out, int length)
throws IOException { throws IOException {
buffer.getBytes(index, out, length); unwrap().getBytes(index, out, length);
return this; return this;
} }
@Override @Override
public int getBytes(int index, GatheringByteChannel out, int length) public int getBytes(int index, GatheringByteChannel out, int length)
throws IOException { throws IOException {
return buffer.getBytes(index, out, length); return unwrap().getBytes(index, out, length);
} }
@Override @Override
public int setBytes(int index, InputStream in, int length) public int setBytes(int index, InputStream in, int length)
throws IOException { throws IOException {
return buffer.setBytes(index, in, length); return unwrap().setBytes(index, in, length);
} }
@Override @Override
public int setBytes(int index, ScatteringByteChannel in, int length) public int setBytes(int index, ScatteringByteChannel in, int length)
throws IOException { throws IOException {
return buffer.setBytes(index, in, length); return unwrap().setBytes(index, in, length);
} }
@Override @Override
public int nioBufferCount() { public int nioBufferCount() {
return buffer.nioBufferCount(); return unwrap().nioBufferCount();
} }
@Override @Override
public ByteBuffer[] nioBuffers(int index, int length) { public ByteBuffer[] nioBuffers(int index, int length) {
return buffer.nioBuffers(index, length); return unwrap().nioBuffers(index, length);
}
@Override
public ByteBuffer internalNioBuffer(int index, int length) {
return nioBuffer(index, length);
} }
@Override @Override
public int forEachByte(int index, int length, ByteBufProcessor processor) { public int forEachByte(int index, int length, ByteBufProcessor processor) {
return buffer.forEachByte(index, length, processor); return unwrap().forEachByte(index, length, processor);
} }
@Override @Override
public int forEachByteDesc(int index, int length, ByteBufProcessor processor) { public int forEachByteDesc(int index, int length, ByteBufProcessor processor) {
return buffer.forEachByteDesc(index, length, processor); return unwrap().forEachByteDesc(index, length, processor);
} }
} }

View File

@ -1,7 +1,9 @@
/* /*
* Copyright 2015 The Netty Project * Copyright 2015 The Netty Project
* *
* The Netty Project licenses this file tothe License at: * 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 * http://www.apache.org/licenses/LICENSE-2.0
* *
@ -92,6 +94,7 @@ final class PooledUnsafeHeapByteBuf extends PooledHeapByteBuf {
} }
@Override @Override
@Deprecated
protected SwappedByteBuf newSwappedByteBuf() { protected SwappedByteBuf newSwappedByteBuf() {
if (PlatformDependent.isUnaligned()) { if (PlatformDependent.isUnaligned()) {
// Only use if unaligned access is supported otherwise there is no gain. // Only use if unaligned access is supported otherwise there is no gain.

View File

@ -28,7 +28,10 @@ import java.nio.channels.ScatteringByteChannel;
* A derived buffer which forbids any write requests to its parent. It is * A derived buffer which forbids any write requests to its parent. It is
* recommended to use {@link Unpooled#unmodifiableBuffer(ByteBuf)} * recommended to use {@link Unpooled#unmodifiableBuffer(ByteBuf)}
* instead of calling the constructor explicitly. * instead of calling the constructor explicitly.
*
* @deprecated Do not use.
*/ */
@Deprecated
public class ReadOnlyByteBuf extends AbstractDerivedByteBuf { public class ReadOnlyByteBuf extends AbstractDerivedByteBuf {
private final ByteBuf buffer; private final ByteBuf buffer;
@ -61,17 +64,18 @@ public class ReadOnlyByteBuf extends AbstractDerivedByteBuf {
@Override @Override
public ByteBufAllocator alloc() { public ByteBufAllocator alloc() {
return buffer.alloc(); return unwrap().alloc();
} }
@Override @Override
@Deprecated
public ByteOrder order() { public ByteOrder order() {
return buffer.order(); return unwrap().order();
} }
@Override @Override
public boolean isDirect() { public boolean isDirect() {
return buffer.isDirect(); return unwrap().isDirect();
} }
@Override @Override
@ -182,31 +186,31 @@ public class ReadOnlyByteBuf extends AbstractDerivedByteBuf {
@Override @Override
public int getBytes(int index, GatheringByteChannel out, int length) public int getBytes(int index, GatheringByteChannel out, int length)
throws IOException { throws IOException {
return buffer.getBytes(index, out, length); return unwrap().getBytes(index, out, length);
} }
@Override @Override
public ByteBuf getBytes(int index, OutputStream out, int length) public ByteBuf getBytes(int index, OutputStream out, int length)
throws IOException { throws IOException {
buffer.getBytes(index, out, length); unwrap().getBytes(index, out, length);
return this; return this;
} }
@Override @Override
public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) { public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
buffer.getBytes(index, dst, dstIndex, length); unwrap().getBytes(index, dst, dstIndex, length);
return this; return this;
} }
@Override @Override
public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
buffer.getBytes(index, dst, dstIndex, length); unwrap().getBytes(index, dst, dstIndex, length);
return this; return this;
} }
@Override @Override
public ByteBuf getBytes(int index, ByteBuffer dst) { public ByteBuf getBytes(int index, ByteBuffer dst) {
buffer.getBytes(index, dst); unwrap().getBytes(index, dst);
return this; return this;
} }
@ -217,97 +221,92 @@ public class ReadOnlyByteBuf extends AbstractDerivedByteBuf {
@Override @Override
public ByteBuf copy(int index, int length) { public ByteBuf copy(int index, int length) {
return buffer.copy(index, length); return unwrap().copy(index, length);
} }
@Override @Override
public ByteBuf slice(int index, int length) { public ByteBuf slice(int index, int length) {
return Unpooled.unmodifiableBuffer(buffer.slice(index, length)); return Unpooled.unmodifiableBuffer(unwrap().slice(index, length));
} }
@Override @Override
public byte getByte(int index) { public byte getByte(int index) {
return _getByte(index); return unwrap().getByte(index);
} }
@Override @Override
protected byte _getByte(int index) { protected byte _getByte(int index) {
return buffer.getByte(index); return unwrap().getByte(index);
} }
@Override @Override
public short getShort(int index) { public short getShort(int index) {
return _getShort(index); return unwrap().getShort(index);
} }
@Override @Override
protected short _getShort(int index) { protected short _getShort(int index) {
return buffer.getShort(index); return unwrap().getShort(index);
} }
@Override @Override
public int getUnsignedMedium(int index) { public int getUnsignedMedium(int index) {
return _getUnsignedMedium(index); return unwrap().getUnsignedMedium(index);
} }
@Override @Override
protected int _getUnsignedMedium(int index) { protected int _getUnsignedMedium(int index) {
return buffer.getUnsignedMedium(index); return unwrap().getUnsignedMedium(index);
} }
@Override @Override
public int getInt(int index) { public int getInt(int index) {
return _getInt(index); return unwrap().getInt(index);
} }
@Override @Override
protected int _getInt(int index) { protected int _getInt(int index) {
return buffer.getInt(index); return unwrap().getInt(index);
} }
@Override @Override
public long getLong(int index) { public long getLong(int index) {
return _getLong(index); return unwrap().getLong(index);
} }
@Override @Override
protected long _getLong(int index) { protected long _getLong(int index) {
return buffer.getLong(index); return unwrap().getLong(index);
} }
@Override @Override
public int nioBufferCount() { public int nioBufferCount() {
return buffer.nioBufferCount(); return unwrap().nioBufferCount();
} }
@Override @Override
public ByteBuffer nioBuffer(int index, int length) { public ByteBuffer nioBuffer(int index, int length) {
return buffer.nioBuffer(index, length).asReadOnlyBuffer(); return unwrap().nioBuffer(index, length).asReadOnlyBuffer();
} }
@Override @Override
public ByteBuffer[] nioBuffers(int index, int length) { public ByteBuffer[] nioBuffers(int index, int length) {
return buffer.nioBuffers(index, length); return unwrap().nioBuffers(index, length);
}
@Override
public ByteBuffer internalNioBuffer(int index, int length) {
return nioBuffer(index, length);
} }
@Override @Override
public int forEachByte(int index, int length, ByteBufProcessor processor) { public int forEachByte(int index, int length, ByteBufProcessor processor) {
return buffer.forEachByte(index, length, processor); return unwrap().forEachByte(index, length, processor);
} }
@Override @Override
public int forEachByteDesc(int index, int length, ByteBufProcessor processor) { public int forEachByteDesc(int index, int length, ByteBufProcessor processor) {
return buffer.forEachByteDesc(index, length, processor); return unwrap().forEachByteDesc(index, length, processor);
} }
@Override @Override
public int capacity() { public int capacity() {
return buffer.capacity(); return unwrap().capacity();
} }
@Override @Override

View File

@ -80,7 +80,9 @@ class ReadOnlyByteBufferBuf extends AbstractReferenceCountedByteBuf {
@Override @Override
protected int _getUnsignedMedium(int index) { protected int _getUnsignedMedium(int index) {
return (getByte(index) & 0xff) << 16 | (getByte(index + 1) & 0xff) << 8 | getByte(index + 2) & 0xff; return (getByte(index) & 0xff) << 16 |
(getByte(index + 1) & 0xff) << 8 |
getByte(index + 2) & 0xff;
} }
@Override @Override
@ -151,26 +153,51 @@ class ReadOnlyByteBufferBuf extends AbstractReferenceCountedByteBuf {
return this; return this;
} }
@Override
public ByteBuf setByte(int index, int value) {
throw new ReadOnlyBufferException();
}
@Override @Override
protected void _setByte(int index, int value) { protected void _setByte(int index, int value) {
throw new ReadOnlyBufferException(); throw new ReadOnlyBufferException();
} }
@Override
public ByteBuf setShort(int index, int value) {
throw new ReadOnlyBufferException();
}
@Override @Override
protected void _setShort(int index, int value) { protected void _setShort(int index, int value) {
throw new ReadOnlyBufferException(); throw new ReadOnlyBufferException();
} }
@Override
public ByteBuf setMedium(int index, int value) {
throw new ReadOnlyBufferException();
}
@Override @Override
protected void _setMedium(int index, int value) { protected void _setMedium(int index, int value) {
throw new ReadOnlyBufferException(); throw new ReadOnlyBufferException();
} }
@Override
public ByteBuf setInt(int index, int value) {
throw new ReadOnlyBufferException();
}
@Override @Override
protected void _setInt(int index, int value) { protected void _setInt(int index, int value) {
throw new ReadOnlyBufferException(); throw new ReadOnlyBufferException();
} }
@Override
public ByteBuf setLong(int index, long value) {
throw new ReadOnlyBufferException();
}
@Override @Override
protected void _setLong(int index, long value) { protected void _setLong(int index, long value) {
throw new ReadOnlyBufferException(); throw new ReadOnlyBufferException();

View File

@ -25,57 +25,59 @@ final class SlicedAbstractByteBuf extends SlicedByteBuf {
super(buffer, index, length); super(buffer, index, length);
} }
@Override
public AbstractByteBuf unwrap() {
return (AbstractByteBuf) super.unwrap();
}
@Override @Override
protected byte _getByte(int index) { protected byte _getByte(int index) {
return unwrap0()._getByte(idx(index)); return unwrap()._getByte(idx(index));
} }
@Override @Override
protected short _getShort(int index) { protected short _getShort(int index) {
return unwrap0()._getShort(idx(index)); return unwrap()._getShort(idx(index));
} }
@Override @Override
protected int _getUnsignedMedium(int index) { protected int _getUnsignedMedium(int index) {
return unwrap0()._getUnsignedMedium(idx(index)); return unwrap()._getUnsignedMedium(idx(index));
} }
@Override @Override
protected int _getInt(int index) { protected int _getInt(int index) {
return unwrap0()._getInt(idx(index)); return unwrap()._getInt(idx(index));
} }
@Override @Override
protected long _getLong(int index) { protected long _getLong(int index) {
return unwrap0()._getLong(idx(index)); return unwrap()._getLong(idx(index));
} }
@Override @Override
protected void _setByte(int index, int value) { protected void _setByte(int index, int value) {
unwrap0()._setByte(idx(index), value); unwrap()._setByte(idx(index), value);
} }
@Override @Override
protected void _setShort(int index, int value) { protected void _setShort(int index, int value) {
unwrap0()._setShort(idx(index), value); unwrap()._setShort(idx(index), value);
} }
@Override @Override
protected void _setMedium(int index, int value) { protected void _setMedium(int index, int value) {
unwrap0()._setMedium(idx(index), value); unwrap()._setMedium(idx(index), value);
} }
@Override @Override
protected void _setInt(int index, int value) { protected void _setInt(int index, int value) {
unwrap0()._setInt(idx(index), value); unwrap()._setInt(idx(index), value);
} }
@Override @Override
protected void _setLong(int index, long value) { protected void _setLong(int index, long value) {
unwrap0()._setLong(idx(index), value); unwrap()._setLong(idx(index), value);
} }
private AbstractByteBuf unwrap0() {
return (AbstractByteBuf) unwrap();
}
} }

View File

@ -23,13 +23,15 @@ import java.nio.ByteOrder;
import java.nio.channels.GatheringByteChannel; import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel; import java.nio.channels.ScatteringByteChannel;
/** /**
* A derived buffer which exposes its parent's sub-region only. It is * A derived buffer which exposes its parent's sub-region only. It is
* recommended to use {@link ByteBuf#slice()} and * recommended to use {@link ByteBuf#slice()} and
* {@link ByteBuf#slice(int, int)} instead of calling the constructor * {@link ByteBuf#slice(int, int)} instead of calling the constructor
* explicitly. * explicitly.
*
* @deprecated Do not use.
*/ */
@Deprecated
public class SlicedByteBuf extends AbstractDerivedByteBuf { public class SlicedByteBuf extends AbstractDerivedByteBuf {
private final ByteBuf buffer; private final ByteBuf buffer;
@ -64,17 +66,18 @@ public class SlicedByteBuf extends AbstractDerivedByteBuf {
@Override @Override
public ByteBufAllocator alloc() { public ByteBufAllocator alloc() {
return buffer.alloc(); return unwrap().alloc();
} }
@Override @Override
@Deprecated
public ByteOrder order() { public ByteOrder order() {
return buffer.order(); return unwrap().order();
} }
@Override @Override
public boolean isDirect() { public boolean isDirect() {
return buffer.isDirect(); return unwrap().isDirect();
} }
@Override @Override
@ -89,57 +92,87 @@ public class SlicedByteBuf extends AbstractDerivedByteBuf {
@Override @Override
public boolean hasArray() { public boolean hasArray() {
return buffer.hasArray(); return unwrap().hasArray();
} }
@Override @Override
public byte[] array() { public byte[] array() {
return buffer.array(); return unwrap().array();
} }
@Override @Override
public int arrayOffset() { public int arrayOffset() {
return idx(buffer.arrayOffset()); return idx(unwrap().arrayOffset());
} }
@Override @Override
public boolean hasMemoryAddress() { public boolean hasMemoryAddress() {
return buffer.hasMemoryAddress(); return unwrap().hasMemoryAddress();
} }
@Override @Override
public long memoryAddress() { public long memoryAddress() {
return buffer.memoryAddress() + adjustment; return unwrap().memoryAddress() + adjustment;
}
@Override
public byte getByte(int index) {
checkIndex0(index, 1);
return unwrap().getByte(idx(index));
} }
@Override @Override
protected byte _getByte(int index) { protected byte _getByte(int index) {
return buffer.getByte(idx(index)); return unwrap().getByte(idx(index));
}
@Override
public short getShort(int index) {
checkIndex0(index, 2);
return unwrap().getShort(idx(index));
} }
@Override @Override
protected short _getShort(int index) { protected short _getShort(int index) {
return buffer.getShort(idx(index)); return unwrap().getShort(idx(index));
}
@Override
public int getUnsignedMedium(int index) {
checkIndex0(index, 3);
return unwrap().getUnsignedMedium(idx(index));
} }
@Override @Override
protected int _getUnsignedMedium(int index) { protected int _getUnsignedMedium(int index) {
return buffer.getUnsignedMedium(idx(index)); return unwrap().getUnsignedMedium(idx(index));
}
@Override
public int getInt(int index) {
checkIndex0(index, 4);
return unwrap().getInt(idx(index));
} }
@Override @Override
protected int _getInt(int index) { protected int _getInt(int index) {
return buffer.getInt(idx(index)); return unwrap().getInt(idx(index));
}
@Override
public long getLong(int index) {
checkIndex0(index, 8);
return unwrap().getLong(idx(index));
} }
@Override @Override
protected long _getLong(int index) { protected long _getLong(int index) {
return buffer.getLong(idx(index)); return unwrap().getLong(idx(index));
} }
@Override @Override
public ByteBuf duplicate() { public ByteBuf duplicate() {
ByteBuf duplicate = buffer.slice(adjustment, length); final ByteBuf duplicate = unwrap().slice(adjustment, length);
duplicate.setIndex(readerIndex(), writerIndex()); duplicate.setIndex(readerIndex(), writerIndex());
return duplicate; return duplicate;
} }
@ -147,133 +180,163 @@ public class SlicedByteBuf extends AbstractDerivedByteBuf {
@Override @Override
public ByteBuf copy(int index, int length) { public ByteBuf copy(int index, int length) {
checkIndex0(index, length); checkIndex0(index, length);
return buffer.copy(idx(index), length); return unwrap().copy(idx(index), length);
} }
@Override @Override
public ByteBuf slice(int index, int length) { public ByteBuf slice(int index, int length) {
checkIndex0(index, length); checkIndex0(index, length);
return buffer.slice(idx(index), length); return unwrap().slice(idx(index), length);
} }
@Override @Override
public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
checkIndex0(index, length); checkIndex0(index, length);
buffer.getBytes(idx(index), dst, dstIndex, length); unwrap().getBytes(idx(index), dst, dstIndex, length);
return this; return this;
} }
@Override @Override
public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) { public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
checkIndex0(index, length); checkIndex0(index, length);
buffer.getBytes(idx(index), dst, dstIndex, length); unwrap().getBytes(idx(index), dst, dstIndex, length);
return this; return this;
} }
@Override @Override
public ByteBuf getBytes(int index, ByteBuffer dst) { public ByteBuf getBytes(int index, ByteBuffer dst) {
checkIndex0(index, dst.remaining()); checkIndex0(index, dst.remaining());
buffer.getBytes(idx(index), dst); unwrap().getBytes(idx(index), dst);
return this;
}
@Override
public ByteBuf setByte(int index, int value) {
checkIndex0(index, 1);
unwrap().setByte(idx(index), value);
return this; return this;
} }
@Override @Override
protected void _setByte(int index, int value) { protected void _setByte(int index, int value) {
buffer.setByte(idx(index), value); unwrap().setByte(idx(index), value);
}
@Override
public ByteBuf setShort(int index, int value) {
checkIndex0(index, 2);
unwrap().setShort(idx(index), value);
return this;
} }
@Override @Override
protected void _setShort(int index, int value) { protected void _setShort(int index, int value) {
buffer.setShort(idx(index), value); unwrap().setShort(idx(index), value);
}
@Override
public ByteBuf setMedium(int index, int value) {
checkIndex0(index, 3);
unwrap().setMedium(idx(index), value);
return this;
} }
@Override @Override
protected void _setMedium(int index, int value) { protected void _setMedium(int index, int value) {
buffer.setMedium(idx(index), value); unwrap().setMedium(idx(index), value);
}
@Override
public ByteBuf setInt(int index, int value) {
checkIndex0(index, 4);
unwrap().setInt(idx(index), value);
return this;
} }
@Override @Override
protected void _setInt(int index, int value) { protected void _setInt(int index, int value) {
buffer.setInt(idx(index), value); unwrap().setInt(idx(index), value);
}
@Override
public ByteBuf setLong(int index, long value) {
checkIndex0(index, 8);
unwrap().setLong(idx(index), value);
return this;
} }
@Override @Override
protected void _setLong(int index, long value) { protected void _setLong(int index, long value) {
buffer.setLong(idx(index), value); unwrap().setLong(idx(index), value);
} }
@Override @Override
public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) { public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
checkIndex0(index, length); checkIndex0(index, length);
buffer.setBytes(idx(index), src, srcIndex, length); unwrap().setBytes(idx(index), src, srcIndex, length);
return this; return this;
} }
@Override @Override
public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) { public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
checkIndex0(index, length); checkIndex0(index, length);
buffer.setBytes(idx(index), src, srcIndex, length); unwrap().setBytes(idx(index), src, srcIndex, length);
return this; return this;
} }
@Override @Override
public ByteBuf setBytes(int index, ByteBuffer src) { public ByteBuf setBytes(int index, ByteBuffer src) {
checkIndex0(index, src.remaining()); checkIndex0(index, src.remaining());
buffer.setBytes(idx(index), src); unwrap().setBytes(idx(index), src);
return this; return this;
} }
@Override @Override
public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException { public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException {
checkIndex0(index, length); checkIndex0(index, length);
buffer.getBytes(idx(index), out, length); unwrap().getBytes(idx(index), out, length);
return this; return this;
} }
@Override @Override
public int getBytes(int index, GatheringByteChannel out, int length) throws IOException { public int getBytes(int index, GatheringByteChannel out, int length) throws IOException {
checkIndex0(index, length); checkIndex0(index, length);
return buffer.getBytes(idx(index), out, length); return unwrap().getBytes(idx(index), out, length);
} }
@Override @Override
public int setBytes(int index, InputStream in, int length) throws IOException { public int setBytes(int index, InputStream in, int length) throws IOException {
checkIndex0(index, length); checkIndex0(index, length);
return buffer.setBytes(idx(index), in, length); return unwrap().setBytes(idx(index), in, length);
} }
@Override @Override
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException { public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
checkIndex0(index, length); checkIndex0(index, length);
return buffer.setBytes(idx(index), in, length); return unwrap().setBytes(idx(index), in, length);
} }
@Override @Override
public int nioBufferCount() { public int nioBufferCount() {
return buffer.nioBufferCount(); return unwrap().nioBufferCount();
} }
@Override @Override
public ByteBuffer nioBuffer(int index, int length) { public ByteBuffer nioBuffer(int index, int length) {
checkIndex0(index, length); checkIndex0(index, length);
return buffer.nioBuffer(idx(index), length); return unwrap().nioBuffer(idx(index), length);
} }
@Override @Override
public ByteBuffer[] nioBuffers(int index, int length) { public ByteBuffer[] nioBuffers(int index, int length) {
checkIndex0(index, length); checkIndex0(index, length);
return buffer.nioBuffers(idx(index), length); return unwrap().nioBuffers(idx(index), length);
}
@Override
public ByteBuffer internalNioBuffer(int index, int length) {
return nioBuffer(index, length);
} }
@Override @Override
public int forEachByte(int index, int length, ByteBufProcessor processor) { public int forEachByte(int index, int length, ByteBufProcessor processor) {
checkIndex0(index, length); checkIndex0(index, length);
int ret = buffer.forEachByte(idx(index), length, processor); int ret = unwrap().forEachByte(idx(index), length, processor);
if (ret >= adjustment) { if (ret >= adjustment) {
return ret - adjustment; return ret - adjustment;
} else { } else {
@ -284,7 +347,7 @@ public class SlicedByteBuf extends AbstractDerivedByteBuf {
@Override @Override
public int forEachByteDesc(int index, int length, ByteBufProcessor processor) { public int forEachByteDesc(int index, int length, ByteBufProcessor processor) {
checkIndex0(index, length); checkIndex0(index, length);
int ret = buffer.forEachByteDesc(idx(index), length, processor); int ret = unwrap().forEachByteDesc(idx(index), length, processor);
if (ret >= adjustment) { if (ret >= adjustment) {
return ret - adjustment; return ret - adjustment;
} else { } else {

View File

@ -15,7 +15,6 @@
*/ */
package io.netty.buffer; package io.netty.buffer;
import io.netty.util.internal.PlatformDependent; import io.netty.util.internal.PlatformDependent;
final class UnpooledUnsafeHeapByteBuf extends UnpooledHeapByteBuf { final class UnpooledUnsafeHeapByteBuf extends UnpooledHeapByteBuf {
@ -146,6 +145,7 @@ final class UnpooledUnsafeHeapByteBuf extends UnpooledHeapByteBuf {
} }
@Override @Override
@Deprecated
protected SwappedByteBuf newSwappedByteBuf() { protected SwappedByteBuf newSwappedByteBuf() {
if (PlatformDependent.isUnaligned()) { if (PlatformDependent.isUnaligned()) {
// Only use if unaligned access is supported otherwise there is no gain. // Only use if unaligned access is supported otherwise there is no gain.