Merge ByteBuf.hasNioBuffer() and hasNioBuffers()

- Fixes #797
This commit is contained in:
Trustin Lee 2012-12-14 12:20:33 +09:00
parent 8e1a6c6cf5
commit 5a4a59406b
20 changed files with 80 additions and 177 deletions

View File

@ -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);

View File

@ -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> {
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<ByteBuf> {
*
* @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<ByteBuf> {
*
* @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<ByteBuf> {
* 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);

View File

@ -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);

View File

@ -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);

View File

@ -252,8 +252,8 @@ final class PooledDirectByteBuf extends PooledByteBuf<ByteBuffer> {
}
@Override
public boolean hasNioBuffer() {
return true;
public int nioBufferCount() {
return 1;
}
@Override
@ -263,11 +263,6 @@ final class PooledDirectByteBuf extends PooledByteBuf<ByteBuffer> {
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) };

View File

@ -222,8 +222,8 @@ final class PooledHeapByteBuf extends PooledByteBuf<byte[]> {
}
@Override
public boolean hasNioBuffer() {
return true;
public int nioBufferCount() {
return 1;
}
@Override
@ -233,11 +233,6 @@ final class PooledHeapByteBuf extends PooledByteBuf<byte[]> {
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) };

View File

@ -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);

View File

@ -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);

View File

@ -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();

View File

@ -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

View File

@ -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

View File

@ -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());
}

View File

@ -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

View File

@ -49,7 +49,7 @@ public class ChannelBuffersTest {
assertEquals(512, payload.readableBytes());
assertEquals(12 + 512, buffer.readableBytes());
assertFalse(buffer.hasNioBuffer());
assertEquals(2, buffer.nioBufferCount());
}
@Test

View File

@ -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();

View File

@ -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();

View File

@ -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) {

View File

@ -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);

View File

@ -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);

View File

@ -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);