From 35c493d74a26db4a3a3fadda8e2360aa2ed56f48 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Tue, 12 Apr 2016 20:17:03 +0900 Subject: [PATCH] Make derived buffers recyclable / Add missing overrides to ByteBuf impls Related: #4333 #4421 Motivation: slice(), duplicate() and readSlice() currently create a non-recyclable derived buffer instance. Under heavy load, an application that creates a lot of derived buffers can put the garbage collector under pressure. Modifications: - Deprecate the old derived buffer implementations - Add the new recyclable derived buffer implementations, which has its own reference count value - When a derived buffer is created, its internal reference count is 0. - When retain() is called on a derived buffer, the internal reference count becomes a positive value and the original buffer's retain() is called. - When release() is called on a derived buffer, the internal reference count is decreased first, and then the original buffer's release() is called when the internal reference count is 0. - Add ByteBufUtil.duplicate/slice() so that a user can easily implement ByteBuf.duplicate/slice() - Add missing overrides in some ByteBuf impls - Fix incorrect range checks in SlicedByteBuf - Miscellaneous: - Merge Duplicated/SlicedAbstractByteBuf.unwrap0() into unwrap() using covariant return type Result: - Derived buffers are now recycled when retained and released, although they are not recycled if a user called release() against the original buffer. buf.slice().retain().release(); // recycled buf.slice().retain(); buf.release(); // not recycled - Correct range checks in SlicedByteBuf --- .../java/io/netty/buffer/AbstractByteBuf.java | 30 +- .../buffer/AbstractByteBufAllocator.java | 2 +- .../netty/buffer/AbstractDerivedByteBuf.java | 5 +- .../buffer/AbstractPooledDerivedByteBuf.java | 147 ++++++ .../AbstractReferenceCountedByteBuf.java | 2 +- .../buffer/AbstractUnsafeSwappedByteBuf.java | 3 +- .../buffer/AdvancedLeakAwareByteBuf.java | 26 +- .../AdvancedLeakAwareCompositeByteBuf.java | 25 + .../main/java/io/netty/buffer/ByteBuf.java | 67 ++- .../io/netty/buffer/ByteBufAllocator.java | 2 +- .../java/io/netty/buffer/ByteBufHolder.java | 2 +- .../io/netty/buffer/ByteBufInputStream.java | 2 +- .../io/netty/buffer/ByteBufOutputStream.java | 2 +- .../io/netty/buffer/ByteBufProcessor.java | 2 +- .../java/io/netty/buffer/ByteBufUtil.java | 61 ++- .../io/netty/buffer/CompositeByteBuf.java | 2 +- .../io/netty/buffer/DefaultByteBufHolder.java | 2 +- .../buffer/DuplicatedAbstractByteBuf.java | 72 ++- .../io/netty/buffer/DuplicatedByteBuf.java | 68 ++- .../java/io/netty/buffer/EmptyByteBuf.java | 22 +- .../netty/buffer/FixedCompositeByteBuf.java | 2 +- .../java/io/netty/buffer/HeapByteBufUtil.java | 2 +- .../main/java/io/netty/buffer/PoolArena.java | 2 +- .../java/io/netty/buffer/PoolArenaMetric.java | 2 +- .../main/java/io/netty/buffer/PoolChunk.java | 2 +- .../java/io/netty/buffer/PoolChunkList.java | 2 +- .../io/netty/buffer/PoolChunkListMetric.java | 2 +- .../java/io/netty/buffer/PoolChunkMetric.java | 2 +- .../java/io/netty/buffer/PoolSubpage.java | 2 +- .../io/netty/buffer/PoolSubpageMetric.java | 2 +- .../java/io/netty/buffer/PoolThreadCache.java | 2 +- .../java/io/netty/buffer/PooledByteBuf.java | 2 +- .../netty/buffer/PooledByteBufAllocator.java | 2 +- .../io/netty/buffer/PooledDirectByteBuf.java | 2 +- .../netty/buffer/PooledDuplicatedByteBuf.java | 379 +++++++++++++++ .../io/netty/buffer/PooledHeapByteBuf.java | 6 +- .../netty/buffer/PooledReadOnlyByteBuf.java | 385 +++++++++++++++ .../io/netty/buffer/PooledSlicedByteBuf.java | 449 ++++++++++++++++++ .../buffer/PooledUnsafeDirectByteBuf.java | 3 +- .../netty/buffer/PooledUnsafeHeapByteBuf.java | 47 +- .../java/io/netty/buffer/ReadOnlyByteBuf.java | 74 ++- .../netty/buffer/ReadOnlyByteBufferBuf.java | 71 ++- .../buffer/ReadOnlyUnsafeDirectByteBuf.java | 2 +- .../netty/buffer/SimpleLeakAwareByteBuf.java | 22 +- .../SimpleLeakAwareCompositeByteBuf.java | 21 + .../netty/buffer/SlicedAbstractByteBuf.java | 70 ++- .../java/io/netty/buffer/SlicedByteBuf.java | 141 +++++- .../java/io/netty/buffer/SwappedByteBuf.java | 23 +- .../main/java/io/netty/buffer/Unpooled.java | 13 +- .../buffer/UnpooledByteBufAllocator.java | 2 +- .../netty/buffer/UnpooledDirectByteBuf.java | 2 +- .../io/netty/buffer/UnpooledHeapByteBuf.java | 54 ++- .../buffer/UnpooledUnsafeDirectByteBuf.java | 3 +- .../buffer/UnpooledUnsafeHeapByteBuf.java | 95 +++- .../io/netty/buffer/UnreleasableByteBuf.java | 22 +- .../io/netty/buffer/UnsafeByteBufUtil.java | 2 +- .../buffer/UnsafeDirectSwappedByteBuf.java | 28 +- .../buffer/UnsafeHeapSwappedByteBuf.java | 28 +- .../java/io/netty/buffer/WrappedByteBuf.java | 22 +- .../netty/buffer/WrappedCompositeByteBuf.java | 21 + .../java/io/netty/buffer/package-info.java | 2 +- .../io/netty/buffer/AbstractByteBufTest.java | 46 ++ .../buffer/AbstractCompositeByteBufTest.java | 2 +- .../netty/buffer/ByteBufDerivationTest.java | 16 +- .../io/netty/buffer/ReadOnlyByteBufTest.java | 24 +- .../io/netty/buffer/SlicedByteBufTest.java | 3 +- .../codec/ReplayingDecoderByteBuf.java | 27 ++ 67 files changed, 2500 insertions(+), 177 deletions(-) create mode 100644 buffer/src/main/java/io/netty/buffer/AbstractPooledDerivedByteBuf.java create mode 100644 buffer/src/main/java/io/netty/buffer/PooledDuplicatedByteBuf.java create mode 100644 buffer/src/main/java/io/netty/buffer/PooledReadOnlyByteBuf.java create mode 100644 buffer/src/main/java/io/netty/buffer/PooledSlicedByteBuf.java diff --git a/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java b/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java index f6872e9d847..d982ded2f30 100644 --- a/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 The Netty Project + * Copyright 2016 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 @@ -799,6 +799,13 @@ public ByteBuf readSlice(int length) { return slice; } + @Override + public ByteBuf readSliceRetained(int length) { + ByteBuf slice = sliceRetained(readerIndex, length); + readerIndex += length; + return slice; + } + @Override public ByteBuf readBytes(byte[] dst, int dstIndex, int length) { checkReadableBytes(length); @@ -1112,17 +1119,32 @@ public ByteBuf copy() { @Override public ByteBuf duplicate() { - return new DuplicatedAbstractByteBuf(this); + return ByteBufUtil.duplicate(this); + } + + @Override + public ByteBuf duplicateRetained() { + return duplicate().retain(); } @Override public ByteBuf slice() { - return slice(readerIndex, readableBytes()); + return ByteBufUtil.slice(this); + } + + @Override + public ByteBuf sliceRetained() { + return slice().retain(); } @Override public ByteBuf slice(int index, int length) { - return new SlicedAbstractByteBuf(this, index, length); + return ByteBufUtil.slice(this, index, length); + } + + @Override + public ByteBuf sliceRetained(int index, int length) { + return slice(index, length).retain(); } @Override diff --git a/buffer/src/main/java/io/netty/buffer/AbstractByteBufAllocator.java b/buffer/src/main/java/io/netty/buffer/AbstractByteBufAllocator.java index 50d82e2512d..05f064348c4 100644 --- a/buffer/src/main/java/io/netty/buffer/AbstractByteBufAllocator.java +++ b/buffer/src/main/java/io/netty/buffer/AbstractByteBufAllocator.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 The Netty Project + * Copyright 2016 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 diff --git a/buffer/src/main/java/io/netty/buffer/AbstractDerivedByteBuf.java b/buffer/src/main/java/io/netty/buffer/AbstractDerivedByteBuf.java index e385fa2c316..d5188405e07 100644 --- a/buffer/src/main/java/io/netty/buffer/AbstractDerivedByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/AbstractDerivedByteBuf.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 The Netty Project + * Copyright 2016 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 @@ -21,7 +21,10 @@ /** * Abstract base class for {@link ByteBuf} implementations that wrap another * {@link ByteBuf}. + * + * @deprecated Do not use. */ +@Deprecated public abstract class AbstractDerivedByteBuf extends AbstractByteBuf { protected AbstractDerivedByteBuf(int maxCapacity) { diff --git a/buffer/src/main/java/io/netty/buffer/AbstractPooledDerivedByteBuf.java b/buffer/src/main/java/io/netty/buffer/AbstractPooledDerivedByteBuf.java new file mode 100644 index 00000000000..33581831360 --- /dev/null +++ b/buffer/src/main/java/io/netty/buffer/AbstractPooledDerivedByteBuf.java @@ -0,0 +1,147 @@ +/* + * Copyright 2016 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.buffer; + +import io.netty.util.Recycler.Handle; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; + +/** + * Abstract base class for derived {@link ByteBuf} implementations. + */ +abstract class AbstractPooledDerivedByteBuf extends AbstractByteBuf { + + private final Handle> recyclerHandle; + private AbstractByteBuf buffer; + + @SuppressWarnings("unchecked") + AbstractPooledDerivedByteBuf(Handle> recyclerHandle) { + super(0); + this.recyclerHandle = (Handle>) recyclerHandle; + } + + @Override + public final AbstractByteBuf unwrap() { + return buffer; + } + + final > U init( + AbstractByteBuf buffer, int readerIndex, int writerIndex, int maxCapacity) { + + this.buffer = buffer; + maxCapacity(maxCapacity); + + setIndex(readerIndex, writerIndex); + + @SuppressWarnings("unchecked") + final U castThis = (U) this; + + return castThis; + } + + @Override + public final int refCnt() { + final AbstractByteBuf unwrapped = unwrap(); + return unwrapped != null ? unwrapped.refCnt() : 0; + } + + @Override + public final ByteBuf retain() { + unwrap().retain(); + return this; + } + + @Override + public final ByteBuf retain(int increment) { + unwrap().retain(increment); + return this; + } + + @Override + public final ByteBuf touch() { + return this; + } + + @Override + public final ByteBuf touch(Object hint) { + return this; + } + + @Override + public final boolean release() { + final boolean deallocated = unwrap().release(); + if (deallocated) { + recycle(); + } + return deallocated; + } + + @Override + public final boolean release(int decrement) { + final boolean deallocated = unwrap().release(decrement); + if (deallocated) { + recycle(); + } + return deallocated; + } + + private void recycle() { + recyclerHandle.recycle(this); + } + + @Override + public final ByteBufAllocator alloc() { + return unwrap().alloc(); + } + + @Override + @Deprecated + public final ByteOrder order() { + return unwrap().order(); + } + + @Override + public final boolean isDirect() { + return unwrap().isDirect(); + } + + @Override + public boolean hasArray() { + return unwrap().hasArray(); + } + + @Override + public byte[] array() { + return unwrap().array(); + } + + @Override + public boolean hasMemoryAddress() { + return unwrap().hasMemoryAddress(); + } + + @Override + public final int nioBufferCount() { + return unwrap().nioBufferCount(); + } + + @Override + public final ByteBuffer internalNioBuffer(int index, int length) { + return nioBuffer(index, length); + } +} diff --git a/buffer/src/main/java/io/netty/buffer/AbstractReferenceCountedByteBuf.java b/buffer/src/main/java/io/netty/buffer/AbstractReferenceCountedByteBuf.java index ff9ef25a4fb..01548ad6b9f 100644 --- a/buffer/src/main/java/io/netty/buffer/AbstractReferenceCountedByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/AbstractReferenceCountedByteBuf.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 The Netty Project + * Copyright 2016 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 diff --git a/buffer/src/main/java/io/netty/buffer/AbstractUnsafeSwappedByteBuf.java b/buffer/src/main/java/io/netty/buffer/AbstractUnsafeSwappedByteBuf.java index 6d9667c7de4..72b9fa290b3 100644 --- a/buffer/src/main/java/io/netty/buffer/AbstractUnsafeSwappedByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/AbstractUnsafeSwappedByteBuf.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 The Netty Project + * Copyright 2016 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 @@ -24,6 +24,7 @@ /** * Special {@link SwappedByteBuf} for {@link ByteBuf}s that is using unsafe. */ +@Deprecated abstract class AbstractUnsafeSwappedByteBuf extends SwappedByteBuf { private final boolean nativeByteOrder; private final AbstractByteBuf wrapped; diff --git a/buffer/src/main/java/io/netty/buffer/AdvancedLeakAwareByteBuf.java b/buffer/src/main/java/io/netty/buffer/AdvancedLeakAwareByteBuf.java index 749f0ca0d06..3d713930457 100644 --- a/buffer/src/main/java/io/netty/buffer/AdvancedLeakAwareByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/AdvancedLeakAwareByteBuf.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 The Netty Project + * Copyright 2016 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 @@ -76,24 +76,48 @@ public ByteBuf slice() { return new AdvancedLeakAwareByteBuf(super.slice(), leak); } + @Override + public ByteBuf sliceRetained() { + recordLeakNonRefCountingOperation(leak); + return new AdvancedLeakAwareByteBuf(super.sliceRetained(), leak); + } + @Override public ByteBuf slice(int index, int length) { recordLeakNonRefCountingOperation(leak); return new AdvancedLeakAwareByteBuf(super.slice(index, length), leak); } + @Override + public ByteBuf sliceRetained(int index, int length) { + recordLeakNonRefCountingOperation(leak); + return new AdvancedLeakAwareByteBuf(super.sliceRetained(index, length), leak); + } + @Override public ByteBuf duplicate() { recordLeakNonRefCountingOperation(leak); return new AdvancedLeakAwareByteBuf(super.duplicate(), leak); } + @Override + public ByteBuf duplicateRetained() { + recordLeakNonRefCountingOperation(leak); + return new AdvancedLeakAwareByteBuf(super.duplicateRetained(), leak); + } + @Override public ByteBuf readSlice(int length) { recordLeakNonRefCountingOperation(leak); return new AdvancedLeakAwareByteBuf(super.readSlice(length), leak); } + @Override + public ByteBuf readSliceRetained(int length) { + recordLeakNonRefCountingOperation(leak); + return new AdvancedLeakAwareByteBuf(super.readSliceRetained(length), leak); + } + @Override public ByteBuf discardReadBytes() { recordLeakNonRefCountingOperation(leak); diff --git a/buffer/src/main/java/io/netty/buffer/AdvancedLeakAwareCompositeByteBuf.java b/buffer/src/main/java/io/netty/buffer/AdvancedLeakAwareCompositeByteBuf.java index 70777ae70f3..bd6f8f58149 100644 --- a/buffer/src/main/java/io/netty/buffer/AdvancedLeakAwareCompositeByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/AdvancedLeakAwareCompositeByteBuf.java @@ -13,6 +13,7 @@ * License for the specific language governing permissions and limitations * under the License. */ + package io.netty.buffer; @@ -58,24 +59,48 @@ public ByteBuf slice() { return new AdvancedLeakAwareByteBuf(super.slice(), leak); } + @Override + public ByteBuf sliceRetained() { + recordLeakNonRefCountingOperation(leak); + return new AdvancedLeakAwareByteBuf(super.sliceRetained(), leak); + } + @Override public ByteBuf slice(int index, int length) { recordLeakNonRefCountingOperation(leak); return new AdvancedLeakAwareByteBuf(super.slice(index, length), leak); } + @Override + public ByteBuf sliceRetained(int index, int length) { + recordLeakNonRefCountingOperation(leak); + return new AdvancedLeakAwareByteBuf(super.sliceRetained(index, length), leak); + } + @Override public ByteBuf duplicate() { recordLeakNonRefCountingOperation(leak); return new AdvancedLeakAwareByteBuf(super.duplicate(), leak); } + @Override + public ByteBuf duplicateRetained() { + recordLeakNonRefCountingOperation(leak); + return new AdvancedLeakAwareByteBuf(super.duplicateRetained(), leak); + } + @Override public ByteBuf readSlice(int length) { recordLeakNonRefCountingOperation(leak); return new AdvancedLeakAwareByteBuf(super.readSlice(length), leak); } + @Override + public ByteBuf readSliceRetained(int length) { + recordLeakNonRefCountingOperation(leak); + return new AdvancedLeakAwareByteBuf(super.readSliceRetained(length), leak); + } + @Override public CompositeByteBuf discardReadBytes() { recordLeakNonRefCountingOperation(leak); diff --git a/buffer/src/main/java/io/netty/buffer/ByteBuf.java b/buffer/src/main/java/io/netty/buffer/ByteBuf.java index 42c1962aedc..54558ae93f5 100644 --- a/buffer/src/main/java/io/netty/buffer/ByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/ByteBuf.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 The Netty Project + * Copyright 2016 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 @@ -194,8 +194,17 @@ * *

