From 5a4a59406b759b042b514b73f23c06e4360f39e7 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Fri, 14 Dec 2012 12:20:33 +0900 Subject: [PATCH] Merge ByteBuf.hasNioBuffer() and hasNioBuffers() - Fixes #797 --- .../java/io/netty/buffer/AbstractByteBuf.java | 18 +----- .../main/java/io/netty/buffer/ByteBuf.java | 55 ++++++++++--------- .../netty/buffer/DefaultCompositeByteBuf.java | 16 ++---- .../io/netty/buffer/DuplicatedByteBuf.java | 9 +-- .../io/netty/buffer/PooledDirectByteBuf.java | 9 +-- .../io/netty/buffer/PooledHeapByteBuf.java | 9 +-- .../java/io/netty/buffer/ReadOnlyByteBuf.java | 9 +-- .../java/io/netty/buffer/SlicedByteBuf.java | 9 +-- .../java/io/netty/buffer/SwappedByteBuf.java | 25 +-------- .../netty/buffer/UnpooledDirectByteBuf.java | 11 +--- .../io/netty/buffer/UnpooledHeapByteBuf.java | 11 +--- .../buffer/AbstractChannelBufferTest.java | 18 +++--- .../AbstractCompositeChannelBufferTest.java | 2 +- .../io/netty/buffer/ChannelBuffersTest.java | 2 +- .../http/multipart/AbstractDiskHttpData.java | 2 +- .../handler/codec/ReplayingDecoderBuffer.java | 26 +-------- .../channel/socket/aio/AioSocketChannel.java | 20 +++---- .../socket/nio/NioDatagramChannel.java | 2 +- .../channel/socket/nio/NioSctpChannel.java | 2 +- .../channel/socket/oio/OioSctpChannel.java | 2 +- 20 files changed, 80 insertions(+), 177 deletions(-) diff --git a/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java b/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java index 20666e39ab..78d6dcbe21 100644 --- a/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java @@ -1,19 +1,3 @@ -/* - * 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. - */ - /* * Copyright 2012 The Netty Project * @@ -829,7 +813,7 @@ public abstract class AbstractByteBuf implements ByteBuf { } ByteBuffer nioBuffer; - if (hasNioBuffer()) { + if (nioBufferCount() == 1) { nioBuffer = nioBuffer(index, length); } else { nioBuffer = ByteBuffer.allocate(length); diff --git a/buffer/src/main/java/io/netty/buffer/ByteBuf.java b/buffer/src/main/java/io/netty/buffer/ByteBuf.java index 0d33b04f3d..277613d25c 100644 --- a/buffer/src/main/java/io/netty/buffer/ByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/ByteBuf.java @@ -1,19 +1,3 @@ -/* - * 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. - */ - /* * Copyright 2012 The Netty Project * @@ -1715,22 +1699,35 @@ public interface ByteBuf extends ChannelBuf, Comparable { ByteBuf duplicate(); /** - * Returns {@code true} if and only if {@link #nioBuffer()} method will not fail. + * 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. + * + * @return {@code -1} if this buffer has no underlying {@link ByteBuffer}. + * the number of the underlying {@link ByteBuffer}s if this buffer has at least one undelying + * {@link ByteBuffer}. Note that this method does not return {@code 0} to avoid confusion. + * + * @see #nioBuffer() + * @see #nioBuffer(int, int) + * @see #nioBuffers() + * @see #nioBuffers(int, int) */ - boolean hasNioBuffer(); + int nioBufferCount(); /** * Exposes this buffer's readable bytes as an NIO {@link ByteBuffer}. The returned buffer * shares the content with this buffer, while changing the position and limit of the returned * NIO buffer does not affect the indexes and marks of this buffer. This method is identical - * to {@code buf.asByteBuffer(buf.readerIndex(), buf.readableBytes())}. This method does not + * to {@code buf.nioBuffer(buf.readerIndex(), buf.readableBytes())}. This method does not * modify {@code readerIndex} or {@code writerIndex} of this buffer. Please note that the * returned NIO buffer will not see the changes of this buffer if this buffer is a dynamic * buffer and it adjusted its capacity. * - * * @throws UnsupportedOperationException * if this buffer cannot create a {@link ByteBuffer} that shares the content with itself + * + * @see #nioBufferCount() + * @see #nioBuffers() + * @see #nioBuffers(int, int) */ ByteBuffer nioBuffer(); @@ -1744,14 +1741,13 @@ public interface ByteBuf extends ChannelBuf, Comparable { * * @throws UnsupportedOperationException * if this buffer cannot create a {@link ByteBuffer} that shares the content with itself + * + * @see #nioBufferCount() + * @see #nioBuffers() + * @see #nioBuffers(int, int) */ ByteBuffer nioBuffer(int index, int length); - /** - * Returns {@code true} if and only if {@link #nioBuffers()} method will not fail. - */ - boolean hasNioBuffers(); - /** * Exposes this buffer's readable bytes as an NIO {@link ByteBuffer}'s. The returned buffer * shares the content with this buffer, while changing the position and limit of the returned @@ -1763,6 +1759,10 @@ public interface ByteBuf extends ChannelBuf, Comparable { * * @throws UnsupportedOperationException * if this buffer cannot create a {@link ByteBuffer} that shares the content with itself + * + * @see #nioBufferCount() + * @see #nioBuffer() + * @see #nioBuffer(int, int) */ ByteBuffer[] nioBuffers(); @@ -1774,9 +1774,12 @@ public interface ByteBuf extends ChannelBuf, Comparable { * returned NIO buffer will not see the changes of this buffer if this buffer is a dynamic * buffer and it adjusted its capacity. * - * * @throws UnsupportedOperationException * if this buffer cannot create a {@link ByteBuffer} that shares the content with itself + * + * @see #nioBufferCount() + * @see #nioBuffer() + * @see #nioBuffer(int, int) */ ByteBuffer[] nioBuffers(int index, int length); diff --git a/buffer/src/main/java/io/netty/buffer/DefaultCompositeByteBuf.java b/buffer/src/main/java/io/netty/buffer/DefaultCompositeByteBuf.java index 06293b2339..45fdb41275 100644 --- a/buffer/src/main/java/io/netty/buffer/DefaultCompositeByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/DefaultCompositeByteBuf.java @@ -1057,11 +1057,8 @@ public class DefaultCompositeByteBuf extends AbstractByteBuf implements Composit } @Override - public boolean hasNioBuffer() { - if (components.size() == 1) { - return components.get(0).buf.hasNioBuffer(); - } - return false; + public int nioBufferCount() { + return components.size(); } @Override @@ -1087,11 +1084,6 @@ public class DefaultCompositeByteBuf extends AbstractByteBuf implements Composit return merged; } - @Override - public boolean hasNioBuffers() { - return true; - } - @Override public ByteBuffer[] nioBuffers(int index, int length) { if (index + length > capacity()) { @@ -1114,7 +1106,7 @@ public class DefaultCompositeByteBuf extends AbstractByteBuf implements Composit ByteBuf s = c.buf; int adjustment = c.offset; int localLength = Math.min(length, s.capacity() - (index - adjustment)); - buffers.add(toNioBuffer(s, index - adjustment, localLength)); + buffers.add(s.nioBuffer(index - adjustment, localLength)); index += localLength; length -= localLength; i ++; @@ -1124,7 +1116,7 @@ public class DefaultCompositeByteBuf extends AbstractByteBuf implements Composit } private static ByteBuffer toNioBuffer(ByteBuf buf, int index, int length) { - if (buf.hasNioBuffer()) { + if (buf.nioBufferCount() == 1) { return buf.nioBuffer(index, length); } else { return buf.copy(index, length).nioBuffer(0, length); diff --git a/buffer/src/main/java/io/netty/buffer/DuplicatedByteBuf.java b/buffer/src/main/java/io/netty/buffer/DuplicatedByteBuf.java index 4f011f5f71..e4ee31c150 100644 --- a/buffer/src/main/java/io/netty/buffer/DuplicatedByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/DuplicatedByteBuf.java @@ -220,8 +220,8 @@ public class DuplicatedByteBuf extends AbstractByteBuf implements Unsafe { } @Override - public boolean hasNioBuffer() { - return buffer.hasNioBuffer(); + public int nioBufferCount() { + return buffer.nioBufferCount(); } @Override @@ -229,11 +229,6 @@ public class DuplicatedByteBuf extends AbstractByteBuf implements Unsafe { return buffer.nioBuffer(index, length); } - @Override - public boolean hasNioBuffers() { - return buffer.hasNioBuffers(); - } - @Override public ByteBuffer[] nioBuffers(int index, int length) { return buffer.nioBuffers(index, length); diff --git a/buffer/src/main/java/io/netty/buffer/PooledDirectByteBuf.java b/buffer/src/main/java/io/netty/buffer/PooledDirectByteBuf.java index b2bb872127..132ce6f85e 100644 --- a/buffer/src/main/java/io/netty/buffer/PooledDirectByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/PooledDirectByteBuf.java @@ -252,8 +252,8 @@ final class PooledDirectByteBuf extends PooledByteBuf { } @Override - public boolean hasNioBuffer() { - return true; + public int nioBufferCount() { + return 1; } @Override @@ -263,11 +263,6 @@ final class PooledDirectByteBuf extends PooledByteBuf { return ((ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length)).slice(); } - @Override - public boolean hasNioBuffers() { - return true; - } - @Override public ByteBuffer[] nioBuffers(int index, int length) { return new ByteBuffer[] { nioBuffer(index, length) }; diff --git a/buffer/src/main/java/io/netty/buffer/PooledHeapByteBuf.java b/buffer/src/main/java/io/netty/buffer/PooledHeapByteBuf.java index 244ccd691d..e9eed8b5da 100644 --- a/buffer/src/main/java/io/netty/buffer/PooledHeapByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/PooledHeapByteBuf.java @@ -222,8 +222,8 @@ final class PooledHeapByteBuf extends PooledByteBuf { } @Override - public boolean hasNioBuffer() { - return true; + public int nioBufferCount() { + return 1; } @Override @@ -233,11 +233,6 @@ final class PooledHeapByteBuf extends PooledByteBuf { return ((ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length)).slice(); } - @Override - public boolean hasNioBuffers() { - return true; - } - @Override public ByteBuffer[] nioBuffers(int index, int length) { return new ByteBuffer[] { nioBuffer(index, length) }; diff --git a/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBuf.java b/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBuf.java index 04ef320603..c816503155 100644 --- a/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBuf.java @@ -208,8 +208,8 @@ public class ReadOnlyByteBuf extends AbstractByteBuf implements Unsafe { } @Override - public boolean hasNioBuffer() { - return buffer.hasNioBuffer(); + public int nioBufferCount() { + return buffer.nioBufferCount(); } @Override @@ -217,11 +217,6 @@ public class ReadOnlyByteBuf extends AbstractByteBuf implements Unsafe { return buffer.nioBuffer(index, length).asReadOnlyBuffer(); } - @Override - public boolean hasNioBuffers() { - return buffer.hasNioBuffers(); - } - @Override public ByteBuffer[] nioBuffers(int index, int length) { return buffer.nioBuffers(index, length); diff --git a/buffer/src/main/java/io/netty/buffer/SlicedByteBuf.java b/buffer/src/main/java/io/netty/buffer/SlicedByteBuf.java index f680bf4f04..f653087378 100644 --- a/buffer/src/main/java/io/netty/buffer/SlicedByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/SlicedByteBuf.java @@ -269,8 +269,8 @@ public class SlicedByteBuf extends AbstractByteBuf implements Unsafe { } @Override - public boolean hasNioBuffer() { - return buffer.hasNioBuffer(); + public int nioBufferCount() { + return buffer.nioBufferCount(); } @Override @@ -279,11 +279,6 @@ public class SlicedByteBuf extends AbstractByteBuf implements Unsafe { return buffer.nioBuffer(index + adjustment, length); } - @Override - public boolean hasNioBuffers() { - return buffer.hasNioBuffers(); - } - @Override public ByteBuffer[] nioBuffers(int index, int length) { checkIndex(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 ab8b0c5cba..a7aa13b986 100644 --- a/buffer/src/main/java/io/netty/buffer/SwappedByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/SwappedByteBuf.java @@ -1,19 +1,3 @@ -/* - * 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. - */ - /* * Copyright 2012 The Netty Project * @@ -727,8 +711,8 @@ public final class SwappedByteBuf implements ByteBuf { } @Override - public boolean hasNioBuffer() { - return buf.hasNioBuffer(); + public int nioBufferCount() { + return buf.nioBufferCount(); } @Override @@ -741,11 +725,6 @@ public final class SwappedByteBuf implements ByteBuf { return buf.nioBuffer(index, length).order(order); } - @Override - public boolean hasNioBuffers() { - return buf.hasNioBuffers(); - } - @Override public ByteBuffer[] nioBuffers() { ByteBuffer[] nioBuffers = buf.nioBuffers(); diff --git a/buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java b/buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java index abe5521002..2686353613 100644 --- a/buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java @@ -440,8 +440,8 @@ final class UnpooledDirectByteBuf extends AbstractByteBuf implements Unsafe { } @Override - public boolean hasNioBuffer() { - return true; + public int nioBufferCount() { + return 1; } @Override @@ -454,14 +454,9 @@ final class UnpooledDirectByteBuf extends AbstractByteBuf implements Unsafe { } } - @Override - public boolean hasNioBuffers() { - return false; - } - @Override public ByteBuffer[] nioBuffers(int index, int length) { - throw new UnsupportedOperationException(); + return new ByteBuffer[] { nioBuffer(index, length) }; } @Override diff --git a/buffer/src/main/java/io/netty/buffer/UnpooledHeapByteBuf.java b/buffer/src/main/java/io/netty/buffer/UnpooledHeapByteBuf.java index 6a146f524f..a2f4ee548b 100644 --- a/buffer/src/main/java/io/netty/buffer/UnpooledHeapByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/UnpooledHeapByteBuf.java @@ -240,8 +240,8 @@ final class UnpooledHeapByteBuf extends AbstractByteBuf implements Unsafe { } @Override - public boolean hasNioBuffer() { - return true; + public int nioBufferCount() { + return 1; } @Override @@ -250,14 +250,9 @@ final class UnpooledHeapByteBuf extends AbstractByteBuf implements Unsafe { return ByteBuffer.wrap(array, index, length); } - @Override - public boolean hasNioBuffers() { - return false; - } - @Override public ByteBuffer[] nioBuffers(int index, int length) { - throw new UnsupportedOperationException(); + return new ByteBuffer[] { nioBuffer(index, length) }; } @Override diff --git a/buffer/src/test/java/io/netty/buffer/AbstractChannelBufferTest.java b/buffer/src/test/java/io/netty/buffer/AbstractChannelBufferTest.java index 2d41886f5e..55be593b64 100644 --- a/buffer/src/test/java/io/netty/buffer/AbstractChannelBufferTest.java +++ b/buffer/src/test/java/io/netty/buffer/AbstractChannelBufferTest.java @@ -15,9 +15,11 @@ */ package io.netty.buffer; -import static io.netty.buffer.Unpooled.*; -import static org.junit.Assert.*; import io.netty.util.CharsetUtil; +import org.junit.After; +import org.junit.Assume; +import org.junit.Before; +import org.junit.Test; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -27,10 +29,8 @@ import java.util.HashSet; import java.util.Random; import java.util.Set; -import org.junit.After; -import org.junit.Assume; -import org.junit.Before; -import org.junit.Test; +import static io.netty.buffer.Unpooled.*; +import static org.junit.Assert.*; /** * An abstract test class for channel buffers @@ -1529,7 +1529,7 @@ public abstract class AbstractChannelBufferTest { @Test public void testNioBuffer1() { - Assume.assumeTrue(buffer.hasNioBuffer()); + Assume.assumeTrue(buffer.nioBufferCount() == 1); byte[] value = new byte[buffer.capacity()]; random.nextBytes(value); @@ -1541,7 +1541,7 @@ public abstract class AbstractChannelBufferTest { @Test public void testToByteBuffer2() { - Assume.assumeTrue(buffer.hasNioBuffer()); + Assume.assumeTrue(buffer.nioBufferCount() == 1); byte[] value = new byte[buffer.capacity()]; random.nextBytes(value); @@ -1555,7 +1555,7 @@ public abstract class AbstractChannelBufferTest { @Test public void testToByteBuffer3() { - Assume.assumeTrue(buffer.hasNioBuffer()); + Assume.assumeTrue(buffer.nioBufferCount() == 1); assertEquals(buffer.order(), buffer.nioBuffer().order()); } diff --git a/buffer/src/test/java/io/netty/buffer/AbstractCompositeChannelBufferTest.java b/buffer/src/test/java/io/netty/buffer/AbstractCompositeChannelBufferTest.java index b99540e25d..d183d3f281 100644 --- a/buffer/src/test/java/io/netty/buffer/AbstractCompositeChannelBufferTest.java +++ b/buffer/src/test/java/io/netty/buffer/AbstractCompositeChannelBufferTest.java @@ -216,7 +216,7 @@ public abstract class AbstractCompositeChannelBufferTest extends assertEquals(512, payload.readableBytes()); assertEquals(12 + 512, buffer.readableBytes()); - assertFalse(buffer.hasNioBuffer()); + assertEquals(2, buffer.nioBufferCount()); } @Test diff --git a/buffer/src/test/java/io/netty/buffer/ChannelBuffersTest.java b/buffer/src/test/java/io/netty/buffer/ChannelBuffersTest.java index 7779bfcafd..2c4c7aa8a5 100644 --- a/buffer/src/test/java/io/netty/buffer/ChannelBuffersTest.java +++ b/buffer/src/test/java/io/netty/buffer/ChannelBuffersTest.java @@ -49,7 +49,7 @@ public class ChannelBuffersTest { assertEquals(512, payload.readableBytes()); assertEquals(12 + 512, buffer.readableBytes()); - assertFalse(buffer.hasNioBuffer()); + assertEquals(2, buffer.nioBufferCount()); } @Test diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/AbstractDiskHttpData.java b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/AbstractDiskHttpData.java index 76a22ad938..5d080e7bb9 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/AbstractDiskHttpData.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/AbstractDiskHttpData.java @@ -133,7 +133,7 @@ public abstract class AbstractDiskHttpData extends AbstractHttpData { throw new IOException("Out of size: " + (size + localsize) + " > " + definedSize); } - ByteBuffer byteBuffer = buffer.hasNioBuffer() ? buffer.nioBuffer() : buffer.copy().nioBuffer(); + ByteBuffer byteBuffer = buffer.nioBufferCount() == 1 ? buffer.nioBuffer() : buffer.copy().nioBuffer(); int written = 0; if (file == null) { file = tempFile(); diff --git a/codec/src/main/java/io/netty/handler/codec/ReplayingDecoderBuffer.java b/codec/src/main/java/io/netty/handler/codec/ReplayingDecoderBuffer.java index 1297e4e13b..0316e1982d 100644 --- a/codec/src/main/java/io/netty/handler/codec/ReplayingDecoderBuffer.java +++ b/codec/src/main/java/io/netty/handler/codec/ReplayingDecoderBuffer.java @@ -14,22 +14,7 @@ * under the License. */ -/* - * 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.handler.codec; +ackage io.netty.handler.codec; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf.Unsafe; @@ -684,8 +669,8 @@ final class ReplayingDecoderBuffer implements ByteBuf, Unsafe { } @Override - public boolean hasNioBuffer() { - return buffer.hasNioBuffer(); + public int nioBufferCount() { + return buffer.nioBufferCount(); } @Override @@ -699,11 +684,6 @@ final class ReplayingDecoderBuffer implements ByteBuf, Unsafe { return buffer.nioBuffer(index, length); } - @Override - public boolean hasNioBuffers() { - return buffer.hasNioBuffers(); - } - @Override public ByteBuffer[] nioBuffers() { throw new UnreplayableOperationException(); diff --git a/transport/src/main/java/io/netty/channel/socket/aio/AioSocketChannel.java b/transport/src/main/java/io/netty/channel/socket/aio/AioSocketChannel.java index 2c08caf097..04676ff236 100755 --- a/transport/src/main/java/io/netty/channel/socket/aio/AioSocketChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/aio/AioSocketChannel.java @@ -258,7 +258,10 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne buf.discardReadBytes(); asyncWriteInProgress = true; - if (buf.hasNioBuffers()) { + if (buf.nioBufferCount() == 1) { + javaChannel().write( + buf.nioBuffer(), config.getWriteTimeout(), TimeUnit.MILLISECONDS, this, WRITE_HANDLER); + } else { ByteBuffer[] buffers = buf.nioBuffers(buf.readerIndex(), buf.readableBytes()); if (buffers.length == 1) { javaChannel().write( @@ -268,9 +271,6 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne buffers, 0, buffers.length, config.getWriteTimeout(), TimeUnit.MILLISECONDS, this, GATHERING_WRITE_HANDLER); } - } else { - javaChannel().write( - buf.nioBuffer(), config.getWriteTimeout(), TimeUnit.MILLISECONDS, this, WRITE_HANDLER); } if (asyncWriteInProgress) { @@ -322,7 +322,12 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne expandReadBuffer(byteBuf); asyncReadInProgress = true; - if (byteBuf.hasNioBuffers()) { + if (byteBuf.nioBufferCount() == 1) { + // Get a ByteBuffer view on the ByteBuf + ByteBuffer buffer = byteBuf.nioBuffer(byteBuf.writerIndex(), byteBuf.writableBytes()); + javaChannel().read( + buffer, config.getReadTimeout(), TimeUnit.MILLISECONDS, this, READ_HANDLER); + } else { ByteBuffer[] buffers = byteBuf.nioBuffers(byteBuf.writerIndex(), byteBuf.writableBytes()); if (buffers.length == 1) { javaChannel().read( @@ -332,11 +337,6 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne buffers, 0, buffers.length, config.getReadTimeout(), TimeUnit.MILLISECONDS, this, SCATTERING_READ_HANDLER); } - } else { - // Get a ByteBuffer view on the ByteBuf - ByteBuffer buffer = byteBuf.nioBuffer(byteBuf.writerIndex(), byteBuf.writableBytes()); - javaChannel().read( - buffer, config.getReadTimeout(), TimeUnit.MILLISECONDS, this, READ_HANDLER); } if (asyncReadInProgress) { diff --git a/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java b/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java index b84b274ebd..12fe6030cf 100755 --- a/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java @@ -193,7 +193,7 @@ public final class NioDatagramChannel ByteBuf data = packet.data(); int dataLen = data.readableBytes(); ByteBuffer nioData; - if (data.hasNioBuffer()) { + if (data.nioBufferCount() == 1) { nioData = data.nioBuffer(); } else { nioData = ByteBuffer.allocate(dataLen); diff --git a/transport/src/main/java/io/netty/channel/socket/nio/NioSctpChannel.java b/transport/src/main/java/io/netty/channel/socket/nio/NioSctpChannel.java index d5fde2cd04..b2c3e5abc9 100644 --- a/transport/src/main/java/io/netty/channel/socket/nio/NioSctpChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/nio/NioSctpChannel.java @@ -241,7 +241,7 @@ public class NioSctpChannel extends AbstractNioMessageChannel implements io.nett ByteBuf data = packet.getPayloadBuffer(); int dataLen = data.readableBytes(); ByteBuffer nioData; - if (data.hasNioBuffer()) { + if (data.nioBufferCount() == 1) { nioData = data.nioBuffer(); } else { nioData = ByteBuffer.allocate(dataLen); diff --git a/transport/src/main/java/io/netty/channel/socket/oio/OioSctpChannel.java b/transport/src/main/java/io/netty/channel/socket/oio/OioSctpChannel.java index 525a5f3260..94dd78eef9 100755 --- a/transport/src/main/java/io/netty/channel/socket/oio/OioSctpChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/oio/OioSctpChannel.java @@ -136,7 +136,7 @@ public class OioSctpChannel extends AbstractOioMessageChannel ByteBuf data = packet.getPayloadBuffer(); int dataLen = data.readableBytes(); ByteBuffer nioData; - if (data.hasNioBuffer()) { + if (data.nioBufferCount() == 1) { nioData = data.nioBuffer(); } else { nioData = ByteBuffer.allocate(dataLen);