Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Remove ChannelBuf/ByteBuf.Unsafe

- Fixes #826
Unsafe.isFreed(), free(), suspend/resumeIntermediaryAllocations() are not that dangerous. internalNioBuffer() and internalNioBuffers() are dangerous but it seems like nobody is using it even inside Netty. Removing those two methods also removes the necessity to keep Unsafe interface at all.
  • Loading branch information...
commit 03e68482bba54aa0c0d35649ac9c090b0ab75473 1 parent 33134b1
@trustin trustin authored
Showing with 225 additions and 332 deletions.
  1. +21 −0 buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java
  2. +25 −46 buffer/src/main/java/io/netty/buffer/ByteBuf.java
  3. +7 −15 buffer/src/main/java/io/netty/buffer/ChannelBuf.java
  4. +8 −0 buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java
  5. +10 −32 buffer/src/main/java/io/netty/buffer/DefaultCompositeByteBuf.java
  6. +1 −8 buffer/src/main/java/io/netty/buffer/DefaultMessageBuf.java
  7. +9 −25 buffer/src/main/java/io/netty/buffer/DuplicatedByteBuf.java
  8. +40 −0 buffer/src/main/java/io/netty/buffer/IllegalMemoryAccessException.java
  9. +8 −32 buffer/src/main/java/io/netty/buffer/PooledByteBuf.java
  10. +1 −8 buffer/src/main/java/io/netty/buffer/QueueBackedMessageBuf.java
  11. +9 −25 buffer/src/main/java/io/netty/buffer/ReadOnlyByteBuf.java
  12. +9 −25 buffer/src/main/java/io/netty/buffer/SlicedByteBuf.java
  13. +28 −5 buffer/src/main/java/io/netty/buffer/SwappedByteBuf.java
  14. +7 −30 buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java
  15. +8 −30 buffer/src/main/java/io/netty/buffer/UnpooledHeapByteBuf.java
  16. +1 −1  codec/src/main/java/io/netty/handler/codec/ByteToByteDecoder.java
  17. +1 −1  codec/src/main/java/io/netty/handler/codec/ByteToByteEncoder.java
  18. +2 −2 codec/src/main/java/io/netty/handler/codec/ByteToMessageDecoder.java
  19. +2 −2 codec/src/main/java/io/netty/handler/codec/MessageToMessageCodec.java
  20. +1 −1  codec/src/main/java/io/netty/handler/codec/ReplayingDecoder.java
  21. +5 −21 codec/src/main/java/io/netty/handler/codec/ReplayingDecoderBuffer.java
  22. +1 −1  codec/src/main/java/io/netty/handler/codec/serialization/CompatibleObjectEncoder.java
  23. +2 −2 handler/src/main/java/io/netty/handler/logging/ByteLoggingHandler.java
  24. +4 −4 handler/src/main/java/io/netty/handler/ssl/SslHandler.java
  25. +2 −2 microbench/src/test/java/io/netty/microbench/buffer/ByteBufAllocatorBenchmark.java
  26. +1 −1  transport/src/main/java/io/netty/channel/ChannelInboundByteHandlerAdapter.java
  27. +1 −1  transport/src/main/java/io/netty/channel/ChannelInboundHandlerAdapter.java
  28. +1 −1  transport/src/main/java/io/netty/channel/ChannelOutboundHandlerAdapter.java
  29. +2 −3 transport/src/main/java/io/netty/channel/DefaultChannelHandlerContext.java
  30. +1 −1  transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java
  31. +4 −4 transport/src/main/java/io/netty/channel/socket/aio/AioSocketChannel.java
  32. +3 −3 transport/src/test/java/io/netty/channel/local/LocalTransportThreadModelTest.java