Derived buffers

* - * You can create a view of an existing buffer by calling either - * {@link #duplicate()}, {@link #slice()} or {@link #slice(int, int)}. + * You can create a view of an existing buffer using the following methods: + *
    + *
  • {@link #duplicate()}
  • + *
  • {@link #slice()}
  • + *
  • {@link #slice(int, int)}
  • + *
  • {@link #readSlice(int)}
  • + *
  • {@link #duplicateRetained()}
  • + *
  • {@link #sliceRetained()}
  • + *
  • {@link #sliceRetained(int, int)}
  • + *
  • {@link #readSliceRetained(int)}
  • + *
* A derived buffer will have an independent {@link #readerIndex() readerIndex}, * {@link #writerIndex() writerIndex} and marker indexes, while it shares * other internal data representation, just like a NIO buffer does. @@ -203,8 +212,11 @@ * In case a completely fresh copy of an existing buffer is required, please * call {@link #copy()} method instead. *

- * Also be aware that obtaining derived buffers will NOT call {@link #retain()} and so the - * reference count will NOT be increased. + * Note that the {@link #duplicate()}, {@link #slice()}, {@link #slice(int, int)} and {@link #readSlice(int)} does NOT + * call {@link #retain()} on the returned derived buffer, and thus its reference count will NOT be increased. If you + * need to create a derived buffer with increased reference count, consider using {@link #duplicateRetained()}, + * {@link #sliceRetained()}, {@link #sliceRetained(int, int)} and {@link #readSliceRetained(int)} which may yield + * better memory footprint than its non-retaining counterpart. * *

Conversion to existing JDK types

* @@ -267,6 +279,7 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { * @deprecated use the Little Endian accessors, e.g. {@code getShortLE}, {@code getIntLE} * instead of creating a buffer with swapped {@code endianness}. */ + @Deprecated public abstract ByteOrder order(); /** @@ -280,6 +293,7 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { * @deprecated use the Little Endian accessors, e.g. {@code getShortLE}, {@code getIntLE} * instead of creating a buffer with swapped {@code endianness}. */ + @Deprecated public abstract ByteBuf order(ByteOrder endianness); /** @@ -1455,6 +1469,20 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { */ public abstract ByteBuf readSlice(int length); + /** + * Returns a new retained slice of this buffer's sub-region starting at the current + * {@code readerIndex} and increases the {@code readerIndex} by the size + * of the new slice (= {@code length}). + * + * @param length the size of the new slice + * + * @return the newly created retained slice + * + * @throws IndexOutOfBoundsException + * if {@code length} is greater than {@code this.readableBytes} + */ + public abstract ByteBuf readSliceRetained(int length); + /** * Transfers this buffer's data to the specified destination starting at * the current {@code readerIndex} until the destination becomes @@ -1999,6 +2027,16 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { */ public abstract ByteBuf slice(); + /** + * Returns a retained slice of this buffer's readable bytes. Modifying the content + * of the returned buffer or this buffer affects each other's content + * while they maintain separate indexes and marks. This method is + * identical to {@code buf.sliceRetained(buf.readerIndex(), buf.readableBytes())}. + * This method does not modify {@code readerIndex} or {@code writerIndex} of + * this buffer. + */ + public abstract ByteBuf sliceRetained(); + /** * Returns a slice of this buffer's sub-region. Modifying the content of * the returned buffer or this buffer affects each other's content while @@ -2011,6 +2049,15 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { */ public abstract ByteBuf slice(int index, int length); + /** + * Returns a retained slice of this buffer's sub-region. Modifying the content of + * the returned buffer or this buffer affects each other's content while + * they maintain separate indexes and marks. + * This method does not modify {@code readerIndex} or {@code writerIndex} of + * this buffer. + */ + public abstract ByteBuf sliceRetained(int index, int length); + /** * Returns a buffer which shares the whole region of this buffer. * Modifying the content of the returned buffer or this buffer affects @@ -2024,6 +2071,16 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { */ public abstract ByteBuf duplicate(); + /** + * Returns a retained buffer which shares the whole region of this buffer. + * Modifying the content of the returned buffer or this buffer affects + * each other's content while they maintain separate indexes and marks. + * This method is identical to {@code buf.sliceRetained(0, buf.capacity())}. + * This method does not modify {@code readerIndex} or {@code writerIndex} of + * this buffer. + */ + public abstract ByteBuf duplicateRetained(); + /** * Returns the maximum number of NIO {@link ByteBuffer}s that consist this buffer. Note that {@link #nioBuffers()} * or {@link #nioBuffers(int, int)} might return a less number of {@link ByteBuffer}s. diff --git a/buffer/src/main/java/io/netty/buffer/ByteBufAllocator.java b/buffer/src/main/java/io/netty/buffer/ByteBufAllocator.java index c30d9620462..8280d430c85 100644 --- a/buffer/src/main/java/io/netty/buffer/ByteBufAllocator.java +++ b/buffer/src/main/java/io/netty/buffer/ByteBufAllocator.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 The Netty Project + * Copyright 2016 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 diff --git a/buffer/src/main/java/io/netty/buffer/ByteBufHolder.java b/buffer/src/main/java/io/netty/buffer/ByteBufHolder.java index c78cb6a4597..ddcb89e7e5c 100644 --- a/buffer/src/main/java/io/netty/buffer/ByteBufHolder.java +++ b/buffer/src/main/java/io/netty/buffer/ByteBufHolder.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 The Netty Project + * Copyright 2016 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 diff --git a/buffer/src/main/java/io/netty/buffer/ByteBufInputStream.java b/buffer/src/main/java/io/netty/buffer/ByteBufInputStream.java index 56496fadf45..25c38622734 100644 --- a/buffer/src/main/java/io/netty/buffer/ByteBufInputStream.java +++ b/buffer/src/main/java/io/netty/buffer/ByteBufInputStream.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 The Netty Project + * Copyright 2016 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 diff --git a/buffer/src/main/java/io/netty/buffer/ByteBufOutputStream.java b/buffer/src/main/java/io/netty/buffer/ByteBufOutputStream.java index 489472591a5..c5935bdf252 100644 --- a/buffer/src/main/java/io/netty/buffer/ByteBufOutputStream.java +++ b/buffer/src/main/java/io/netty/buffer/ByteBufOutputStream.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 The Netty Project + * Copyright 2016 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 diff --git a/buffer/src/main/java/io/netty/buffer/ByteBufProcessor.java b/buffer/src/main/java/io/netty/buffer/ByteBufProcessor.java index e70d67b3bd0..2b8fe7a7fc6 100644 --- a/buffer/src/main/java/io/netty/buffer/ByteBufProcessor.java +++ b/buffer/src/main/java/io/netty/buffer/ByteBufProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 The Netty Project + * Copyright 2016 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 diff --git a/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java b/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java index 3e231285a00..53072147a2d 100644 --- a/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java +++ b/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 The Netty Project + * Copyright 2016 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 @@ -730,6 +730,65 @@ public static void appendPrettyHexDump(StringBuilder dump, ByteBuf buf, int offs HexUtil.appendPrettyHexDump(dump, buf, offset, length); } + /** + * Creates a duplicate of the specified {@link ByteBuf}. + */ + public static ByteBuf duplicate(ByteBuf buf) { + final boolean readOnly = buf instanceof PooledReadOnlyByteBuf || buf instanceof ReadOnlyByteBuf; + final int readerIndex = buf.readerIndex(); + final int writerIndex = buf.writerIndex(); + + if (readOnly) { + buf = buf.unwrap(); + } + + final AbstractByteBuf duplicate; + if (buf instanceof PooledSlicedByteBuf || buf instanceof SlicedAbstractByteBuf) { + duplicate = PooledSlicedByteBuf.newInstance((AbstractByteBuf) buf, 0, buf.capacity()); + duplicate.setIndex(readerIndex, writerIndex); + duplicate.markReaderIndex(); + duplicate.markWriterIndex(); + } else if (buf instanceof SlicedByteBuf) { + duplicate = new SlicedByteBuf(buf, 0, buf.capacity()); + duplicate.setIndex(readerIndex, writerIndex); + duplicate.markReaderIndex(); + duplicate.markWriterIndex(); + } else if (buf instanceof AbstractByteBuf) { + duplicate = PooledDuplicatedByteBuf.newInstance((AbstractByteBuf) buf, readerIndex, writerIndex); + } else { + duplicate = new DuplicatedByteBuf(buf, readerIndex, writerIndex); + } + + return readOnly ? PooledReadOnlyByteBuf.newInstance(duplicate) : duplicate; + } + + /** + * Creates a slice of the specified {@link ByteBuf}. + */ + public static ByteBuf slice(ByteBuf buf) { + final int index = buf.readerIndex(); + return slice(buf, index, buf.writerIndex() - index); + } + + /** + * Creates a slice of the specified {@link ByteBuf}. + */ + public static ByteBuf slice(ByteBuf buf, int index, int length) { + final boolean readOnly = buf instanceof PooledReadOnlyByteBuf || buf instanceof ReadOnlyByteBuf; + if (readOnly) { + buf = buf.unwrap(); + } + + final AbstractByteBuf slice; + if (buf instanceof AbstractByteBuf) { + slice = PooledSlicedByteBuf.newInstance((AbstractByteBuf) buf, index, length); + } else { + slice = new SlicedByteBuf(buf, index, length); + } + + return readOnly ? PooledReadOnlyByteBuf.newInstance(slice) : slice; + } + /* Separate class so that the expensive static initialization is only done when needed */ private static final class HexUtil { diff --git a/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java b/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java index 42e4f301374..533ba8be08a 100644 --- a/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 The Netty Project + * Copyright 2016 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 diff --git a/buffer/src/main/java/io/netty/buffer/DefaultByteBufHolder.java b/buffer/src/main/java/io/netty/buffer/DefaultByteBufHolder.java index ef39045dcdd..217986fbc38 100644 --- a/buffer/src/main/java/io/netty/buffer/DefaultByteBufHolder.java +++ b/buffer/src/main/java/io/netty/buffer/DefaultByteBufHolder.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 The Netty Project + * Copyright 2016 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 diff --git a/buffer/src/main/java/io/netty/buffer/DuplicatedAbstractByteBuf.java b/buffer/src/main/java/io/netty/buffer/DuplicatedAbstractByteBuf.java index d49e960d5da..e30d63dc928 100644 --- a/buffer/src/main/java/io/netty/buffer/DuplicatedAbstractByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/DuplicatedAbstractByteBuf.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 The Netty Project + * Copyright 2016 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 @@ -18,63 +18,107 @@ /** * {@link DuplicatedByteBuf} implementation that can do optimizations because it knows the duplicated buffer * is of type {@link AbstractByteBuf}. + * + * @deprecated Do not use. */ +@Deprecated final class DuplicatedAbstractByteBuf extends DuplicatedByteBuf { - public DuplicatedAbstractByteBuf(AbstractByteBuf buffer) { + DuplicatedAbstractByteBuf(AbstractByteBuf buffer) { super(buffer); } + @Override + public AbstractByteBuf unwrap() { + return (AbstractByteBuf) super.unwrap(); + } + @Override protected byte _getByte(int index) { - return unwrap0()._getByte(index); + return unwrap()._getByte(index); } @Override protected short _getShort(int index) { - return unwrap0()._getShort(index); + return unwrap()._getShort(index); + } + + @Override + protected short _getShortLE(int index) { + return unwrap()._getShortLE(index); } @Override protected int _getUnsignedMedium(int index) { - return unwrap0()._getUnsignedMedium(index); + return unwrap()._getUnsignedMedium(index); + } + + @Override + protected int _getUnsignedMediumLE(int index) { + return unwrap()._getUnsignedMediumLE(index); } @Override protected int _getInt(int index) { - return unwrap0()._getInt(index); + return unwrap()._getInt(index); + } + + @Override + protected int _getIntLE(int index) { + return unwrap()._getIntLE(index); } @Override protected long _getLong(int index) { - return unwrap0()._getLong(index); + return unwrap()._getLong(index); + } + + @Override + protected long _getLongLE(int index) { + return unwrap()._getLongLE(index); } @Override protected void _setByte(int index, int value) { - unwrap0()._setByte(index, value); + unwrap()._setByte(index, value); } @Override protected void _setShort(int index, int value) { - unwrap0()._setShort(index, value); + unwrap()._setShort(index, value); + } + + @Override + protected void _setShortLE(int index, int value) { + unwrap()._setShortLE(index, value); } @Override protected void _setMedium(int index, int value) { - unwrap0()._setMedium(index, value); + unwrap()._setMedium(index, value); + } + + @Override + protected void _setMediumLE(int index, int value) { + unwrap()._setMediumLE(index, value); } @Override protected void _setInt(int index, int value) { - unwrap0()._setInt(index, value); + unwrap()._setInt(index, value); + } + + @Override + protected void _setIntLE(int index, int value) { + unwrap()._setIntLE(index, value); } @Override protected void _setLong(int index, long value) { - unwrap0()._setLong(index, value); + unwrap()._setLong(index, value); } - private AbstractByteBuf unwrap0() { - return (AbstractByteBuf) unwrap(); + @Override + protected void _setLongLE(int index, long value) { + unwrap()._setLongLE(index, value); } } diff --git a/buffer/src/main/java/io/netty/buffer/DuplicatedByteBuf.java b/buffer/src/main/java/io/netty/buffer/DuplicatedByteBuf.java index 3ebe1f68eb5..f0dcc13cc30 100644 --- a/buffer/src/main/java/io/netty/buffer/DuplicatedByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/DuplicatedByteBuf.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 The Netty Project + * Copyright 2016 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 @@ -31,21 +31,28 @@ * A derived buffer which simply forwards all data access requests to its * parent. It is recommended to use {@link ByteBuf#duplicate()} instead * of calling the constructor explicitly. + * + * @deprecated Do not use. */ +@Deprecated public class DuplicatedByteBuf extends AbstractDerivedByteBuf { private final ByteBuf buffer; public DuplicatedByteBuf(ByteBuf buffer) { + this(buffer, buffer.readerIndex(), buffer.writerIndex()); + } + + DuplicatedByteBuf(ByteBuf buffer, int readerIndex, int writerIndex) { super(buffer.maxCapacity()); if (buffer instanceof DuplicatedByteBuf) { - this.buffer = ((DuplicatedByteBuf) buffer).buffer; + this.buffer = buffer.unwrap(); } else { this.buffer = buffer; } - setIndex(buffer.readerIndex(), buffer.writerIndex()); + setIndex(readerIndex, writerIndex); markReaderIndex(); markWriterIndex(); } @@ -61,6 +68,7 @@ public ByteBufAllocator alloc() { } @Override + @Deprecated public ByteOrder order() { return buffer.order(); } @@ -126,6 +134,11 @@ protected short _getShort(int index) { return buffer.getShort(index); } + @Override + public short getShortLE(int index) { + return buffer.getShortLE(index); + } + @Override protected short _getShortLE(int index) { return buffer.getShortLE(index); @@ -141,6 +154,11 @@ protected int _getUnsignedMedium(int index) { return buffer.getUnsignedMedium(index); } + @Override + public int getUnsignedMediumLE(int index) { + return buffer.getUnsignedMediumLE(index); + } + @Override protected int _getUnsignedMediumLE(int index) { return buffer.getUnsignedMediumLE(index); @@ -156,6 +174,11 @@ protected int _getInt(int index) { return buffer.getInt(index); } + @Override + public int getIntLE(int index) { + return buffer.getIntLE(index); + } + @Override protected int _getIntLE(int index) { return buffer.getIntLE(index); @@ -171,6 +194,11 @@ protected long _getLong(int index) { return buffer.getLong(index); } + @Override + public long getLongLE(int index) { + return buffer.getLongLE(index); + } + @Override protected long _getLongLE(int index) { return buffer.getLongLE(index); @@ -186,6 +214,11 @@ public ByteBuf slice(int index, int length) { return buffer.slice(index, length); } + @Override + public ByteBuf sliceRetained(int index, int length) { + return buffer.sliceRetained(index, length); + } + @Override public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { buffer.getBytes(index, dst, dstIndex, length); @@ -226,6 +259,12 @@ protected void _setShort(int index, int value) { buffer.setShort(index, value); } + @Override + public ByteBuf setShortLE(int index, int value) { + buffer.setShortLE(index, value); + return this; + } + @Override protected void _setShortLE(int index, int value) { buffer.setShortLE(index, value); @@ -242,6 +281,12 @@ protected void _setMedium(int index, int value) { buffer.setMedium(index, value); } + @Override + public ByteBuf setMediumLE(int index, int value) { + buffer.setMediumLE(index, value); + return this; + } + @Override protected void _setMediumLE(int index, int value) { buffer.setMediumLE(index, value); @@ -258,6 +303,12 @@ protected void _setInt(int index, int value) { buffer.setInt(index, value); } + @Override + public ByteBuf setIntLE(int index, int value) { + buffer.setIntLE(index, value); + return this; + } + @Override protected void _setIntLE(int index, int value) { buffer.setIntLE(index, value); @@ -274,6 +325,12 @@ protected void _setLong(int index, long value) { buffer.setLong(index, value); } + @Override + public ByteBuf setLongLE(int index, long value) { + buffer.setLongLE(index, value); + return this; + } + @Override protected void _setLongLE(int index, long value) { buffer.setLongLE(index, value); @@ -344,11 +401,6 @@ public ByteBuffer[] nioBuffers(int index, int length) { return buffer.nioBuffers(index, length); } - @Override - public ByteBuffer internalNioBuffer(int index, int length) { - return nioBuffer(index, length); - } - @Override public int forEachByte(int index, int length, ByteProcessor processor) { return buffer.forEachByte(index, length, processor); diff --git a/buffer/src/main/java/io/netty/buffer/EmptyByteBuf.java b/buffer/src/main/java/io/netty/buffer/EmptyByteBuf.java index ca67db07f57..da6ae998efc 100644 --- a/buffer/src/main/java/io/netty/buffer/EmptyByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/EmptyByteBuf.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 The Netty Project + * Copyright 2016 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 @@ -609,6 +609,11 @@ public ByteBuf readSlice(int length) { return checkLength(length); } + @Override + public ByteBuf readSliceRetained(int length) { + return checkLength(length); + } + @Override public ByteBuf readBytes(ByteBuf dst) { return checkLength(dst.writableBytes()); @@ -840,16 +845,31 @@ public ByteBuf slice() { return this; } + @Override + public ByteBuf sliceRetained() { + return this; + } + @Override public ByteBuf slice(int index, int length) { return checkIndex(index, length); } + @Override + public ByteBuf sliceRetained(int index, int length) { + return checkIndex(index, length); + } + @Override public ByteBuf duplicate() { return this; } + @Override + public ByteBuf duplicateRetained() { + return this; + } + @Override public int nioBufferCount() { return 1; diff --git a/buffer/src/main/java/io/netty/buffer/FixedCompositeByteBuf.java b/buffer/src/main/java/io/netty/buffer/FixedCompositeByteBuf.java index 268f75cb369..87bdc01e084 100644 --- a/buffer/src/main/java/io/netty/buffer/FixedCompositeByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/FixedCompositeByteBuf.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 The Netty Project + * Copyright 2016 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 diff --git a/buffer/src/main/java/io/netty/buffer/HeapByteBufUtil.java b/buffer/src/main/java/io/netty/buffer/HeapByteBufUtil.java index abb93cda74e..62582a63364 100644 --- a/buffer/src/main/java/io/netty/buffer/HeapByteBufUtil.java +++ b/buffer/src/main/java/io/netty/buffer/HeapByteBufUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 The Netty Project + * Copyright 2016 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 diff --git a/buffer/src/main/java/io/netty/buffer/PoolArena.java b/buffer/src/main/java/io/netty/buffer/PoolArena.java index e5d799d4865..f6bc7992c44 100644 --- a/buffer/src/main/java/io/netty/buffer/PoolArena.java +++ b/buffer/src/main/java/io/netty/buffer/PoolArena.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 The Netty Project + * Copyright 2016 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 diff --git a/buffer/src/main/java/io/netty/buffer/PoolArenaMetric.java b/buffer/src/main/java/io/netty/buffer/PoolArenaMetric.java index d3281f3bee4..71494e1b115 100644 --- a/buffer/src/main/java/io/netty/buffer/PoolArenaMetric.java +++ b/buffer/src/main/java/io/netty/buffer/PoolArenaMetric.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 The Netty Project + * Copyright 2016 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 diff --git a/buffer/src/main/java/io/netty/buffer/PoolChunk.java b/buffer/src/main/java/io/netty/buffer/PoolChunk.java index 18dfd77078f..5d375dff92d 100644 --- a/buffer/src/main/java/io/netty/buffer/PoolChunk.java +++ b/buffer/src/main/java/io/netty/buffer/PoolChunk.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 The Netty Project + * Copyright 2016 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 diff --git a/buffer/src/main/java/io/netty/buffer/PoolChunkList.java b/buffer/src/main/java/io/netty/buffer/PoolChunkList.java index 9573dedddc2..618520440f5 100644 --- a/buffer/src/main/java/io/netty/buffer/PoolChunkList.java +++ b/buffer/src/main/java/io/netty/buffer/PoolChunkList.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 The Netty Project + * Copyright 2016 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 diff --git a/buffer/src/main/java/io/netty/buffer/PoolChunkListMetric.java b/buffer/src/main/java/io/netty/buffer/PoolChunkListMetric.java index 471f0de8780..92158b3c81a 100644 --- a/buffer/src/main/java/io/netty/buffer/PoolChunkListMetric.java +++ b/buffer/src/main/java/io/netty/buffer/PoolChunkListMetric.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 The Netty Project + * Copyright 2016 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 diff --git a/buffer/src/main/java/io/netty/buffer/PoolChunkMetric.java b/buffer/src/main/java/io/netty/buffer/PoolChunkMetric.java index b08ad06f0e6..fb23540e464 100644 --- a/buffer/src/main/java/io/netty/buffer/PoolChunkMetric.java +++ b/buffer/src/main/java/io/netty/buffer/PoolChunkMetric.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 The Netty Project + * Copyright 2016 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 diff --git a/buffer/src/main/java/io/netty/buffer/PoolSubpage.java b/buffer/src/main/java/io/netty/buffer/PoolSubpage.java index 0f7cbd0e48d..52fe9c140d7 100644 --- a/buffer/src/main/java/io/netty/buffer/PoolSubpage.java +++ b/buffer/src/main/java/io/netty/buffer/PoolSubpage.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 The Netty Project + * Copyright 2016 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 diff --git a/buffer/src/main/java/io/netty/buffer/PoolSubpageMetric.java b/buffer/src/main/java/io/netty/buffer/PoolSubpageMetric.java index d6747674622..01be3a5980b 100644 --- a/buffer/src/main/java/io/netty/buffer/PoolSubpageMetric.java +++ b/buffer/src/main/java/io/netty/buffer/PoolSubpageMetric.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 The Netty Project + * Copyright 2016 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 diff --git a/buffer/src/main/java/io/netty/buffer/PoolThreadCache.java b/buffer/src/main/java/io/netty/buffer/PoolThreadCache.java index 68953a1a4e8..1ce35308e8f 100644 --- a/buffer/src/main/java/io/netty/buffer/PoolThreadCache.java +++ b/buffer/src/main/java/io/netty/buffer/PoolThreadCache.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 The Netty Project + * Copyright 2016 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 diff --git a/buffer/src/main/java/io/netty/buffer/PooledByteBuf.java b/buffer/src/main/java/io/netty/buffer/PooledByteBuf.java index 6b348e4a358..6a2d48758ee 100644 --- a/buffer/src/main/java/io/netty/buffer/PooledByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/PooledByteBuf.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 The Netty Project + * Copyright 2016 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 diff --git a/buffer/src/main/java/io/netty/buffer/PooledByteBufAllocator.java b/buffer/src/main/java/io/netty/buffer/PooledByteBufAllocator.java index 7869e7fab5d..cc422736b5a 100644 --- a/buffer/src/main/java/io/netty/buffer/PooledByteBufAllocator.java +++ b/buffer/src/main/java/io/netty/buffer/PooledByteBufAllocator.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 The Netty Project + * Copyright 2016 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 diff --git a/buffer/src/main/java/io/netty/buffer/PooledDirectByteBuf.java b/buffer/src/main/java/io/netty/buffer/PooledDirectByteBuf.java index b301e483c38..83a80213821 100644 --- a/buffer/src/main/java/io/netty/buffer/PooledDirectByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/PooledDirectByteBuf.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 The Netty Project + * Copyright 2016 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 diff --git a/buffer/src/main/java/io/netty/buffer/PooledDuplicatedByteBuf.java b/buffer/src/main/java/io/netty/buffer/PooledDuplicatedByteBuf.java new file mode 100644 index 00000000000..751c1a58c6f --- /dev/null +++ b/buffer/src/main/java/io/netty/buffer/PooledDuplicatedByteBuf.java @@ -0,0 +1,379 @@ +/* + * Copyright 2016 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.buffer; + +import io.netty.util.ByteProcessor; +import io.netty.util.Recycler; +import io.netty.util.Recycler.Handle; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.channels.GatheringByteChannel; +import java.nio.channels.ScatteringByteChannel; + +final class PooledDuplicatedByteBuf extends AbstractPooledDerivedByteBuf { + + private static final Recycler RECYCLER = new Recycler() { + @Override + protected PooledDuplicatedByteBuf newObject(Handle handle) { + return new PooledDuplicatedByteBuf(handle); + } + }; + + static PooledDuplicatedByteBuf newInstance(AbstractByteBuf buffer, int readerIndex, int writerIndex) { + final AbstractByteBuf unwrapped; + if (buffer instanceof DuplicatedAbstractByteBuf || + buffer instanceof PooledDuplicatedByteBuf) { + + unwrapped = (AbstractByteBuf) buffer.unwrap(); + } else { + unwrapped = buffer; + } + + final PooledDuplicatedByteBuf duplicate = RECYCLER.get(); + duplicate.init(unwrapped, readerIndex, writerIndex, buffer.maxCapacity()); + duplicate.markReaderIndex(); + duplicate.markWriterIndex(); + + return duplicate; + } + + private PooledDuplicatedByteBuf(Handle handle) { + super(handle); + } + + @Override + public int capacity() { + return unwrap().capacity(); + } + + @Override + public ByteBuf capacity(int newCapacity) { + unwrap().capacity(newCapacity); + return this; + } + + @Override + public int arrayOffset() { + return unwrap().arrayOffset(); + } + + @Override + public long memoryAddress() { + return unwrap().memoryAddress(); + } + + @Override + public ByteBuffer nioBuffer(int index, int length) { + return unwrap().nioBuffer(index, length); + } + + @Override + public ByteBuffer[] nioBuffers(int index, int length) { + return unwrap().nioBuffers(index, length); + } + + @Override + public ByteBuf copy(int index, int length) { + return unwrap().copy(index, length); + } + + @Override + public ByteBuf slice(int index, int length) { + return unwrap().slice(index, length); + } + + @Override + public ByteBuf sliceRetained(int index, int length) { + return unwrap().sliceRetained(index, length); + } + + @Override + public byte getByte(int index) { + return unwrap().getByte(index); + } + + @Override + protected byte _getByte(int index) { + return unwrap()._getByte(index); + } + + @Override + public short getShort(int index) { + return unwrap().getShort(index); + } + + @Override + protected short _getShort(int index) { + return unwrap()._getShort(index); + } + + @Override + public short getShortLE(int index) { + return unwrap().getShortLE(index); + } + + @Override + protected short _getShortLE(int index) { + return unwrap()._getShortLE(index); + } + + @Override + public int getUnsignedMedium(int index) { + return unwrap().getUnsignedMedium(index); + } + + @Override + protected int _getUnsignedMedium(int index) { + return unwrap()._getUnsignedMedium(index); + } + + @Override + public int getUnsignedMediumLE(int index) { + return unwrap().getUnsignedMediumLE(index); + } + + @Override + protected int _getUnsignedMediumLE(int index) { + return unwrap()._getUnsignedMediumLE(index); + } + + @Override + public int getInt(int index) { + return unwrap().getInt(index); + } + + @Override + protected int _getInt(int index) { + return unwrap()._getInt(index); + } + + @Override + public int getIntLE(int index) { + return unwrap().getIntLE(index); + } + + @Override + protected int _getIntLE(int index) { + return unwrap()._getIntLE(index); + } + + @Override + public long getLong(int index) { + return unwrap().getLong(index); + } + + @Override + protected long _getLong(int index) { + return unwrap()._getLong(index); + } + + @Override + public long getLongLE(int index) { + return unwrap().getLongLE(index); + } + + @Override + protected long _getLongLE(int index) { + return unwrap()._getLongLE(index); + } + + @Override + public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { + unwrap().getBytes(index, dst, dstIndex, length); + return this; + } + + @Override + public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) { + unwrap().getBytes(index, dst, dstIndex, length); + return this; + } + + @Override + public ByteBuf getBytes(int index, ByteBuffer dst) { + unwrap().getBytes(index, dst); + return this; + } + + @Override + public ByteBuf setByte(int index, int value) { + unwrap().setByte(index, value); + return this; + } + + @Override + protected void _setByte(int index, int value) { + unwrap()._setByte(index, value); + } + + @Override + public ByteBuf setShort(int index, int value) { + unwrap().setShort(index, value); + return this; + } + + @Override + protected void _setShort(int index, int value) { + unwrap()._setShort(index, value); + } + + @Override + public ByteBuf setShortLE(int index, int value) { + unwrap().setShortLE(index, value); + return this; + } + + @Override + protected void _setShortLE(int index, int value) { + unwrap()._setShortLE(index, value); + } + + @Override + public ByteBuf setMedium(int index, int value) { + unwrap().setMedium(index, value); + return this; + } + + @Override + protected void _setMedium(int index, int value) { + unwrap()._setMedium(index, value); + } + + @Override + public ByteBuf setMediumLE(int index, int value) { + unwrap().setMediumLE(index, value); + return this; + } + + @Override + protected void _setMediumLE(int index, int value) { + unwrap()._setMediumLE(index, value); + } + + @Override + public ByteBuf setInt(int index, int value) { + unwrap().setInt(index, value); + return this; + } + + @Override + protected void _setInt(int index, int value) { + unwrap()._setInt(index, value); + } + + @Override + public ByteBuf setIntLE(int index, int value) { + unwrap().setIntLE(index, value); + return this; + } + + @Override + protected void _setIntLE(int index, int value) { + unwrap()._setIntLE(index, value); + } + + @Override + public ByteBuf setLong(int index, long value) { + unwrap().setLong(index, value); + return this; + } + + @Override + protected void _setLong(int index, long value) { + unwrap()._setLong(index, value); + } + + @Override + public ByteBuf setLongLE(int index, long value) { + unwrap().setLongLE(index, value); + return this; + } + + @Override + protected void _setLongLE(int index, long value) { + unwrap().setLongLE(index, value); + } + + @Override + public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) { + unwrap().setBytes(index, src, srcIndex, length); + return this; + } + + @Override + public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) { + unwrap().setBytes(index, src, srcIndex, length); + return this; + } + + @Override + public ByteBuf setBytes(int index, ByteBuffer src) { + unwrap().setBytes(index, src); + return this; + } + + @Override + public ByteBuf getBytes(int index, OutputStream out, int length) + throws IOException { + unwrap().getBytes(index, out, length); + return this; + } + + @Override + public int getBytes(int index, GatheringByteChannel out, int length) + throws IOException { + return unwrap().getBytes(index, out, length); + } + + @Override + public int getBytes(int index, FileChannel out, long position, int length) + throws IOException { + return unwrap().getBytes(index, out, position, length); + } + + @Override + public int setBytes(int index, InputStream in, int length) + throws IOException { + return unwrap().setBytes(index, in, length); + } + + @Override + public int setBytes(int index, ScatteringByteChannel in, int length) + throws IOException { + return unwrap().setBytes(index, in, length); + } + + @Override + public int setBytes(int index, FileChannel in, long position, int length) + throws IOException { + return unwrap().setBytes(index, in, position, length); + } + + @Override + public int forEachByte(int index, int length, ByteProcessor processor) { + return unwrap().forEachByte(index, length, processor); + } + + @Override + public int forEachByteDesc(int index, int length, ByteProcessor processor) { + return unwrap().forEachByteDesc(index, length, processor); + } +} diff --git a/buffer/src/main/java/io/netty/buffer/PooledHeapByteBuf.java b/buffer/src/main/java/io/netty/buffer/PooledHeapByteBuf.java index 0ea9598530d..d9c326736f4 100644 --- a/buffer/src/main/java/io/netty/buffer/PooledHeapByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/PooledHeapByteBuf.java @@ -1,7 +1,9 @@ /* - * Copyright 2012 The Netty Project + * Copyright 2016 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 * diff --git a/buffer/src/main/java/io/netty/buffer/PooledReadOnlyByteBuf.java b/buffer/src/main/java/io/netty/buffer/PooledReadOnlyByteBuf.java new file mode 100644 index 00000000000..4d92a4c8fcc --- /dev/null +++ b/buffer/src/main/java/io/netty/buffer/PooledReadOnlyByteBuf.java @@ -0,0 +1,385 @@ +/* + * Copyright 2016 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.buffer; + +import io.netty.util.ByteProcessor; +import io.netty.util.Recycler; +import io.netty.util.Recycler.Handle; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.ByteBuffer; +import java.nio.ReadOnlyBufferException; +import java.nio.channels.FileChannel; +import java.nio.channels.GatheringByteChannel; +import java.nio.channels.ScatteringByteChannel; + +final class PooledReadOnlyByteBuf extends AbstractPooledDerivedByteBuf { + + private static final Recycler RECYCLER = new Recycler() { + @Override + protected PooledReadOnlyByteBuf newObject(Handle handle) { + return new PooledReadOnlyByteBuf(handle); + } + }; + + static PooledReadOnlyByteBuf newInstance(AbstractByteBuf buffer) { + final AbstractByteBuf unwrapped; + if (buffer instanceof PooledReadOnlyByteBuf || + buffer instanceof PooledDuplicatedByteBuf || buffer instanceof DuplicatedAbstractByteBuf) { + unwrapped = (AbstractByteBuf) buffer.unwrap(); + } else { + unwrapped = buffer; + } + + final PooledReadOnlyByteBuf readOnly = RECYCLER.get(); + readOnly.init(unwrapped, buffer.readerIndex(), buffer.writerIndex(), buffer.maxCapacity()); + readOnly.markReaderIndex(); + readOnly.markWriterIndex(); + + return readOnly; + } + + private PooledReadOnlyByteBuf(Handle handle) { + super(handle); + } + + @Override + public boolean isWritable() { + return false; + } + + @Override + public boolean isWritable(int numBytes) { + return false; + } + + @Override + public int capacity() { + return unwrap().capacity(); + } + + @Override + public ByteBuf capacity(int newCapacity) { + unwrap().capacity(newCapacity); + return this; + } + + @Override + public boolean hasArray() { + return false; + } + + @Override + public byte[] array() { + throw new ReadOnlyBufferException(); + } + + @Override + public int arrayOffset() { + throw new ReadOnlyBufferException(); + } + + @Override + public boolean hasMemoryAddress() { + return false; + } + + @Override + public long memoryAddress() { + throw new ReadOnlyBufferException(); + } + + @Override + public ByteBuffer nioBuffer(int index, int length) { + return unwrap().nioBuffer(index, length).asReadOnlyBuffer(); + } + + @Override + public ByteBuffer[] nioBuffers(int index, int length) { + final ByteBuffer[] nioBuffers = unwrap().nioBuffers(index, length); + for (int i = 0; i < nioBuffers.length; i++) { + nioBuffers[i] = nioBuffers[i].asReadOnlyBuffer(); + } + return nioBuffers; + } + + @Override + public ByteBuf discardReadBytes() { + throw new ReadOnlyBufferException(); + } + + @Override + public ByteBuf copy(int index, int length) { + return unwrap().copy(index, length); + } + + @Override + public byte getByte(int index) { + return unwrap().getByte(index); + } + + @Override + protected byte _getByte(int index) { + return unwrap()._getByte(index); + } + + @Override + public short getShort(int index) { + return unwrap().getShort(index); + } + + @Override + protected short _getShort(int index) { + return unwrap()._getShort(index); + } + + @Override + public short getShortLE(int index) { + return unwrap().getShortLE(index); + } + + @Override + protected short _getShortLE(int index) { + return unwrap()._getShortLE(index); + } + + @Override + public int getUnsignedMedium(int index) { + return unwrap().getUnsignedMedium(index); + } + + @Override + protected int _getUnsignedMedium(int index) { + return unwrap()._getUnsignedMedium(index); + } + + @Override + public int getUnsignedMediumLE(int index) { + return unwrap().getUnsignedMediumLE(index); + } + + @Override + protected int _getUnsignedMediumLE(int index) { + return unwrap()._getUnsignedMediumLE(index); + } + + @Override + public int getInt(int index) { + return unwrap().getInt(index); + } + + @Override + protected int _getInt(int index) { + return unwrap()._getInt(index); + } + + @Override + public int getIntLE(int index) { + return unwrap().getIntLE(index); + } + + @Override + protected int _getIntLE(int index) { + return unwrap()._getIntLE(index); + } + + @Override + public long getLong(int index) { + return unwrap().getLong(index); + } + + @Override + protected long _getLong(int index) { + return unwrap()._getLong(index); + } + + @Override + public long getLongLE(int index) { + return unwrap().getLongLE(index); + } + + @Override + protected long _getLongLE(int index) { + return unwrap()._getLongLE(index); + } + + @Override + public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { + unwrap().getBytes(index, dst, dstIndex, length); + return this; + } + + @Override + public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) { + unwrap().getBytes(index, dst, dstIndex, length); + return this; + } + + @Override + public ByteBuf getBytes(int index, ByteBuffer dst) { + unwrap().getBytes(index, dst); + return this; + } + + @Override + public ByteBuf setByte(int index, int value) { + throw new ReadOnlyBufferException(); + } + + @Override + protected void _setByte(int index, int value) { + throw new ReadOnlyBufferException(); + } + + @Override + public ByteBuf setShort(int index, int value) { + throw new ReadOnlyBufferException(); + } + + @Override + protected void _setShort(int index, int value) { + throw new ReadOnlyBufferException(); + } + + @Override + public ByteBuf setShortLE(int index, int value) { + throw new ReadOnlyBufferException(); + } + + @Override + protected void _setShortLE(int index, int value) { + throw new ReadOnlyBufferException(); + } + + @Override + public ByteBuf setMedium(int index, int value) { + throw new ReadOnlyBufferException(); + } + + @Override + protected void _setMedium(int index, int value) { + throw new ReadOnlyBufferException(); + } + + @Override + public ByteBuf setMediumLE(int index, int value) { + throw new ReadOnlyBufferException(); + } + + @Override + protected void _setMediumLE(int index, int value) { + throw new ReadOnlyBufferException(); + } + + @Override + public ByteBuf setInt(int index, int value) { + throw new ReadOnlyBufferException(); + } + + @Override + protected void _setInt(int index, int value) { + throw new ReadOnlyBufferException(); + } + + @Override + public ByteBuf setIntLE(int index, int value) { + throw new ReadOnlyBufferException(); + } + + @Override + protected void _setIntLE(int index, int value) { + throw new ReadOnlyBufferException(); + } + + @Override + public ByteBuf setLong(int index, long value) { + throw new ReadOnlyBufferException(); + } + + @Override + protected void _setLong(int index, long value) { + throw new ReadOnlyBufferException(); + } + + @Override + public ByteBuf setLongLE(int index, long value) { + throw new ReadOnlyBufferException(); + } + + @Override + protected void _setLongLE(int index, long value) { + throw new ReadOnlyBufferException(); + } + + @Override + public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) { + throw new ReadOnlyBufferException(); + } + + @Override + public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) { + throw new ReadOnlyBufferException(); + } + + @Override + public ByteBuf setBytes(int index, ByteBuffer src) { + throw new ReadOnlyBufferException(); + } + + @Override + public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException { + unwrap().getBytes(index, out, length); + return this; + } + + @Override + public int getBytes(int index, GatheringByteChannel out, int length) throws IOException { + return unwrap().getBytes(index, out, length); + } + + @Override + public int getBytes(int index, FileChannel out, long position, int length) throws IOException { + return unwrap().getBytes(index, out, position, length); + } + + @Override + public int setBytes(int index, InputStream in, int length) { + throw new ReadOnlyBufferException(); + } + + @Override + public int setBytes(int index, ScatteringByteChannel in, int length) { + throw new ReadOnlyBufferException(); + } + + @Override + public int setBytes(int index, FileChannel in, long position, int length) { + throw new ReadOnlyBufferException(); + } + + @Override + public int forEachByte(int index, int length, ByteProcessor processor) { + return unwrap().forEachByte(index, length, processor); + } + + @Override + public int forEachByteDesc(int index, int length, ByteProcessor processor) { + return unwrap().forEachByteDesc(index, length, processor); + } +} diff --git a/buffer/src/main/java/io/netty/buffer/PooledSlicedByteBuf.java b/buffer/src/main/java/io/netty/buffer/PooledSlicedByteBuf.java new file mode 100644 index 00000000000..6e1496f20f5 --- /dev/null +++ b/buffer/src/main/java/io/netty/buffer/PooledSlicedByteBuf.java @@ -0,0 +1,449 @@ +/* + * Copyright 2016 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.buffer; + +import io.netty.util.ByteProcessor; +import io.netty.util.Recycler; +import io.netty.util.Recycler.Handle; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.channels.GatheringByteChannel; +import java.nio.channels.ScatteringByteChannel; + +final class PooledSlicedByteBuf extends AbstractPooledDerivedByteBuf { + + private static final Recycler RECYCLER = new Recycler() { + @Override + protected PooledSlicedByteBuf newObject(Handle handle) { + return new PooledSlicedByteBuf(handle); + } + }; + + static PooledSlicedByteBuf newInstance(AbstractByteBuf buffer, int index, int length) { + if (index < 0 || index > buffer.capacity() - length) { + throw new IndexOutOfBoundsException(buffer + ".slice(" + index + ", " + length + ')'); + } + + final AbstractByteBuf unwrapped; + final int adjustment; + if (buffer instanceof PooledSlicedByteBuf) { + unwrapped = ((PooledSlicedByteBuf) buffer).unwrap(); + adjustment = ((PooledSlicedByteBuf) buffer).adjustment + index; + } else if (buffer instanceof PooledDuplicatedByteBuf) { + unwrapped = ((PooledDuplicatedByteBuf) buffer).unwrap(); + adjustment = index; + } else if (buffer instanceof SlicedAbstractByteBuf) { + unwrapped = ((SlicedAbstractByteBuf) buffer).unwrap(); + adjustment = ((SlicedAbstractByteBuf) buffer).adjustment() + index; + } else if (buffer instanceof DuplicatedAbstractByteBuf) { + unwrapped = ((DuplicatedAbstractByteBuf) buffer).unwrap(); + adjustment = index; + } else { + unwrapped = buffer; + adjustment = index; + } + + final PooledSlicedByteBuf slice = RECYCLER.get(); + slice.init(unwrapped, 0, length, length); + slice.discardMarks(); + slice.adjustment = adjustment; + + return slice; + } + + private int adjustment; + + private PooledSlicedByteBuf(Handle handle) { + super(handle); + } + + @Override + public int capacity() { + return maxCapacity(); + } + + @Override + public ByteBuf capacity(int newCapacity) { + return reject(); + } + + @Override + public int arrayOffset() { + return idx(unwrap().arrayOffset()); + } + + @Override + public long memoryAddress() { + return unwrap().memoryAddress() + adjustment; + } + + @Override + public ByteBuffer nioBuffer(int index, int length) { + checkIndex0(index, length); + return unwrap().nioBuffer(idx(index), length); + } + + @Override + public ByteBuffer[] nioBuffers(int index, int length) { + checkIndex0(index, length); + return unwrap().nioBuffers(idx(index), length); + } + + @Override + public ByteBuf copy(int index, int length) { + checkIndex0(index, length); + return unwrap().copy(idx(index), length); + } + + @Override + public ByteBuf slice(int index, int length) { + checkIndex0(index, length); + return unwrap().slice(idx(index), length); + } + + @Override + public ByteBuf sliceRetained(int index, int length) { + checkIndex0(index, length); + return unwrap().sliceRetained(idx(index), length); + } + + @Override + public byte getByte(int index) { + checkIndex0(index, 1); + return unwrap().getByte(idx(index)); + } + + @Override + protected byte _getByte(int index) { + return unwrap()._getByte(idx(index)); + } + + @Override + public short getShort(int index) { + checkIndex0(index, 2); + return unwrap().getShort(idx(index)); + } + + @Override + protected short _getShort(int index) { + return unwrap()._getShort(idx(index)); + } + + @Override + public short getShortLE(int index) { + checkIndex0(index, 2); + return unwrap().getShortLE(idx(index)); + } + + @Override + protected short _getShortLE(int index) { + return unwrap()._getShortLE(idx(index)); + } + + @Override + public int getUnsignedMedium(int index) { + checkIndex0(index, 3); + return unwrap().getUnsignedMedium(idx(index)); + } + + @Override + protected int _getUnsignedMedium(int index) { + return unwrap()._getUnsignedMedium(idx(index)); + } + + @Override + public int getUnsignedMediumLE(int index) { + checkIndex0(index, 3); + return unwrap().getUnsignedMediumLE(idx(index)); + } + + @Override + protected int _getUnsignedMediumLE(int index) { + return unwrap()._getUnsignedMediumLE(idx(index)); + } + + @Override + public int getInt(int index) { + checkIndex0(index, 4); + return unwrap().getInt(idx(index)); + } + + @Override + protected int _getInt(int index) { + return unwrap()._getInt(idx(index)); + } + + @Override + public int getIntLE(int index) { + checkIndex0(index, 4); + return unwrap().getIntLE(idx(index)); + } + + @Override + protected int _getIntLE(int index) { + return unwrap()._getIntLE(idx(index)); + } + + @Override + public long getLong(int index) { + checkIndex0(index, 8); + return unwrap().getLong(idx(index)); + } + + @Override + protected long _getLong(int index) { + return unwrap()._getLong(idx(index)); + } + + @Override + public long getLongLE(int index) { + checkIndex0(index, 8); + return unwrap().getLongLE(idx(index)); + } + + @Override + protected long _getLongLE(int index) { + return unwrap()._getLongLE(idx(index)); + } + + @Override + public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { + checkIndex0(index, length); + unwrap().getBytes(idx(index), dst, dstIndex, length); + return this; + } + + @Override + public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) { + checkIndex0(index, length); + unwrap().getBytes(idx(index), dst, dstIndex, length); + return this; + } + + @Override + public ByteBuf getBytes(int index, ByteBuffer dst) { + checkIndex0(index, dst.remaining()); + 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; + } + + @Override + protected void _setByte(int index, int 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 + protected void _setShort(int index, int value) { + unwrap()._setShort(idx(index), value); + } + + @Override + public ByteBuf setShortLE(int index, int value) { + checkIndex0(index, 2); + unwrap().setShortLE(idx(index), value); + return this; + } + + @Override + protected void _setShortLE(int index, int value) { + unwrap()._setShortLE(idx(index), value); + } + + @Override + public ByteBuf setMedium(int index, int value) { + checkIndex0(index, 3); + unwrap().setMedium(idx(index), value); + return this; + } + + @Override + protected void _setMedium(int index, int value) { + unwrap()._setMedium(idx(index), value); + } + + @Override + public ByteBuf setMediumLE(int index, int value) { + checkIndex0(index, 3); + unwrap().setMediumLE(idx(index), value); + return this; + } + + @Override + protected void _setMediumLE(int index, int value) { + unwrap()._setMediumLE(idx(index), value); + } + + @Override + public ByteBuf setInt(int index, int value) { + checkIndex0(index, 4); + unwrap().setInt(idx(index), value); + return this; + } + + @Override + protected void _setInt(int index, int value) { + unwrap()._setInt(idx(index), value); + } + + @Override + public ByteBuf setIntLE(int index, int value) { + checkIndex0(index, 4); + unwrap().setIntLE(idx(index), value); + return this; + } + + @Override + protected void _setIntLE(int index, int value) { + unwrap()._setIntLE(idx(index), value); + } + + @Override + public ByteBuf setLong(int index, long value) { + checkIndex0(index, 8); + unwrap().setLong(idx(index), value); + return this; + } + + @Override + protected void _setLong(int index, long value) { + unwrap()._setLong(idx(index), value); + } + + @Override + public ByteBuf setLongLE(int index, long value) { + checkIndex0(index, 8); + unwrap().setLongLE(idx(index), value); + return this; + } + + @Override + protected void _setLongLE(int index, long value) { + unwrap().setLongLE(idx(index), value); + } + + @Override + public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) { + checkIndex0(index, length); + unwrap().setBytes(idx(index), src, srcIndex, length); + return this; + } + + @Override + public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) { + checkIndex0(index, length); + unwrap().setBytes(idx(index), src, srcIndex, length); + return this; + } + + @Override + public ByteBuf setBytes(int index, ByteBuffer src) { + checkIndex0(index, src.remaining()); + unwrap().setBytes(idx(index), src); + return this; + } + + @Override + public ByteBuf getBytes(int index, OutputStream out, int length) + throws IOException { + checkIndex0(index, length); + unwrap().getBytes(idx(index), out, length); + return this; + } + + @Override + public int getBytes(int index, GatheringByteChannel out, int length) + throws IOException { + checkIndex0(index, length); + return unwrap().getBytes(idx(index), out, length); + } + + @Override + public int getBytes(int index, FileChannel out, long position, int length) + throws IOException { + checkIndex0(index, length); + return unwrap().getBytes(idx(index), out, position, length); + } + + @Override + public int setBytes(int index, InputStream in, int length) + throws IOException { + checkIndex0(index, length); + return unwrap().setBytes(idx(index), in, length); + } + + @Override + public int setBytes(int index, ScatteringByteChannel in, int length) + throws IOException { + checkIndex0(index, length); + return unwrap().setBytes(idx(index), in, length); + } + + @Override + public int setBytes(int index, FileChannel in, long position, int length) + throws IOException { + checkIndex0(index, length); + return unwrap().setBytes(idx(index), in, position, length); + } + + @Override + public int forEachByte(int index, int length, ByteProcessor processor) { + checkIndex0(index, length); + int ret = unwrap().forEachByte(idx(index), length, processor); + if (ret >= adjustment) { + return ret - adjustment; + } else { + return -1; + } + } + + @Override + public int forEachByteDesc(int index, int length, ByteProcessor processor) { + checkIndex0(index, length); + int ret = unwrap().forEachByteDesc(idx(index), length, processor); + if (ret >= adjustment) { + return ret - adjustment; + } else { + return -1; + } + } + + private int idx(int index) { + return index + adjustment; + } + + private static ByteBuf reject() { + throw new UnsupportedOperationException("sliced buffer"); + } +} diff --git a/buffer/src/main/java/io/netty/buffer/PooledUnsafeDirectByteBuf.java b/buffer/src/main/java/io/netty/buffer/PooledUnsafeDirectByteBuf.java index 2d3ce436402..41fac411e6f 100644 --- a/buffer/src/main/java/io/netty/buffer/PooledUnsafeDirectByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/PooledUnsafeDirectByteBuf.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 The Netty Project + * Copyright 2016 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 @@ -364,6 +364,7 @@ private long addr(int index) { } @Override + @Deprecated protected SwappedByteBuf newSwappedByteBuf() { if (PlatformDependent.isUnaligned()) { // Only use if unaligned access is supported otherwise there is no gain. diff --git a/buffer/src/main/java/io/netty/buffer/PooledUnsafeHeapByteBuf.java b/buffer/src/main/java/io/netty/buffer/PooledUnsafeHeapByteBuf.java index 1219fed5139..822fdeeb575 100644 --- a/buffer/src/main/java/io/netty/buffer/PooledUnsafeHeapByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/PooledUnsafeHeapByteBuf.java @@ -1,7 +1,9 @@ /* - * Copyright 2015 The Netty Project + * Copyright 2016 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 * @@ -46,21 +48,41 @@ protected short _getShort(int index) { return UnsafeByteBufUtil.getShort(memory, idx(index)); } + @Override + protected short _getShortLE(int index) { + return UnsafeByteBufUtil.getShortLE(memory, idx(index)); + } + @Override protected int _getUnsignedMedium(int index) { return UnsafeByteBufUtil.getUnsignedMedium(memory, idx(index)); } + @Override + protected int _getUnsignedMediumLE(int index) { + return UnsafeByteBufUtil.getUnsignedMediumLE(memory, idx(index)); + } + @Override protected int _getInt(int index) { return UnsafeByteBufUtil.getInt(memory, idx(index)); } + @Override + protected int _getIntLE(int index) { + return UnsafeByteBufUtil.getIntLE(memory, idx(index)); + } + @Override protected long _getLong(int index) { return UnsafeByteBufUtil.getLong(memory, idx(index)); } + @Override + protected long _getLongLE(int index) { + return UnsafeByteBufUtil.getLongLE(memory, idx(index)); + } + @Override protected void _setByte(int index, int value) { UnsafeByteBufUtil.setByte(memory, idx(index), value); @@ -71,22 +93,43 @@ protected void _setShort(int index, int value) { UnsafeByteBufUtil.setShort(memory, idx(index), value); } + @Override + protected void _setShortLE(int index, int value) { + UnsafeByteBufUtil.setShortLE(memory, idx(index), value); + } + @Override protected void _setMedium(int index, int value) { UnsafeByteBufUtil.setMedium(memory, idx(index), value); } + @Override + protected void _setMediumLE(int index, int value) { + UnsafeByteBufUtil.setMediumLE(memory, idx(index), value); + } + @Override protected void _setInt(int index, int value) { UnsafeByteBufUtil.setInt(memory, idx(index), value); } + @Override + protected void _setIntLE(int index, int value) { + UnsafeByteBufUtil.setIntLE(memory, idx(index), value); + } + @Override protected void _setLong(int index, long value) { UnsafeByteBufUtil.setLong(memory, idx(index), value); } @Override + protected void _setLongLE(int index, long value) { + UnsafeByteBufUtil.setLongLE(memory, idx(index), value); + } + + @Override + @Deprecated protected SwappedByteBuf newSwappedByteBuf() { if (PlatformDependent.isUnaligned()) { // Only use if unaligned access is supported otherwise there is no gain. diff --git a/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBuf.java b/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBuf.java index ace37e78e37..f268d675829 100644 --- a/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBuf.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 The Netty Project + * Copyright 2016 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 @@ -31,7 +31,10 @@ * A derived buffer which forbids any write requests to its parent. It is * recommended to use {@link Unpooled#unmodifiableBuffer(ByteBuf)} * instead of calling the constructor explicitly. + * + * @deprecated Do not use. */ +@Deprecated public class ReadOnlyByteBuf extends AbstractDerivedByteBuf { private final ByteBuf buffer; @@ -39,7 +42,8 @@ public class ReadOnlyByteBuf extends AbstractDerivedByteBuf { public ReadOnlyByteBuf(ByteBuf buffer) { super(buffer.maxCapacity()); - if (buffer instanceof ReadOnlyByteBuf || buffer instanceof DuplicatedByteBuf) { + if (buffer instanceof ReadOnlyByteBuf || buffer instanceof PooledReadOnlyByteBuf || + buffer instanceof DuplicatedByteBuf || buffer instanceof PooledDuplicatedByteBuf) { this.buffer = buffer.unwrap(); } else { this.buffer = buffer; @@ -68,6 +72,7 @@ public ByteBufAllocator alloc() { } @Override + @Deprecated public ByteOrder order() { return buffer.order(); } @@ -142,6 +147,11 @@ protected void _setShort(int index, int value) { throw new ReadOnlyBufferException(); } + @Override + public ByteBuf setShortLE(int index, int value) { + throw new ReadOnlyBufferException(); + } + @Override protected void _setShortLE(int index, int value) { throw new ReadOnlyBufferException(); @@ -157,6 +167,11 @@ protected void _setMedium(int index, int value) { throw new ReadOnlyBufferException(); } + @Override + public ByteBuf setMediumLE(int index, int value) { + throw new ReadOnlyBufferException(); + } + @Override protected void _setMediumLE(int index, int value) { throw new ReadOnlyBufferException(); @@ -172,6 +187,11 @@ protected void _setInt(int index, int value) { throw new ReadOnlyBufferException(); } + @Override + public ByteBuf setIntLE(int index, int value) { + throw new ReadOnlyBufferException(); + } + @Override protected void _setIntLE(int index, int value) { throw new ReadOnlyBufferException(); @@ -187,6 +207,11 @@ protected void _setLong(int index, long value) { throw new ReadOnlyBufferException(); } + @Override + public ByteBuf setLongLE(int index, long value) { + throw new ReadOnlyBufferException(); + } + @Override protected void _setLongLE(int index, long value) { throw new ReadOnlyBufferException(); @@ -244,24 +269,14 @@ public ByteBuf getBytes(int index, ByteBuffer dst) { return this; } - @Override - public ByteBuf duplicate() { - return new ReadOnlyByteBuf(this); - } - @Override public ByteBuf copy(int index, int length) { return buffer.copy(index, length); } - @Override - public ByteBuf slice(int index, int length) { - return Unpooled.unmodifiableBuffer(buffer.slice(index, length)); - } - @Override public byte getByte(int index) { - return _getByte(index); + return buffer.getByte(index); } @Override @@ -271,7 +286,7 @@ protected byte _getByte(int index) { @Override public short getShort(int index) { - return _getShort(index); + return buffer.getShort(index); } @Override @@ -279,6 +294,11 @@ protected short _getShort(int index) { return buffer.getShort(index); } + @Override + public short getShortLE(int index) { + return buffer.getShortLE(index); + } + @Override protected short _getShortLE(int index) { return buffer.getShortLE(index); @@ -286,7 +306,7 @@ protected short _getShortLE(int index) { @Override public int getUnsignedMedium(int index) { - return _getUnsignedMedium(index); + return buffer.getUnsignedMedium(index); } @Override @@ -294,6 +314,11 @@ protected int _getUnsignedMedium(int index) { return buffer.getUnsignedMedium(index); } + @Override + public int getUnsignedMediumLE(int index) { + return buffer.getUnsignedMediumLE(index); + } + @Override protected int _getUnsignedMediumLE(int index) { return buffer.getUnsignedMediumLE(index); @@ -301,7 +326,7 @@ protected int _getUnsignedMediumLE(int index) { @Override public int getInt(int index) { - return _getInt(index); + return buffer.getInt(index); } @Override @@ -309,6 +334,11 @@ protected int _getInt(int index) { return buffer.getInt(index); } + @Override + public int getIntLE(int index) { + return buffer.getIntLE(index); + } + @Override protected int _getIntLE(int index) { return buffer.getIntLE(index); @@ -316,7 +346,7 @@ protected int _getIntLE(int index) { @Override public long getLong(int index) { - return _getLong(index); + return buffer.getLong(index); } @Override @@ -324,6 +354,11 @@ protected long _getLong(int index) { return buffer.getLong(index); } + @Override + public long getLongLE(int index) { + return buffer.getLongLE(index); + } + @Override protected long _getLongLE(int index) { return buffer.getLongLE(index); @@ -344,11 +379,6 @@ public ByteBuffer[] nioBuffers(int index, int length) { return buffer.nioBuffers(index, length); } - @Override - public ByteBuffer internalNioBuffer(int index, int length) { - return nioBuffer(index, length); - } - @Override public int forEachByte(int index, int length, ByteProcessor processor) { return buffer.forEachByte(index, length, processor); diff --git a/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBufferBuf.java b/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBufferBuf.java index e7bea4913a2..43af913dce4 100644 --- a/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBufferBuf.java +++ b/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBufferBuf.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 The Netty Project + * Copyright 2016 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 @@ -73,6 +73,12 @@ protected short _getShort(int index) { return buffer.getShort(index); } + @Override + public short getShortLE(int index) { + ensureAccessible(); + return _getShortLE(index); + } + @Override protected short _getShortLE(int index) { return ByteBufUtil.swapShort(buffer.getShort(index)); @@ -91,6 +97,12 @@ protected int _getUnsignedMedium(int index) { getByte(index + 2) & 0xff; } + @Override + public int getUnsignedMediumLE(int index) { + ensureAccessible(); + return _getUnsignedMediumLE(index); + } + @Override protected int _getUnsignedMediumLE(int index) { return getByte(index) & 0xff | @@ -109,6 +121,12 @@ protected int _getInt(int index) { return buffer.getInt(index); } + @Override + public int getIntLE(int index) { + ensureAccessible(); + return _getIntLE(index); + } + @Override protected int _getIntLE(int index) { return ByteBufUtil.swapInt(buffer.getInt(index)); @@ -125,6 +143,12 @@ protected long _getLong(int index) { return buffer.getLong(index); } + @Override + public long getLongLE(int index) { + ensureAccessible(); + return _getLongLE(index); + } + @Override protected long _getLongLE(int index) { return ByteBufUtil.swapLong(buffer.getLong(index)); @@ -176,46 +200,91 @@ public ByteBuf getBytes(int index, ByteBuffer dst) { return this; } + @Override + public ByteBuf setByte(int index, int value) { + throw new ReadOnlyBufferException(); + } + @Override protected void _setByte(int index, int value) { throw new ReadOnlyBufferException(); } + @Override + public ByteBuf setShort(int index, int value) { + throw new ReadOnlyBufferException(); + } + @Override protected void _setShort(int index, int value) { throw new ReadOnlyBufferException(); } + @Override + public ByteBuf setShortLE(int index, int value) { + throw new ReadOnlyBufferException(); + } + @Override protected void _setShortLE(int index, int value) { throw new ReadOnlyBufferException(); } + @Override + public ByteBuf setMedium(int index, int value) { + throw new ReadOnlyBufferException(); + } + @Override protected void _setMedium(int index, int value) { throw new ReadOnlyBufferException(); } + @Override + public ByteBuf setMediumLE(int index, int value) { + throw new ReadOnlyBufferException(); + } + @Override protected void _setMediumLE(int index, int value) { throw new ReadOnlyBufferException(); } + @Override + public ByteBuf setInt(int index, int value) { + throw new ReadOnlyBufferException(); + } + @Override protected void _setInt(int index, int value) { throw new ReadOnlyBufferException(); } + @Override + public ByteBuf setIntLE(int index, int value) { + throw new ReadOnlyBufferException(); + } + @Override protected void _setIntLE(int index, int value) { throw new ReadOnlyBufferException(); } + @Override + public ByteBuf setLong(int index, long value) { + throw new ReadOnlyBufferException(); + } + @Override protected void _setLong(int index, long value) { throw new ReadOnlyBufferException(); } + @Override + public ByteBuf setLongLE(int index, long value) { + throw new ReadOnlyBufferException(); + } + @Override protected void _setLongLE(int index, long value) { throw new ReadOnlyBufferException(); diff --git a/buffer/src/main/java/io/netty/buffer/ReadOnlyUnsafeDirectByteBuf.java b/buffer/src/main/java/io/netty/buffer/ReadOnlyUnsafeDirectByteBuf.java index 732c9dc895a..175d9abbc0b 100644 --- a/buffer/src/main/java/io/netty/buffer/ReadOnlyUnsafeDirectByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/ReadOnlyUnsafeDirectByteBuf.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 The Netty Project + * Copyright 2016 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 diff --git a/buffer/src/main/java/io/netty/buffer/SimpleLeakAwareByteBuf.java b/buffer/src/main/java/io/netty/buffer/SimpleLeakAwareByteBuf.java index 0a9fe3f20b7..25acc00f72d 100644 --- a/buffer/src/main/java/io/netty/buffer/SimpleLeakAwareByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/SimpleLeakAwareByteBuf.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 The Netty Project + * Copyright 2016 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 @@ -72,18 +72,38 @@ public ByteBuf slice() { return new SimpleLeakAwareByteBuf(super.slice(), leak); } + @Override + public ByteBuf sliceRetained() { + return new SimpleLeakAwareByteBuf(super.sliceRetained(), leak); + } + @Override public ByteBuf slice(int index, int length) { return new SimpleLeakAwareByteBuf(super.slice(index, length), leak); } + @Override + public ByteBuf sliceRetained(int index, int length) { + return new SimpleLeakAwareByteBuf(super.sliceRetained(index, length), leak); + } + @Override public ByteBuf duplicate() { return new SimpleLeakAwareByteBuf(super.duplicate(), leak); } + @Override + public ByteBuf duplicateRetained() { + return new SimpleLeakAwareByteBuf(super.duplicateRetained(), leak); + } + @Override public ByteBuf readSlice(int length) { return new SimpleLeakAwareByteBuf(super.readSlice(length), leak); } + + @Override + public ByteBuf readSliceRetained(int length) { + return new SimpleLeakAwareByteBuf(super.readSliceRetained(length), leak); + } } diff --git a/buffer/src/main/java/io/netty/buffer/SimpleLeakAwareCompositeByteBuf.java b/buffer/src/main/java/io/netty/buffer/SimpleLeakAwareCompositeByteBuf.java index bede44fee5d..b0e0a2f4992 100644 --- a/buffer/src/main/java/io/netty/buffer/SimpleLeakAwareCompositeByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/SimpleLeakAwareCompositeByteBuf.java @@ -13,6 +13,7 @@ * License for the specific language governing permissions and limitations * under the License. */ + package io.netty.buffer; @@ -62,18 +63,38 @@ public ByteBuf slice() { return new SimpleLeakAwareByteBuf(super.slice(), leak); } + @Override + public ByteBuf sliceRetained() { + return new SimpleLeakAwareByteBuf(super.sliceRetained(), leak); + } + @Override public ByteBuf slice(int index, int length) { return new SimpleLeakAwareByteBuf(super.slice(index, length), leak); } + @Override + public ByteBuf sliceRetained(int index, int length) { + return new SimpleLeakAwareByteBuf(super.sliceRetained(index, length), leak); + } + @Override public ByteBuf duplicate() { return new SimpleLeakAwareByteBuf(super.duplicate(), leak); } + @Override + public ByteBuf duplicateRetained() { + return new SimpleLeakAwareByteBuf(super.duplicateRetained(), leak); + } + @Override public ByteBuf readSlice(int length) { return new SimpleLeakAwareByteBuf(super.readSlice(length), leak); } + + @Override + public ByteBuf readSliceRetained(int length) { + return new SimpleLeakAwareByteBuf(super.readSliceRetained(length), leak); + } } diff --git a/buffer/src/main/java/io/netty/buffer/SlicedAbstractByteBuf.java b/buffer/src/main/java/io/netty/buffer/SlicedAbstractByteBuf.java index 8ff68ff6752..49404cca6d3 100644 --- a/buffer/src/main/java/io/netty/buffer/SlicedAbstractByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/SlicedAbstractByteBuf.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 The Netty Project + * Copyright 2016 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 @@ -18,64 +18,108 @@ /** * A special {@link SlicedByteBuf} that can make optimizations because it knows the sliced buffer is of type * {@link AbstractByteBuf}. + * + * @deprecated Do not use. */ +@Deprecated final class SlicedAbstractByteBuf extends SlicedByteBuf { SlicedAbstractByteBuf(AbstractByteBuf buffer, int index, int length) { super(buffer, index, length); } + @Override + public AbstractByteBuf unwrap() { + return (AbstractByteBuf) super.unwrap(); + } + @Override protected byte _getByte(int index) { - return unwrap0()._getByte(idx(index)); + return unwrap()._getByte(idx(index)); } @Override protected short _getShort(int index) { - return unwrap0()._getShort(idx(index)); + return unwrap()._getShort(idx(index)); + } + + @Override + protected short _getShortLE(int index) { + return unwrap()._getShortLE(idx(index)); } @Override protected int _getUnsignedMedium(int index) { - return unwrap0()._getUnsignedMedium(idx(index)); + return unwrap()._getUnsignedMedium(idx(index)); + } + + @Override + protected int _getUnsignedMediumLE(int index) { + return unwrap()._getUnsignedMediumLE(idx(index)); } @Override protected int _getInt(int index) { - return unwrap0()._getInt(idx(index)); + return unwrap()._getInt(idx(index)); + } + + @Override + protected int _getIntLE(int index) { + return unwrap()._getIntLE(idx(index)); } @Override protected long _getLong(int index) { - return unwrap0()._getLong(idx(index)); + return unwrap()._getLong(idx(index)); + } + + @Override + protected long _getLongLE(int index) { + return unwrap()._getLongLE(idx(index)); } @Override protected void _setByte(int index, int value) { - unwrap0()._setByte(idx(index), value); + unwrap()._setByte(idx(index), value); } @Override protected void _setShort(int index, int value) { - unwrap0()._setShort(idx(index), value); + unwrap()._setShort(idx(index), value); + } + + @Override + protected void _setShortLE(int index, int value) { + unwrap()._setShortLE(idx(index), value); } @Override protected void _setMedium(int index, int value) { - unwrap0()._setMedium(idx(index), value); + unwrap()._setMedium(idx(index), value); + } + + @Override + protected void _setMediumLE(int index, int value) { + unwrap()._setMediumLE(idx(index), value); } @Override protected void _setInt(int index, int value) { - unwrap0()._setInt(idx(index), value); + unwrap()._setInt(idx(index), value); + } + + @Override + protected void _setIntLE(int index, int value) { + unwrap()._setIntLE(idx(index), value); } @Override protected void _setLong(int index, long value) { - unwrap0()._setLong(idx(index), value); + unwrap()._setLong(idx(index), value); } - private AbstractByteBuf unwrap0() { - return (AbstractByteBuf) unwrap(); + @Override + protected void _setLongLE(int index, long value) { + unwrap()._setLongLE(idx(index), value); } } diff --git a/buffer/src/main/java/io/netty/buffer/SlicedByteBuf.java b/buffer/src/main/java/io/netty/buffer/SlicedByteBuf.java index 33fadd75423..b39d7b64f81 100644 --- a/buffer/src/main/java/io/netty/buffer/SlicedByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/SlicedByteBuf.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 The Netty Project + * Copyright 2016 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 @@ -32,7 +32,10 @@ * recommended to use {@link ByteBuf#slice()} and * {@link ByteBuf#slice(int, int)} instead of calling the constructor * explicitly. + * + * @deprecated Do not use. */ +@Deprecated public class SlicedByteBuf extends AbstractDerivedByteBuf { private final ByteBuf buffer; @@ -60,6 +63,10 @@ public SlicedByteBuf(ByteBuf buffer, int index, int length) { writerIndex(length); } + int adjustment() { + return adjustment; + } + @Override public ByteBuf unwrap() { return buffer; @@ -71,6 +78,7 @@ public ByteBufAllocator alloc() { } @Override + @Deprecated public ByteOrder order() { return buffer.order(); } @@ -115,56 +123,103 @@ public long memoryAddress() { return buffer.memoryAddress() + adjustment; } + @Override + public byte getByte(int index) { + checkIndex0(index, 1); + return buffer.getByte(idx(index)); + } + @Override protected byte _getByte(int index) { return buffer.getByte(idx(index)); } + @Override + public short getShort(int index) { + checkIndex0(index, 2); + return buffer.getShort(idx(index)); + } + @Override protected short _getShort(int index) { return buffer.getShort(idx(index)); } + @Override + public short getShortLE(int index) { + checkIndex0(index, 2); + return buffer.getShortLE(idx(index)); + } + @Override protected short _getShortLE(int index) { return buffer.getShortLE(idx(index)); } + @Override + public int getUnsignedMedium(int index) { + checkIndex0(index, 3); + return buffer.getUnsignedMedium(idx(index)); + } + @Override protected int _getUnsignedMedium(int index) { return buffer.getUnsignedMedium(idx(index)); } + @Override + public int getUnsignedMediumLE(int index) { + checkIndex0(index, 3); + return buffer.getUnsignedMediumLE(idx(index)); + } + @Override protected int _getUnsignedMediumLE(int index) { return buffer.getUnsignedMediumLE(idx(index)); } + @Override + public int getInt(int index) { + checkIndex0(index, 4); + return buffer.getInt(idx(index)); + } + @Override protected int _getInt(int index) { return buffer.getInt(idx(index)); } + @Override + public int getIntLE(int index) { + checkIndex0(index, 4); + return buffer.getIntLE(idx(index)); + } + @Override protected int _getIntLE(int index) { return buffer.getIntLE(idx(index)); } + @Override + public long getLong(int index) { + checkIndex0(index, 8); + return buffer.getLong(idx(index)); + } + @Override protected long _getLong(int index) { return buffer.getLong(idx(index)); } @Override - protected long _getLongLE(int index) { + public long getLongLE(int index) { + checkIndex0(index, 8); return buffer.getLongLE(idx(index)); } @Override - public ByteBuf duplicate() { - ByteBuf duplicate = buffer.slice(adjustment, length); - duplicate.setIndex(readerIndex(), writerIndex()); - return duplicate; + protected long _getLongLE(int index) { + return buffer.getLongLE(idx(index)); } @Override @@ -179,6 +234,12 @@ public ByteBuf slice(int index, int length) { return buffer.slice(idx(index), length); } + @Override + public ByteBuf sliceRetained(int index, int length) { + checkIndex0(index, length); + return buffer.sliceRetained(idx(index), length); + } + @Override public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { checkIndex0(index, length); @@ -200,46 +261,109 @@ public ByteBuf getBytes(int index, ByteBuffer dst) { return this; } + @Override + public ByteBuf setByte(int index, int value) { + checkIndex0(index, 1); + buffer.setByte(idx(index), value); + return this; + } + @Override protected void _setByte(int index, int value) { buffer.setByte(idx(index), value); } + @Override + public ByteBuf setShort(int index, int value) { + checkIndex0(index, 2); + buffer.setShort(idx(index), value); + return this; + } + @Override protected void _setShort(int index, int value) { buffer.setShort(idx(index), value); } + @Override + public ByteBuf setShortLE(int index, int value) { + checkIndex0(index, 2); + buffer.setShortLE(idx(index), value); + return this; + } + @Override protected void _setShortLE(int index, int value) { buffer.setShortLE(idx(index), value); } + @Override + public ByteBuf setMedium(int index, int value) { + checkIndex0(index, 3); + buffer.setMedium(idx(index), value); + return this; + } + @Override protected void _setMedium(int index, int value) { buffer.setMedium(idx(index), value); } + @Override + public ByteBuf setMediumLE(int index, int value) { + checkIndex0(index, 3); + buffer.setMediumLE(idx(index), value); + return this; + } + @Override protected void _setMediumLE(int index, int value) { buffer.setMediumLE(idx(index), value); } + @Override + public ByteBuf setInt(int index, int value) { + checkIndex0(index, 4); + buffer.setInt(idx(index), value); + return this; + } + @Override protected void _setInt(int index, int value) { buffer.setInt(idx(index), value); } + @Override + public ByteBuf setIntLE(int index, int value) { + checkIndex0(index, 4); + buffer.setIntLE(idx(index), value); + return this; + } + @Override protected void _setIntLE(int index, int value) { buffer.setIntLE(idx(index), value); } + @Override + public ByteBuf setLong(int index, long value) { + checkIndex0(index, 8); + buffer.setLong(idx(index), value); + return this; + } + @Override protected void _setLong(int index, long value) { buffer.setLong(idx(index), value); } + @Override + public ByteBuf setLongLE(int index, long value) { + checkIndex0(index, 8); + buffer.setLongLE(idx(index), value); + return this; + } + @Override protected void _setLongLE(int index, long value) { buffer.setLongLE(idx(index), value); @@ -320,11 +444,6 @@ public ByteBuffer[] nioBuffers(int index, int length) { return buffer.nioBuffers(idx(index), length); } - @Override - public ByteBuffer internalNioBuffer(int index, int length) { - return nioBuffer(index, length); - } - @Override public int forEachByte(int index, int length, ByteProcessor processor) { checkIndex0(index, length); diff --git a/buffer/src/main/java/io/netty/buffer/SwappedByteBuf.java b/buffer/src/main/java/io/netty/buffer/SwappedByteBuf.java index 0133a229fd7..ff01f37f714 100644 --- a/buffer/src/main/java/io/netty/buffer/SwappedByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/SwappedByteBuf.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 The Netty Project + * Copyright 2016 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 @@ -33,6 +33,7 @@ * @deprecated use the Little Endian accessors, e.g. {@code getShortLE}, {@code getIntLE} * instead. */ +@Deprecated public class SwappedByteBuf extends ByteBuf { private final ByteBuf buf; @@ -610,6 +611,11 @@ public ByteBuf readSlice(int length) { return buf.readSlice(length).order(order); } + @Override + public ByteBuf readSliceRetained(int length) { + return buf.readSliceRetained(length).order(order); + } + @Override public ByteBuf readBytes(ByteBuf dst) { buf.readBytes(dst); @@ -858,16 +864,31 @@ public ByteBuf slice() { return buf.slice().order(order); } + @Override + public ByteBuf sliceRetained() { + return buf.sliceRetained().order(order); + } + @Override public ByteBuf slice(int index, int length) { return buf.slice(index, length).order(order); } + @Override + public ByteBuf sliceRetained(int index, int length) { + return buf.sliceRetained(index, length).order(order); + } + @Override public ByteBuf duplicate() { return buf.duplicate().order(order); } + @Override + public ByteBuf duplicateRetained() { + return buf.duplicateRetained().order(order); + } + @Override public int nioBufferCount() { return buf.nioBufferCount(); diff --git a/buffer/src/main/java/io/netty/buffer/Unpooled.java b/buffer/src/main/java/io/netty/buffer/Unpooled.java index c2b45752cab..b62d59f2d98 100644 --- a/buffer/src/main/java/io/netty/buffer/Unpooled.java +++ b/buffer/src/main/java/io/netty/buffer/Unpooled.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 The Netty Project + * Copyright 2016 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 @@ -676,12 +676,15 @@ private static ByteBuf copiedBuffer(CharBuffer buffer, Charset charset) { * {@code buffer}. */ public static ByteBuf unmodifiableBuffer(ByteBuf buffer) { - ByteOrder endianness = buffer.order(); - if (endianness == BIG_ENDIAN) { - return new ReadOnlyByteBuf(buffer); + final ByteBuf bigEndianBuffer = buffer.order(BIG_ENDIAN); + final ByteBuf unmodifiable; + if (bigEndianBuffer instanceof AbstractByteBuf) { + unmodifiable = PooledReadOnlyByteBuf.newInstance((AbstractByteBuf) bigEndianBuffer); + } else { + unmodifiable = new ReadOnlyByteBuf(bigEndianBuffer); } - return new ReadOnlyByteBuf(buffer.order(BIG_ENDIAN)).order(LITTLE_ENDIAN); + return buffer.order() == BIG_ENDIAN ? unmodifiable : unmodifiable.order(LITTLE_ENDIAN); } /** diff --git a/buffer/src/main/java/io/netty/buffer/UnpooledByteBufAllocator.java b/buffer/src/main/java/io/netty/buffer/UnpooledByteBufAllocator.java index 90278211bc3..39a3dddf472 100644 --- a/buffer/src/main/java/io/netty/buffer/UnpooledByteBufAllocator.java +++ b/buffer/src/main/java/io/netty/buffer/UnpooledByteBufAllocator.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 The Netty Project + * Copyright 2016 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 diff --git a/buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java b/buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java index ad2add41eda..fe4b6a89be1 100644 --- a/buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 The Netty Project + * Copyright 2016 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 diff --git a/buffer/src/main/java/io/netty/buffer/UnpooledHeapByteBuf.java b/buffer/src/main/java/io/netty/buffer/UnpooledHeapByteBuf.java index f28949bbb7e..4fe8c110784 100644 --- a/buffer/src/main/java/io/netty/buffer/UnpooledHeapByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/UnpooledHeapByteBuf.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 The Netty Project + * Copyright 2016 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 @@ -334,6 +334,12 @@ protected short _getShort(int index) { return HeapByteBufUtil.getShort(array, index); } + @Override + public short getShortLE(int index) { + ensureAccessible(); + return _getShortLE(index); + } + @Override protected short _getShortLE(int index) { return HeapByteBufUtil.getShortLE(array, index); @@ -350,6 +356,12 @@ protected int _getUnsignedMedium(int index) { return HeapByteBufUtil.getUnsignedMedium(array, index); } + @Override + public int getUnsignedMediumLE(int index) { + ensureAccessible(); + return _getUnsignedMediumLE(index); + } + @Override protected int _getUnsignedMediumLE(int index) { return HeapByteBufUtil.getUnsignedMediumLE(array, index); @@ -366,6 +378,12 @@ protected int _getInt(int index) { return HeapByteBufUtil.getInt(array, index); } + @Override + public int getIntLE(int index) { + ensureAccessible(); + return _getIntLE(index); + } + @Override protected int _getIntLE(int index) { return HeapByteBufUtil.getIntLE(array, index); @@ -382,6 +400,12 @@ protected long _getLong(int index) { return HeapByteBufUtil.getLong(array, index); } + @Override + public long getLongLE(int index) { + ensureAccessible(); + return _getLongLE(index); + } + @Override protected long _getLongLE(int index) { return HeapByteBufUtil.getLongLE(array, index); @@ -411,6 +435,13 @@ protected void _setShort(int index, int value) { HeapByteBufUtil.setShort(array, index, value); } + @Override + public ByteBuf setShortLE(int index, int value) { + ensureAccessible(); + _setShortLE(index, value); + return this; + } + @Override protected void _setShortLE(int index, int value) { HeapByteBufUtil.setShortLE(array, index, value); @@ -428,6 +459,13 @@ protected void _setMedium(int index, int value) { HeapByteBufUtil.setMedium(array, index, value); } + @Override + public ByteBuf setMediumLE(int index, int value) { + ensureAccessible(); + _setMediumLE(index, value); + return this; + } + @Override protected void _setMediumLE(int index, int value) { HeapByteBufUtil.setMediumLE(array, index, value); @@ -445,6 +483,13 @@ protected void _setInt(int index, int value) { HeapByteBufUtil.setInt(array, index, value); } + @Override + public ByteBuf setIntLE(int index, int value) { + ensureAccessible(); + _setIntLE(index, value); + return this; + } + @Override protected void _setIntLE(int index, int value) { HeapByteBufUtil.setIntLE(array, index, value); @@ -462,6 +507,13 @@ protected void _setLong(int index, long value) { HeapByteBufUtil.setLong(array, index, value); } + @Override + public ByteBuf setLongLE(int index, long value) { + ensureAccessible(); + _setLongLE(index, value); + return this; + } + @Override protected void _setLongLE(int index, long value) { HeapByteBufUtil.setLongLE(array, index, value); diff --git a/buffer/src/main/java/io/netty/buffer/UnpooledUnsafeDirectByteBuf.java b/buffer/src/main/java/io/netty/buffer/UnpooledUnsafeDirectByteBuf.java index 9b55445d24e..1548ac4922d 100644 --- a/buffer/src/main/java/io/netty/buffer/UnpooledUnsafeDirectByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/UnpooledUnsafeDirectByteBuf.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 The Netty Project + * Copyright 2016 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 @@ -496,6 +496,7 @@ long addr(int index) { } @Override + @Deprecated protected SwappedByteBuf newSwappedByteBuf() { if (PlatformDependent.isUnaligned()) { // Only use if unaligned access is supported otherwise there is no gain. diff --git a/buffer/src/main/java/io/netty/buffer/UnpooledUnsafeHeapByteBuf.java b/buffer/src/main/java/io/netty/buffer/UnpooledUnsafeHeapByteBuf.java index 9a40f61df63..5ffc6e97d7f 100644 --- a/buffer/src/main/java/io/netty/buffer/UnpooledUnsafeHeapByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/UnpooledUnsafeHeapByteBuf.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 The Netty Project + * Copyright 2016 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 @@ -52,6 +52,17 @@ protected short _getShort(int index) { return UnsafeByteBufUtil.getShort(array, index); } + @Override + public short getShortLE(int index) { + checkIndex(index, 2); + return _getShortLE(index); + } + + @Override + protected short _getShortLE(int index) { + return UnsafeByteBufUtil.getShortLE(array, index); + } + @Override public int getUnsignedMedium(int index) { checkIndex(index, 3); @@ -63,6 +74,17 @@ protected int _getUnsignedMedium(int index) { return UnsafeByteBufUtil.getUnsignedMedium(array, index); } + @Override + public int getUnsignedMediumLE(int index) { + checkIndex(index, 3); + return _getUnsignedMediumLE(index); + } + + @Override + protected int _getUnsignedMediumLE(int index) { + return UnsafeByteBufUtil.getUnsignedMediumLE(array, index); + } + @Override public int getInt(int index) { checkIndex(index, 4); @@ -74,6 +96,17 @@ protected int _getInt(int index) { return UnsafeByteBufUtil.getInt(array, index); } + @Override + public int getIntLE(int index) { + checkIndex(index, 4); + return _getIntLE(index); + } + + @Override + protected int _getIntLE(int index) { + return UnsafeByteBufUtil.getIntLE(array, index); + } + @Override public long getLong(int index) { checkIndex(index, 8); @@ -85,6 +118,17 @@ protected long _getLong(int index) { return UnsafeByteBufUtil.getLong(array, index); } + @Override + public long getLongLE(int index) { + checkIndex(index, 8); + return _getLongLE(index); + } + + @Override + protected long _getLongLE(int index) { + return UnsafeByteBufUtil.getLongLE(array, index); + } + @Override public ByteBuf setByte(int index, int value) { checkIndex(index); @@ -109,6 +153,18 @@ protected void _setShort(int index, int value) { UnsafeByteBufUtil.setShort(array, index, value); } + @Override + public ByteBuf setShortLE(int index, int value) { + checkIndex(index, 2); + _setShortLE(index, value); + return this; + } + + @Override + protected void _setShortLE(int index, int value) { + UnsafeByteBufUtil.setShortLE(array, index, value); + } + @Override public ByteBuf setMedium(int index, int value) { checkIndex(index, 3); @@ -121,6 +177,18 @@ protected void _setMedium(int index, int value) { UnsafeByteBufUtil.setMedium(array, index, value); } + @Override + public ByteBuf setMediumLE(int index, int value) { + checkIndex(index, 3); + _setMediumLE(index, value); + return this; + } + + @Override + protected void _setMediumLE(int index, int value) { + UnsafeByteBufUtil.setMediumLE(array, index, value); + } + @Override public ByteBuf setInt(int index, int value) { checkIndex(index, 4); @@ -133,6 +201,18 @@ protected void _setInt(int index, int value) { UnsafeByteBufUtil.setInt(array, index, value); } + @Override + public ByteBuf setIntLE(int index, int value) { + checkIndex(index, 4); + _setIntLE(index, value); + return this; + } + + @Override + protected void _setIntLE(int index, int value) { + UnsafeByteBufUtil.setIntLE(array, index, value); + } + @Override public ByteBuf setLong(int index, long value) { checkIndex(index, 8); @@ -146,6 +226,19 @@ protected void _setLong(int index, long value) { } @Override + public ByteBuf setLongLE(int index, long value) { + checkIndex(index, 8); + _setLongLE(index, value); + return this; + } + + @Override + protected void _setLongLE(int index, long value) { + UnsafeByteBufUtil.setLongLE(array, index, value); + } + + @Override + @Deprecated protected SwappedByteBuf newSwappedByteBuf() { if (PlatformDependent.isUnaligned()) { // Only use if unaligned access is supported otherwise there is no gain. diff --git a/buffer/src/main/java/io/netty/buffer/UnreleasableByteBuf.java b/buffer/src/main/java/io/netty/buffer/UnreleasableByteBuf.java index 2b26f29137c..91f9cef1ab2 100644 --- a/buffer/src/main/java/io/netty/buffer/UnreleasableByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/UnreleasableByteBuf.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 The Netty Project + * Copyright 2016 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 @@ -50,21 +50,41 @@ public ByteBuf readSlice(int length) { return new UnreleasableByteBuf(buf.readSlice(length)); } + @Override + public ByteBuf readSliceRetained(int length) { + return new UnreleasableByteBuf(buf.readSlice(length)); + } + @Override public ByteBuf slice() { return new UnreleasableByteBuf(buf.slice()); } + @Override + public ByteBuf sliceRetained() { + return new UnreleasableByteBuf(buf.slice()); + } + @Override public ByteBuf slice(int index, int length) { return new UnreleasableByteBuf(buf.slice(index, length)); } + @Override + public ByteBuf sliceRetained(int index, int length) { + return new UnreleasableByteBuf(buf.slice(index, length)); + } + @Override public ByteBuf duplicate() { return new UnreleasableByteBuf(buf.duplicate()); } + @Override + public ByteBuf duplicateRetained() { + return new UnreleasableByteBuf(buf.duplicate()); + } + @Override public ByteBuf retain(int increment) { return this; diff --git a/buffer/src/main/java/io/netty/buffer/UnsafeByteBufUtil.java b/buffer/src/main/java/io/netty/buffer/UnsafeByteBufUtil.java index fd2f886f7c7..bf07d30a7e9 100644 --- a/buffer/src/main/java/io/netty/buffer/UnsafeByteBufUtil.java +++ b/buffer/src/main/java/io/netty/buffer/UnsafeByteBufUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 The Netty Project + * Copyright 2016 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 diff --git a/buffer/src/main/java/io/netty/buffer/UnsafeDirectSwappedByteBuf.java b/buffer/src/main/java/io/netty/buffer/UnsafeDirectSwappedByteBuf.java index 206b637b5d9..8ec4cf62eef 100644 --- a/buffer/src/main/java/io/netty/buffer/UnsafeDirectSwappedByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/UnsafeDirectSwappedByteBuf.java @@ -1,18 +1,18 @@ /* -* Copyright 2014 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. -*/ + * Copyright 2016 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.buffer; diff --git a/buffer/src/main/java/io/netty/buffer/UnsafeHeapSwappedByteBuf.java b/buffer/src/main/java/io/netty/buffer/UnsafeHeapSwappedByteBuf.java index 2a76906593c..b56be27b135 100644 --- a/buffer/src/main/java/io/netty/buffer/UnsafeHeapSwappedByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/UnsafeHeapSwappedByteBuf.java @@ -1,18 +1,18 @@ /* -* Copyright 2014 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. -*/ + * Copyright 2016 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.buffer; diff --git a/buffer/src/main/java/io/netty/buffer/WrappedByteBuf.java b/buffer/src/main/java/io/netty/buffer/WrappedByteBuf.java index 9ddcfd8bde5..5588998e2ef 100644 --- a/buffer/src/main/java/io/netty/buffer/WrappedByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/WrappedByteBuf.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 The Netty Project + * Copyright 2016 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 @@ -601,6 +601,11 @@ public ByteBuf readSlice(int length) { return buf.readSlice(length); } + @Override + public ByteBuf readSliceRetained(int length) { + return buf.readSliceRetained(length); + } + @Override public ByteBuf readBytes(ByteBuf dst) { buf.readBytes(dst); @@ -849,16 +854,31 @@ public ByteBuf slice() { return buf.slice(); } + @Override + public ByteBuf sliceRetained() { + return buf.sliceRetained(); + } + @Override public ByteBuf slice(int index, int length) { return buf.slice(index, length); } + @Override + public ByteBuf sliceRetained(int index, int length) { + return buf.sliceRetained(index, length); + } + @Override public ByteBuf duplicate() { return buf.duplicate(); } + @Override + public ByteBuf duplicateRetained() { + return buf.duplicateRetained(); + } + @Override public int nioBufferCount() { return buf.nioBufferCount(); diff --git a/buffer/src/main/java/io/netty/buffer/WrappedCompositeByteBuf.java b/buffer/src/main/java/io/netty/buffer/WrappedCompositeByteBuf.java index 029d9c1372a..ea24fe84c66 100644 --- a/buffer/src/main/java/io/netty/buffer/WrappedCompositeByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/WrappedCompositeByteBuf.java @@ -13,6 +13,7 @@ * License for the specific language governing permissions and limitations * under the License. */ + package io.netty.buffer; import io.netty.util.ByteProcessor; @@ -332,11 +333,21 @@ public ByteBuf slice() { return wrapped.slice(); } + @Override + public ByteBuf sliceRetained() { + return wrapped.sliceRetained(); + } + @Override public ByteBuf slice(int index, int length) { return wrapped.slice(index, length); } + @Override + public ByteBuf sliceRetained(int index, int length) { + return wrapped.sliceRetained(index, length); + } + @Override public ByteBuffer nioBuffer() { return wrapped.nioBuffer(); @@ -417,11 +428,21 @@ public ByteBuf duplicate() { return wrapped.duplicate(); } + @Override + public ByteBuf duplicateRetained() { + return wrapped.duplicateRetained(); + } + @Override public ByteBuf readSlice(int length) { return wrapped.readSlice(length); } + @Override + public ByteBuf readSliceRetained(int length) { + return wrapped.readSliceRetained(length); + } + @Override public int readBytes(GatheringByteChannel out, int length) throws IOException { return wrapped.readBytes(out, length); diff --git a/buffer/src/main/java/io/netty/buffer/package-info.java b/buffer/src/main/java/io/netty/buffer/package-info.java index cc0d28851a7..b191525edd6 100644 --- a/buffer/src/main/java/io/netty/buffer/package-info.java +++ b/buffer/src/main/java/io/netty/buffer/package-info.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 The Netty Project + * Copyright 2016 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 diff --git a/buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java b/buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java index 6946b9d1515..0c9b41ec3ec 100644 --- a/buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java @@ -2839,6 +2839,29 @@ public void testSliceRelease() { assertEquals(0, buf.refCnt()); } + @Test + public void testSliceRetained() { + ByteBuf buf = newBuffer(8); + assertEquals(1, buf.refCnt()); + assertFalse(buf.sliceRetained().release()); + assertEquals(1, buf.refCnt()); + assertTrue(buf.release()); + + buf = newBuffer(8); + ByteBuf slice = buf.slice().retain(); + assertEquals(2, buf.refCnt()); + assertFalse(slice.release(1)); + assertEquals(1, buf.refCnt()); + assertTrue(slice.release(1)); + assertEquals(0, buf.refCnt()); + + buf = newBuffer(8); + slice = buf.slice().retain(2); + assertEquals(3, buf.refCnt()); + assertTrue(slice.release(3)); + assertEquals(0, buf.refCnt()); + } + @Test public void testDuplicateRelease() { ByteBuf buf = newBuffer(8); @@ -2847,6 +2870,29 @@ public void testDuplicateRelease() { assertEquals(0, buf.refCnt()); } + @Test + public void testDuplicateRetained() { + ByteBuf buf = newBuffer(8); + assertEquals(1, buf.refCnt()); + assertFalse(buf.duplicateRetained().release()); + assertEquals(1, buf.refCnt()); + assertTrue(buf.release()); + + buf = newBuffer(8); + ByteBuf duplicate = buf.duplicate().retain(); + assertEquals(2, buf.refCnt()); + assertFalse(duplicate.release(1)); + assertEquals(1, buf.refCnt()); + assertTrue(duplicate.release(1)); + assertEquals(0, buf.refCnt()); + + buf = newBuffer(8); + duplicate = buf.duplicate().retain(2); + assertEquals(3, buf.refCnt()); + assertTrue(duplicate.release(3)); + assertEquals(0, buf.refCnt()); + } + // Test-case trying to reproduce: // https://github.com/netty/netty/issues/2843 @Test diff --git a/buffer/src/test/java/io/netty/buffer/AbstractCompositeByteBufTest.java b/buffer/src/test/java/io/netty/buffer/AbstractCompositeByteBufTest.java index be6aa2b9684..19d58fdc340 100644 --- a/buffer/src/test/java/io/netty/buffer/AbstractCompositeByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/AbstractCompositeByteBufTest.java @@ -456,7 +456,7 @@ public void testReadWithEmptyCompositeBuffer() { public void testComponentMustBeSlice() { CompositeByteBuf buf = releaseLater(compositeBuffer()); buf.addComponent(buffer(4).setIndex(1, 3)); - assertThat(buf.component(0), is(instanceOf(SlicedByteBuf.class))); + assertThat(buf.component(0), is(instanceOf(PooledSlicedByteBuf.class))); assertThat(buf.component(0).capacity(), is(2)); assertThat(buf.component(0).maxCapacity(), is(2)); } diff --git a/buffer/src/test/java/io/netty/buffer/ByteBufDerivationTest.java b/buffer/src/test/java/io/netty/buffer/ByteBufDerivationTest.java index ad60acb6b2d..6a1b05de0c4 100644 --- a/buffer/src/test/java/io/netty/buffer/ByteBufDerivationTest.java +++ b/buffer/src/test/java/io/netty/buffer/ByteBufDerivationTest.java @@ -34,7 +34,7 @@ public void testSlice() throws Exception { ByteBuf buf = Unpooled.buffer(8).setIndex(1, 7); ByteBuf slice = buf.slice(1, 7); - assertThat(slice, instanceOf(SlicedByteBuf.class)); + assertThat(slice, instanceOf(PooledSlicedByteBuf.class)); assertThat(slice.unwrap(), sameInstance(buf)); assertThat(slice.readerIndex(), is(0)); assertThat(slice.writerIndex(), is(7)); @@ -53,7 +53,7 @@ public void testSliceOfSlice() throws Exception { ByteBuf slice2 = slice.slice(0, 6); assertThat(slice2, not(sameInstance(slice))); - assertThat(slice2, instanceOf(SlicedByteBuf.class)); + assertThat(slice2, instanceOf(PooledSlicedByteBuf.class)); assertThat(slice2.unwrap(), sameInstance(buf)); assertThat(slice2.writerIndex(), is(6)); assertThat(slice2.capacity(), is(6)); @@ -64,7 +64,7 @@ public void testDuplicate() throws Exception { ByteBuf buf = Unpooled.buffer(8).setIndex(1, 7); ByteBuf dup = buf.duplicate(); - assertThat(dup, instanceOf(DuplicatedByteBuf.class)); + assertThat(dup, instanceOf(PooledDuplicatedByteBuf.class)); assertThat(dup.unwrap(), sameInstance(buf)); assertThat(dup.readerIndex(), is(buf.readerIndex())); assertThat(dup.writerIndex(), is(buf.writerIndex())); @@ -83,7 +83,7 @@ public void testDuplicateOfDuplicate() throws Exception { ByteBuf dup2 = dup.duplicate(); assertThat(dup2, not(sameInstance(dup))); - assertThat(dup2, instanceOf(DuplicatedByteBuf.class)); + assertThat(dup2, instanceOf(PooledDuplicatedByteBuf.class)); assertThat(dup2.unwrap(), sameInstance(buf)); assertThat(dup2.readerIndex(), is(dup.readerIndex())); assertThat(dup2.writerIndex(), is(dup.writerIndex())); @@ -96,7 +96,7 @@ public void testReadOnly() throws Exception { ByteBuf buf = Unpooled.buffer(8).setIndex(1, 7); ByteBuf ro = Unpooled.unmodifiableBuffer(buf); - assertThat(ro, instanceOf(ReadOnlyByteBuf.class)); + assertThat(ro, instanceOf(PooledReadOnlyByteBuf.class)); assertThat(ro.unwrap(), sameInstance(buf)); assertThat(ro.readerIndex(), is(buf.readerIndex())); assertThat(ro.writerIndex(), is(buf.writerIndex())); @@ -114,7 +114,7 @@ public void testReadOnlyOfReadOnly() throws Exception { ByteBuf ro2 = Unpooled.unmodifiableBuffer(ro); assertThat(ro2, not(sameInstance(ro))); - assertThat(ro2, instanceOf(ReadOnlyByteBuf.class)); + assertThat(ro2, instanceOf(PooledReadOnlyByteBuf.class)); assertThat(ro2.unwrap(), sameInstance(buf)); assertThat(ro2.readerIndex(), is(ro.readerIndex())); assertThat(ro2.writerIndex(), is(ro.writerIndex())); @@ -128,7 +128,7 @@ public void testReadOnlyOfDuplicate() throws Exception { ByteBuf dup = buf.duplicate().setIndex(2, 6); ByteBuf ro = Unpooled.unmodifiableBuffer(dup); - assertThat(ro, instanceOf(ReadOnlyByteBuf.class)); + assertThat(ro, instanceOf(PooledReadOnlyByteBuf.class)); assertThat(ro.unwrap(), sameInstance(buf)); assertThat(ro.readerIndex(), is(dup.readerIndex())); assertThat(ro.writerIndex(), is(dup.writerIndex())); @@ -142,7 +142,7 @@ public void testDuplicateOfReadOnly() throws Exception { ByteBuf ro = Unpooled.unmodifiableBuffer(buf).setIndex(2, 6); ByteBuf dup = ro.duplicate(); - assertThat(dup, instanceOf(ReadOnlyByteBuf.class)); + assertThat(dup, instanceOf(PooledReadOnlyByteBuf.class)); assertThat(dup.unwrap(), sameInstance(buf)); assertThat(dup.readerIndex(), is(ro.readerIndex())); assertThat(dup.writerIndex(), is(ro.writerIndex())); diff --git a/buffer/src/test/java/io/netty/buffer/ReadOnlyByteBufTest.java b/buffer/src/test/java/io/netty/buffer/ReadOnlyByteBufTest.java index 5023821a54b..bd4557a6513 100644 --- a/buffer/src/test/java/io/netty/buffer/ReadOnlyByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/ReadOnlyByteBufTest.java @@ -35,12 +35,12 @@ public class ReadOnlyByteBufTest { @Test(expected = NullPointerException.class) public void shouldNotAllowNullInConstructor() { - new ReadOnlyByteBuf(null); + PooledReadOnlyByteBuf.newInstance(null); } @Test public void testUnmodifiableBuffer() { - assertTrue(unmodifiableBuffer(buffer(1)) instanceof ReadOnlyByteBuf); + assertTrue(unmodifiableBuffer(buffer(1)) instanceof PooledReadOnlyByteBuf); } @Test @@ -60,26 +60,27 @@ public void shouldHaveSameByteOrder() { @Test public void shouldReturnReadOnlyDerivedBuffer() { ByteBuf buf = unmodifiableBuffer(buffer(1)); - assertTrue(buf.duplicate() instanceof ReadOnlyByteBuf); - assertTrue(buf.slice() instanceof ReadOnlyByteBuf); - assertTrue(buf.slice(0, 1) instanceof ReadOnlyByteBuf); - assertTrue(buf.duplicate() instanceof ReadOnlyByteBuf); + assertTrue(buf.duplicate() instanceof PooledReadOnlyByteBuf); + assertTrue(buf.slice() instanceof PooledReadOnlyByteBuf); + assertTrue(buf.slice(0, 1) instanceof PooledReadOnlyByteBuf); + assertTrue(buf.duplicate() instanceof PooledReadOnlyByteBuf); } @Test public void shouldReturnWritableCopy() { ByteBuf buf = unmodifiableBuffer(buffer(1)); - assertFalse(buf.copy() instanceof ReadOnlyByteBuf); + assertFalse(buf.copy() instanceof PooledReadOnlyByteBuf); } @Test public void shouldForwardReadCallsBlindly() throws Exception { - ByteBuf buf = createStrictMock(ByteBuf.class); - expect(buf.order()).andReturn(BIG_ENDIAN).anyTimes(); - expect(buf.maxCapacity()).andReturn(65536).anyTimes(); + ByteBuf buf = createStrictMock(AbstractByteBuf.class); + expect(buf.order(BIG_ENDIAN)).andReturn(buf).anyTimes(); expect(buf.readerIndex()).andReturn(0).anyTimes(); expect(buf.writerIndex()).andReturn(0).anyTimes(); + expect(buf.maxCapacity()).andReturn(65536).anyTimes(); expect(buf.capacity()).andReturn(0).anyTimes(); + expect(buf.order()).andReturn(BIG_ENDIAN).anyTimes(); expect(buf.getBytes(1, (GatheringByteChannel) null, 2)).andReturn(3); expect(buf.getBytes(4, (OutputStream) null, 5)).andReturn(buf); @@ -100,6 +101,7 @@ public void shouldForwardReadCallsBlindly() throws Exception { replay(buf); ByteBuf roBuf = unmodifiableBuffer(buf); + assertEquals(3, roBuf.getBytes(1, (GatheringByteChannel) null, 2)); roBuf.getBytes(4, (OutputStream) null, 5); roBuf.getBytes(6, (byte[]) null, 7, 8); @@ -175,10 +177,12 @@ public void shouldRejectSetBytes5() { unmodifiableBuffer(EMPTY_BUFFER).setBytes(0, (ByteBuffer) null); } + @Test public void shouldIndicateNotWriteable() { assertFalse(unmodifiableBuffer(buffer(1)).isWritable()); } + @Test public void shouldIndicteNotWritableAnyNumber() { assertFalse(unmodifiableBuffer(buffer(1)).isWritable(1)); } diff --git a/buffer/src/test/java/io/netty/buffer/SlicedByteBufTest.java b/buffer/src/test/java/io/netty/buffer/SlicedByteBufTest.java index 0e06307df38..7e1375f345c 100644 --- a/buffer/src/test/java/io/netty/buffer/SlicedByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/SlicedByteBufTest.java @@ -31,8 +31,7 @@ public class SlicedByteBufTest extends AbstractByteBufTest { @Override protected ByteBuf newBuffer(int length) { - ByteBuf buffer = Unpooled.wrappedBuffer( - new byte[length * 2], random.nextInt(length - 1) + 1, length); + ByteBuf buffer = Unpooled.wrappedBuffer(new byte[length * 2], random.nextInt(length - 1) + 1, length); assertEquals(length, buffer.writerIndex()); return buffer; } diff --git a/codec/src/main/java/io/netty/handler/codec/ReplayingDecoderByteBuf.java b/codec/src/main/java/io/netty/handler/codec/ReplayingDecoderByteBuf.java index 99f8b04eab9..e54f732a06d 100644 --- a/codec/src/main/java/io/netty/handler/codec/ReplayingDecoderByteBuf.java +++ b/codec/src/main/java/io/netty/handler/codec/ReplayingDecoderByteBuf.java @@ -41,6 +41,7 @@ final class ReplayingDecoderByteBuf extends ByteBuf { private ByteBuf buffer; private boolean terminated; + @SuppressWarnings("deprecation") private SwappedByteBuf swapped; static final ReplayingDecoderByteBuf EMPTY_BUFFER = new ReplayingDecoderByteBuf(Unpooled.EMPTY_BUFFER); @@ -171,6 +172,12 @@ public ByteBuf duplicate() { return this; } + @Override + public ByteBuf duplicateRetained() { + reject(); + return this; + } + @Override public boolean getBoolean(int index) { checkIndex(index, 1); @@ -461,11 +468,13 @@ public ByteBuf markWriterIndex() { } @Override + @Deprecated public ByteOrder order() { return buffer.order(); } @Override + @Deprecated public ByteBuf order(ByteOrder endianness) { if (endianness == null) { throw new NullPointerException("endianness"); @@ -582,6 +591,12 @@ public ByteBuf readSlice(int length) { return buffer.readSlice(length); } + @Override + public ByteBuf readSliceRetained(int length) { + checkReadableBytes(length); + return buffer.readSliceRetained(length); + } + @Override public ByteBuf readBytes(OutputStream out, int length) { reject(); @@ -870,12 +885,24 @@ public ByteBuf slice() { return this; } + @Override + public ByteBuf sliceRetained() { + reject(); + return this; + } + @Override public ByteBuf slice(int index, int length) { checkIndex(index, length); return buffer.slice(index, length); } + @Override + public ByteBuf sliceRetained(int index, int length) { + checkIndex(index, length); + return buffer.slice(index, length); + } + @Override public int nioBufferCount() { return buffer.nioBufferCount();