Move utility methods in Unpooled to ByteBufUtil

This commit is contained in:
Trustin Lee 2012-06-11 20:41:19 +09:00
parent 754392aaa9
commit 40a7659784
6 changed files with 436 additions and 410 deletions

View File

@ -657,17 +657,17 @@ public abstract class AbstractByteBuf implements ByteBuf {
nioBuffer.flip(); nioBuffer.flip();
} }
return Unpooled.decodeString(nioBuffer, charset); return ByteBufUtil.decodeString(nioBuffer, charset);
} }
@Override @Override
public int indexOf(int fromIndex, int toIndex, byte value) { public int indexOf(int fromIndex, int toIndex, byte value) {
return Unpooled.indexOf(this, fromIndex, toIndex, value); return ByteBufUtil.indexOf(this, fromIndex, toIndex, value);
} }
@Override @Override
public int indexOf(int fromIndex, int toIndex, ByteBufIndexFinder indexFinder) { public int indexOf(int fromIndex, int toIndex, ByteBufIndexFinder indexFinder) {
return Unpooled.indexOf(this, fromIndex, toIndex, indexFinder); return ByteBufUtil.indexOf(this, fromIndex, toIndex, indexFinder);
} }
@Override @Override
@ -713,7 +713,7 @@ public abstract class AbstractByteBuf implements ByteBuf {
@Override @Override
public int hashCode() { public int hashCode() {
return Unpooled.hashCode(this); return ByteBufUtil.hashCode(this);
} }
@Override @Override
@ -722,14 +722,14 @@ public abstract class AbstractByteBuf implements ByteBuf {
return true; return true;
} }
if (o instanceof ByteBuf) { if (o instanceof ByteBuf) {
return Unpooled.equals(this, (ByteBuf) o); return ByteBufUtil.equals(this, (ByteBuf) o);
} }
return false; return false;
} }
@Override @Override
public int compareTo(ByteBuf that) { public int compareTo(ByteBuf that) {
return Unpooled.compare(this, that); return ByteBufUtil.compare(this, that);
} }
@Override @Override

View File