View
21 buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java
@@ -171,6 +171,27 @@ public ByteBuf discardReadBytes() {
return this;
}
+ @Override
+ public ByteBuf discardSomeReadBytes() {
+ if (readerIndex == 0) {
+ return this;
+ }
+
+ if (readerIndex == writerIndex) {
+ adjustMarkers(readerIndex);
+ writerIndex = readerIndex = 0;
+ return this;
+ }
+
+ if (readerIndex >= capacity() >>> 1) {
+ setBytes(0, this, readerIndex, writerIndex - readerIndex);
+ writerIndex -= readerIndex;
+ adjustMarkers(readerIndex);
+ readerIndex = 0;
+ }
+ return this;
+ }
+
protected void adjustMarkers(int decrement) {
markedReaderIndex = Math.max(markedReaderIndex - decrement, 0);
markedWriterIndex = Math.max(markedWriterIndex - decrement, 0);
View
71 buffer/src/main/java/io/netty/buffer/ByteBuf.java
@@ -458,6 +458,14 @@
ByteBuf discardReadBytes();
/**
+ * Similar to {@link ByteBuf#discardReadBytes()} except that this method might discard
+ * some, all, or none of read bytes depending on its internal implementation to reduce
+ * overall memory bandwidth consumption at the cost of potentially additional memory
+ * consumption.
+ */
+ ByteBuf discardSomeReadBytes();
+
+ /**
* Makes sure the number of {@linkplain #writableBytes() the writable bytes}
* is equal to or greater than the specified value. If there is enough
* writable bytes in this buffer, this method returns with no side effect.
@@ -1828,6 +1836,23 @@
String toString(int index, int length, Charset charset);
/**
+ * Suspends the intermediary deallocation of the internal memory block of this buffer until asked via
+ * {@link #resumeIntermediaryDeallocations()}. An intermediary deallocation is usually made when the capacity of
+ * a buffer changes.
+ *
+ * @throws UnsupportedOperationException if this buffer is derived
+ */
+ ByteBuf suspendIntermediaryDeallocations();
+
+ /**
+ * Resumes the intermediary deallocation of the internal memory block of this buffer, suspended by
+ * {@link #suspendIntermediaryDeallocations()}.
+ *
+ * @throws UnsupportedOperationException if this buffer is derived
+ */
+ ByteBuf resumeIntermediaryDeallocations();
+
+ /**
* Returns a hash code which was calculated from the content of this
* buffer. If there's a byte array which is
* {@linkplain #equals(Object) equal to} this array, both arrays should
@@ -1868,50 +1893,4 @@
*/
@Override
String toString();
-
- /**
- * Provides access to potentially unsafe operations of this buffer.
- */
- @Override
- Unsafe unsafe();
-
- /**
- * Provides the potentially unsafe operations of {@link ByteBuf}.
- */
- interface Unsafe extends ChannelBuf.Unsafe {
- /**
- * Returns the internal NIO buffer that is reused for I/O.
- *
- * @throws UnsupportedOperationException if the buffer has no internal NIO buffer
- */
- ByteBuffer internalNioBuffer();
-
- /**
- * Returns the internal NIO buffer array that is reused for I/O.
- *
- * @throws UnsupportedOperationException if the buffer has no internal NIO buffer array
- */
- ByteBuffer[] internalNioBuffers();
-
- /**
- * Similar to {@link ByteBuf#discardReadBytes()} except that this method might discard
- * some, all, or none of read bytes depending on its internal implementation to reduce
- * overall memory bandwidth consumption at the cost of potentially additional memory
- * consumption.
- */
- void discardSomeReadBytes();
-
- /**
- * Suspends the intermediary deallocation of the internal memory block of this buffer until asked via
- * {@link #resumeIntermediaryDeallocations()}. An intermediary deallocation is usually made when the capacity of
- * a buffer changes.
- */
- void suspendIntermediaryDeallocations();
-
- /**
- * Resumes the intermediary deallocation of the internal memory block of this buffer, suspended by
- * {@link #suspendIntermediaryDeallocations()}.
- */
- void resumeIntermediaryDeallocations();
- }
}
View
22 buffer/src/main/java/io/netty/buffer/ChannelBuf.java
@@ -22,23 +22,15 @@
ChannelBufType type();
/**
- * Provides access to potentially unsafe operations of this buffer.
+ * Returns {@code true} if and only if this buffer has been deallocated by {@link #free()}.
*/
- Unsafe unsafe();
+ boolean isFreed();
/**
- * Provides the potentially unsafe operations of {@link ByteBuf}.
+ * Deallocates the internal memory block of this buffer or returns it to the allocator or pool it came from.
+ * The result of accessing a released buffer is unspecified and can even cause JVM crash.
+ *
+ * @throws UnsupportedOperationException if this buffer is derived
*/
- interface Unsafe {
- /**
- * Returns {@code true} if and only if this buffer has been deallocated by {@link #free()}.
- */
- boolean isFreed();
-
- /**
- * Deallocates the internal memory block of this buffer or returns it to the {@link ByteBufAllocator} it came
- * from. The result of accessing a released buffer is unspecified and can even cause JVM crash.
- */
- void free();
- }
+ void free();
}
View
8 buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java
@@ -82,6 +82,9 @@
CompositeByteBuf discardReadBytes();
@Override
+ CompositeByteBuf discardSomeReadBytes();
+
+ @Override
CompositeByteBuf ensureWritableBytes(int minWritableBytes);
@Override
@@ -225,4 +228,9 @@
@Override
CompositeByteBuf writeZero(int length);
+ @Override
+ CompositeByteBuf suspendIntermediaryDeallocations();
+
+ @Override
+ CompositeByteBuf resumeIntermediaryDeallocations();
}
View
42 buffer/src/main/java/io/netty/buffer/DefaultCompositeByteBuf.java
@@ -15,7 +15,6 @@
*/
package io.netty.buffer;
-import io.netty.buffer.ByteBuf.Unsafe;
import io.netty.util.internal.DetectionUtil;
import java.io.IOException;
@@ -40,7 +39,7 @@
* is recommended to use {@link Unpooled#wrappedBuffer(ByteBuf...)}
* instead of calling the constructor explicitly.
*/
-public class DefaultCompositeByteBuf extends AbstractByteBuf implements CompositeByteBuf, Unsafe {
+public class DefaultCompositeByteBuf extends AbstractByteBuf implements CompositeByteBuf {
private static final ByteBuffer[] EMPTY_NIOBUFFERS = new ByteBuffer[0];
@@ -1292,7 +1291,7 @@ void freeIfNecessary() {
}
if (suspendedDeallocations == null) {
- buf.unsafe().free(); // We should not get a NPE here. If so, it must be a bug.
+ buf.free(); // We should not get a NPE here. If so, it must be a bug.
} else {
suspendedDeallocations.add(buf);
}
@@ -1525,26 +1524,8 @@ public CompositeByteBuf writeZero(int length) {
}
@Override
- public ByteBuffer internalNioBuffer() {
- if (components.size() == 1) {
- return components.get(0).buf.unsafe().internalNioBuffer();
- }
- throw new UnsupportedOperationException();
- }
-
- @Override
- public ByteBuffer[] internalNioBuffers() {
- ByteBuffer[] nioBuffers = new ByteBuffer[components.size()];
- int index = 0;
- for (Component component : components) {
- nioBuffers[index++] = component.buf.unsafe().internalNioBuffer();
- }
- return nioBuffers;
- }
-
- @Override
- public void discardSomeReadBytes() {
- discardReadComponents();
+ public CompositeByteBuf discardSomeReadBytes() {
+ return discardReadComponents();
}
@Override
@@ -1566,33 +1547,30 @@ public void free() {
}
@Override
- public void suspendIntermediaryDeallocations() {
+ public CompositeByteBuf suspendIntermediaryDeallocations() {
if (suspendedDeallocations == null) {
suspendedDeallocations = new ArrayDeque<ByteBuf>(2);
}
+ return this;
}
@Override
- public void resumeIntermediaryDeallocations() {
+ public CompositeByteBuf resumeIntermediaryDeallocations() {
if (suspendedDeallocations == null) {
- return;
+ return this;
}
Queue<ByteBuf> suspendedDeallocations = this.suspendedDeallocations;
this.suspendedDeallocations = null;
for (ByteBuf buf: suspendedDeallocations) {
- buf.unsafe().free();
+ buf.free();
}
+ return this;
}
@Override
public ByteBuf unwrap() {
return null;
}
-
- @Override
- public Unsafe unsafe() {
- return this;
- }
}
View
9 buffer/src/main/java/io/netty/buffer/DefaultMessageBuf.java
@@ -15,12 +15,10 @@
*/
package io.netty.buffer;
-import io.netty.buffer.ChannelBuf.Unsafe;
-
import java.util.ArrayDeque;
import java.util.Collection;
-final class DefaultMessageBuf<T> extends ArrayDeque<T> implements MessageBuf<T>, Unsafe {
+final class DefaultMessageBuf<T> extends ArrayDeque<T> implements MessageBuf<T> {
private static final long serialVersionUID = 1229808623624907552L;
@@ -66,11 +64,6 @@ public int drainTo(Collection<? super T> c, int maxElements) {
}
@Override
- public Unsafe unsafe() {
- return this;
- }
-
- @Override
public boolean isFreed() {
return freed;
}
View
34 buffer/src/main/java/io/netty/buffer/DuplicatedByteBuf.java
@@ -15,8 +15,6 @@
*/
package io.netty.buffer;
-import io.netty.buffer.ByteBuf.Unsafe;
-
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -31,7 +29,7 @@
* parent. It is recommended to use {@link ByteBuf#duplicate()} instead
* of calling the constructor explicitly.
*/
-public class DuplicatedByteBuf extends AbstractByteBuf implements Unsafe {
+public class DuplicatedByteBuf extends AbstractByteBuf {
private final ByteBuf buffer;
@@ -235,37 +233,23 @@ public ByteBuffer nioBuffer(int index, int length) {
}
@Override
- public ByteBuffer internalNioBuffer() {
- return buffer.unsafe().internalNioBuffer();
- }
-
- @Override
- public ByteBuffer[] internalNioBuffers() {
- return buffer.unsafe().internalNioBuffers();
+ public boolean isFreed() {
+ return buffer.isFreed();
}
@Override
- public void discardSomeReadBytes() {
- throw new UnsupportedOperationException();
+ public void free() {
+ throw new UnsupportedOperationException("derived");
}
@Override
- public boolean isFreed() {
- return buffer.unsafe().isFreed();
+ public ByteBuf suspendIntermediaryDeallocations() {
+ throw new UnsupportedOperationException("derived");
}
@Override
- public void free() { }
-
- @Override
- public void suspendIntermediaryDeallocations() { }
-
- @Override
- public void resumeIntermediaryDeallocations() { }
-
- @Override
- public Unsafe unsafe() {
- return this;
+ public ByteBuf resumeIntermediaryDeallocations() {
+ throw new UnsupportedOperationException("derived");
}
}
View
40 buffer/src/main/java/io/netty/buffer/IllegalMemoryAccessException.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2012 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;
+
+/**
+ * A {@link RuntimeException} raised by a {@link ByteBuf} where a user requested an operation on a freed
+ * {@link ByteBuf}.
+ */
+public class IllegalMemoryAccessException extends RuntimeException {
+
+ private static final long serialVersionUID = -6734326916218551327L;
+
+ public IllegalMemoryAccessException() { }
+
+ public IllegalMemoryAccessException(String message) {
+ super(message);
+ }
+
+ public IllegalMemoryAccessException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public IllegalMemoryAccessException(Throwable cause) {
+ super(cause);
+ }
+}
View
40 buffer/src/main/java/io/netty/buffer/PooledByteBuf.java
@@ -16,14 +16,12 @@
package io.netty.buffer;
-import io.netty.buffer.ByteBuf.Unsafe;
-
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayDeque;
import java.util.Queue;
-abstract class PooledByteBuf<T> extends AbstractByteBuf implements Unsafe {
+abstract class PooledByteBuf<T> extends AbstractByteBuf {
protected PoolChunk<T> chunk;
protected long handle;
@@ -85,13 +83,7 @@ public ByteBuf unwrap() {
return null;
}
- @Override
- public Unsafe unsafe() {
- return this;
- }
-
- @Override
- public ByteBuffer internalNioBuffer() {
+ protected ByteBuffer internalNioBuffer() {
ByteBuffer tmpNioBuf = this.tmpNioBuf;
if (tmpNioBuf == null) {
this.tmpNioBuf = tmpNioBuf = newInternalNioBuffer(memory);
@@ -99,50 +91,34 @@ public ByteBuffer internalNioBuffer() {
return tmpNioBuf;
}
- @Override
- public ByteBuffer[] internalNioBuffers() {
- return new ByteBuffer[] { internalNioBuffer() };
- }
-
protected abstract ByteBuffer newInternalNioBuffer(T memory);
@Override
- public void discardSomeReadBytes() {
- final int readerIndex = readerIndex();
- if (readerIndex == writerIndex()) {
- discardReadBytes();
- return;
- }
-
- if (readerIndex > 0 && readerIndex >= capacity() >>> 1) {
- discardReadBytes();
- }
- }
-
- @Override
- public void suspendIntermediaryDeallocations() {
+ public ByteBuf suspendIntermediaryDeallocations() {
assert !isFreed();
if (suspendedDeallocations == null) {
suspendedDeallocations = new ArrayDeque<Allocation<T>>(2);
}
+ return this;
}
@Override
- public void resumeIntermediaryDeallocations() {
+ public ByteBuf resumeIntermediaryDeallocations() {
if (suspendedDeallocations == null) {
- return;
+ return this;
}
Queue<Allocation<T>> suspendedDeallocations = this.suspendedDeallocations;
this.suspendedDeallocations = null;
if (suspendedDeallocations.isEmpty()) {
- return;
+ return this;
}
for (Allocation<T> a: suspendedDeallocations) {
chunk.arena.free(a.chunk, a.handle);
}
+ return this;
}
@Override
View
9 buffer/src/main/java/io/netty/buffer/QueueBackedMessageBuf.java
@@ -15,13 +15,11 @@
*/
package io.netty.buffer;
-import io.netty.buffer.ChannelBuf.Unsafe;
-
import java.util.Collection;
import java.util.Iterator;
import java.util.Queue;
-final class QueueBackedMessageBuf<T> implements MessageBuf<T>, Unsafe {
+final class QueueBackedMessageBuf<T> implements MessageBuf<T> {
private final Queue<T> queue;
private boolean freed;
@@ -157,11 +155,6 @@ public int drainTo(Collection<? super T> c, int maxElements) {
}
@Override
- public Unsafe unsafe() {
- return this;
- }
-
- @Override
public boolean isFreed() {
return freed;
}
View
34 buffer/src/main/java/io/netty/buffer/ReadOnlyByteBuf.java
@@ -15,8 +15,6 @@
*/
package io.netty.buffer;
-import io.netty.buffer.ByteBuf.Unsafe;
-
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -31,7 +29,7 @@
* recommended to use {@link Unpooled#unmodifiableBuffer(ByteBuf)}
* instead of calling the constructor explicitly.
*/
-public class ReadOnlyByteBuf extends AbstractByteBuf implements Unsafe {
+public class ReadOnlyByteBuf extends AbstractByteBuf {
private final ByteBuf buffer;
@@ -233,36 +231,22 @@ public ByteBuf capacity(int newCapacity) {
}
@Override
- public ByteBuffer internalNioBuffer() {
- return buffer.unsafe().internalNioBuffer();
- }
-
- @Override
- public ByteBuffer[] internalNioBuffers() {
- return buffer.unsafe().internalNioBuffers();
+ public boolean isFreed() {
+ return buffer.isFreed();
}
@Override
- public void discardSomeReadBytes() {
- throw new UnsupportedOperationException();
+ public void free() {
+ throw new UnsupportedOperationException("derived");
}
@Override
- public boolean isFreed() {
- return buffer.unsafe().isFreed();
+ public ByteBuf suspendIntermediaryDeallocations() {
+ throw new UnsupportedOperationException("derived");
}
@Override
- public void free() { }
-
- @Override
- public void suspendIntermediaryDeallocations() { }
-
- @Override
- public void resumeIntermediaryDeallocations() { }
-
- @Override
- public Unsafe unsafe() {
- return this;
+ public ByteBuf resumeIntermediaryDeallocations() {
+ throw new UnsupportedOperationException("derived");
}
}
View
34 buffer/src/main/java/io/netty/buffer/SlicedByteBuf.java
@@ -15,8 +15,6 @@
*/
package io.netty.buffer;
-import io.netty.buffer.ByteBuf.Unsafe;
-
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -32,7 +30,7 @@
* {@link ByteBuf#slice(int, int)} instead of calling the constructor
* explicitly.
*/
-public class SlicedByteBuf extends AbstractByteBuf implements Unsafe {
+public class SlicedByteBuf extends AbstractByteBuf {
private final ByteBuf buffer;
private final int adjustment;
@@ -307,36 +305,22 @@ private void checkIndex(int startIndex, int length) {
}
@Override
- public ByteBuffer internalNioBuffer() {
- return buffer.nioBuffer(adjustment, length);
- }
-
- @Override
- public ByteBuffer[] internalNioBuffers() {
- return buffer.nioBuffers(adjustment, length);
+ public boolean isFreed() {
+ return buffer.isFreed();
}
@Override
- public void discardSomeReadBytes() {
- throw new UnsupportedOperationException();
+ public void free() {
+ throw new UnsupportedOperationException("derived");
}
@Override
- public boolean isFreed() {
- return buffer.unsafe().isFreed();
+ public ByteBuf suspendIntermediaryDeallocations() {
+ throw new UnsupportedOperationException("derived");
}
@Override
- public void free() { }
-
- @Override
- public void suspendIntermediaryDeallocations() { }
-
- @Override
- public void resumeIntermediaryDeallocations() { }
-
- @Override
- public Unsafe unsafe() {
- return this;
+ public ByteBuf resumeIntermediaryDeallocations() {
+ throw new UnsupportedOperationException("derived");
}
}
View
33 buffer/src/main/java/io/netty/buffer/SwappedByteBuf.java
@@ -183,6 +183,12 @@ public ByteBuf discardReadBytes() {
}
@Override
+ public ByteBuf discardSomeReadBytes() {
+ buf.discardSomeReadBytes();
+ return this;
+ }
+
+ @Override
public ByteBuf ensureWritableBytes(int writableBytes) {
buf.ensureWritableBytes(writableBytes);
return this;
@@ -769,6 +775,28 @@ public String toString(int index, int length, Charset charset) {
}
@Override
+ public ByteBuf suspendIntermediaryDeallocations() {
+ buf.suspendIntermediaryDeallocations();
+ return this;
+ }
+
+ @Override
+ public ByteBuf resumeIntermediaryDeallocations() {
+ buf.resumeIntermediaryDeallocations();
+ return this;
+ }
+
+ @Override
+ public boolean isFreed() {
+ return buf.isFreed();
+ }
+
+ @Override
+ public void free() {
+ buf.free();
+ }
+
+ @Override
public int hashCode() {
return buf.hashCode();
}
@@ -793,9 +821,4 @@ public int compareTo(ByteBuf buffer) {
public String toString() {
return "Swapped(" + buf.toString() + ')';
}
-
- @Override
- public Unsafe unsafe() {
- return buf.unsafe();
- }
}
View
37 buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java
@@ -15,7 +15,6 @@
*/
package io.netty.buffer;
-import io.netty.buffer.ByteBuf.Unsafe;
import sun.misc.Cleaner;
import java.io.IOException;
@@ -36,7 +35,7 @@
* constructor explicitly.
*/
@SuppressWarnings("restriction")
-final class UnpooledDirectByteBuf extends AbstractByteBuf implements Unsafe {
+final class UnpooledDirectByteBuf extends AbstractByteBuf {
private static final Field CLEANER_FIELD;
@@ -477,8 +476,7 @@ public ByteBuf copy(int index, int length) {
return new UnpooledDirectByteBuf(alloc(), dst, maxCapacity());
}
- @Override
- public ByteBuffer internalNioBuffer() {
+ private ByteBuffer internalNioBuffer() {
ByteBuffer tmpNioBuf = this.tmpNioBuf;
if (tmpNioBuf == null) {
this.tmpNioBuf = tmpNioBuf = buffer.duplicate();
@@ -487,24 +485,6 @@ public ByteBuffer internalNioBuffer() {
}
@Override
- public ByteBuffer[] internalNioBuffers() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void discardSomeReadBytes() {
- final int readerIndex = readerIndex();
- if (readerIndex == writerIndex()) {
- discardReadBytes();
- return;
- }
-
- if (readerIndex > 0 && readerIndex >= capacity >>> 1) {
- discardReadBytes();
- }
- }
-
- @Override
public boolean isFreed() {
return freed;
}
@@ -525,16 +505,17 @@ public void free() {
}
@Override
- public void suspendIntermediaryDeallocations() {
+ public ByteBuf suspendIntermediaryDeallocations() {
if (suspendedDeallocations == null) {
suspendedDeallocations = new ArrayDeque<ByteBuffer>(2);
}
+ return this;
}
@Override
- public void resumeIntermediaryDeallocations() {
+ public ByteBuf resumeIntermediaryDeallocations() {
if (suspendedDeallocations == null) {
- return;
+ return this;
}
Queue<ByteBuffer> suspendedDeallocations = this.suspendedDeallocations;
@@ -543,15 +524,11 @@ public void resumeIntermediaryDeallocations() {
for (ByteBuffer buf: suspendedDeallocations) {
freeDirect(buf);
}
+ return this;
}
@Override
public ByteBuf unwrap() {
return null;
}
-
- @Override
- public Unsafe unsafe() {
- return this;
- }
}
View
38 buffer/src/main/java/io/netty/buffer/UnpooledHeapByteBuf.java
@@ -15,8 +15,6 @@
*/
package io.netty.buffer;
-import io.netty.buffer.ByteBuf.Unsafe;
-
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -29,7 +27,7 @@
/**
* Big endian Java heap buffer implementation.
*/
-final class UnpooledHeapByteBuf extends AbstractByteBuf implements Unsafe {
+final class UnpooledHeapByteBuf extends AbstractByteBuf {
private final ByteBufAllocator alloc;
private byte[] array;
@@ -345,8 +343,7 @@ public ByteBuf copy(int index, int length) {
return new UnpooledHeapByteBuf(alloc(), copiedArray, maxCapacity());
}
- @Override
- public ByteBuffer internalNioBuffer() {
+ private ByteBuffer internalNioBuffer() {
ByteBuffer tmpNioBuf = this.tmpNioBuf;
if (tmpNioBuf == null) {
this.tmpNioBuf = tmpNioBuf = ByteBuffer.wrap(array);
@@ -355,24 +352,6 @@ public ByteBuffer internalNioBuffer() {
}
@Override
- public ByteBuffer[] internalNioBuffers() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void discardSomeReadBytes() {
- final int readerIndex = readerIndex();
- if (readerIndex == writerIndex()) {
- discardReadBytes();
- return;
- }
-
- if (readerIndex > 0 && readerIndex >= capacity() >>> 1) {
- discardReadBytes();
- }
- }
-
- @Override
public boolean isFreed() {
return freed;
}
@@ -383,18 +362,17 @@ public void free() {
}
@Override
- public void suspendIntermediaryDeallocations() { }
+ public ByteBuf suspendIntermediaryDeallocations() {
+ return this;
+ }
@Override
- public void resumeIntermediaryDeallocations() { }
+ public ByteBuf resumeIntermediaryDeallocations() {
+ return this;
+ }
@Override
public ByteBuf unwrap() {
return null;
}
-
- @Override
- public Unsafe unsafe() {
- return this;
- }
}
View
2  codec/src/main/java/io/netty/handler/codec/ByteToByteDecoder.java
@@ -70,7 +70,7 @@ private void callDecode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) {
}
}
- in.unsafe().discardSomeReadBytes();
+ in.discardSomeReadBytes();
if (out.readableBytes() > oldOutSize) {
ctx.fireInboundBufferUpdated();
}
View
2  codec/src/main/java/io/netty/handler/codec/ByteToByteEncoder.java
@@ -43,7 +43,7 @@ public void flush(ChannelHandlerContext ctx, ChannelFuture future) throws Except
}
}
- in.unsafe().discardSomeReadBytes();
+ in.discardSomeReadBytes();
ctx.flush(future);
}
View
4 codec/src/main/java/io/netty/handler/codec/ByteToMessageDecoder.java
@@ -91,7 +91,7 @@ protected void callDecode(ChannelHandlerContext ctx) {
break;
}
} catch (Throwable t) {
- in.unsafe().discardSomeReadBytes();
+ in.discardSomeReadBytes();
if (decoded) {
decoded = false;
@@ -106,7 +106,7 @@ protected void callDecode(ChannelHandlerContext ctx) {
}
}
- in.unsafe().discardSomeReadBytes();
+ in.discardSomeReadBytes();
if (decoded) {
ctx.fireInboundBufferUpdated();
View
4 codec/src/main/java/io/netty/handler/codec/MessageToMessageCodec.java
@@ -75,7 +75,7 @@ protected MessageToMessageCodec(
@Override
public void freeInboundBuffer(ChannelHandlerContext ctx, ChannelBuf buf) throws Exception {
- buf.unsafe().free();
+ buf.free();
}
@Override
@@ -91,7 +91,7 @@ public void inboundBufferUpdated(
@Override
public void freeOutboundBuffer(ChannelHandlerContext ctx, ChannelBuf buf) throws Exception {
- buf.unsafe().free();
+ buf.free();
}
@Override
View
2  codec/src/main/java/io/netty/handler/codec/ReplayingDecoder.java
@@ -458,7 +458,7 @@ protected void callDecode(ChannelHandlerContext ctx) {
private void fireInboundBufferUpdated(ChannelHandlerContext ctx, ByteBuf in) {
final int oldReaderIndex = in.readerIndex();
- in.unsafe().discardSomeReadBytes();
+ in.discardSomeReadBytes();
final int newReaderIndex = in.readerIndex();
checkpoint -= oldReaderIndex - newReaderIndex;
ctx.fireInboundBufferUpdated();
View
26 codec/src/main/java/io/netty/handler/codec/ReplayingDecoderBuffer.java
@@ -16,7 +16,6 @@
package io.netty.handler.codec;
import io.netty.buffer.ByteBuf;
-import io.netty.buffer.ByteBuf.Unsafe;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufIndexFinder;
import io.netty.buffer.ChannelBufType;
@@ -32,7 +31,7 @@
import java.nio.channels.ScatteringByteChannel;
import java.nio.charset.Charset;
-final class ReplayingDecoderBuffer implements ByteBuf, Unsafe {
+final class ReplayingDecoderBuffer implements ByteBuf {
private static final Signal REPLAY = ReplayingDecoder.REPLAY;
@@ -844,23 +843,13 @@ private void checkReadableBytes(int readableBytes) {
}
@Override
- public ByteBuffer internalNioBuffer() {
- throw new UnreplayableOperationException();
- }
-
- @Override
- public ByteBuffer[] internalNioBuffers() {
- throw new UnreplayableOperationException();
- }
-
- @Override
- public void discardSomeReadBytes() {
+ public ByteBuf discardSomeReadBytes() {
throw new UnreplayableOperationException();
}
@Override
public boolean isFreed() {
- return buffer.unsafe().isFreed();
+ return buffer.isFreed();
}
@Override
@@ -869,12 +858,12 @@ public void free() {
}
@Override
- public void suspendIntermediaryDeallocations() {
+ public ByteBuf suspendIntermediaryDeallocations() {
throw new UnreplayableOperationException();
}
@Override
- public void resumeIntermediaryDeallocations() {
+ public ByteBuf resumeIntermediaryDeallocations() {
throw new UnreplayableOperationException();
}
@@ -882,9 +871,4 @@ public void resumeIntermediaryDeallocations() {
public ByteBuf unwrap() {
throw new UnreplayableOperationException();
}
-
- @Override
- public Unsafe unsafe() {
- return this;
- }
}
View
2  codec/src/main/java/io/netty/handler/codec/serialization/CompatibleObjectEncoder.java
@@ -97,7 +97,7 @@ public void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Ex
oos.reset();
// Also discard the byproduct to avoid OOM on the sending side.
- out.unsafe().discardSomeReadBytes();
+ out.discardSomeReadBytes();
}
}
View
4 handler/src/main/java/io/netty/handler/logging/ByteLoggingHandler.java
@@ -118,12 +118,12 @@ public ByteBuf newInboundBuffer(ChannelHandlerContext ctx) throws Exception {
@Override
public void freeInboundBuffer(ChannelHandlerContext ctx, ChannelBuf buf) throws Exception {
- buf.unsafe().free();
+ buf.free();
}
@Override
public void freeOutboundBuffer(ChannelHandlerContext ctx, ChannelBuf buf) throws Exception {
- buf.unsafe().free();
+ buf.free();
}
@Override
View
8 handler/src/main/java/io/netty/handler/ssl/SslHandler.java
@@ -384,12 +384,12 @@ public ByteBuf newInboundBuffer(ChannelHandlerContext ctx) throws Exception {
@Override
public void freeInboundBuffer(ChannelHandlerContext ctx, ChannelBuf buf) throws Exception {
- buf.unsafe().free();
+ buf.free();
}
@Override
public void freeOutboundBuffer(ChannelHandlerContext ctx, ChannelBuf buf) throws Exception {
- buf.unsafe().free();
+ buf.free();
}
@Override
@@ -409,7 +409,7 @@ public void flush(final ChannelHandlerContext ctx, ChannelFuture future) throws
final ByteBuf in = ctx.outboundByteBuffer();
final ByteBuf out = ctx.nextOutboundByteBuffer();
- out.unsafe().discardSomeReadBytes();
+ out.discardSomeReadBytes();
// Do not encrypt the first write request if this handler is
// created with startTLS flag turned on.
@@ -481,7 +481,7 @@ public void flush(final ChannelHandlerContext ctx, ChannelFuture future) throws
setHandshakeFailure(e);
throw e;
} finally {
- in.unsafe().discardSomeReadBytes();
+ in.discardSomeReadBytes();
flush0(ctx, bytesConsumed);
}
}
View
4 microbench/src/test/java/io/netty/microbench/buffer/ByteBufAllocatorBenchmark.java
@@ -51,7 +51,7 @@ protected void setUp() throws Exception {
@Override
protected void tearDown() throws Exception {
for (ByteBuf b: queue) {
- b.unsafe().free();
+ b.free();
}
queue.clear();
}
@@ -63,7 +63,7 @@ public void timeAllocAndFree(int reps) {
for (int i = 0; i < reps; i ++) {
queue.add(alloc.buffer(size));
- queue.removeFirst().unsafe().free();
+ queue.removeFirst().free();
}
}
View
2  transport/src/main/java/io/netty/channel/ChannelInboundByteHandlerAdapter.java
@@ -43,7 +43,7 @@ public final void inboundBufferUpdated(ChannelHandlerContext ctx) throws Excepti
inboundBufferUpdated(ctx, in);
} finally {
if (!in.readable()) {
- in.unsafe().discardSomeReadBytes();
+ in.discardReadBytes();
}
}
}
View
2  transport/src/main/java/io/netty/channel/ChannelInboundHandlerAdapter.java
@@ -23,7 +23,7 @@
@Override
public void freeInboundBuffer(ChannelHandlerContext ctx, ChannelBuf buf) throws Exception {
- buf.unsafe().free();
+ buf.free();
}
/**
View
2  transport/src/main/java/io/netty/channel/ChannelOutboundHandlerAdapter.java
@@ -21,7 +21,7 @@
extends ChannelOperationHandlerAdapter implements ChannelOutboundHandler {
@Override
public void freeOutboundBuffer(ChannelHandlerContext ctx, ChannelBuf buf) throws Exception {
- buf.unsafe().free();
+ buf.free();
}
@Override
View
5 transport/src/main/java/io/netty/channel/DefaultChannelHandlerContext.java
@@ -1237,8 +1237,7 @@ private void fill() {
data = ctx.alloc().buffer(dataLen, dataLen);
}
- byteBuf.readBytes(data, dataLen);
- byteBuf.unsafe().discardSomeReadBytes();
+ byteBuf.readBytes(data, dataLen).discardSomeReadBytes();
exchangeBuf.add(data);
}
@@ -1259,7 +1258,7 @@ private void flush(ByteBuf out) {
try {
out.writeBytes(data);
} finally {
- data.unsafe().free();
+ data.free();
}
}
}
View
2  transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java
@@ -1472,7 +1472,7 @@ public ChannelBuf newOutboundBuffer(ChannelHandlerContext ctx) throws Exception
@Override
public void freeOutboundBuffer(ChannelHandlerContext ctx, ChannelBuf buf) {
- buf.unsafe().free();
+ buf.free();
}
@Override
View
8 transport/src/main/java/io/netty/channel/socket/aio/AioSocketChannel.java
@@ -254,7 +254,7 @@ protected void doFlushByteBuffer(ByteBuf buf) throws Exception {
try {
if (buf.readable()) {
for (;;) {
- if (buf.unsafe().isFreed()) {
+ if (buf.isFreed()) {
break;
}
// Ensure the readerIndex of the buffer is 0 before beginning an async write.
@@ -280,7 +280,7 @@ protected void doFlushByteBuffer(ByteBuf buf) throws Exception {
if (asyncWriteInProgress) {
// JDK decided to write data (or notify handler) later.
- buf.unsafe().suspendIntermediaryDeallocations();
+ buf.suspendIntermediaryDeallocations();
break;
}
@@ -316,7 +316,7 @@ private void beginRead() {
try {
for (;;) {
ByteBuf byteBuf = pipeline().inboundByteBuffer();
- if (byteBuf.unsafe().isFreed()) {
+ if (byteBuf.isFreed()) {
break;
}
@@ -363,7 +363,7 @@ protected void completed0(T result, AioSocketChannel channel) {
channel.asyncWriteInProgress = false;
ByteBuf buf = channel.unsafe().directOutboundContext().outboundByteBuffer();
- buf.unsafe().resumeIntermediaryDeallocations();
+ buf.resumeIntermediaryDeallocations();
int writtenBytes = result.intValue();
if (writtenBytes > 0) {
View
6 transport/src/test/java/io/netty/channel/local/LocalTransportThreadModelTest.java
@@ -412,7 +412,7 @@ public void freeInboundBuffer(ChannelHandlerContext ctx, ChannelBuf buf) throws
@Override
public void freeOutboundBuffer(ChannelHandlerContext ctx, ChannelBuf buf) {
- buf.unsafe().free();
+ buf.free();
}
@Override
@@ -464,7 +464,7 @@ public void flush(ChannelHandlerContext ctx,
out.add(msg);
}
}
- in.unsafe().discardSomeReadBytes();
+ in.discardSomeReadBytes();
if (swallow) {
future.setSuccess();
} else {
@@ -507,7 +507,7 @@ public ByteBuf newInboundBuffer(
@Override
public void freeInboundBuffer(ChannelHandlerContext ctx, ChannelBuf buf) throws Exception {
- buf.unsafe().free();
+ buf.free();
}
@Override
Please sign in to comment.
Something went wrong with that request. Please try again.