Add ByteBuf.order(ByteOrder) method to simplify little endian access

- Removed all methods that requires ByteOrder as a parameter
  from Unpooled (formerly ByteBufs/ChannelBuffers)
  - Instead, a user calls order(ByteOrder) to get a little endian
    version of the user's buffer
  - This gives less overwhelming number of methods in Unpooled.
This commit is contained in:
Trustin Lee 2012-06-11 20:24:44 +09:00
parent 876847fd20
commit 754392aaa9
27 changed files with 1147 additions and 776 deletions

View File

@ -19,6 +19,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel;
import java.nio.charset.Charset;
@ -29,11 +30,22 @@ import java.nio.charset.Charset;
*/
public abstract class AbstractByteBuf implements ByteBuf {
private final SwappedByteBuf swappedBuf;
private final ByteOrder order;
private int readerIndex;
private int writerIndex;
private int markedReaderIndex;
private int markedWriterIndex;
protected AbstractByteBuf(ByteOrder endianness) {
if (endianness == null) {
throw new NullPointerException("endianness");
}
order = endianness;
swappedBuf = new SwappedByteBuf(this);
}
@Override
public boolean isPooled() {
return false;
@ -149,6 +161,22 @@ public abstract class AbstractByteBuf implements ByteBuf {
}
}
@Override
public final ByteOrder order() {
return order;
}
@Override
public ByteBuf order(ByteOrder endianness) {
if (endianness == null) {
throw new NullPointerException("endianness");
}
if (endianness == order()) {
return this;
}
return swappedBuf;
}
@Override
public boolean getBoolean(int index) {
return getByte(index) != 0;
@ -690,10 +718,13 @@ public abstract class AbstractByteBuf implements ByteBuf {
@Override
public boolean equals(Object o) {
if (!(o instanceof ByteBuf)) {
return false;
if (this == o) {
return true;
}
return Unpooled.equals(this, (ByteBuf) o);
if (o instanceof ByteBuf) {
return Unpooled.equals(this, (ByteBuf) o);
}
return false;
}
@Override

View File

@ -1,141 +0,0 @@
/*
* 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;
import java.nio.ByteOrder;
/**
* A big-endian Java heap buffer. It is recommended to use {@link Unpooled#buffer(int)}
* and {@link Unpooled#wrappedBuffer(byte[])} instead of calling the
* constructor explicitly.
*/
public class BigEndianHeapByteBuf extends HeapByteBuf {
/**
* Creates a new big-endian heap buffer with a newly allocated byte array.
*
* @param length the length of the new byte array
*/
public BigEndianHeapByteBuf(int length) {
super(length);
}
/**
* Creates a new big-endian heap buffer with an existing byte array.
*
* @param array the byte array to wrap
*/
public BigEndianHeapByteBuf(byte[] array) {
super(array);
}
private BigEndianHeapByteBuf(byte[] array, int readerIndex, int writerIndex) {
super(array, readerIndex, writerIndex);
}
@Override
public ByteBufFactory factory() {
return HeapByteBufFactory.getInstance(ByteOrder.BIG_ENDIAN);
}
@Override
public ByteOrder order() {
return ByteOrder.BIG_ENDIAN;
}
@Override
public short getShort(int index) {
return (short) (array[index] << 8 | array[index + 1] & 0xFF);
}
@Override
public int getUnsignedMedium(int index) {
return (array[index] & 0xff) << 16 |
(array[index + 1] & 0xff) << 8 |
(array[index + 2] & 0xff) << 0;
}
@Override
public int getInt(int index) {
return (array[index] & 0xff) << 24 |
(array[index + 1] & 0xff) << 16 |
(array[index + 2] & 0xff) << 8 |
(array[index + 3] & 0xff) << 0;
}
@Override
public long getLong(int index) {
return ((long) array[index] & 0xff) << 56 |
((long) array[index + 1] & 0xff) << 48 |
((long) array[index + 2] & 0xff) << 40 |
((long) array[index + 3] & 0xff) << 32 |
((long) array[index + 4] & 0xff) << 24 |
((long) array[index + 5] & 0xff) << 16 |
((long) array[index + 6] & 0xff) << 8 |
((long) array[index + 7] & 0xff) << 0;
}
@Override
public void setShort(int index, int value) {
array[index] = (byte) (value >>> 8);
array[index + 1] = (byte) (value >>> 0);
}
@Override
public void setMedium(int index, int value) {
array[index] = (byte) (value >>> 16);
array[index + 1] = (byte) (value >>> 8);
array[index + 2] = (byte) (value >>> 0);
}
@Override
public void setInt(int index, int value) {
array[index] = (byte) (value >>> 24);
array[index + 1] = (byte) (value >>> 16);
array[index + 2] = (byte) (value >>> 8);
array[index + 3] = (byte) (value >>> 0);
}
@Override
public void setLong(int index, long value) {
array[index] = (byte) (value >>> 56);
array[index + 1] = (byte) (value >>> 48);
array[index + 2] = (byte) (value >>> 40);
array[index + 3] = (byte) (value >>> 32);
array[index + 4] = (byte) (value >>> 24);
array[index + 5] = (byte) (value >>> 16);
array[index + 6] = (byte) (value >>> 8);
array[index + 7] = (byte) (value >>> 0);
}
@Override
public ByteBuf duplicate() {
return new BigEndianHeapByteBuf(array, readerIndex(), writerIndex());
}
@Override
public ByteBuf copy(int index, int length) {
if (index < 0 || length < 0 || index + length > array.length) {
throw new IndexOutOfBoundsException("Too many bytes to copy - Need "
+ (index + length) + ", maximum is " + array.length);
}
byte[] copiedArray = new byte[length];
System.arraycopy(array, index, copiedArray, 0, length);
return new BigEndianHeapByteBuf(copiedArray);
}
}

View File

@ -247,6 +247,16 @@ public interface ByteBuf extends ChannelBuf, Comparable<ByteBuf> {
*/
ByteOrder order();
/**
* Returns a buffer with the specified {@code endianness} which shares the whole region,
* indexes, and marks of this buffer. Modifying the content, the indexes, or the marks of the
* returned buffer or this buffer affects each other's content, indexes, and marks. If the
* specified {@code endianness} is identical to this buffer's byte order, this method can
* return {@code this}. This method does not modify {@code readerIndex} or {@code writerIndex}
* of this buffer.
*/
ByteBuf order(ByteOrder endianness);
/**
* Returns {@code true} if and only if this buffer is backed by an
* NIO direct buffer.

View File

@ -36,13 +36,12 @@ import java.util.List;
*/
public class CompositeByteBuf extends AbstractByteBuf {
private final ByteOrder order;
private ByteBuf[] components;
private int[] indices;
private int lastAccessedComponentId;
public CompositeByteBuf(ByteOrder endianness, List<ByteBuf> buffers) {
order = endianness;
super(endianness);
setComponents(buffers);
}
@ -130,7 +129,7 @@ public class CompositeByteBuf extends AbstractByteBuf {
}
private CompositeByteBuf(CompositeByteBuf buffer) {
order = buffer.order;
super(buffer.order());
components = buffer.components.clone();
indices = buffer.indices.clone();
setIndex(buffer.readerIndex(), buffer.writerIndex());
@ -141,11 +140,6 @@ public class CompositeByteBuf extends AbstractByteBuf {
return HeapByteBufFactory.getInstance(order());
}
@Override
public ByteOrder order() {
return order;
}
@Override
public boolean isDirect() {
return false;
@ -710,7 +704,7 @@ public class CompositeByteBuf extends AbstractByteBuf {
// Add a new buffer so that the capacity of this composite buffer does
// not decrease due to the discarded components.
// XXX Might create too many components if discarded by small amount.
final ByteBuf padding = Unpooled.buffer(order(), localReaderIndex);
final ByteBuf padding = Unpooled.buffer(localReaderIndex).order(order());
padding.writerIndex(localReaderIndex);
list.add(padding);

View File

@ -114,7 +114,7 @@ public class DirectByteBufFactory extends AbstractByteBufFactory {
return Unpooled.EMPTY_BUFFER;
}
if (capacity >= preallocatedBufCapacity) {
return Unpooled.directBuffer(order, capacity);
return Unpooled.directBuffer(capacity).order(order);
}
ByteBuf slice;
@ -164,14 +164,14 @@ public class DirectByteBufFactory extends AbstractByteBufFactory {
ByteBuf slice;
synchronized (bigEndianLock) {
if (preallocatedBEBuf == null) {
preallocatedBEBuf = Unpooled.directBuffer(ByteOrder.BIG_ENDIAN, preallocatedBufCapacity);
preallocatedBEBuf = Unpooled.directBuffer(preallocatedBufCapacity);
slice = preallocatedBEBuf.slice(0, capacity);
preallocatedBEBufPos = capacity;
} else if (preallocatedBEBuf.capacity() - preallocatedBEBufPos >= capacity) {
slice = preallocatedBEBuf.slice(preallocatedBEBufPos, capacity);
preallocatedBEBufPos += capacity;
} else {
preallocatedBEBuf = Unpooled.directBuffer(ByteOrder.BIG_ENDIAN, preallocatedBufCapacity);
preallocatedBEBuf = Unpooled.directBuffer(preallocatedBufCapacity);
slice = preallocatedBEBuf.slice(0, capacity);
preallocatedBEBufPos = capacity;
}
@ -183,14 +183,16 @@ public class DirectByteBufFactory extends AbstractByteBufFactory {
ByteBuf slice;
synchronized (littleEndianLock) {
if (preallocatedLEBuf == null) {
preallocatedLEBuf = Unpooled.directBuffer(ByteOrder.LITTLE_ENDIAN, preallocatedBufCapacity);
preallocatedLEBuf = Unpooled.directBuffer(
preallocatedBufCapacity).order(ByteOrder.LITTLE_ENDIAN);
slice = preallocatedLEBuf.slice(0, capacity);
preallocatedLEBufPos = capacity;
} else if (preallocatedLEBuf.capacity() - preallocatedLEBufPos >= capacity) {
slice = preallocatedLEBuf.slice(preallocatedLEBufPos, capacity);
preallocatedLEBufPos += capacity;
} else {
preallocatedLEBuf = Unpooled.directBuffer(ByteOrder.LITTLE_ENDIAN, preallocatedBufCapacity);
preallocatedLEBuf = Unpooled.directBuffer(
preallocatedBufCapacity).order(ByteOrder.LITTLE_ENDIAN);
slice = preallocatedLEBuf.slice(0, capacity);
preallocatedLEBufPos = capacity;
}

View File

@ -19,7 +19,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel;
@ -34,14 +33,13 @@ public class DuplicatedByteBuf extends AbstractByteBuf implements WrappedByteBuf
private final ByteBuf buffer;
public DuplicatedByteBuf(ByteBuf buffer) {
if (buffer == null) {
throw new NullPointerException("buffer");
}
super(buffer.order());
this.buffer = buffer;
setIndex(buffer.readerIndex(), buffer.writerIndex());
}
private DuplicatedByteBuf(DuplicatedByteBuf buffer) {
super(buffer.buffer.order());
this.buffer = buffer.buffer;
setIndex(buffer.readerIndex(), buffer.writerIndex());
}
@ -56,11 +54,6 @@ public class DuplicatedByteBuf extends AbstractByteBuf implements WrappedByteBuf
return buffer.factory();
}
@Override
public ByteOrder order() {
return buffer.order();
}
@Override
public boolean isDirect() {
return buffer.isDirect();

View File

@ -32,30 +32,22 @@ import java.nio.channels.ScatteringByteChannel;
public class DynamicByteBuf extends AbstractByteBuf {
private final ByteBufFactory factory;
private final ByteOrder endianness;
private ByteBuf buffer;
public DynamicByteBuf(int estimatedLength) {
this(ByteOrder.BIG_ENDIAN, estimatedLength);
this(estimatedLength, HeapByteBufFactory.getInstance(ByteOrder.BIG_ENDIAN));
}
public DynamicByteBuf(ByteOrder endianness, int estimatedLength) {
this(endianness, estimatedLength, HeapByteBufFactory.getInstance(endianness));
}
public DynamicByteBuf(ByteOrder endianness, int estimatedLength, ByteBufFactory factory) {
public DynamicByteBuf(int estimatedLength, ByteBufFactory factory) {
super(ByteOrder.BIG_ENDIAN);
if (estimatedLength < 0) {
throw new IllegalArgumentException("estimatedLength: " + estimatedLength);
}
if (endianness == null) {
throw new NullPointerException("endianness");
}
if (factory == null) {
throw new NullPointerException("factory");
}
this.factory = factory;
this.endianness = endianness;
buffer = factory.getBuffer(order(), estimatedLength);
buffer = factory.getBuffer(ByteOrder.BIG_ENDIAN, estimatedLength);
}
@Override
@ -92,11 +84,6 @@ public class DynamicByteBuf extends AbstractByteBuf {
return factory;
}
@Override
public ByteOrder order() {
return endianness;
}
@Override
public boolean isDirect() {
return buffer.isDirect();
@ -300,7 +287,7 @@ public class DynamicByteBuf extends AbstractByteBuf {
@Override
public ByteBuf copy(int index, int length) {
DynamicByteBuf copiedBuffer = new DynamicByteBuf(order(), Math.max(length, 64), factory());
DynamicByteBuf copiedBuffer = new DynamicByteBuf(Math.max(length, 64), factory());
copiedBuffer.buffer = buffer.copy(index, length);
copiedBuffer.setIndex(0, length);
return copiedBuffer;

View File

@ -19,14 +19,15 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel;
/**
* A skeletal implementation for Java heap buffers.
* Big endian Java heap buffer implementation.
*/
public abstract class HeapByteBuf extends AbstractByteBuf {
public class HeapByteBuf extends AbstractByteBuf {
/**
* The underlying heap byte array that this buffer is wrapping.
@ -61,6 +62,7 @@ public abstract class HeapByteBuf extends AbstractByteBuf {
* @param writerIndex the initial writer index of this buffer
*/
protected HeapByteBuf(byte[] array, int readerIndex, int writerIndex) {
super(ByteOrder.BIG_ENDIAN);
if (array == null) {
throw new NullPointerException("array");
}
@ -198,4 +200,91 @@ public abstract class HeapByteBuf extends AbstractByteBuf {
public ByteBuffer nioBuffer(int index, int length) {
return ByteBuffer.wrap(array, index, length).order(order());
}
@Override
public ByteBufFactory factory() {
return HeapByteBufFactory.getInstance(ByteOrder.BIG_ENDIAN);
}
@Override
public short getShort(int index) {
return (short) (array[index] << 8 | array[index + 1] & 0xFF);
}
@Override
public int getUnsignedMedium(int index) {
return (array[index] & 0xff) << 16 |
(array[index + 1] & 0xff) << 8 |
(array[index + 2] & 0xff) << 0;
}
@Override
public int getInt(int index) {
return (array[index] & 0xff) << 24 |
(array[index + 1] & 0xff) << 16 |
(array[index + 2] & 0xff) << 8 |
(array[index + 3] & 0xff) << 0;
}
@Override
public long getLong(int index) {
return ((long) array[index] & 0xff) << 56 |
((long) array[index + 1] & 0xff) << 48 |
((long) array[index + 2] & 0xff) << 40 |
((long) array[index + 3] & 0xff) << 32 |
((long) array[index + 4] & 0xff) << 24 |
((long) array[index + 5] & 0xff) << 16 |
((long) array[index + 6] & 0xff) << 8 |
((long) array[index + 7] & 0xff) << 0;
}
@Override
public void setShort(int index, int value) {
array[index] = (byte) (value >>> 8);
array[index + 1] = (byte) (value >>> 0);
}
@Override
public void setMedium(int index, int value) {
array[index] = (byte) (value >>> 16);
array[index + 1] = (byte) (value >>> 8);
array[index + 2] = (byte) (value >>> 0);
}
@Override
public void setInt(int index, int value) {
array[index] = (byte) (value >>> 24);
array[index + 1] = (byte) (value >>> 16);
array[index + 2] = (byte) (value >>> 8);
array[index + 3] = (byte) (value >>> 0);
}
@Override
public void setLong(int index, long value) {
array[index] = (byte) (value >>> 56);
array[index + 1] = (byte) (value >>> 48);
array[index + 2] = (byte) (value >>> 40);
array[index + 3] = (byte) (value >>> 32);
array[index + 4] = (byte) (value >>> 24);
array[index + 5] = (byte) (value >>> 16);
array[index + 6] = (byte) (value >>> 8);
array[index + 7] = (byte) (value >>> 0);
}
@Override
public ByteBuf duplicate() {
return new HeapByteBuf(array, readerIndex(), writerIndex());
}
@Override
public ByteBuf copy(int index, int length) {
if (index < 0 || length < 0 || index + length > array.length) {
throw new IndexOutOfBoundsException("Too many bytes to copy - Need "
+ (index + length) + ", maximum is " + array.length);
}
byte[] copiedArray = new byte[length];
System.arraycopy(array, index, copiedArray, 0, length);
return new HeapByteBuf(copiedArray);
}
}

View File

@ -66,12 +66,12 @@ public class HeapByteBufFactory extends AbstractByteBufFactory {
@Override
public ByteBuf getBuffer(ByteOrder order, int capacity) {
return Unpooled.buffer(order, capacity);
return Unpooled.buffer(capacity).order(order);
}
@Override
public ByteBuf getBuffer(ByteOrder order, byte[] array, int offset, int length) {
return Unpooled.wrappedBuffer(order, array, offset, length);
return Unpooled.wrappedBuffer(array, offset, length).order(order);
}
@Override

View File

@ -1,141 +0,0 @@
/*
* 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;
import java.nio.ByteOrder;
/**
* A little-endian Java heap buffer. It is recommended to use {@link Unpooled#buffer(ByteOrder, int)}
* and {@link Unpooled#wrappedBuffer(ByteOrder, byte[])} instead of
* calling the constructor explicitly.
*/
public class LittleEndianHeapByteBuf extends HeapByteBuf {
/**
* Creates a new little-endian heap buffer with a newly allocated byte array.
*
* @param length the length of the new byte array
*/
public LittleEndianHeapByteBuf(int length) {
super(length);
}
/**
* Creates a new little-endian heap buffer with an existing byte array.
*
* @param array the byte array to wrap
*/
public LittleEndianHeapByteBuf(byte[] array) {
super(array);
}
private LittleEndianHeapByteBuf(byte[] array, int readerIndex, int writerIndex) {
super(array, readerIndex, writerIndex);
}
@Override
public ByteBufFactory factory() {
return HeapByteBufFactory.getInstance(ByteOrder.LITTLE_ENDIAN);
}
@Override
public ByteOrder order() {
return ByteOrder.LITTLE_ENDIAN;
}
@Override
public short getShort(int index) {
return (short) (array[index] & 0xFF | array[index + 1] << 8);
}
@Override
public int getUnsignedMedium(int index) {
return (array[index] & 0xff) << 0 |
(array[index + 1] & 0xff) << 8 |
(array[index + 2] & 0xff) << 16;
}
@Override
public int getInt(int index) {
return (array[index] & 0xff) << 0 |
(array[index + 1] & 0xff) << 8 |
(array[index + 2] & 0xff) << 16 |
(array[index + 3] & 0xff) << 24;
}
@Override
public long getLong(int index) {
return ((long) array[index] & 0xff) << 0 |
((long) array[index + 1] & 0xff) << 8 |
((long) array[index + 2] & 0xff) << 16 |
((long) array[index + 3] & 0xff) << 24 |
((long) array[index + 4] & 0xff) << 32 |
((long) array[index + 5] & 0xff) << 40 |
((long) array[index + 6] & 0xff) << 48 |
((long) array[index + 7] & 0xff) << 56;
}
@Override
public void setShort(int index, int value) {
array[index] = (byte) (value >>> 0);
array[index + 1] = (byte) (value >>> 8);
}
@Override
public void setMedium(int index, int value) {
array[index] = (byte) (value >>> 0);
array[index + 1] = (byte) (value >>> 8);
array[index + 2] = (byte) (value >>> 16);
}
@Override
public void setInt(int index, int value) {
array[index] = (byte) (value >>> 0);
array[index + 1] = (byte) (value >>> 8);
array[index + 2] = (byte) (value >>> 16);
array[index + 3] = (byte) (value >>> 24);
}
@Override
public void setLong(int index, long value) {
array[index] = (byte) (value >>> 0);
array[index + 1] = (byte) (value >>> 8);
array[index + 2] = (byte) (value >>> 16);
array[index + 3] = (byte) (value >>> 24);
array[index + 4] = (byte) (value >>> 32);
array[index + 5] = (byte) (value >>> 40);
array[index + 6] = (byte) (value >>> 48);
array[index + 7] = (byte) (value >>> 56);
}
@Override
public ByteBuf duplicate() {
return new LittleEndianHeapByteBuf(array, readerIndex(), writerIndex());
}
@Override
public ByteBuf copy(int index, int length) {
if (index < 0 || length < 0 || index + length > array.length) {
throw new IndexOutOfBoundsException("Copy could not be completed. Bytes needed: "
+ (index + length) + ", maximum: " + array.length);
}
byte[] copiedArray = new byte[length];
System.arraycopy(array, index, copiedArray, 0, length);
return new LittleEndianHeapByteBuf(copiedArray);
}
}

View File

@ -19,7 +19,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel;
@ -33,28 +32,23 @@ public class NioBufferBackedByteBuf extends AbstractByteBuf {
private final ByteBuffer buffer;
private final ByteBuffer tmpBuf;
private final ByteOrder order;
private final int capacity;
/**
* Creates a new buffer which wraps the specified buffer's slice.
*/
public NioBufferBackedByteBuf(ByteBuffer buffer) {
if (buffer == null) {
throw new NullPointerException("buffer");
}
order = buffer.order();
this.buffer = buffer.slice().order(order);
super(buffer.order());
this.buffer = buffer.slice().order(order());
tmpBuf = this.buffer.duplicate();
capacity = buffer.remaining();
writerIndex(capacity);
}
private NioBufferBackedByteBuf(NioBufferBackedByteBuf buffer) {
super(buffer.order());
this.buffer = buffer.buffer;
tmpBuf = this.buffer.duplicate();
order = buffer.order;
capacity = buffer.capacity;
setIndex(buffer.readerIndex(), buffer.writerIndex());
}
@ -73,11 +67,6 @@ public class NioBufferBackedByteBuf extends AbstractByteBuf {
return buffer.isDirect();
}
@Override
public ByteOrder order() {
return order;
}
@Override
public int capacity() {
return capacity;

View File

@ -19,7 +19,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.ReadOnlyBufferException;
import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel;
@ -34,14 +33,13 @@ public class ReadOnlyByteBuf extends AbstractByteBuf implements WrappedByteBuf {
private final ByteBuf buffer;
public ReadOnlyByteBuf(ByteBuf buffer) {
if (buffer == null) {
throw new NullPointerException("buffer");
}
super(buffer.order());
this.buffer = buffer;
setIndex(buffer.readerIndex(), buffer.writerIndex());
}
private ReadOnlyByteBuf(ReadOnlyByteBuf buffer) {
super(buffer.buffer.order());
this.buffer = buffer.buffer;
setIndex(buffer.readerIndex(), buffer.writerIndex());
}
@ -56,11 +54,6 @@ public class ReadOnlyByteBuf extends AbstractByteBuf implements WrappedByteBuf {
return buffer.factory();
}
@Override
public ByteOrder order() {
return buffer.order();
}
@Override
public boolean isDirect() {
return buffer.isDirect();

View File

@ -19,7 +19,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel;
@ -37,6 +36,7 @@ public class SlicedByteBuf extends AbstractByteBuf implements WrappedByteBuf {
private final int length;
public SlicedByteBuf(ByteBuf buffer, int index, int length) {
super(buffer.order());
if (index < 0 || index > buffer.capacity()) {
throw new IndexOutOfBoundsException("Invalid index of " + index
+ ", maximum is " + buffer.capacity());
@ -63,11 +63,6 @@ public class SlicedByteBuf extends AbstractByteBuf implements WrappedByteBuf {
return buffer.factory();
}
@Override
public ByteOrder order() {
return buffer.order();
}
@Override
public boolean isDirect() {
return buffer.isDirect();

View File

@ -0,0 +1,700 @@
/*
* 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;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel;
import java.nio.charset.Charset;
public class SwappedByteBuf implements WrappedByteBuf {
private final ByteBuf buf;
private final ByteOrder order;
public SwappedByteBuf(ByteBuf buf) {
if (buf == null) {
throw new NullPointerException("buf");
}
this.buf = buf;
if (buf.order() == ByteOrder.BIG_ENDIAN) {
order = ByteOrder.LITTLE_ENDIAN;
} else {
order = ByteOrder.BIG_ENDIAN;
}
}
@Override
public ByteOrder order() {
return order;
}
@Override
public ByteBuf order(ByteOrder endianness) {
if (endianness == null) {
throw new NullPointerException("endianness");
}
if (endianness == order) {
return this;
}
return buf;
}
@Override
public ByteBuf unwrap() {
return buf;
}
@Override
public boolean isPooled() {
return buf.isPooled();
}
@Override
public ByteBufFactory factory() {
return buf.factory();
}
@Override
public int capacity() {
return buf.capacity();
}
@Override
public boolean isDirect() {
return buf.isDirect();
}
@Override
public int readerIndex() {
return buf.readerIndex();
}
@Override
public void readerIndex(int readerIndex) {
buf.readerIndex(readerIndex);
}
@Override
public int writerIndex() {
return buf.writerIndex();
}
@Override
public void writerIndex(int writerIndex) {
buf.writerIndex(writerIndex);
}
@Override
public void setIndex(int readerIndex, int writerIndex) {
buf.setIndex(readerIndex, writerIndex);
}
@Override
public int readableBytes() {
return buf.readableBytes();
}
@Override
public int writableBytes() {
return buf.writableBytes();
}
@Override
public boolean readable() {
return buf.readable();
}
@Override
public boolean writable() {
return buf.writable();
}
@Override
public void clear() {
buf.clear();
}
@Override
public void markReaderIndex() {
buf.markReaderIndex();
}
@Override
public void resetReaderIndex() {
buf.resetReaderIndex();
}
@Override
public void markWriterIndex() {
buf.markWriterIndex();
}
@Override
public void resetWriterIndex() {
buf.resetWriterIndex();
}
@Override
public void discardReadBytes() {
buf.discardReadBytes();
}
@Override
public void ensureWritableBytes(int writableBytes) {
buf.ensureWritableBytes(writableBytes);
}
@Override
public boolean getBoolean(int index) {
return buf.getBoolean(index);
}
@Override
public byte getByte(int index) {
return buf.getByte(index);
}
@Override
public short getUnsignedByte(int index) {
return buf.getUnsignedByte(index);
}
@Override
public short getShort(int index) {
return Unpooled.swapShort(buf.getShort(index));
}
@Override
public int getUnsignedShort(int index) {
return getShort(index) & 0xFFFF;
}
@Override
public int getMedium(int index) {
return Unpooled.swapMedium(buf.getMedium(index));
}
@Override
public int getUnsignedMedium(int index) {
return getMedium(index) & 0xFFFFFF;
}
@Override
public int getInt(int index) {
return Unpooled.swapInt(buf.getInt(index));
}
@Override
public long getUnsignedInt(int index) {
return getInt(index) & 0xFFFFFFFFL;
}
@Override
public long getLong(int index) {
return Unpooled.swapLong(buf.getLong(index));
}
@Override
public char getChar(int index) {
return (char) getShort(index);
}
@Override
public float getFloat(int index) {
return Float.intBitsToFloat(getInt(index));
}
@Override
public double getDouble(int index) {
return Double.longBitsToDouble(getLong(index));
}
@Override
public void getBytes(int index, ByteBuf dst) {
buf.getBytes(index, dst);
}
@Override
public void getBytes(int index, ByteBuf dst, int length) {
buf.getBytes(index, dst, length);
}
@Override
public void getBytes(int index, ByteBuf dst, int dstIndex, int length) {
buf.getBytes(index, dst, dstIndex, length);
}
@Override
public void getBytes(int index, byte[] dst) {
buf.getBytes(index, dst);
}
@Override
public void getBytes(int index, byte[] dst, int dstIndex, int length) {
buf.getBytes(index, dst, dstIndex, length);
}
@Override
public void getBytes(int index, ByteBuffer dst) {
buf.getBytes(index, dst);
}
@Override
public void getBytes(int index, OutputStream out, int length) throws IOException {
buf.getBytes(index, out, length);
}
@Override
public int getBytes(int index, GatheringByteChannel out, int length) throws IOException {
return buf.getBytes(index, out, length);
}
@Override
public void setBoolean(int index, boolean value) {
buf.setBoolean(index, value);
}
@Override
public void setByte(int index, int value) {
buf.setByte(index, value);
}
@Override
public void setShort(int index, int value) {
buf.setShort(index, Unpooled.swapShort((short) value));
}
@Override
public void setMedium(int index, int value) {
buf.setMedium(index, Unpooled.swapMedium(value));
}
@Override
public void setInt(int index, int value) {
buf.setInt(index, Unpooled.swapInt(value));
}
@Override
public void setLong(int index, long value) {
buf.setLong(index, Unpooled.swapLong(value));
}
@Override
public void setChar(int index, int value) {
setShort(index, value);
}
@Override
public void setFloat(int index, float value) {
setInt(index, Float.floatToRawIntBits(value));
}
@Override
public void setDouble(int index, double value) {
setLong(index, Double.doubleToRawLongBits(value));
}
@Override
public void setBytes(int index, ByteBuf src) {
buf.setBytes(index, src);
}
@Override
public void setBytes(int index, ByteBuf src, int length) {
buf.setBytes(index, src, length);
}
@Override
public void setBytes(int index, ByteBuf src, int srcIndex, int length) {
buf.setBytes(index, src, srcIndex, length);
}
@Override
public void setBytes(int index, byte[] src) {
buf.setBytes(index, src);
}
@Override
public void setBytes(int index, byte[] src, int srcIndex, int length) {
buf.setBytes(index, src, srcIndex, length);
}
@Override
public void setBytes(int index, ByteBuffer src) {
buf.setBytes(index, src);
}
@Override
public int setBytes(int index, InputStream in, int length) throws IOException {
return buf.setBytes(index, in, length);
}
@Override
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
return buf.setBytes(index, in, length);
}
@Override
public void setZero(int index, int length) {
buf.setZero(index, length);
}
@Override
public boolean readBoolean() {
return buf.readBoolean();
}
@Override
public byte readByte() {
return buf.readByte();
}
@Override
public short readUnsignedByte() {
return buf.readUnsignedByte();
}
@Override
public short readShort() {
return Unpooled.swapShort(buf.readShort());
}
@Override
public int readUnsignedShort() {
return readShort() & 0xFFFF;
}
@Override
public int readMedium() {
return Unpooled.swapMedium(buf.readMedium());
}
@Override
public int readUnsignedMedium() {
return readMedium() & 0xFFFFFF;
}
@Override
public int readInt() {
return Unpooled.swapInt(buf.readInt());
}
@Override
public long readUnsignedInt() {
return readInt() & 0xFFFFFFFFL;
}
@Override
public long readLong() {
return Unpooled.swapLong(buf.readLong());
}
@Override
public char readChar() {
return (char) readShort();
}
@Override
public float readFloat() {
return Float.intBitsToFloat(readInt());
}
@Override
public double readDouble() {
return Double.longBitsToDouble(readLong());
}
@Override
public ByteBuf readBytes(int length) {
return buf.readBytes(length);
}
@Override
public ByteBuf readSlice(int length) {
return buf.readSlice(length);
}
@Override
public void readBytes(ByteBuf dst) {
buf.readBytes(dst);
}
@Override
public void readBytes(ByteBuf dst, int length) {
buf.readBytes(dst, length);
}
@Override
public void readBytes(ByteBuf dst, int dstIndex, int length) {
buf.readBytes(dst, dstIndex, length);
}
@Override
public void readBytes(byte[] dst) {
buf.readBytes(dst);
}
@Override
public void readBytes(byte[] dst, int dstIndex, int length) {
buf.readBytes(dst, dstIndex, length);
}
@Override
public void readBytes(ByteBuffer dst) {
buf.readBytes(dst);
}
@Override
public void readBytes(OutputStream out, int length) throws IOException {
buf.readBytes(out, length);
}
@Override
public int readBytes(GatheringByteChannel out, int length) throws IOException {
return buf.readBytes(out, length);
}
@Override
public void skipBytes(int length) {
buf.skipBytes(length);
}
@Override
public void writeBoolean(boolean value) {
buf.writeBoolean(value);
}
@Override
public void writeByte(int value) {
buf.writeByte(value);
}
@Override
public void writeShort(int value) {
buf.writeShort(Unpooled.swapShort((short) value));
}
@Override
public void writeMedium(int value) {
buf.writeMedium(Unpooled.swapMedium(value));
}
@Override
public void writeInt(int value) {
buf.writeInt(Unpooled.swapInt(value));
}
@Override
public void writeLong(long value) {
buf.writeLong(Unpooled.swapLong(value));
}
@Override
public void writeChar(int value) {
writeShort(value);
}
@Override
public void writeFloat(float value) {
writeInt(Float.floatToRawIntBits(value));
}
@Override
public void writeDouble(double value) {
writeLong(Double.doubleToRawLongBits(value));
}
@Override
public void writeBytes(ByteBuf src) {
buf.writeBytes(src);
}
@Override
public void writeBytes(ByteBuf src, int length) {
buf.writeBytes(src, length);
}
@Override
public void writeBytes(ByteBuf src, int srcIndex, int length) {
buf.writeBytes(src, srcIndex, length);
}
@Override
public void writeBytes(byte[] src) {
buf.writeBytes(src);
}
@Override
public void writeBytes(byte[] src, int srcIndex, int length) {
buf.writeBytes(src, srcIndex, length);
}
@Override
public void writeBytes(ByteBuffer src) {
buf.writeBytes(src);
}
@Override
public int writeBytes(InputStream in, int length) throws IOException {
return buf.writeBytes(in, length);
}
@Override
public int writeBytes(ScatteringByteChannel in, int length) throws IOException {
return buf.writeBytes(in, length);
}
@Override
public void writeZero(int length) {
buf.writeZero(length);
}
@Override
public int indexOf(int fromIndex, int toIndex, byte value) {
return buf.indexOf(fromIndex, toIndex, value);
}
@Override
public int indexOf(int fromIndex, int toIndex, ByteBufIndexFinder indexFinder) {
return buf.indexOf(fromIndex, toIndex, indexFinder);
}
@Override
public int bytesBefore(byte value) {
return buf.bytesBefore(value);
}
@Override
public int bytesBefore(ByteBufIndexFinder indexFinder) {
return buf.bytesBefore(indexFinder);
}
@Override
public int bytesBefore(int length, byte value) {
return buf.bytesBefore(length, value);
}
@Override
public int bytesBefore(int length, ByteBufIndexFinder indexFinder) {
return buf.bytesBefore(length, indexFinder);
}
@Override
public int bytesBefore(int index, int length, byte value) {
return buf.bytesBefore(index, length, value);
}
@Override
public int bytesBefore(int index, int length, ByteBufIndexFinder indexFinder) {
return buf.bytesBefore(index, length, indexFinder);
}
@Override
public ByteBuf copy() {
return buf.copy().order(order);
}
@Override
public ByteBuf copy(int index, int length) {
return buf.copy(index, length).order(order);
}
@Override
public ByteBuf slice() {
return buf.slice().order(order);
}
@Override
public ByteBuf slice(int index, int length) {
return buf.slice(index, length).order(order);
}
@Override
public ByteBuf duplicate() {
return buf.duplicate().order(order);
}
@Override
public boolean hasNioBuffer() {
return buf.hasNioBuffer();
}
@Override
public ByteBuffer nioBuffer() {
return buf.nioBuffer().order(order);
}
@Override
public ByteBuffer nioBuffer(int index, int length) {
return buf.nioBuffer(index, length).order(order);
}
@Override
public boolean hasArray() {
return buf.hasArray();
}
@Override
public byte[] array() {
return buf.array();
}
@Override
public int arrayOffset() {
return buf.arrayOffset();
}
@Override
public String toString(Charset charset) {
return buf.toString(charset);
}
@Override
public String toString(int index, int length, Charset charset) {
return buf.toString(index, length, charset);
}
@Override
public int hashCode() {
return buf.hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof ByteBuf) {
return Unpooled.equals(this, (ByteBuf) obj);
}
return false;
}
@Override
public int compareTo(ByteBuf buffer) {
return Unpooled.compare(this, buffer);
}
@Override
public String toString() {
return "Swapped(" + buf.toString() + ')';
}
}

View File

@ -19,7 +19,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel;
@ -36,6 +35,7 @@ public class TruncatedByteBuf extends AbstractByteBuf implements WrappedByteBuf
private final int length;
public TruncatedByteBuf(ByteBuf buffer, int length) {
super(buffer.order());
if (length > buffer.capacity()) {
throw new IndexOutOfBoundsException("Length is too large, got "
+ length + " but can't go higher than " + buffer.capacity());
@ -56,11 +56,6 @@ public class TruncatedByteBuf extends AbstractByteBuf implements WrappedByteBuf
return buffer.factory();
}
@Override
public ByteOrder order() {
return buffer.order();
}
@Override
public boolean isDirect() {
return buffer.isDirect();

View File

@ -100,7 +100,15 @@ public final class Unpooled {
/**
* A buffer whose capacity is {@code 0}.
*/
public static final ByteBuf EMPTY_BUFFER = new BigEndianHeapByteBuf(0);
public static final ByteBuf EMPTY_BUFFER = new HeapByteBuf(0) {
@Override
public ByteBuf order(ByteOrder endianness) {
if (endianness == null) {
throw new NullPointerException("endianness");
}
return this;
}
};
private static final char[] HEXDUMP_TABLE = new char[256 * 4];
@ -130,28 +138,10 @@ public final class Unpooled {
* {@code writerIndex} are {@code 0}.
*/
public static ByteBuf buffer(int capacity) {
return buffer(BIG_ENDIAN, capacity);
}
/**
* Creates a new Java heap buffer with the specified {@code endianness}
* and {@code capacity}. The new buffer's {@code readerIndex} and
* {@code writerIndex} are {@code 0}.
*/
public static ByteBuf buffer(ByteOrder endianness, int capacity) {
if (endianness == BIG_ENDIAN) {
if (capacity == 0) {
return EMPTY_BUFFER;
}
return new BigEndianHeapByteBuf(capacity);
} else if (endianness == LITTLE_ENDIAN) {
if (capacity == 0) {
return EMPTY_BUFFER;
}
return new LittleEndianHeapByteBuf(capacity);
} else {
throw new NullPointerException("endianness");
if (capacity == 0) {
return EMPTY_BUFFER;
}
return new HeapByteBuf(capacity);
}
/**
@ -160,24 +150,11 @@ public final class Unpooled {
* {@code writerIndex} are {@code 0}.
*/
public static ByteBuf directBuffer(int capacity) {
return directBuffer(BIG_ENDIAN, capacity);
}
/**
* Creates a new direct buffer with the specified {@code endianness} and
* {@code capacity}. The new buffer's {@code readerIndex} and
* {@code writerIndex} are {@code 0}.
*/
public static ByteBuf directBuffer(ByteOrder endianness, int capacity) {
if (endianness == null) {
throw new NullPointerException("endianness");
}
if (capacity == 0) {
return EMPTY_BUFFER;
}
ByteBuf buffer = new NioBufferBackedByteBuf(
ByteBuffer.allocateDirect(capacity).order(endianness));
ByteBuf buffer = new NioBufferBackedByteBuf(ByteBuffer.allocateDirect(capacity));
buffer.clear();
return buffer;
}
@ -188,15 +165,20 @@ public final class Unpooled {
* {@code writerIndex} are {@code 0}.
*/
public static ByteBuf dynamicBuffer() {
return dynamicBuffer(BIG_ENDIAN, 256);
return dynamicBuffer(256);
}
/**
* Creates a new big-endian dynamic buffer whose estimated data length is
* {@code 256} bytes. The new buffer's {@code readerIndex} and
* {@code writerIndex} are {@code 0}.
*/
public static ByteBuf dynamicBuffer(ByteBufFactory factory) {
if (factory == null) {
throw new NullPointerException("factory");
}
return new DynamicByteBuf(factory.getDefaultOrder(), 256, factory);
return new DynamicByteBuf(256, factory);
}
/**
@ -206,17 +188,7 @@ public final class Unpooled {
* {@code writerIndex} are {@code 0}.
*/
public static ByteBuf dynamicBuffer(int estimatedLength) {
return dynamicBuffer(BIG_ENDIAN, estimatedLength);
}
/**
* Creates a new dynamic buffer with the specified endianness and
* the specified estimated data length. More accurate estimation yields
* less unexpected reallocation overhead. The new buffer's
* {@code readerIndex} and {@code writerIndex} are {@code 0}.
*/
public static ByteBuf dynamicBuffer(ByteOrder endianness, int estimatedLength) {
return new DynamicByteBuf(endianness, estimatedLength);
return new DynamicByteBuf(estimatedLength);
}
/**
@ -230,18 +202,7 @@ public final class Unpooled {
throw new NullPointerException("factory");
}
return new DynamicByteBuf(factory.getDefaultOrder(), estimatedLength, factory);
}
/**
* Creates a new dynamic buffer with the specified endianness and
* the specified estimated data length using the specified factory.
* More accurate estimation yields less unexpected reallocation overhead.
* The new buffer's {@code readerIndex} and {@code writerIndex} are {@code 0}.
*/
public static ByteBuf dynamicBuffer(
ByteOrder endianness, int estimatedLength, ByteBufFactory factory) {
return new DynamicByteBuf(endianness, estimatedLength, factory);
return new DynamicByteBuf(estimatedLength, factory);
}
/**
@ -250,28 +211,10 @@ public final class Unpooled {
* returned buffer.
*/
public static ByteBuf wrappedBuffer(byte[] array) {
return wrappedBuffer(BIG_ENDIAN, array);
}
/**
* Creates a new buffer which wraps the specified {@code array} with the
* specified {@code endianness}. A modification on the specified array's
* content will be visible to the returned buffer.
*/
public static ByteBuf wrappedBuffer(ByteOrder endianness, byte[] array) {
if (endianness == BIG_ENDIAN) {
if (array.length == 0) {
return EMPTY_BUFFER;
}
return new BigEndianHeapByteBuf(array);
} else if (endianness == LITTLE_ENDIAN) {
if (array.length == 0) {
return EMPTY_BUFFER;
}
return new LittleEndianHeapByteBuf(array);
} else {
throw new NullPointerException("endianness");
if (array.length == 0) {
return EMPTY_BUFFER;
}
return new HeapByteBuf(array);
}
/**
@ -280,33 +223,21 @@ public final class Unpooled {
* content will be visible to the returned buffer.
*/
public static ByteBuf wrappedBuffer(byte[] array, int offset, int length) {
return wrappedBuffer(BIG_ENDIAN, array, offset, length);
}
/**
* Creates a new buffer which wraps the sub-region of the specified
* {@code array} with the specified {@code endianness}. A modification on
* the specified array's content will be visible to the returned buffer.
*/
public static ByteBuf wrappedBuffer(ByteOrder endianness, byte[] array, int offset, int length) {
if (endianness == null) {
throw new NullPointerException("endianness");
}
if (offset == 0) {
if (length == array.length) {
return wrappedBuffer(endianness, array);
return wrappedBuffer(array);
} else {
if (length == 0) {
return EMPTY_BUFFER;
} else {
return new TruncatedByteBuf(wrappedBuffer(endianness, array), length);
return new TruncatedByteBuf(wrappedBuffer(array), length);
}
}
} else {
if (length == 0) {
return EMPTY_BUFFER;
} else {
return new SlicedByteBuf(wrappedBuffer(endianness, array), offset, length);
return new SlicedByteBuf(wrappedBuffer(array), offset, length);
}
}
}
@ -322,10 +253,9 @@ public final class Unpooled {
}
if (buffer.hasArray()) {
return wrappedBuffer(
buffer.order(),
buffer.array(),
buffer.arrayOffset() + buffer.position(),
buffer.remaining());
buffer.remaining()).order(buffer.order());
} else {
return new NioBufferBackedByteBuf(buffer);
}
@ -350,23 +280,12 @@ public final class Unpooled {
* content will be visible to the returned buffer.
*/
public static ByteBuf wrappedBuffer(byte[]... arrays) {
return wrappedBuffer(BIG_ENDIAN, arrays);
}
/**
* Creates a new composite buffer which wraps the specified arrays without
* copying them. A modification on the specified arrays' content will be
* visible to the returned buffer.
*
* @param endianness the endianness of the new buffer
*/
public static ByteBuf wrappedBuffer(ByteOrder endianness, byte[]... arrays) {
switch (arrays.length) {
case 0:
break;
case 1:
if (arrays[0].length != 0) {
return wrappedBuffer(endianness, arrays[0]);
return wrappedBuffer(arrays[0]);
}
break;
default:
@ -377,17 +296,21 @@ public final class Unpooled {
break;
}
if (a.length > 0) {
components.add(wrappedBuffer(endianness, a));
components.add(wrappedBuffer(a));
}
}
return compositeBuffer(endianness, components);
return compositeBuffer(BIG_ENDIAN, components);
}
return EMPTY_BUFFER;
}
private static ByteBuf compositeBuffer(
ByteOrder endianness, List<ByteBuf> components) {
/**
* Creates a new composite buffer which wraps the specified
* components without copying them. A modification on the specified components'
* content will be visible to the returned buffer.
*/
private static ByteBuf compositeBuffer(ByteOrder endianness, List<ByteBuf> components) {
switch (components.size()) {
case 0:
return EMPTY_BUFFER;
@ -426,8 +349,7 @@ public final class Unpooled {
if (c.readable()) {
if (order != null) {
if (!order.equals(c.order())) {
throw new IllegalArgumentException(
"inconsistent byte order");
throw new IllegalArgumentException("inconsistent byte order");
}
} else {
order = c.order();
@ -476,8 +398,7 @@ public final class Unpooled {
if (b.hasRemaining()) {
if (order != null) {
if (!order.equals(b.order())) {
throw new IllegalArgumentException(
"inconsistent byte order");
throw new IllegalArgumentException("inconsistent byte order");
}
} else {
order = b.order();
@ -497,29 +418,10 @@ public final class Unpooled {
* {@code writerIndex} are {@code 0} and {@code array.length} respectively.
*/
public static ByteBuf copiedBuffer(byte[] array) {
return copiedBuffer(BIG_ENDIAN, array);
}
/**
* Creates a new buffer with the specified {@code endianness} whose
* content is a copy of the specified {@code array}. The new buffer's
* {@code readerIndex} and {@code writerIndex} are {@code 0} and
* {@code array.length} respectively.
*/
public static ByteBuf copiedBuffer(ByteOrder endianness, byte[] array) {
if (endianness == BIG_ENDIAN) {
if (array.length == 0) {
return EMPTY_BUFFER;
}
return new BigEndianHeapByteBuf(array.clone());
} else if (endianness == LITTLE_ENDIAN) {
if (array.length == 0) {
return EMPTY_BUFFER;
}
return new LittleEndianHeapByteBuf(array.clone());
} else {
throw new NullPointerException("endianness");
if (array.length == 0) {
return EMPTY_BUFFER;
}
return new HeapByteBuf(array.clone());
}
/**
@ -529,25 +431,12 @@ public final class Unpooled {
* the specified {@code length} respectively.
*/
public static ByteBuf copiedBuffer(byte[] array, int offset, int length) {
return copiedBuffer(BIG_ENDIAN, array, offset, length);
}
/**
* Creates a new buffer with the specified {@code endianness} whose
* content is a copy of the specified {@code array}'s sub-region. The new
* buffer's {@code readerIndex} and {@code writerIndex} are {@code 0} and
* the specified {@code length} respectively.
*/
public static ByteBuf copiedBuffer(ByteOrder endianness, byte[] array, int offset, int length) {
if (endianness == null) {
throw new NullPointerException("endianness");
}
if (length == 0) {
return EMPTY_BUFFER;
}
byte[] copy = new byte[length];
System.arraycopy(array, offset, copy, 0, length);
return wrappedBuffer(endianness, copy);
return wrappedBuffer(copy);
}
/**
@ -568,7 +457,7 @@ public final class Unpooled {
} finally {
buffer.position(position);
}
return wrappedBuffer(buffer.order(), copy);
return wrappedBuffer(copy).order(buffer.order());
}
/**
@ -592,16 +481,6 @@ public final class Unpooled {
* {@code length} respectively.
*/
public static ByteBuf copiedBuffer(byte[]... arrays) {
return copiedBuffer(BIG_ENDIAN, arrays);
}
/**
* Creates a new buffer with the specified {@code endianness} whose
* content is a merged copy of the specified {@code arrays}. The new
* buffer's {@code readerIndex} and {@code writerIndex} are {@code 0}
* and the sum of all arrays' {@code length} respectively.
*/
public static ByteBuf copiedBuffer(ByteOrder endianness, byte[]... arrays) {
switch (arrays.length) {
case 0:
return EMPTY_BUFFER;
@ -609,7 +488,7 @@ public final class Unpooled {
if (arrays[0].length == 0) {
return EMPTY_BUFFER;
} else {
return copiedBuffer(endianness, arrays[0]);
return copiedBuffer(arrays[0]);
}
}
@ -634,7 +513,7 @@ public final class Unpooled {
j += a.length;
}
return wrappedBuffer(endianness, mergedArray);
return wrappedBuffer(mergedArray);
}
/**
@ -655,11 +534,41 @@ public final class Unpooled {
return copiedBuffer(buffers[0]);
}
ByteBuf[] copiedBuffers = new ByteBuf[buffers.length];
for (int i = 0; i < buffers.length; i ++) {
copiedBuffers[i] = copiedBuffer(buffers[i]);
// Merge the specified buffers into one buffer.
ByteOrder order = null;
int length = 0;
for (ByteBuf b: buffers) {
int bLen = b.readableBytes();
if (bLen <= 0) {
continue;
}
if (Integer.MAX_VALUE - length < bLen) {
throw new IllegalArgumentException(
"The total length of the specified buffers is too big.");
}
length += bLen;
if (order != null) {
if (!order.equals(b.order())) {
throw new IllegalArgumentException("inconsistent byte order");
}
} else {
order = b.order();
}
}
return wrappedBuffer(copiedBuffers);
if (length == 0) {
return EMPTY_BUFFER;
}
byte[] mergedArray = new byte[length];
for (int i = 0, j = 0; i < buffers.length; i ++) {
ByteBuf b = buffers[i];
int bLen = b.readableBytes();
b.getBytes(b.readerIndex(), mergedArray, j, bLen);
j += bLen;
}
return wrappedBuffer(mergedArray).order(order);
}
/**
@ -680,11 +589,43 @@ public final class Unpooled {
return copiedBuffer(buffers[0]);
}
ByteBuf[] copiedBuffers = new ByteBuf[buffers.length];
for (int i = 0; i < buffers.length; i ++) {
copiedBuffers[i] = copiedBuffer(buffers[i]);
// Merge the specified buffers into one buffer.
ByteOrder order = null;
int length = 0;
for (ByteBuffer b: buffers) {
int bLen = b.remaining();
if (bLen <= 0) {
continue;
}
if (Integer.MAX_VALUE - length < bLen) {
throw new IllegalArgumentException(
"The total length of the specified buffers is too big.");
}
length += bLen;
if (order != null) {
if (!order.equals(b.order())) {
throw new IllegalArgumentException("inconsistent byte order");
}
} else {
order = b.order();
}
}
return wrappedBuffer(copiedBuffers);
if (length == 0) {
return EMPTY_BUFFER;
}
byte[] mergedArray = new byte[length];
for (int i = 0, j = 0; i < buffers.length; i ++) {
ByteBuffer b = buffers[i];
int bLen = b.remaining();
int oldPos = b.position();
b.get(mergedArray, j, bLen);
b.position(oldPos);
j += bLen;
}
return wrappedBuffer(mergedArray).order(order);
}
/**
@ -694,7 +635,15 @@ public final class Unpooled {
* {@code 0} and the length of the encoded string respectively.
*/
public static ByteBuf copiedBuffer(CharSequence string, Charset charset) {
return copiedBuffer(BIG_ENDIAN, string, charset);
if (string == null) {
throw new NullPointerException("string");
}
if (string instanceof CharBuffer) {
return copiedBuffer((CharBuffer) string, charset);
}
return copiedBuffer(CharBuffer.wrap(string), charset);
}
/**
@ -705,37 +654,6 @@ public final class Unpooled {
*/
public static ByteBuf copiedBuffer(
CharSequence string, int offset, int length, Charset charset) {
return copiedBuffer(BIG_ENDIAN, string, offset, length, charset);
}
/**
* Creates a new buffer with the specified {@code endianness} whose
* content is the specified {@code string} encoded in the specified
* {@code charset}. The new buffer's {@code readerIndex} and
* {@code writerIndex} are {@code 0} and the length of the encoded string
* respectively.
*/
public static ByteBuf copiedBuffer(ByteOrder endianness, CharSequence string, Charset charset) {
if (string == null) {
throw new NullPointerException("string");
}
if (string instanceof CharBuffer) {
return copiedBuffer(endianness, (CharBuffer) string, charset);
}
return copiedBuffer(endianness, CharBuffer.wrap(string), charset);
}
/**
* Creates a new buffer with the specified {@code endianness} whose
* content is a subregion of the specified {@code string} encoded in the
* specified {@code charset}. The new buffer's {@code readerIndex} and
* {@code writerIndex} are {@code 0} and the length of the encoded string
* respectively.
*/
public static ByteBuf copiedBuffer(
ByteOrder endianness, CharSequence string, int offset, int length, Charset charset) {
if (string == null) {
throw new NullPointerException("string");
}
@ -747,7 +665,6 @@ public final class Unpooled {
CharBuffer buf = (CharBuffer) string;
if (buf.hasArray()) {
return copiedBuffer(
endianness,
buf.array(),
buf.arrayOffset() + buf.position() + offset,
length, charset);
@ -756,12 +673,10 @@ public final class Unpooled {
buf = buf.slice();
buf.limit(length);
buf.position(offset);
return copiedBuffer(endianness, buf, charset);
return copiedBuffer(buf, charset);
}
return copiedBuffer(
endianness, CharBuffer.wrap(string, offset, offset + length),
charset);
return copiedBuffer(CharBuffer.wrap(string, offset, offset + length), charset);
}
/**
@ -771,7 +686,7 @@ public final class Unpooled {
* {@code 0} and the length of the encoded string respectively.
*/
public static ByteBuf copiedBuffer(char[] array, Charset charset) {
return copiedBuffer(BIG_ENDIAN, array, 0, array.length, charset);
return copiedBuffer(array, 0, array.length, charset);
}
/**
@ -780,44 +695,19 @@ public final class Unpooled {
* The new buffer's {@code readerIndex} and {@code writerIndex} are
* {@code 0} and the length of the encoded string respectively.
*/
public static ByteBuf copiedBuffer(
char[] array, int offset, int length, Charset charset) {
return copiedBuffer(BIG_ENDIAN, array, offset, length, charset);
}
/**
* Creates a new buffer with the specified {@code endianness} whose
* content is the specified {@code array} encoded in the specified
* {@code charset}. The new buffer's {@code readerIndex} and
* {@code writerIndex} are {@code 0} and the length of the encoded string
* respectively.
*/
public static ByteBuf copiedBuffer(ByteOrder endianness, char[] array, Charset charset) {
return copiedBuffer(endianness, array, 0, array.length, charset);
}
/**
* Creates a new buffer with the specified {@code endianness} whose
* content is a subregion of the specified {@code array} encoded in the
* specified {@code charset}. The new buffer's {@code readerIndex} and
* {@code writerIndex} are {@code 0} and the length of the encoded string
* respectively.
*/
public static ByteBuf copiedBuffer(
ByteOrder endianness, char[] array, int offset, int length, Charset charset) {
public static ByteBuf copiedBuffer(char[] array, int offset, int length, Charset charset) {
if (array == null) {
throw new NullPointerException("array");
}
if (length == 0) {
return EMPTY_BUFFER;
}
return copiedBuffer(
endianness, CharBuffer.wrap(array, offset, length), charset);
return copiedBuffer(CharBuffer.wrap(array, offset, length), charset);
}
private static ByteBuf copiedBuffer(ByteOrder endianness, CharBuffer buffer, Charset charset) {
private static ByteBuf copiedBuffer(CharBuffer buffer, Charset charset) {
ByteBuffer dst = Unpooled.encodeString(buffer, charset);
ByteBuf result = wrappedBuffer(endianness, dst.array());
ByteBuf result = wrappedBuffer(dst.array());
result.writerIndex(dst.remaining());
return result;
}
@ -836,7 +726,7 @@ public final class Unpooled {
}
/**
* Creates a new 4-byte buffer that holds the specified 32-bit integer.
* Creates a new 4-byte big-endian buffer that holds the specified 32-bit integer.
*/
public static ByteBuf copyInt(int value) {
ByteBuf buf = buffer(4);
@ -845,7 +735,7 @@ public final class Unpooled {
}
/**
* Create a {@link ByteBuf} that holds all the given values as int's
* Create a big-endian buffer that holds a sequence of the specified 32-bit integers.
*/
public static ByteBuf copyInt(int... values) {
if (values == null || values.length == 0) {
@ -859,7 +749,7 @@ public final class Unpooled {
}
/**
* Creates a new 2-byte buffer that holds the specified 16-bit integer.
* Creates a new 2-byte big-endian buffer that holds the specified 16-bit integer.
*/
public static ByteBuf copyShort(int value) {
ByteBuf buf = buffer(2);
@ -868,7 +758,7 @@ public final class Unpooled {
}
/**
* Create a new buffer that holds a sequence of the specified 16-bit integers.
* Create a new big-endian buffer that holds a sequence of the specified 16-bit integers.
*/
public static ByteBuf copyShort(short... values) {
if (values == null || values.length == 0) {
@ -882,7 +772,7 @@ public final class Unpooled {
}
/**
* Create a new buffer that holds a sequence of the specified 16-bit integers.
* Create a new big-endian buffer that holds a sequence of the specified 16-bit integers.
*/
public static ByteBuf copyShort(int... values) {
if (values == null || values.length == 0) {
@ -896,7 +786,7 @@ public final class Unpooled {
}
/**
* Creates a new 3-byte buffer that holds the specified 24-bit integer.
* Creates a new 3-byte big-endian buffer that holds the specified 24-bit integer.
*/
public static ByteBuf copyMedium(int value) {
ByteBuf buf = buffer(3);
@ -905,7 +795,7 @@ public final class Unpooled {
}
/**
* Create a new buffer that holds a sequence of the specified 24-bit integers.
* Create a new big-endian buffer that holds a sequence of the specified 24-bit integers.
*/
public static ByteBuf copyMedium(int... values) {
if (values == null || values.length == 0) {
@ -919,7 +809,7 @@ public final class Unpooled {
}
/**
* Creates a new 8-byte buffer that holds the specified 64-bit integer.
* Creates a new 8-byte big-endian buffer that holds the specified 64-bit integer.
*/
public static ByteBuf copyLong(long value) {
ByteBuf buf = buffer(8);
@ -928,7 +818,7 @@ public final class Unpooled {
}
/**
* Create a new buffer that holds a sequence of the specified 64-bit integers.
* Create a new big-endian buffer that holds a sequence of the specified 64-bit integers.
*/
public static ByteBuf copyLong(long... values) {
if (values == null || values.length == 0) {
@ -942,7 +832,7 @@ public final class Unpooled {
}
/**
* Creates a new single-byte buffer that holds the specified boolean value.
* Creates a new single-byte big-endian buffer that holds the specified boolean value.
*/
public static ByteBuf copyBoolean(boolean value) {
ByteBuf buf = buffer(1);
@ -951,7 +841,7 @@ public final class Unpooled {
}
/**
* Create a new buffer that holds a sequence of the specified boolean values.
* Create a new big-endian buffer that holds a sequence of the specified boolean values.
*/
public static ByteBuf copyBoolean(boolean... values) {
if (values == null || values.length == 0) {
@ -965,7 +855,7 @@ public final class Unpooled {
}
/**
* Creates a new 4-byte buffer that holds the specified 32-bit floating point number.
* Creates a new 4-byte big-endian buffer that holds the specified 32-bit floating point number.
*/
public static ByteBuf copyFloat(float value) {
ByteBuf buf = buffer(4);
@ -974,7 +864,7 @@ public final class Unpooled {
}
/**
* Create a new buffer that holds a sequence of the specified 32-bit floating point numbers.
* Create a new big-endian buffer that holds a sequence of the specified 32-bit floating point numbers.
*/
public static ByteBuf copyFloat(float... values) {
if (values == null || values.length == 0) {
@ -988,7 +878,7 @@ public final class Unpooled {
}
/**
* Creates a new 8-byte buffer that holds the specified 64-bit floating point number.
* Creates a new 8-byte big-endian buffer that holds the specified 64-bit floating point number.
*/
public static ByteBuf copyDouble(double value) {
ByteBuf buf = buffer(8);
@ -997,7 +887,7 @@ public final class Unpooled {
}
/**
* Create a new buffer that holds a sequence of the specified 64-bit floating point numbers.
* Create a new big-endian buffer that holds a sequence of the specified 64-bit floating point numbers.
*/
public static ByteBuf copyDouble(double... values) {
if (values == null || values.length == 0) {
@ -1214,7 +1104,11 @@ public final class Unpooled {
* Toggles the endianness of the specified 24-bit medium integer.
*/
public static int swapMedium(int value) {
return value << 16 & 0xff0000 | value & 0xff00 | value >>> 16 & 0xff;
int swapped = value << 16 & 0xff0000 | value & 0xff00 | value >>> 16 & 0xff;
if ((swapped & 0x800000) != 0) {
swapped |= 0xff000000;
}
return swapped;
}
/**

View File

@ -1458,12 +1458,12 @@ public abstract class AbstractChannelBufferTest {
random.nextBytes(value);
buffer.setBytes(0, value);
assertEquals(buffer, wrappedBuffer(BIG_ENDIAN, value));
assertEquals(buffer, wrappedBuffer(LITTLE_ENDIAN, value));
assertEquals(buffer, wrappedBuffer(value));
assertEquals(buffer, wrappedBuffer(value).order(LITTLE_ENDIAN));
value[0] ++;
assertFalse(buffer.equals(wrappedBuffer(BIG_ENDIAN, value)));
assertFalse(buffer.equals(wrappedBuffer(LITTLE_ENDIAN, value)));
assertFalse(buffer.equals(wrappedBuffer(value)));
assertFalse(buffer.equals(wrappedBuffer(value).order(LITTLE_ENDIAN)));
}
@Test
@ -1489,21 +1489,21 @@ public abstract class AbstractChannelBufferTest {
buffer.setBytes(0, value);
assertEquals(0, buffer.compareTo(wrappedBuffer(BIG_ENDIAN, value)));
assertEquals(0, buffer.compareTo(wrappedBuffer(LITTLE_ENDIAN, value)));
assertEquals(0, buffer.compareTo(wrappedBuffer(value)));
assertEquals(0, buffer.compareTo(wrappedBuffer(value).order(LITTLE_ENDIAN)));
value[0] ++;
assertTrue(buffer.compareTo(wrappedBuffer(BIG_ENDIAN, value)) < 0);
assertTrue(buffer.compareTo(wrappedBuffer(LITTLE_ENDIAN, value)) < 0);
assertTrue(buffer.compareTo(wrappedBuffer(value)) < 0);
assertTrue(buffer.compareTo(wrappedBuffer(value).order(LITTLE_ENDIAN)) < 0);
value[0] -= 2;
assertTrue(buffer.compareTo(wrappedBuffer(BIG_ENDIAN, value)) > 0);
assertTrue(buffer.compareTo(wrappedBuffer(LITTLE_ENDIAN, value)) > 0);
assertTrue(buffer.compareTo(wrappedBuffer(value)) > 0);
assertTrue(buffer.compareTo(wrappedBuffer(value).order(LITTLE_ENDIAN)) > 0);
value[0] ++;
assertTrue(buffer.compareTo(wrappedBuffer(BIG_ENDIAN, value, 0, 31)) > 0);
assertTrue(buffer.compareTo(wrappedBuffer(LITTLE_ENDIAN, value, 0, 31)) > 0);
assertTrue(buffer.slice(0, 31).compareTo(wrappedBuffer(BIG_ENDIAN, value)) < 0);
assertTrue(buffer.slice(0, 31).compareTo(wrappedBuffer(LITTLE_ENDIAN, value)) < 0);
assertTrue(buffer.compareTo(wrappedBuffer(value, 0, 31)) > 0);
assertTrue(buffer.compareTo(wrappedBuffer(value, 0, 31).order(LITTLE_ENDIAN)) > 0);
assertTrue(buffer.slice(0, 31).compareTo(wrappedBuffer(value)) < 0);
assertTrue(buffer.slice(0, 31).compareTo(wrappedBuffer(value).order(LITTLE_ENDIAN)) < 0);
}
@Test

View File

@ -48,23 +48,23 @@ public abstract class AbstractCompositeChannelBufferTest extends
buffers = new ArrayList<ByteBuf>();
for (int i = 0; i < length; i += 10) {
buffers.add(Unpooled.EMPTY_BUFFER);
buffers.add(Unpooled.wrappedBuffer(order, new byte[1]));
buffers.add(Unpooled.wrappedBuffer(new byte[1]).order(order));
buffers.add(Unpooled.EMPTY_BUFFER);
buffers.add(Unpooled.wrappedBuffer(order, new byte[2]));
buffers.add(Unpooled.wrappedBuffer(new byte[2]).order(order));
buffers.add(Unpooled.EMPTY_BUFFER);
buffers.add(Unpooled.wrappedBuffer(order, new byte[3]));
buffers.add(Unpooled.wrappedBuffer(new byte[3]).order(order));
buffers.add(Unpooled.EMPTY_BUFFER);
buffers.add(Unpooled.wrappedBuffer(order, new byte[4]));
buffers.add(Unpooled.wrappedBuffer(new byte[4]).order(order));
buffers.add(Unpooled.EMPTY_BUFFER);
buffers.add(Unpooled.wrappedBuffer(order, new byte[5]));
buffers.add(Unpooled.wrappedBuffer(new byte[5]).order(order));
buffers.add(Unpooled.EMPTY_BUFFER);
buffers.add(Unpooled.wrappedBuffer(order, new byte[6]));
buffers.add(Unpooled.wrappedBuffer(new byte[6]).order(order));
buffers.add(Unpooled.EMPTY_BUFFER);
buffers.add(Unpooled.wrappedBuffer(order, new byte[7]));
buffers.add(Unpooled.wrappedBuffer(new byte[7]).order(order));
buffers.add(Unpooled.EMPTY_BUFFER);
buffers.add(Unpooled.wrappedBuffer(order, new byte[8]));
buffers.add(Unpooled.wrappedBuffer(new byte[8]).order(order));
buffers.add(Unpooled.EMPTY_BUFFER);
buffers.add(Unpooled.wrappedBuffer(order, new byte[9]));
buffers.add(Unpooled.wrappedBuffer(new byte[9]).order(order));
buffers.add(Unpooled.EMPTY_BUFFER);
}
@ -93,10 +93,10 @@ public abstract class AbstractCompositeChannelBufferTest extends
@Test
public void testDiscardReadBytes3() {
ByteBuf a, b;
a = wrappedBuffer(order, new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).order(order);
b = wrappedBuffer(
wrappedBuffer(order, new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 5),
wrappedBuffer(order, new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 5, 5));
wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 5).order(order),
wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 5, 5).order(order));
a.skipBytes(6);
a.markReaderIndex();
b.skipBytes(6);
@ -131,8 +131,8 @@ public abstract class AbstractCompositeChannelBufferTest extends
@Test
public void testCompositeWrappedBuffer() {
ByteBuf header = dynamicBuffer(order, 12);
ByteBuf payload = dynamicBuffer(order, 512);
ByteBuf header = dynamicBuffer(12).order(order);
ByteBuf payload = dynamicBuffer(512).order(order);
header.writeBytes(new byte[12]);
payload.writeBytes(new byte[512]);
@ -151,59 +151,59 @@ public abstract class AbstractCompositeChannelBufferTest extends
ByteBuf a, b;
//XXX Same tests with several buffers in wrappedCheckedBuffer
// Different length.
a = wrappedBuffer(order, new byte[] { 1 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 1 }),
wrappedBuffer(order, new byte[] { 2 }));
a = wrappedBuffer(new byte[] { 1 }).order(order);
b = wrappedBuffer(wrappedBuffer(new byte[] { 1 }).order(order),
wrappedBuffer(new byte[] { 2 }).order(order));
assertFalse(Unpooled.equals(a, b));
// Same content, same firstIndex, short length.
a = wrappedBuffer(order, new byte[] { 1, 2, 3 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 1 }),
wrappedBuffer(order, new byte[] { 2 }),
wrappedBuffer(order, new byte[] { 3 }));
a = wrappedBuffer(new byte[] { 1, 2, 3 }).order(order);
b = wrappedBuffer(wrappedBuffer(new byte[] { 1 }).order(order),
wrappedBuffer(new byte[] { 2 }).order(order),
wrappedBuffer(new byte[] { 3 }).order(order));
assertTrue(Unpooled.equals(a, b));
// Same content, different firstIndex, short length.
a = wrappedBuffer(order, new byte[] { 1, 2, 3 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 0, 1, 2, 3, 4 }, 1, 2),
wrappedBuffer(order, new byte[] { 0, 1, 2, 3, 4 }, 3, 1));
a = wrappedBuffer(new byte[] { 1, 2, 3 }).order(order);
b = wrappedBuffer(wrappedBuffer(new byte[] { 0, 1, 2, 3, 4 }, 1, 2).order(order),
wrappedBuffer(new byte[] { 0, 1, 2, 3, 4 }, 3, 1).order(order));
assertTrue(Unpooled.equals(a, b));
// Different content, same firstIndex, short length.
a = wrappedBuffer(order, new byte[] { 1, 2, 3 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 1, 2 }),
wrappedBuffer(order, new byte[] { 4 }));
a = wrappedBuffer(new byte[] { 1, 2, 3 }).order(order);
b = wrappedBuffer(wrappedBuffer(new byte[] { 1, 2 }).order(order),
wrappedBuffer(new byte[] { 4 }).order(order));
assertFalse(Unpooled.equals(a, b));
// Different content, different firstIndex, short length.
a = wrappedBuffer(order, new byte[] { 1, 2, 3 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 0, 1, 2, 4, 5 }, 1, 2),
wrappedBuffer(order, new byte[] { 0, 1, 2, 4, 5 }, 3, 1));
a = wrappedBuffer(new byte[] { 1, 2, 3 }).order(order);
b = wrappedBuffer(wrappedBuffer(new byte[] { 0, 1, 2, 4, 5 }, 1, 2).order(order),
wrappedBuffer(new byte[] { 0, 1, 2, 4, 5 }, 3, 1).order(order));
assertFalse(Unpooled.equals(a, b));
// Same content, same firstIndex, long length.
a = wrappedBuffer(order, new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 1, 2, 3 }),
wrappedBuffer(order, new byte[] { 4, 5, 6 }),
wrappedBuffer(order, new byte[] { 7, 8, 9, 10 }));
a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).order(order);
b = wrappedBuffer(wrappedBuffer(new byte[] { 1, 2, 3 }).order(order),
wrappedBuffer(new byte[] { 4, 5, 6 }).order(order),
wrappedBuffer(new byte[] { 7, 8, 9, 10 }).order(order));
assertTrue(Unpooled.equals(a, b));
// Same content, different firstIndex, long length.
a = wrappedBuffer(order, new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 1, 5),
wrappedBuffer(order, new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 6, 5));
a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).order(order);
b = wrappedBuffer(wrappedBuffer(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 1, 5).order(order),
wrappedBuffer(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 6, 5).order(order));
assertTrue(Unpooled.equals(a, b));
// Different content, same firstIndex, long length.
a = wrappedBuffer(order, new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 1, 2, 3, 4, 6 }),
wrappedBuffer(order, new byte[] { 7, 8, 5, 9, 10 }));
a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).order(order);
b = wrappedBuffer(wrappedBuffer(new byte[] { 1, 2, 3, 4, 6 }).order(order),
wrappedBuffer(new byte[] { 7, 8, 5, 9, 10 }).order(order));
assertFalse(Unpooled.equals(a, b));
// Different content, different firstIndex, long length.
a = wrappedBuffer(order, new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 0, 1, 2, 3, 4, 6, 7, 8, 5, 9, 10, 11 }, 1, 5),
wrappedBuffer(order, new byte[] { 0, 1, 2, 3, 4, 6, 7, 8, 5, 9, 10, 11 }, 6, 5));
a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).order(order);
b = wrappedBuffer(wrappedBuffer(new byte[] { 0, 1, 2, 3, 4, 6, 7, 8, 5, 9, 10, 11 }, 1, 5).order(order),
wrappedBuffer(new byte[] { 0, 1, 2, 3, 4, 6, 7, 8, 5, 9, 10, 11 }, 6, 5).order(order));
assertFalse(Unpooled.equals(a, b));
}
@Test
@ -212,37 +212,37 @@ public abstract class AbstractCompositeChannelBufferTest extends
assertEquals(16, wrappedBuffer(wrappedBuffer(ByteBuffer.allocateDirect(16))).capacity());
assertEquals(
wrappedBuffer(wrappedBuffer(order, new byte[] { 1, 2, 3 })),
wrappedBuffer(wrappedBuffer(order, new byte[][] { new byte[] { 1, 2, 3 } })));
wrappedBuffer(wrappedBuffer(new byte[] { 1, 2, 3 }).order(order)),
wrappedBuffer(wrappedBuffer(new byte[][] { new byte[] { 1, 2, 3 } }).order(order)));
assertEquals(
wrappedBuffer(wrappedBuffer(order, new byte[] { 1, 2, 3 })),
wrappedBuffer(wrappedBuffer(order,
wrappedBuffer(wrappedBuffer(new byte[] { 1, 2, 3 }).order(order)),
wrappedBuffer(wrappedBuffer(
new byte[] { 1 },
new byte[] { 2 },
new byte[] { 3 })));
new byte[] { 3 }).order(order)));
assertEquals(
wrappedBuffer(wrappedBuffer(order, new byte[] { 1, 2, 3 })),
wrappedBuffer(wrappedBuffer(new byte[] { 1, 2, 3 }).order(order)),
wrappedBuffer(new ByteBuf[] {
wrappedBuffer(order, new byte[] { 1, 2, 3 })
wrappedBuffer(new byte[] { 1, 2, 3 }).order(order)
}));
assertEquals(
wrappedBuffer(wrappedBuffer(order, new byte[] { 1, 2, 3 })),
wrappedBuffer(wrappedBuffer(new byte[] { 1, 2, 3 }).order(order)),
wrappedBuffer(
wrappedBuffer(order, new byte[] { 1 }),
wrappedBuffer(order, new byte[] { 2 }),
wrappedBuffer(order, new byte[] { 3 })));
wrappedBuffer(new byte[] { 1 }).order(order),
wrappedBuffer(new byte[] { 2 }).order(order),
wrappedBuffer(new byte[] { 3 }).order(order)));
assertEquals(
wrappedBuffer(wrappedBuffer(order, new byte[] { 1, 2, 3 })),
wrappedBuffer(wrappedBuffer(new byte[] { 1, 2, 3 }).order(order)),
wrappedBuffer(wrappedBuffer(new ByteBuffer[] {
ByteBuffer.wrap(new byte[] { 1, 2, 3 })
})));
assertEquals(
wrappedBuffer(wrappedBuffer(order, new byte[] { 1, 2, 3 })),
wrappedBuffer(wrappedBuffer(new byte[] { 1, 2, 3 }).order(order)),
wrappedBuffer(wrappedBuffer(
ByteBuffer.wrap(new byte[] { 1 }),
ByteBuffer.wrap(new byte[] { 2 }),
@ -253,87 +253,87 @@ public abstract class AbstractCompositeChannelBufferTest extends
//XXX Same tests than testEquals with written AggregateChannelBuffers
ByteBuf a, b;
// Different length.
a = wrappedBuffer(order, new byte[] { 1 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 1 }, new byte[1]));
a = wrappedBuffer(new byte[] { 1 }).order(order);
b = wrappedBuffer(wrappedBuffer(new byte[] { 1 }, new byte[1]).order(order));
// to enable writeBytes
b.writerIndex(b.writerIndex() - 1);
b.writeBytes(
wrappedBuffer(order, new byte[] { 2 }));
wrappedBuffer(new byte[] { 2 }).order(order));
assertFalse(Unpooled.equals(a, b));
// Same content, same firstIndex, short length.
a = wrappedBuffer(order, new byte[] { 1, 2, 3 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 1 }, new byte[2]));
a = wrappedBuffer(new byte[] { 1, 2, 3 }).order(order);
b = wrappedBuffer(wrappedBuffer(new byte[] { 1 }, new byte[2]).order(order));
// to enable writeBytes
b.writerIndex(b.writerIndex() - 2);
b.writeBytes(
wrappedBuffer(order, new byte[] { 2 }));
b.writeBytes(wrappedBuffer(order, new byte[] { 3 }));
wrappedBuffer(new byte[] { 2 }).order(order));
b.writeBytes(wrappedBuffer(new byte[] { 3 }).order(order));
assertTrue(Unpooled.equals(a, b));
// Same content, different firstIndex, short length.
a = wrappedBuffer(order, new byte[] { 1, 2, 3 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 0, 1, 2, 3, 4 }, 1, 3));
a = wrappedBuffer(new byte[] { 1, 2, 3 }).order(order);
b = wrappedBuffer(wrappedBuffer(new byte[] { 0, 1, 2, 3, 4 }, 1, 3).order(order));
// to enable writeBytes
b.writerIndex(b.writerIndex() - 1);
b.writeBytes(
wrappedBuffer(order, new byte[] { 0, 1, 2, 3, 4 }, 3, 1));
wrappedBuffer(new byte[] { 0, 1, 2, 3, 4 }, 3, 1).order(order));
assertTrue(Unpooled.equals(a, b));
// Different content, same firstIndex, short length.
a = wrappedBuffer(order, new byte[] { 1, 2, 3 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 1, 2 }, new byte[1]));
a = wrappedBuffer(new byte[] { 1, 2, 3 }).order(order);
b = wrappedBuffer(wrappedBuffer(new byte[] { 1, 2 }, new byte[1]).order(order));
// to enable writeBytes
b.writerIndex(b.writerIndex() - 1);
b.writeBytes(
wrappedBuffer(order, new byte[] { 4 }));
wrappedBuffer(new byte[] { 4 }).order(order));
assertFalse(Unpooled.equals(a, b));
// Different content, different firstIndex, short length.
a = wrappedBuffer(order, new byte[] { 1, 2, 3 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 0, 1, 2, 4, 5 }, 1, 3));
a = wrappedBuffer(new byte[] { 1, 2, 3 }).order(order);
b = wrappedBuffer(wrappedBuffer(new byte[] { 0, 1, 2, 4, 5 }, 1, 3).order(order));
// to enable writeBytes
b.writerIndex(b.writerIndex() - 1);
b.writeBytes(
wrappedBuffer(order, new byte[] { 0, 1, 2, 4, 5 }, 3, 1));
wrappedBuffer(new byte[] { 0, 1, 2, 4, 5 }, 3, 1).order(order));
assertFalse(Unpooled.equals(a, b));
// Same content, same firstIndex, long length.
a = wrappedBuffer(order, new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 1, 2, 3 }, new byte[7]));
a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).order(order);
b = wrappedBuffer(wrappedBuffer(new byte[] { 1, 2, 3 }, new byte[7]).order(order));
// to enable writeBytes
b.writerIndex(b.writerIndex() - 7);
b.writeBytes(
wrappedBuffer(order, new byte[] { 4, 5, 6 }));
wrappedBuffer(new byte[] { 4, 5, 6 }).order(order));
b.writeBytes(
wrappedBuffer(order, new byte[] { 7, 8, 9, 10 }));
wrappedBuffer(new byte[] { 7, 8, 9, 10 }).order(order));
assertTrue(Unpooled.equals(a, b));
// Same content, different firstIndex, long length.
a = wrappedBuffer(order, new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 1, 10));
a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).order(order);
b = wrappedBuffer(wrappedBuffer(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 1, 10).order(order));
// to enable writeBytes
b.writerIndex(b.writerIndex() - 5);
b.writeBytes(
wrappedBuffer(order, new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 6, 5));
wrappedBuffer(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 6, 5).order(order));
assertTrue(Unpooled.equals(a, b));
// Different content, same firstIndex, long length.
a = wrappedBuffer(order, new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 1, 2, 3, 4, 6 }, new byte[5]));
a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).order(order);
b = wrappedBuffer(wrappedBuffer(new byte[] { 1, 2, 3, 4, 6 }, new byte[5]).order(order));
// to enable writeBytes
b.writerIndex(b.writerIndex() - 5);
b.writeBytes(
wrappedBuffer(order, new byte[] { 7, 8, 5, 9, 10 }));
wrappedBuffer(new byte[] { 7, 8, 5, 9, 10 }).order(order));
assertFalse(Unpooled.equals(a, b));
// Different content, different firstIndex, long length.
a = wrappedBuffer(order, new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 0, 1, 2, 3, 4, 6, 7, 8, 5, 9, 10, 11 }, 1, 10));
a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).order(order);
b = wrappedBuffer(wrappedBuffer(new byte[] { 0, 1, 2, 3, 4, 6, 7, 8, 5, 9, 10, 11 }, 1, 10).order(order));
// to enable writeBytes
b.writerIndex(b.writerIndex() - 5);
b.writeBytes(
wrappedBuffer(order, new byte[] { 0, 1, 2, 3, 4, 6, 7, 8, 5, 9, 10, 11 }, 6, 5));
wrappedBuffer(new byte[] { 0, 1, 2, 3, 4, 6, 7, 8, 5, 9, 10, 11 }, 6, 5).order(order));
assertFalse(Unpooled.equals(a, b));
}
}

View File

@ -28,7 +28,7 @@ public class BigEndianDirectChannelBufferTest extends AbstractChannelBufferTest
@Override
protected ByteBuf newBuffer(int length) {
buffer = Unpooled.directBuffer(ByteOrder.BIG_ENDIAN, length);
buffer = Unpooled.directBuffer(length);
assertSame(ByteOrder.BIG_ENDIAN, buffer.order());
assertEquals(0, buffer.writerIndex());
return buffer;

View File

@ -40,6 +40,6 @@ public class BigEndianHeapChannelBufferTest extends AbstractChannelBufferTest {
@Test(expected = NullPointerException.class)
public void shouldNotAllowNullInConstructor() {
new BigEndianHeapByteBuf(null);
new HeapByteBuf(null);
}
}

View File

@ -157,15 +157,15 @@ public class ChannelBuffersTest {
@Test
public void shouldReturnEmptyBufferWhenLengthIsZero() {
assertSame(EMPTY_BUFFER, buffer(0));
assertSame(EMPTY_BUFFER, buffer(LITTLE_ENDIAN, 0));
assertSame(EMPTY_BUFFER, buffer(0).order(LITTLE_ENDIAN));
assertSame(EMPTY_BUFFER, directBuffer(0));
assertSame(EMPTY_BUFFER, wrappedBuffer(new byte[0]));
assertSame(EMPTY_BUFFER, wrappedBuffer(LITTLE_ENDIAN, new byte[0]));
assertSame(EMPTY_BUFFER, wrappedBuffer(new byte[0]).order(LITTLE_ENDIAN));
assertSame(EMPTY_BUFFER, wrappedBuffer(new byte[8], 0, 0));
assertSame(EMPTY_BUFFER, wrappedBuffer(LITTLE_ENDIAN, new byte[8], 0, 0));
assertSame(EMPTY_BUFFER, wrappedBuffer(new byte[8], 0, 0).order(LITTLE_ENDIAN));
assertSame(EMPTY_BUFFER, wrappedBuffer(new byte[8], 8, 0));
assertSame(EMPTY_BUFFER, wrappedBuffer(LITTLE_ENDIAN, new byte[8], 8, 0));
assertSame(EMPTY_BUFFER, wrappedBuffer(new byte[8], 8, 0).order(LITTLE_ENDIAN));
assertSame(EMPTY_BUFFER, wrappedBuffer(ByteBuffer.allocateDirect(0)));
assertSame(EMPTY_BUFFER, wrappedBuffer(EMPTY_BUFFER));
assertSame(EMPTY_BUFFER, wrappedBuffer(new byte[0][]));
@ -178,11 +178,11 @@ public class ChannelBuffersTest {
assertSame(EMPTY_BUFFER, wrappedBuffer(buffer(0), buffer(0)));
assertSame(EMPTY_BUFFER, copiedBuffer(new byte[0]));
assertSame(EMPTY_BUFFER, copiedBuffer(LITTLE_ENDIAN, new byte[0]));
assertSame(EMPTY_BUFFER, copiedBuffer(new byte[0]).order(LITTLE_ENDIAN));
assertSame(EMPTY_BUFFER, copiedBuffer(new byte[8], 0, 0));
assertSame(EMPTY_BUFFER, copiedBuffer(LITTLE_ENDIAN, new byte[8], 0, 0));
assertSame(EMPTY_BUFFER, copiedBuffer(new byte[8], 0, 0).order(LITTLE_ENDIAN));
assertSame(EMPTY_BUFFER, copiedBuffer(new byte[8], 8, 0));
assertSame(EMPTY_BUFFER, copiedBuffer(LITTLE_ENDIAN, new byte[8], 8, 0));
assertSame(EMPTY_BUFFER, copiedBuffer(new byte[8], 8, 0).order(LITTLE_ENDIAN));
assertSame(EMPTY_BUFFER, copiedBuffer(ByteBuffer.allocateDirect(0)));
assertSame(EMPTY_BUFFER, copiedBuffer(EMPTY_BUFFER));
assertSame(EMPTY_BUFFER, copiedBuffer(new byte[0][]));
@ -208,31 +208,11 @@ public class ChannelBuffersTest {
> 0);
}
@Test(expected = NullPointerException.class)
public void shouldDisallowNullEndian1() {
buffer(null, 0);
}
@Test(expected = NullPointerException.class)
public void shouldDisallowNullEndian2() {
directBuffer(null, 0);
}
@Test(expected = NullPointerException.class)
public void shouldDisallowNullEndian3() {
wrappedBuffer(null, new byte[0]);
}
@Test(expected = NullPointerException.class)
public void shouldDisallowNullEndian4() {
wrappedBuffer(null, new byte[0], 0, 0);
}
@Test
public void shouldAllowEmptyBufferToCreateCompositeBuffer() {
ByteBuf buf = wrappedBuffer(
EMPTY_BUFFER,
wrappedBuffer(LITTLE_ENDIAN, new byte[16]),
wrappedBuffer(new byte[16]).order(LITTLE_ENDIAN),
EMPTY_BUFFER);
assertEquals(16, buf.capacity());
}

View File

@ -17,8 +17,6 @@ package io.netty.buffer;
import static org.junit.Assert.*;
import java.nio.ByteOrder;
import org.junit.Test;
/**
@ -44,24 +42,19 @@ public class DynamicChannelBufferTest extends AbstractChannelBufferTest {
return new ByteBuf[] { buffer };
}
@Test(expected = NullPointerException.class)
public void shouldNotAllowNullInConstructor() {
new DynamicByteBuf(null, 0);
}
@Test
public void shouldNotFailOnInitialIndexUpdate() {
new DynamicByteBuf(ByteOrder.BIG_ENDIAN, 10).setIndex(0, 10);
new DynamicByteBuf(10).setIndex(0, 10);
}
@Test
public void shouldNotFailOnInitialIndexUpdate2() {
new DynamicByteBuf(ByteOrder.BIG_ENDIAN, 10).writerIndex(10);
new DynamicByteBuf(10).writerIndex(10);
}
@Test
public void shouldNotFailOnInitialIndexUpdate3() {
ByteBuf buf = new DynamicByteBuf(ByteOrder.BIG_ENDIAN, 10);
ByteBuf buf = new DynamicByteBuf(10);
buf.writerIndex(10);
buf.readerIndex(10);
}

View File

@ -28,7 +28,7 @@ public class LittleEndianDirectChannelBufferTest extends AbstractChannelBufferTe
@Override
protected ByteBuf newBuffer(int length) {
buffer = Unpooled.directBuffer(ByteOrder.LITTLE_ENDIAN, length);
buffer = Unpooled.directBuffer(length).order(ByteOrder.LITTLE_ENDIAN);
assertSame(ByteOrder.LITTLE_ENDIAN, buffer.order());
assertEquals(0, buffer.writerIndex());
return buffer;

View File

@ -19,8 +19,6 @@ import static org.junit.Assert.*;
import java.nio.ByteOrder;
import org.junit.Test;
/**
* Tests little-endian heap channel buffers
*/
@ -30,7 +28,7 @@ public class LittleEndianHeapChannelBufferTest extends AbstractChannelBufferTest
@Override
protected ByteBuf newBuffer(int length) {
buffer = Unpooled.buffer(ByteOrder.LITTLE_ENDIAN, length);
buffer = Unpooled.buffer(length).order(ByteOrder.LITTLE_ENDIAN);
assertEquals(0, buffer.writerIndex());
return buffer;
}
@ -39,9 +37,4 @@ public class LittleEndianHeapChannelBufferTest extends AbstractChannelBufferTest
protected ByteBuf[] components() {
return new ByteBuf[] { buffer };
}
@Test(expected = NullPointerException.class)
public void shouldNotAllowNullInConstructor() {
new LittleEndianHeapByteBuf(null);
}
}

View File

@ -51,7 +51,9 @@ public class ReadOnlyChannelBufferTest {
@Test
public void shouldHaveSameByteOrder() {
ByteBuf buf = Unpooled.buffer(Unpooled.LITTLE_ENDIAN, 1);
ByteBuf buf = Unpooled.buffer(1);
assertSame(Unpooled.BIG_ENDIAN, Unpooled.unmodifiableBuffer(buf).order());
buf = buf.order(LITTLE_ENDIAN);
assertSame(Unpooled.LITTLE_ENDIAN, Unpooled.unmodifiableBuffer(buf).order());
}
@ -73,6 +75,7 @@ public class ReadOnlyChannelBufferTest {
@Test
public void shouldForwardReadCallsBlindly() throws Exception {
ByteBuf buf = createStrictMock(ByteBuf.class);
expect(buf.order()).andReturn(BIG_ENDIAN).anyTimes();
expect(buf.readerIndex()).andReturn(0).anyTimes();
expect(buf.writerIndex()).andReturn(0).anyTimes();
expect(buf.capacity()).andReturn(0).anyTimes();

View File

@ -24,7 +24,6 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.handler.codec.UnsupportedMessageTypeException;
import java.nio.ByteOrder;
import java.util.Set;
/**
@ -311,8 +310,7 @@ public class SpdyFrameEncoder extends MessageToByteEncoder<Object> {
throw new IllegalArgumentException(
"header block contains too many headers");
}
ByteBuf headerBlock = Unpooled.dynamicBuffer(
ByteOrder.BIG_ENDIAN, 256);
ByteBuf headerBlock = Unpooled.dynamicBuffer(256);
writeLengthField(version, headerBlock, numHeaders);
for (String name: names) {
byte[] nameBytes = name.getBytes("UTF-8");

View File

@ -18,6 +18,7 @@ package io.netty.handler.codec;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufFactory;
import io.netty.buffer.ByteBufIndexFinder;
import io.netty.buffer.SwappedByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.Signal;
@ -35,6 +36,7 @@ class ReplayingDecoderBuffer implements ByteBuf {
private static final Signal REPLAY = ReplayingDecoder.REPLAY;
private final ByteBuf buffer;
private final SwappedByteBuf swapped;
private boolean terminated;
public static ReplayingDecoderBuffer EMPTY_BUFFER = new ReplayingDecoderBuffer(Unpooled.EMPTY_BUFFER);
@ -45,6 +47,7 @@ class ReplayingDecoderBuffer implements ByteBuf {
ReplayingDecoderBuffer(ByteBuf buffer) {
this.buffer = buffer;
swapped = new SwappedByteBuf(this);
}
void terminate() {
@ -350,6 +353,17 @@ class ReplayingDecoderBuffer implements ByteBuf {
return buffer.order();
}
@Override
public ByteBuf order(ByteOrder endianness) {
if (endianness == null) {
throw new NullPointerException("endianness");
}
if (endianness == order()) {
return this;
}
return swapped;
}
@Override
public boolean readable() {
return terminated? buffer.readable() : true;