@ -0,0 +1,370 @@
/*
* 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 io.netty.util.CharsetUtil;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;
public final class ByteBufUtil {
private static final char[] HEXDUMP_TABLE = new char[256 * 4];
static {
final char[] DIGITS = "0123456789abcdef".toCharArray();
for (int i = 0; i < 256; i ++) {
HEXDUMP_TABLE[(i << 1) + 0] = DIGITS[i >>> 4 & 0x0F];
HEXDUMP_TABLE[(i << 1) + 1] = DIGITS[i >>> 0 & 0x0F];
}
}
/**
* Returns a <a href="http://en.wikipedia.org/wiki/Hex_dump">hex dump</a>
* of the specified buffer's readable bytes.
*/
public static String hexDump(ByteBuf buffer) {
return hexDump(buffer, buffer.readerIndex(), buffer.readableBytes());
}
/**
* Returns a <a href="http://en.wikipedia.org/wiki/Hex_dump">hex dump</a>
* of the specified buffer's sub-region.
*/
public static String hexDump(ByteBuf buffer, int fromIndex, int length) {
if (length < 0) {
throw new IllegalArgumentException("length: " + length);
}
if (length == 0) {
return "";
}
int endIndex = fromIndex + length;
char[] buf = new char[length << 1];
int srcIdx = fromIndex;
int dstIdx = 0;
for (; srcIdx < endIndex; srcIdx ++, dstIdx += 2) {
System.arraycopy(
HEXDUMP_TABLE, buffer.getUnsignedByte(srcIdx) << 1,
buf, dstIdx, 2);
}
return new String(buf);
}
/**
* Calculates the hash code of the specified buffer. This method is
* useful when implementing a new buffer type.
*/
public static int hashCode(ByteBuf buffer) {
final int aLen = buffer.readableBytes();
final int intCount = aLen >>> 2;
final int byteCount = aLen & 3;
int hashCode = 1;
int arrayIndex = buffer.readerIndex();
if (buffer.order() == ByteOrder.BIG_ENDIAN) {
for (int i = intCount; i > 0; i --) {
hashCode = 31 * hashCode + buffer.getInt(arrayIndex);
arrayIndex += 4;
}
} else {
for (int i = intCount; i > 0; i --) {
hashCode = 31 * hashCode + swapInt(buffer.getInt(arrayIndex));
arrayIndex += 4;
}
}
for (int i = byteCount; i > 0; i --) {
hashCode = 31 * hashCode + buffer.getByte(arrayIndex ++);
}
if (hashCode == 0) {
hashCode = 1;
}
return hashCode;
}
/**
* Returns {@code true} if and only if the two specified buffers are
* identical to each other as described in {@code ChannelBuffer#equals(Object)}.
* This method is useful when implementing a new buffer type.
*/
public static boolean equals(ByteBuf bufferA, ByteBuf bufferB) {
final int aLen = bufferA.readableBytes();
if (aLen != bufferB.readableBytes()) {
return false;
}
final int longCount = aLen >>> 3;
final int byteCount = aLen & 7;
int aIndex = bufferA.readerIndex();
int bIndex = bufferB.readerIndex();
if (bufferA.order() == bufferB.order()) {
for (int i = longCount; i > 0; i --) {
if (bufferA.getLong(aIndex) != bufferB.getLong(bIndex)) {
return false;
}
aIndex += 8;
bIndex += 8;
}
} else {
for (int i = longCount; i > 0; i --) {
if (bufferA.getLong(aIndex) != swapLong(bufferB.getLong(bIndex))) {
return false;
}
aIndex += 8;
bIndex += 8;
}
}
for (int i = byteCount; i > 0; i --) {
if (bufferA.getByte(aIndex) != bufferB.getByte(bIndex)) {
return false;
}
aIndex ++;
bIndex ++;
}
return true;
}
/**
* Compares the two specified buffers as described in {@link ByteBuf#compareTo(ByteBuf)}.
* This method is useful when implementing a new buffer type.
*/
public static int compare(ByteBuf bufferA, ByteBuf bufferB) {
final int aLen = bufferA.readableBytes();
final int bLen = bufferB.readableBytes();
final int minLength = Math.min(aLen, bLen);
final int uintCount = minLength >>> 2;
final int byteCount = minLength & 3;
int aIndex = bufferA.readerIndex();
int bIndex = bufferB.readerIndex();
if (bufferA.order() == bufferB.order()) {
for (int i = uintCount; i > 0; i --) {
long va = bufferA.getUnsignedInt(aIndex);
long vb = bufferB.getUnsignedInt(bIndex);
if (va > vb) {
return 1;
} else if (va < vb) {
return -1;
}
aIndex += 4;
bIndex += 4;
}
} else {
for (int i = uintCount; i > 0; i --) {
long va = bufferA.getUnsignedInt(aIndex);
long vb = swapInt(bufferB.getInt(bIndex)) & 0xFFFFFFFFL;
if (va > vb) {
return 1;
} else if (va < vb) {
return -1;
}
aIndex += 4;
bIndex += 4;
}
}
for (int i = byteCount; i > 0; i --) {
short va = bufferA.getUnsignedByte(aIndex);
short vb = bufferB.getUnsignedByte(bIndex);
if (va > vb) {
return 1;
} else if (va < vb) {
return -1;
}
aIndex ++;
bIndex ++;
}
return aLen - bLen;
}
/**
* The default implementation of {@link ByteBuf#indexOf(int, int, byte)}.
* This method is useful when implementing a new buffer type.
*/
public static int indexOf(ByteBuf buffer, int fromIndex, int toIndex, byte value) {
if (fromIndex <= toIndex) {
return firstIndexOf(buffer, fromIndex, toIndex, value);
} else {
return lastIndexOf(buffer, fromIndex, toIndex, value);
}
}
/**
* The default implementation of {@link ByteBuf#indexOf(int, int, ByteBufIndexFinder)}.
* This method is useful when implementing a new buffer type.
*/
public static int indexOf(ByteBuf buffer, int fromIndex, int toIndex, ByteBufIndexFinder indexFinder) {
if (fromIndex <= toIndex) {
return firstIndexOf(buffer, fromIndex, toIndex, indexFinder);
} else {
return lastIndexOf(buffer, fromIndex, toIndex, indexFinder);
}
}
/**
* Toggles the endianness of the specified 16-bit short integer.
*/
public static short swapShort(short value) {
return (short) (value << 8 | value >>> 8 & 0xff);
}
/**
* Toggles the endianness of the specified 24-bit medium integer.
*/
public static int swapMedium(int value) {
int swapped = value << 16 & 0xff0000 | value & 0xff00 | value >>> 16 & 0xff;
if ((swapped & 0x800000) != 0) {
swapped |= 0xff000000;
}
return swapped;
}
/**
* Toggles the endianness of the specified 32-bit integer.
*/
public static int swapInt(int value) {
return swapShort((short) value) << 16 |
swapShort((short) (value >>> 16)) & 0xffff;
}
/**
* Toggles the endianness of the specified 64-bit long integer.
*/
public static long swapLong(long value) {
return (long) swapInt((int) value) << 32 |
swapInt((int) (value >>> 32)) & 0xffffffffL;
}
private static int firstIndexOf(ByteBuf buffer, int fromIndex, int toIndex, byte value) {
fromIndex = Math.max(fromIndex, 0);
if (fromIndex >= toIndex || buffer.capacity() == 0) {
return -1;
}
for (int i = fromIndex; i < toIndex; i ++) {
if (buffer.getByte(i) == value) {
return i;
}
}
return -1;
}
private static int lastIndexOf(ByteBuf buffer, int fromIndex, int toIndex, byte value) {
fromIndex = Math.min(fromIndex, buffer.capacity());
if (fromIndex < 0 || buffer.capacity() == 0) {
return -1;
}
for (int i = fromIndex - 1; i >= toIndex; i --) {
if (buffer.getByte(i) == value) {
return i;
}
}
return -1;
}
private static int firstIndexOf(
ByteBuf buffer, int fromIndex, int toIndex, ByteBufIndexFinder indexFinder) {
fromIndex = Math.max(fromIndex, 0);
if (fromIndex >= toIndex || buffer.capacity() == 0) {
return -1;
}
for (int i = fromIndex; i < toIndex; i ++) {
if (indexFinder.find(buffer, i)) {
return i;
}
}
return -1;
}
private static int lastIndexOf(
ByteBuf buffer, int fromIndex, int toIndex, ByteBufIndexFinder indexFinder) {
fromIndex = Math.min(fromIndex, buffer.capacity());
if (fromIndex < 0 || buffer.capacity() == 0) {
return -1;
}
for (int i = fromIndex - 1; i >= toIndex; i --) {
if (indexFinder.find(buffer, i)) {
return i;
}
}
return -1;
}
static ByteBuffer encodeString(CharBuffer src, Charset charset) {
final CharsetEncoder encoder = CharsetUtil.getEncoder(charset);
final ByteBuffer dst = ByteBuffer.allocate(
(int) ((double) src.remaining() * encoder.maxBytesPerChar()));
try {
CoderResult cr = encoder.encode(src, dst, true);
if (!cr.isUnderflow()) {
cr.throwException();
}
cr = encoder.flush(dst);
if (!cr.isUnderflow()) {
cr.throwException();
}
} catch (CharacterCodingException x) {
throw new IllegalStateException(x);
}
dst.flip();
return dst;
}
static String decodeString(ByteBuffer src, Charset charset) {
final CharsetDecoder decoder = CharsetUtil.getDecoder(charset);
final CharBuffer dst = CharBuffer.allocate(
(int) ((double) src.remaining() * decoder.maxCharsPerByte()));
try {
CoderResult cr = decoder.decode(src, dst, true);
if (!cr.isUnderflow()) {
cr.throwException();
}
cr = decoder.flush(dst);
if (!cr.isUnderflow()) {
cr.throwException();
}
} catch (CharacterCodingException x) {
throw new IllegalStateException(x);
}
return dst.flip().toString();
}
private ByteBufUtil() { }
}

View File

@ -179,7 +179,7 @@ public class SwappedByteBuf implements WrappedByteBuf {
@Override @Override
public short getShort(int index) { public short getShort(int index) {
return Unpooled.swapShort(buf.getShort(index)); return ByteBufUtil.swapShort(buf.getShort(index));
} }
@Override @Override
@ -189,7 +189,7 @@ public class SwappedByteBuf implements WrappedByteBuf {
@Override @Override
public int getMedium(int index) { public int getMedium(int index) {
return Unpooled.swapMedium(buf.getMedium(index)); return ByteBufUtil.swapMedium(buf.getMedium(index));
} }
@Override @Override
@ -199,7 +199,7 @@ public class SwappedByteBuf implements WrappedByteBuf {
@Override @Override
public int getInt(int index) { public int getInt(int index) {
return Unpooled.swapInt(buf.getInt(index)); return ByteBufUtil.swapInt(buf.getInt(index));
} }
@Override @Override
@ -209,7 +209,7 @@ public class SwappedByteBuf implements WrappedByteBuf {
@Override @Override
public long getLong(int index) { public long getLong(int index) {
return Unpooled.swapLong(buf.getLong(index)); return ByteBufUtil.swapLong(buf.getLong(index));
} }
@Override @Override
@ -279,22 +279,22 @@ public class SwappedByteBuf implements WrappedByteBuf {
@Override @Override
public void setShort(int index, int value) { public void setShort(int index, int value) {
buf.setShort(index, Unpooled.swapShort((short) value)); buf.setShort(index, ByteBufUtil.swapShort((short) value));
} }
@Override @Override
public void setMedium(int index, int value) { public void setMedium(int index, int value) {
buf.setMedium(index, Unpooled.swapMedium(value)); buf.setMedium(index, ByteBufUtil.swapMedium(value));
} }
@Override @Override
public void setInt(int index, int value) { public void setInt(int index, int value) {
buf.setInt(index, Unpooled.swapInt(value)); buf.setInt(index, ByteBufUtil.swapInt(value));
} }
@Override @Override
public void setLong(int index, long value) { public void setLong(int index, long value) {
buf.setLong(index, Unpooled.swapLong(value)); buf.setLong(index, ByteBufUtil.swapLong(value));
} }
@Override @Override
@ -374,7 +374,7 @@ public class SwappedByteBuf implements WrappedByteBuf {
@Override @Override
public short readShort() { public short readShort() {
return Unpooled.swapShort(buf.readShort()); return ByteBufUtil.swapShort(buf.readShort());
} }
@Override @Override
@ -384,7 +384,7 @@ public class SwappedByteBuf implements WrappedByteBuf {
@Override @Override
public int readMedium() { public int readMedium() {
return Unpooled.swapMedium(buf.readMedium()); return ByteBufUtil.swapMedium(buf.readMedium());
} }
@Override @Override
@ -394,7 +394,7 @@ public class SwappedByteBuf implements WrappedByteBuf {
@Override @Override
public int readInt() { public int readInt() {
return Unpooled.swapInt(buf.readInt()); return ByteBufUtil.swapInt(buf.readInt());
} }
@Override @Override
@ -404,7 +404,7 @@ public class SwappedByteBuf implements WrappedByteBuf {
@Override @Override
public long readLong() { public long readLong() {
return Unpooled.swapLong(buf.readLong()); return ByteBufUtil.swapLong(buf.readLong());
} }
@Override @Override
@ -489,22 +489,22 @@ public class SwappedByteBuf implements WrappedByteBuf {
@Override @Override
public void writeShort(int value) { public void writeShort(int value) {
buf.writeShort(Unpooled.swapShort((short) value)); buf.writeShort(ByteBufUtil.swapShort((short) value));
} }
@Override @Override
public void writeMedium(int value) { public void writeMedium(int value) {
buf.writeMedium(Unpooled.swapMedium(value)); buf.writeMedium(ByteBufUtil.swapMedium(value));
} }
@Override @Override
public void writeInt(int value) { public void writeInt(int value) {
buf.writeInt(Unpooled.swapInt(value)); buf.writeInt(ByteBufUtil.swapInt(value));
} }
@Override @Override
public void writeLong(long value) { public void writeLong(long value) {
buf.writeLong(Unpooled.swapLong(value)); buf.writeLong(ByteBufUtil.swapLong(value));
} }
@Override @Override
@ -683,14 +683,14 @@ public class SwappedByteBuf implements WrappedByteBuf {
return true; return true;
} }
if (obj instanceof ByteBuf) { if (obj instanceof ByteBuf) {
return Unpooled.equals(this, (ByteBuf) obj); return ByteBufUtil.equals(this, (ByteBuf) obj);
} }
return false; return false;
} }
@Override @Override
public int compareTo(ByteBuf buffer) { public int compareTo(ByteBuf buffer) {
return Unpooled.compare(this, buffer); return ByteBufUtil.compare(this, buffer);
} }
@Override @Override

View File

@ -15,16 +15,10 @@
*/ */
package io.netty.buffer; package io.netty.buffer;
import io.netty.util.CharsetUtil;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.nio.CharBuffer; import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Queue; import java.util.Queue;
@ -110,16 +104,6 @@ public final class Unpooled {
} }
}; };
private static final char[] HEXDUMP_TABLE = new char[256 * 4];
static {
final char[] DIGITS = "0123456789abcdef".toCharArray();
for (int i = 0; i < 256; i ++) {
HEXDUMP_TABLE[(i << 1) + 0] = DIGITS[i >>> 4 & 0x0F];
HEXDUMP_TABLE[(i << 1) + 1] = DIGITS[i >>> 0 & 0x0F];
}
}
public static <T> MessageBuf<T> messageBuffer() { public static <T> MessageBuf<T> messageBuffer() {
return new DefaultMessageBuf<T>(); return new DefaultMessageBuf<T>();
} }
@ -706,7 +690,7 @@ public final class Unpooled {
} }
private static ByteBuf copiedBuffer(CharBuffer buffer, Charset charset) { private static ByteBuf copiedBuffer(CharBuffer buffer, Charset charset) {
ByteBuffer dst = Unpooled.encodeString(buffer, charset); ByteBuffer dst = ByteBufUtil.encodeString(buffer, charset);
ByteBuf result = wrappedBuffer(dst.array()); ByteBuf result = wrappedBuffer(dst.array());
result.writerIndex(dst.remaining()); result.writerIndex(dst.remaining());
return result; return result;
@ -900,334 +884,6 @@ public final class Unpooled {
return buffer; return buffer;
} }
/**
* Returns a <a href="http://en.wikipedia.org/wiki/Hex_dump">hex dump</a>
* of the specified buffer's readable bytes.
*/
public static String hexDump(ByteBuf buffer) {
return hexDump(buffer, buffer.readerIndex(), buffer.readableBytes());
}
/**
* Returns a <a href="http://en.wikipedia.org/wiki/Hex_dump">hex dump</a>
* of the specified buffer's sub-region.
*/
public static String hexDump(ByteBuf buffer, int fromIndex, int length) {
if (length < 0) {
throw new IllegalArgumentException("length: " + length);
}
if (length == 0) {
return "";
}
int endIndex = fromIndex + length;
char[] buf = new char[length << 1];
int srcIdx = fromIndex;
int dstIdx = 0;
for (; srcIdx < endIndex; srcIdx ++, dstIdx += 2) {
System.arraycopy(
HEXDUMP_TABLE, buffer.getUnsignedByte(srcIdx) << 1,
buf, dstIdx, 2);
}
return new String(buf);
}
/**
* Calculates the hash code of the specified buffer. This method is
* useful when implementing a new buffer type.
*/
public static int hashCode(ByteBuf buffer) {
final int aLen = buffer.readableBytes();
final int intCount = aLen >>> 2;
final int byteCount = aLen & 3;
int hashCode = 1;
int arrayIndex = buffer.readerIndex();
if (buffer.order() == BIG_ENDIAN) {
for (int i = intCount; i > 0; i --) {
hashCode = 31 * hashCode + buffer.getInt(arrayIndex);
arrayIndex += 4;
}
} else {
for (int i = intCount; i > 0; i --) {
hashCode = 31 * hashCode + swapInt(buffer.getInt(arrayIndex));
arrayIndex += 4;
}
}
for (int i = byteCount; i > 0; i --) {
hashCode = 31 * hashCode + buffer.getByte(arrayIndex ++);
}
if (hashCode == 0) {
hashCode = 1;
}
return hashCode;
}
/**
* Returns {@code true} if and only if the two specified buffers are
* identical to each other as described in {@code ChannelBuffer#equals(Object)}.
* This method is useful when implementing a new buffer type.
*/
public static boolean equals(ByteBuf bufferA, ByteBuf bufferB) {
final int aLen = bufferA.readableBytes();
if (aLen != bufferB.readableBytes()) {
return false;
}
final int longCount = aLen >>> 3;
final int byteCount = aLen & 7;
int aIndex = bufferA.readerIndex();
int bIndex = bufferB.readerIndex();
if (bufferA.order() == bufferB.order()) {
for (int i = longCount; i > 0; i --) {
if (bufferA.getLong(aIndex) != bufferB.getLong(bIndex)) {
return false;
}
aIndex += 8;
bIndex += 8;
}
} else {
for (int i = longCount; i > 0; i --) {
if (bufferA.getLong(aIndex) != swapLong(bufferB.getLong(bIndex))) {
return false;
}
aIndex += 8;
bIndex += 8;
}
}
for (int i = byteCount; i > 0; i --) {
if (bufferA.getByte(aIndex) != bufferB.getByte(bIndex)) {
return false;
}
aIndex ++;
bIndex ++;
}
return true;
}
/**
* Compares the two specified buffers as described in {@link ByteBuf#compareTo(ByteBuf)}.
* This method is useful when implementing a new buffer type.
*/
public static int compare(ByteBuf bufferA, ByteBuf bufferB) {
final int aLen = bufferA.readableBytes();
final int bLen = bufferB.readableBytes();
final int minLength = Math.min(aLen, bLen);
final int uintCount = minLength >>> 2;
final int byteCount = minLength & 3;
int aIndex = bufferA.readerIndex();
int bIndex = bufferB.readerIndex();
if (bufferA.order() == bufferB.order()) {
for (int i = uintCount; i > 0; i --) {
long va = bufferA.getUnsignedInt(aIndex);
long vb = bufferB.getUnsignedInt(bIndex);
if (va > vb) {
return 1;
} else if (va < vb) {
return -1;
}
aIndex += 4;
bIndex += 4;
}
} else {
for (int i = uintCount; i > 0; i --) {
long va = bufferA.getUnsignedInt(aIndex);
long vb = swapInt(bufferB.getInt(bIndex)) & 0xFFFFFFFFL;
if (va > vb) {
return 1;
} else if (va < vb) {
return -1;
}
aIndex += 4;
bIndex += 4;
}
}
for (int i = byteCount; i > 0; i --) {
short va = bufferA.getUnsignedByte(aIndex);
short vb = bufferB.getUnsignedByte(bIndex);
if (va > vb) {
return 1;
} else if (va < vb) {
return -1;
}
aIndex ++;
bIndex ++;
}
return aLen - bLen;
}
/**
* The default implementation of {@link ByteBuf#indexOf(int, int, byte)}.
* This method is useful when implementing a new buffer type.
*/
public static int indexOf(ByteBuf buffer, int fromIndex, int toIndex, byte value) {
if (fromIndex <= toIndex) {
return firstIndexOf(buffer, fromIndex, toIndex, value);
} else {
return lastIndexOf(buffer, fromIndex, toIndex, value);
}
}
/**
* The default implementation of {@link ByteBuf#indexOf(int, int, ByteBufIndexFinder)}.
* This method is useful when implementing a new buffer type.
*/
public static int indexOf(ByteBuf buffer, int fromIndex, int toIndex, ByteBufIndexFinder indexFinder) {
if (fromIndex <= toIndex) {
return firstIndexOf(buffer, fromIndex, toIndex, indexFinder);
} else {
return lastIndexOf(buffer, fromIndex, toIndex, indexFinder);
}
}
/**
* Toggles the endianness of the specified 16-bit short integer.
*/
public static short swapShort(short value) {
return (short) (value << 8 | value >>> 8 & 0xff);
}
/**
* Toggles the endianness of the specified 24-bit medium integer.
*/
public static int swapMedium(int value) {
int swapped = value << 16 & 0xff0000 | value & 0xff00 | value >>> 16 & 0xff;
if ((swapped & 0x800000) != 0) {
swapped |= 0xff000000;
}
return swapped;
}
/**
* Toggles the endianness of the specified 32-bit integer.
*/
public static int swapInt(int value) {
return swapShort((short) value) << 16 |
swapShort((short) (value >>> 16)) & 0xffff;
}
/**
* Toggles the endianness of the specified 64-bit long integer.
*/
public static long swapLong(long value) {
return (long) swapInt((int) value) << 32 |
swapInt((int) (value >>> 32)) & 0xffffffffL;
}
private static int firstIndexOf(ByteBuf buffer, int fromIndex, int toIndex, byte value) {
fromIndex = Math.max(fromIndex, 0);
if (fromIndex >= toIndex || buffer.capacity() == 0) {
return -1;
}
for (int i = fromIndex; i < toIndex; i ++) {
if (buffer.getByte(i) == value) {
return i;
}
}
return -1;
}
private static int lastIndexOf(ByteBuf buffer, int fromIndex, int toIndex, byte value) {
fromIndex = Math.min(fromIndex, buffer.capacity());
if (fromIndex < 0 || buffer.capacity() == 0) {
return -1;
}
for (int i = fromIndex - 1; i >= toIndex; i --) {
if (buffer.getByte(i) == value) {
return i;
}
}
return -1;
}
private static int firstIndexOf(
ByteBuf buffer, int fromIndex, int toIndex, ByteBufIndexFinder indexFinder) {
fromIndex = Math.max(fromIndex, 0);
if (fromIndex >= toIndex || buffer.capacity() == 0) {
return -1;
}
for (int i = fromIndex; i < toIndex; i ++) {
if (indexFinder.find(buffer, i)) {
return i;
}
}
return -1;
}
private static int lastIndexOf(
ByteBuf buffer, int fromIndex, int toIndex, ByteBufIndexFinder indexFinder) {
fromIndex = Math.min(fromIndex, buffer.capacity());
if (fromIndex < 0 || buffer.capacity() == 0) {
return -1;
}
for (int i = fromIndex - 1; i >= toIndex; i --) {
if (indexFinder.find(buffer, i)) {
return i;
}
}
return -1;
}
static ByteBuffer encodeString(CharBuffer src, Charset charset) {
final CharsetEncoder encoder = CharsetUtil.getEncoder(charset);
final ByteBuffer dst = ByteBuffer.allocate(
(int) ((double) src.remaining() * encoder.maxBytesPerChar()));
try {
CoderResult cr = encoder.encode(src, dst, true);
if (!cr.isUnderflow()) {
cr.throwException();
}
cr = encoder.flush(dst);
if (!cr.isUnderflow()) {
cr.throwException();
}
} catch (CharacterCodingException x) {
throw new IllegalStateException(x);
}
dst.flip();
return dst;
}
static String decodeString(ByteBuffer src, Charset charset) {
final CharsetDecoder decoder = CharsetUtil.getDecoder(charset);
final CharBuffer dst = CharBuffer.allocate(
(int) ((double) src.remaining() * decoder.maxCharsPerByte()));
try {
CoderResult cr = decoder.decode(src, dst, true);
if (!cr.isUnderflow()) {
cr.throwException();
}
cr = decoder.flush(dst);
if (!cr.isUnderflow()) {
cr.throwException();
}
} catch (CharacterCodingException x) {
throw new IllegalStateException(x);
}
return dst.flip().toString();
}
private Unpooled() { private Unpooled() {
// Unused // Unused
} }

View File

@ -113,20 +113,20 @@ public abstract class AbstractCompositeChannelBufferTest extends
a.writerIndex(a.writerIndex() + 1); a.writerIndex(a.writerIndex() + 1);
b.writerIndex(b.writerIndex() + 1); b.writerIndex(b.writerIndex() + 1);
assertEquals(a.writerIndex(), b.writerIndex()); assertEquals(a.writerIndex(), b.writerIndex());
assertTrue(Unpooled.equals(a, b)); assertTrue(ByteBufUtil.equals(a, b));
// now discard // now discard
a.discardReadBytes(); a.discardReadBytes();
b.discardReadBytes(); b.discardReadBytes();
assertEquals(a.readerIndex(), b.readerIndex()); assertEquals(a.readerIndex(), b.readerIndex());
assertEquals(a.writerIndex(), b.writerIndex()); assertEquals(a.writerIndex(), b.writerIndex());
assertTrue(Unpooled.equals(a, b)); assertTrue(ByteBufUtil.equals(a, b));
a.resetReaderIndex(); a.resetReaderIndex();
b.resetReaderIndex(); b.resetReaderIndex();
assertEquals(a.readerIndex(), b.readerIndex()); assertEquals(a.readerIndex(), b.readerIndex());
a.resetWriterIndex(); a.resetWriterIndex();
b.resetWriterIndex(); b.resetWriterIndex();
assertEquals(a.writerIndex(), b.writerIndex()); assertEquals(a.writerIndex(), b.writerIndex());
assertTrue(Unpooled.equals(a, b)); assertTrue(ByteBufUtil.equals(a, b));
} }
@Test @Test
@ -154,57 +154,57 @@ public abstract class AbstractCompositeChannelBufferTest extends
a = wrappedBuffer(new byte[] { 1 }).order(order); a = wrappedBuffer(new byte[] { 1 }).order(order);
b = wrappedBuffer(wrappedBuffer(new byte[] { 1 }).order(order), b = wrappedBuffer(wrappedBuffer(new byte[] { 1 }).order(order),
wrappedBuffer(new byte[] { 2 }).order(order)); wrappedBuffer(new byte[] { 2 }).order(order));
assertFalse(Unpooled.equals(a, b)); assertFalse(ByteBufUtil.equals(a, b));
// Same content, same firstIndex, short length. // Same content, same firstIndex, short length.
a = wrappedBuffer(new byte[] { 1, 2, 3 }).order(order); a = wrappedBuffer(new byte[] { 1, 2, 3 }).order(order);
b = wrappedBuffer(wrappedBuffer(new byte[] { 1 }).order(order), b = wrappedBuffer(wrappedBuffer(new byte[] { 1 }).order(order),
wrappedBuffer(new byte[] { 2 }).order(order), wrappedBuffer(new byte[] { 2 }).order(order),
wrappedBuffer(new byte[] { 3 }).order(order)); wrappedBuffer(new byte[] { 3 }).order(order));
assertTrue(Unpooled.equals(a, b)); assertTrue(ByteBufUtil.equals(a, b));
// Same content, different firstIndex, short length. // Same content, different firstIndex, short length.
a = wrappedBuffer(new byte[] { 1, 2, 3 }).order(order); a = wrappedBuffer(new byte[] { 1, 2, 3 }).order(order);
b = wrappedBuffer(wrappedBuffer(new byte[] { 0, 1, 2, 3, 4 }, 1, 2).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)); wrappedBuffer(new byte[] { 0, 1, 2, 3, 4 }, 3, 1).order(order));
assertTrue(Unpooled.equals(a, b)); assertTrue(ByteBufUtil.equals(a, b));
// Different content, same firstIndex, short length. // Different content, same firstIndex, short length.
a = wrappedBuffer(new byte[] { 1, 2, 3 }).order(order); a = wrappedBuffer(new byte[] { 1, 2, 3 }).order(order);
b = wrappedBuffer(wrappedBuffer(new byte[] { 1, 2 }).order(order), b = wrappedBuffer(wrappedBuffer(new byte[] { 1, 2 }).order(order),
wrappedBuffer(new byte[] { 4 }).order(order)); wrappedBuffer(new byte[] { 4 }).order(order));
assertFalse(Unpooled.equals(a, b)); assertFalse(ByteBufUtil.equals(a, b));
// Different content, different firstIndex, short length. // Different content, different firstIndex, short length.
a = wrappedBuffer(new byte[] { 1, 2, 3 }).order(order); a = wrappedBuffer(new byte[] { 1, 2, 3 }).order(order);
b = wrappedBuffer(wrappedBuffer(new byte[] { 0, 1, 2, 4, 5 }, 1, 2).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)); wrappedBuffer(new byte[] { 0, 1, 2, 4, 5 }, 3, 1).order(order));
assertFalse(Unpooled.equals(a, b)); assertFalse(ByteBufUtil.equals(a, b));
// Same content, same firstIndex, long length. // Same content, same firstIndex, long length.
a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).order(order); 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), b = wrappedBuffer(wrappedBuffer(new byte[] { 1, 2, 3 }).order(order),
wrappedBuffer(new byte[] { 4, 5, 6 }).order(order), wrappedBuffer(new byte[] { 4, 5, 6 }).order(order),
wrappedBuffer(new byte[] { 7, 8, 9, 10 }).order(order)); wrappedBuffer(new byte[] { 7, 8, 9, 10 }).order(order));
assertTrue(Unpooled.equals(a, b)); assertTrue(ByteBufUtil.equals(a, b));
// Same content, different firstIndex, long length. // Same content, different firstIndex, long length.
a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).order(order); 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), 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)); wrappedBuffer(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 6, 5).order(order));
assertTrue(Unpooled.equals(a, b)); assertTrue(ByteBufUtil.equals(a, b));
// Different content, same firstIndex, long length. // Different content, same firstIndex, long length.
a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).order(order); 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), b = wrappedBuffer(wrappedBuffer(new byte[] { 1, 2, 3, 4, 6 }).order(order),
wrappedBuffer(new byte[] { 7, 8, 5, 9, 10 }).order(order)); wrappedBuffer(new byte[] { 7, 8, 5, 9, 10 }).order(order));
assertFalse(Unpooled.equals(a, b)); assertFalse(ByteBufUtil.equals(a, b));
// Different content, different firstIndex, long length. // Different content, different firstIndex, long length.
a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).order(order); 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), 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)); wrappedBuffer(new byte[] { 0, 1, 2, 3, 4, 6, 7, 8, 5, 9, 10, 11 }, 6, 5).order(order));
assertFalse(Unpooled.equals(a, b)); assertFalse(ByteBufUtil.equals(a, b));
} }
@Test @Test
public void testWrappedBuffer() { public void testWrappedBuffer() {
@ -259,7 +259,7 @@ public abstract class AbstractCompositeChannelBufferTest extends
b.writerIndex(b.writerIndex() - 1); b.writerIndex(b.writerIndex() - 1);
b.writeBytes( b.writeBytes(
wrappedBuffer(new byte[] { 2 }).order(order)); wrappedBuffer(new byte[] { 2 }).order(order));
assertFalse(Unpooled.equals(a, b)); assertFalse(ByteBufUtil.equals(a, b));
// Same content, same firstIndex, short length. // Same content, same firstIndex, short length.
a = wrappedBuffer(new byte[] { 1, 2, 3 }).order(order); a = wrappedBuffer(new byte[] { 1, 2, 3 }).order(order);
@ -269,7 +269,7 @@ public abstract class AbstractCompositeChannelBufferTest extends
b.writeBytes( b.writeBytes(
wrappedBuffer(new byte[] { 2 }).order(order)); wrappedBuffer(new byte[] { 2 }).order(order));
b.writeBytes(wrappedBuffer(new byte[] { 3 }).order(order)); b.writeBytes(wrappedBuffer(new byte[] { 3 }).order(order));
assertTrue(Unpooled.equals(a, b)); assertTrue(ByteBufUtil.equals(a, b));
// Same content, different firstIndex, short length. // Same content, different firstIndex, short length.
a = wrappedBuffer(new byte[] { 1, 2, 3 }).order(order); a = wrappedBuffer(new byte[] { 1, 2, 3 }).order(order);
@ -278,7 +278,7 @@ public abstract class AbstractCompositeChannelBufferTest extends
b.writerIndex(b.writerIndex() - 1); b.writerIndex(b.writerIndex() - 1);
b.writeBytes( b.writeBytes(
wrappedBuffer(new byte[] { 0, 1, 2, 3, 4 }, 3, 1).order(order)); wrappedBuffer(new byte[] { 0, 1, 2, 3, 4 }, 3, 1).order(order));
assertTrue(Unpooled.equals(a, b)); assertTrue(ByteBufUtil.equals(a, b));
// Different content, same firstIndex, short length. // Different content, same firstIndex, short length.
a = wrappedBuffer(new byte[] { 1, 2, 3 }).order(order); a = wrappedBuffer(new byte[] { 1, 2, 3 }).order(order);
@ -287,7 +287,7 @@ public abstract class AbstractCompositeChannelBufferTest extends
b.writerIndex(b.writerIndex() - 1); b.writerIndex(b.writerIndex() - 1);
b.writeBytes( b.writeBytes(
wrappedBuffer(new byte[] { 4 }).order(order)); wrappedBuffer(new byte[] { 4 }).order(order));
assertFalse(Unpooled.equals(a, b)); assertFalse(ByteBufUtil.equals(a, b));
// Different content, different firstIndex, short length. // Different content, different firstIndex, short length.
a = wrappedBuffer(new byte[] { 1, 2, 3 }).order(order); a = wrappedBuffer(new byte[] { 1, 2, 3 }).order(order);
@ -296,7 +296,7 @@ public abstract class AbstractCompositeChannelBufferTest extends
b.writerIndex(b.writerIndex() - 1); b.writerIndex(b.writerIndex() - 1);
b.writeBytes( b.writeBytes(
wrappedBuffer(new byte[] { 0, 1, 2, 4, 5 }, 3, 1).order(order)); wrappedBuffer(new byte[] { 0, 1, 2, 4, 5 }, 3, 1).order(order));
assertFalse(Unpooled.equals(a, b)); assertFalse(ByteBufUtil.equals(a, b));
// Same content, same firstIndex, long length. // Same content, same firstIndex, long length.
a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).order(order); a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).order(order);
@ -307,7 +307,7 @@ public abstract class AbstractCompositeChannelBufferTest extends
wrappedBuffer(new byte[] { 4, 5, 6 }).order(order)); wrappedBuffer(new byte[] { 4, 5, 6 }).order(order));
b.writeBytes( b.writeBytes(
wrappedBuffer(new byte[] { 7, 8, 9, 10 }).order(order)); wrappedBuffer(new byte[] { 7, 8, 9, 10 }).order(order));
assertTrue(Unpooled.equals(a, b)); assertTrue(ByteBufUtil.equals(a, b));
// Same content, different firstIndex, long length. // Same content, different firstIndex, long length.
a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).order(order); a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).order(order);
@ -316,7 +316,7 @@ public abstract class AbstractCompositeChannelBufferTest extends
b.writerIndex(b.writerIndex() - 5); b.writerIndex(b.writerIndex() - 5);
b.writeBytes( b.writeBytes(
wrappedBuffer(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 6, 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)); assertTrue(ByteBufUtil.equals(a, b));
// Different content, same firstIndex, long length. // Different content, same firstIndex, long length.
a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).order(order); a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).order(order);
@ -325,7 +325,7 @@ public abstract class AbstractCompositeChannelBufferTest extends
b.writerIndex(b.writerIndex() - 5); b.writerIndex(b.writerIndex() - 5);
b.writeBytes( b.writeBytes(
wrappedBuffer(new byte[] { 7, 8, 5, 9, 10 }).order(order)); wrappedBuffer(new byte[] { 7, 8, 5, 9, 10 }).order(order));
assertFalse(Unpooled.equals(a, b)); assertFalse(ByteBufUtil.equals(a, b));
// Different content, different firstIndex, long length. // Different content, different firstIndex, long length.
a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).order(order); a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).order(order);
@ -334,6 +334,6 @@ public abstract class AbstractCompositeChannelBufferTest extends
b.writerIndex(b.writerIndex() - 5); b.writerIndex(b.writerIndex() - 5);
b.writeBytes( b.writeBytes(
wrappedBuffer(new byte[] { 0, 1, 2, 3, 4, 6, 7, 8, 5, 9, 10, 11 }, 6, 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)); assertFalse(ByteBufUtil.equals(a, b));
} }
} }

View File

@ -67,7 +67,7 @@ public class ChannelBuffersTest {
for (Entry<byte[], Integer> e: map.entrySet()) { for (Entry<byte[], Integer> e: map.entrySet()) {
assertEquals( assertEquals(
e.getValue().intValue(), e.getValue().intValue(),
Unpooled.hashCode(wrappedBuffer(e.getKey()))); ByteBufUtil.hashCode(wrappedBuffer(e.getKey())));
} }
} }
@ -78,47 +78,47 @@ public class ChannelBuffersTest {
// Different length. // Different length.
a = wrappedBuffer(new byte[] { 1 }); a = wrappedBuffer(new byte[] { 1 });
b = wrappedBuffer(new byte[] { 1, 2 }); b = wrappedBuffer(new byte[] { 1, 2 });
assertFalse(Unpooled.equals(a, b)); assertFalse(ByteBufUtil.equals(a, b));
// Same content, same firstIndex, short length. // Same content, same firstIndex, short length.
a = wrappedBuffer(new byte[] { 1, 2, 3 }); a = wrappedBuffer(new byte[] { 1, 2, 3 });
b = wrappedBuffer(new byte[] { 1, 2, 3 }); b = wrappedBuffer(new byte[] { 1, 2, 3 });
assertTrue(Unpooled.equals(a, b)); assertTrue(ByteBufUtil.equals(a, b));
// Same content, different firstIndex, short length. // Same content, different firstIndex, short length.
a = wrappedBuffer(new byte[] { 1, 2, 3 }); a = wrappedBuffer(new byte[] { 1, 2, 3 });
b = wrappedBuffer(new byte[] { 0, 1, 2, 3, 4 }, 1, 3); b = wrappedBuffer(new byte[] { 0, 1, 2, 3, 4 }, 1, 3);
assertTrue(Unpooled.equals(a, b)); assertTrue(ByteBufUtil.equals(a, b));
// Different content, same firstIndex, short length. // Different content, same firstIndex, short length.
a = wrappedBuffer(new byte[] { 1, 2, 3 }); a = wrappedBuffer(new byte[] { 1, 2, 3 });
b = wrappedBuffer(new byte[] { 1, 2, 4 }); b = wrappedBuffer(new byte[] { 1, 2, 4 });
assertFalse(Unpooled.equals(a, b)); assertFalse(ByteBufUtil.equals(a, b));
// Different content, different firstIndex, short length. // Different content, different firstIndex, short length.
a = wrappedBuffer(new byte[] { 1, 2, 3 }); a = wrappedBuffer(new byte[] { 1, 2, 3 });
b = wrappedBuffer(new byte[] { 0, 1, 2, 4, 5 }, 1, 3); b = wrappedBuffer(new byte[] { 0, 1, 2, 4, 5 }, 1, 3);
assertFalse(Unpooled.equals(a, b)); assertFalse(ByteBufUtil.equals(a, b));
// Same content, same firstIndex, long length. // Same content, same firstIndex, long length.
a = wrappedBuffer(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 });
b = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); b = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
assertTrue(Unpooled.equals(a, b)); assertTrue(ByteBufUtil.equals(a, b));
// Same content, different firstIndex, long length. // Same content, different firstIndex, long length.
a = wrappedBuffer(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 });
b = wrappedBuffer(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 1, 10); b = wrappedBuffer(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 1, 10);
assertTrue(Unpooled.equals(a, b)); assertTrue(ByteBufUtil.equals(a, b));
// Different content, same firstIndex, long length. // Different content, same firstIndex, long length.
a = wrappedBuffer(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 });
b = wrappedBuffer(new byte[] { 1, 2, 3, 4, 6, 7, 8, 5, 9, 10 }); b = wrappedBuffer(new byte[] { 1, 2, 3, 4, 6, 7, 8, 5, 9, 10 });
assertFalse(Unpooled.equals(a, b)); assertFalse(ByteBufUtil.equals(a, b));
// Different content, different firstIndex, long length. // Different content, different firstIndex, long length.
a = wrappedBuffer(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 });
b = wrappedBuffer(new byte[] { 0, 1, 2, 3, 4, 6, 7, 8, 5, 9, 10, 11 }, 1, 10); b = wrappedBuffer(new byte[] { 0, 1, 2, 3, 4, 6, 7, 8, 5, 9, 10, 11 }, 1, 10);
assertFalse(Unpooled.equals(a, b)); assertFalse(ByteBufUtil.equals(a, b));
} }
@Test @Test
@ -144,11 +144,11 @@ public class ChannelBuffersTest {
for (int i = 0; i < expected.size(); i ++) { for (int i = 0; i < expected.size(); i ++) {
for (int j = 0; j < expected.size(); j ++) { for (int j = 0; j < expected.size(); j ++) {
if (i == j) { if (i == j) {
assertEquals(0, compare(expected.get(i), expected.get(j))); assertEquals(0, ByteBufUtil.compare(expected.get(i), expected.get(j)));
} else if (i < j) { } else if (i < j) {
assertTrue(compare(expected.get(i), expected.get(j)) < 0); assertTrue(ByteBufUtil.compare(expected.get(i), expected.get(j)) < 0);
} else { } else {
assertTrue(compare(expected.get(i), expected.get(j)) > 0); assertTrue(ByteBufUtil.compare(expected.get(i), expected.get(j)) > 0);
} }
} }
} }
@ -197,12 +197,12 @@ public class ChannelBuffersTest {
@Test @Test
public void testCompare2() { public void testCompare2() {
assertTrue(Unpooled.compare( assertTrue(ByteBufUtil.compare(
Unpooled.wrappedBuffer(new byte[]{(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}), Unpooled.wrappedBuffer(new byte[]{(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}),
Unpooled.wrappedBuffer(new byte[]{(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00})) Unpooled.wrappedBuffer(new byte[]{(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00}))
> 0); > 0);
assertTrue(Unpooled.compare( assertTrue(ByteBufUtil.compare(
Unpooled.wrappedBuffer(new byte[]{(byte) 0xFF}), Unpooled.wrappedBuffer(new byte[]{(byte) 0xFF}),
Unpooled.wrappedBuffer(new byte[]{(byte) 0x00})) Unpooled.wrappedBuffer(new byte[]{(byte) 0x00}))
> 0); > 0);
@ -303,14 +303,14 @@ public class ChannelBuffersTest {
@Test @Test
public void testHexDump() { public void testHexDump() {
assertEquals("", hexDump(EMPTY_BUFFER)); assertEquals("", ByteBufUtil.hexDump(EMPTY_BUFFER));
assertEquals("123456", hexDump(wrappedBuffer( assertEquals("123456", ByteBufUtil.hexDump(wrappedBuffer(
new byte[] { new byte[] {
0x12, 0x34, 0x56 0x12, 0x34, 0x56
}))); })));
assertEquals("1234567890abcdef", hexDump(wrappedBuffer( assertEquals("1234567890abcdef", ByteBufUtil.hexDump(wrappedBuffer(
new byte[] { new byte[] {
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
(byte) 0x90, (byte) 0xAB, (byte) 0xCD, (byte) 0xEF (byte) 0x90, (byte) 0xAB, (byte) 0xCD, (byte) 0xEF
@ -319,8 +319,8 @@ public class ChannelBuffersTest {
@Test @Test
public void testSwapMedium() { public void testSwapMedium() {
assertEquals(0x563412, swapMedium(0x123456)); assertEquals(0x563412, ByteBufUtil.swapMedium(0x123456));
assertEquals(0x80, swapMedium(0x800000)); assertEquals(0x80, ByteBufUtil.swapMedium(0x800000));
} }
@Test @Test