Rename wrapPrimitive() to copyPrimitive() / Add tests / Tidy up (#167)
- Add ChannelBuffers.copyShort(short...)
This commit is contained in:
parent
429535e6c8
commit
c8fa42beaf
@ -824,7 +824,7 @@ public final class ChannelBuffers {
|
|||||||
/**
|
/**
|
||||||
* Creates a new 4-byte buffer that holds the specified 32-bit integer.
|
* Creates a new 4-byte buffer that holds the specified 32-bit integer.
|
||||||
*/
|
*/
|
||||||
public static ChannelBuffer wrapInt(int value) {
|
public static ChannelBuffer copyInt(int value) {
|
||||||
ChannelBuffer buf = buffer(4);
|
ChannelBuffer buf = buffer(4);
|
||||||
buf.writeInt(value);
|
buf.writeInt(value);
|
||||||
return buf;
|
return buf;
|
||||||
@ -833,7 +833,7 @@ public final class ChannelBuffers {
|
|||||||
/**
|
/**
|
||||||
* Create a {@link ChannelBuffer} that holds all the given values as int's
|
* Create a {@link ChannelBuffer} that holds all the given values as int's
|
||||||
*/
|
*/
|
||||||
public static ChannelBuffer wrapInt(int... values) {
|
public static ChannelBuffer copyInt(int... values) {
|
||||||
if (values == null || values.length == 0) {
|
if (values == null || values.length == 0) {
|
||||||
return EMPTY_BUFFER;
|
return EMPTY_BUFFER;
|
||||||
}
|
}
|
||||||
@ -847,7 +847,7 @@ public final class ChannelBuffers {
|
|||||||
/**
|
/**
|
||||||
* Creates a new 2-byte buffer that holds the specified 16-bit integer.
|
* Creates a new 2-byte buffer that holds the specified 16-bit integer.
|
||||||
*/
|
*/
|
||||||
public static ChannelBuffer wrapShort(int value) {
|
public static ChannelBuffer copyShort(int value) {
|
||||||
ChannelBuffer buf = buffer(2);
|
ChannelBuffer buf = buffer(2);
|
||||||
buf.writeShort(value);
|
buf.writeShort(value);
|
||||||
return buf;
|
return buf;
|
||||||
@ -856,7 +856,21 @@ public final class ChannelBuffers {
|
|||||||
/**
|
/**
|
||||||
* Create a new buffer that holds a sequence of the specified 16-bit integers.
|
* Create a new buffer that holds a sequence of the specified 16-bit integers.
|
||||||
*/
|
*/
|
||||||
public static ChannelBuffer wrapShort(int... values) {
|
public static ChannelBuffer copyShort(short... values) {
|
||||||
|
if (values == null || values.length == 0) {
|
||||||
|
return EMPTY_BUFFER;
|
||||||
|
}
|
||||||
|
ChannelBuffer buffer = buffer(values.length * 2);
|
||||||
|
for (int v: values) {
|
||||||
|
buffer.writeShort(v);
|
||||||
|
}
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new buffer that holds a sequence of the specified 16-bit integers.
|
||||||
|
*/
|
||||||
|
public static ChannelBuffer copyShort(int... values) {
|
||||||
if (values == null || values.length == 0) {
|
if (values == null || values.length == 0) {
|
||||||
return EMPTY_BUFFER;
|
return EMPTY_BUFFER;
|
||||||
}
|
}
|
||||||
@ -870,7 +884,7 @@ public final class ChannelBuffers {
|
|||||||
/**
|
/**
|
||||||
* Creates a new 3-byte buffer that holds the specified 24-bit integer.
|
* Creates a new 3-byte buffer that holds the specified 24-bit integer.
|
||||||
*/
|
*/
|
||||||
public static ChannelBuffer wrapMedium(int value) {
|
public static ChannelBuffer copyMedium(int value) {
|
||||||
ChannelBuffer buf = buffer(3);
|
ChannelBuffer buf = buffer(3);
|
||||||
buf.writeMedium(value);
|
buf.writeMedium(value);
|
||||||
return buf;
|
return buf;
|
||||||
@ -879,7 +893,7 @@ public final class ChannelBuffers {
|
|||||||
/**
|
/**
|
||||||
* Create a new buffer that holds a sequence of the specified 24-bit integers.
|
* Create a new buffer that holds a sequence of the specified 24-bit integers.
|
||||||
*/
|
*/
|
||||||
public static ChannelBuffer wrapMedium(int... values) {
|
public static ChannelBuffer copyMedium(int... values) {
|
||||||
if (values == null || values.length == 0) {
|
if (values == null || values.length == 0) {
|
||||||
return EMPTY_BUFFER;
|
return EMPTY_BUFFER;
|
||||||
}
|
}
|
||||||
@ -893,7 +907,7 @@ public final class ChannelBuffers {
|
|||||||
/**
|
/**
|
||||||
* Creates a new 8-byte buffer that holds the specified 64-bit integer.
|
* Creates a new 8-byte buffer that holds the specified 64-bit integer.
|
||||||
*/
|
*/
|
||||||
public static ChannelBuffer wrapLong(long value) {
|
public static ChannelBuffer copyLong(long value) {
|
||||||
ChannelBuffer buf = buffer(8);
|
ChannelBuffer buf = buffer(8);
|
||||||
buf.writeLong(value);
|
buf.writeLong(value);
|
||||||
return buf;
|
return buf;
|
||||||
@ -902,7 +916,7 @@ public final class ChannelBuffers {
|
|||||||
/**
|
/**
|
||||||
* Create a new buffer that holds a sequence of the specified 64-bit integers.
|
* Create a new buffer that holds a sequence of the specified 64-bit integers.
|
||||||
*/
|
*/
|
||||||
public static ChannelBuffer wrapLong(long... values) {
|
public static ChannelBuffer copyLong(long... values) {
|
||||||
if (values == null || values.length == 0) {
|
if (values == null || values.length == 0) {
|
||||||
return EMPTY_BUFFER;
|
return EMPTY_BUFFER;
|
||||||
}
|
}
|
||||||
@ -916,7 +930,7 @@ public final class ChannelBuffers {
|
|||||||
/**
|
/**
|
||||||
* Creates a new single-byte buffer that holds the specified boolean value.
|
* Creates a new single-byte buffer that holds the specified boolean value.
|
||||||
*/
|
*/
|
||||||
public static ChannelBuffer wrapBoolean(boolean value) {
|
public static ChannelBuffer copyBoolean(boolean value) {
|
||||||
ChannelBuffer buf = buffer(1);
|
ChannelBuffer buf = buffer(1);
|
||||||
buf.writeBoolean(value);
|
buf.writeBoolean(value);
|
||||||
return buf;
|
return buf;
|
||||||
@ -925,7 +939,7 @@ public final class ChannelBuffers {
|
|||||||
/**
|
/**
|
||||||
* Create a new buffer that holds a sequence of the specified boolean values.
|
* Create a new buffer that holds a sequence of the specified boolean values.
|
||||||
*/
|
*/
|
||||||
public static ChannelBuffer wrapBoolean(boolean... values) {
|
public static ChannelBuffer copyBoolean(boolean... values) {
|
||||||
if (values == null || values.length == 0) {
|
if (values == null || values.length == 0) {
|
||||||
return EMPTY_BUFFER;
|
return EMPTY_BUFFER;
|
||||||
}
|
}
|
||||||
@ -939,7 +953,7 @@ public final class ChannelBuffers {
|
|||||||
/**
|
/**
|
||||||
* Creates a new 4-byte buffer that holds the specified 32-bit floating point number.
|
* Creates a new 4-byte buffer that holds the specified 32-bit floating point number.
|
||||||
*/
|
*/
|
||||||
public static ChannelBuffer wrapFloat(float value) {
|
public static ChannelBuffer copyFloat(float value) {
|
||||||
ChannelBuffer buf = buffer(4);
|
ChannelBuffer buf = buffer(4);
|
||||||
buf.writeFloat(value);
|
buf.writeFloat(value);
|
||||||
return buf;
|
return buf;
|
||||||
@ -948,7 +962,7 @@ public final class ChannelBuffers {
|
|||||||
/**
|
/**
|
||||||
* Create a new buffer that holds a sequence of the specified 32-bit floating point numbers.
|
* Create a new buffer that holds a sequence of the specified 32-bit floating point numbers.
|
||||||
*/
|
*/
|
||||||
public static ChannelBuffer wrapFloat(float... values) {
|
public static ChannelBuffer copyFloat(float... values) {
|
||||||
if (values == null || values.length == 0) {
|
if (values == null || values.length == 0) {
|
||||||
return EMPTY_BUFFER;
|
return EMPTY_BUFFER;
|
||||||
}
|
}
|
||||||
@ -962,7 +976,7 @@ public final class ChannelBuffers {
|
|||||||
/**
|
/**
|
||||||
* Creates a new 8-byte buffer that holds the specified 64-bit floating point number.
|
* Creates a new 8-byte buffer that holds the specified 64-bit floating point number.
|
||||||
*/
|
*/
|
||||||
public static ChannelBuffer wrapDouble(double value) {
|
public static ChannelBuffer copyDouble(double value) {
|
||||||
ChannelBuffer buf = buffer(8);
|
ChannelBuffer buf = buffer(8);
|
||||||
buf.writeDouble(value);
|
buf.writeDouble(value);
|
||||||
return buf;
|
return buf;
|
||||||
@ -971,7 +985,7 @@ public final class ChannelBuffers {
|
|||||||
/**
|
/**
|
||||||
* Create a new buffer that holds a sequence of the specified 64-bit floating point numbers.
|
* Create a new buffer that holds a sequence of the specified 64-bit floating point numbers.
|
||||||
*/
|
*/
|
||||||
public static ChannelBuffer wrapDouble(double... values) {
|
public static ChannelBuffer copyDouble(double... values) {
|
||||||
if (values == null || values.length == 0) {
|
if (values == null || values.length == 0) {
|
||||||
return EMPTY_BUFFER;
|
return EMPTY_BUFFER;
|
||||||
}
|
}
|
||||||
|
@ -426,69 +426,148 @@ public class ChannelBuffersTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWrapSingleInt() {
|
||||||
|
ChannelBuffer buffer = ChannelBuffers.copyInt(42);
|
||||||
|
assertEquals(4, buffer.capacity());
|
||||||
|
assertEquals(42, buffer.readInt());
|
||||||
|
assertFalse(buffer.readable());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWrapInt() {
|
public void testWrapInt() {
|
||||||
ChannelBuffer buffer = ChannelBuffers.wrapInt(1,4);
|
ChannelBuffer buffer = ChannelBuffers.copyInt(1, 4);
|
||||||
assertEquals(8, buffer.capacity());
|
assertEquals(8, buffer.capacity());
|
||||||
assertEquals(1, buffer.readInt());
|
assertEquals(1, buffer.readInt());
|
||||||
assertEquals(4, buffer.readInt());
|
assertEquals(4, buffer.readInt());
|
||||||
assertFalse(buffer.readable());
|
assertFalse(buffer.readable());
|
||||||
|
|
||||||
assertEquals(0, ChannelBuffers.wrapInt(null).capacity());
|
assertEquals(0, ChannelBuffers.copyInt(null).capacity());
|
||||||
assertEquals(0, ChannelBuffers.wrapInt(new int[0]).capacity());
|
assertEquals(0, ChannelBuffers.copyInt(new int[0]).capacity());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWrapShort() {
|
public void testWrapSingleShort() {
|
||||||
ChannelBuffer buffer = ChannelBuffers.wrapShort(1,4);
|
ChannelBuffer buffer = ChannelBuffers.copyShort(42);
|
||||||
|
assertEquals(2, buffer.capacity());
|
||||||
|
assertEquals(42, buffer.readShort());
|
||||||
|
assertFalse(buffer.readable());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWrapShortFromShortArray() {
|
||||||
|
ChannelBuffer buffer = ChannelBuffers.copyShort(new short[] { 1, 4 });
|
||||||
assertEquals(4, buffer.capacity());
|
assertEquals(4, buffer.capacity());
|
||||||
assertEquals(1, buffer.readShort());
|
assertEquals(1, buffer.readShort());
|
||||||
assertEquals(4, buffer.readShort());
|
assertEquals(4, buffer.readShort());
|
||||||
assertFalse(buffer.readable());
|
assertFalse(buffer.readable());
|
||||||
|
|
||||||
assertEquals(0, ChannelBuffers.wrapShort(null).capacity());
|
assertEquals(0, ChannelBuffers.copyShort((short[]) null).capacity());
|
||||||
assertEquals(0, ChannelBuffers.wrapShort(new int[0]).capacity());
|
assertEquals(0, ChannelBuffers.copyShort(new short[0]).capacity());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWrapShortFromIntArray() {
|
||||||
|
ChannelBuffer buffer = ChannelBuffers.copyShort(1, 4);
|
||||||
|
assertEquals(4, buffer.capacity());
|
||||||
|
assertEquals(1, buffer.readShort());
|
||||||
|
assertEquals(4, buffer.readShort());
|
||||||
|
assertFalse(buffer.readable());
|
||||||
|
|
||||||
|
assertEquals(0, ChannelBuffers.copyShort((int[]) null).capacity());
|
||||||
|
assertEquals(0, ChannelBuffers.copyShort(new int[0]).capacity());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWrapSingleMedium() {
|
||||||
|
ChannelBuffer buffer = ChannelBuffers.copyMedium(42);
|
||||||
|
assertEquals(3, buffer.capacity());
|
||||||
|
assertEquals(42, buffer.readMedium());
|
||||||
|
assertFalse(buffer.readable());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWrapMedium() {
|
public void testWrapMedium() {
|
||||||
ChannelBuffer buffer = ChannelBuffers.wrapMedium(1,4);
|
ChannelBuffer buffer = ChannelBuffers.copyMedium(1, 4);
|
||||||
assertEquals(6, buffer.capacity());
|
assertEquals(6, buffer.capacity());
|
||||||
assertEquals(1, buffer.readMedium());
|
assertEquals(1, buffer.readMedium());
|
||||||
assertEquals(4, buffer.readMedium());
|
assertEquals(4, buffer.readMedium());
|
||||||
assertFalse(buffer.readable());
|
assertFalse(buffer.readable());
|
||||||
|
|
||||||
assertEquals(0, ChannelBuffers.wrapMedium(null).capacity());
|
assertEquals(0, ChannelBuffers.copyMedium(null).capacity());
|
||||||
assertEquals(0, ChannelBuffers.wrapMedium(new int[0]).capacity());
|
assertEquals(0, ChannelBuffers.copyMedium(new int[0]).capacity());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWrapSingleLong() {
|
||||||
|
ChannelBuffer buffer = ChannelBuffers.copyLong(42);
|
||||||
|
assertEquals(8, buffer.capacity());
|
||||||
|
assertEquals(42, buffer.readLong());
|
||||||
|
assertFalse(buffer.readable());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWrapLong() {
|
public void testWrapLong() {
|
||||||
ChannelBuffer buffer = ChannelBuffers.wrapLong(1,4);
|
ChannelBuffer buffer = ChannelBuffers.copyLong(1, 4);
|
||||||
assertEquals(16, buffer.capacity());
|
assertEquals(16, buffer.capacity());
|
||||||
assertEquals(1, buffer.readLong());
|
assertEquals(1, buffer.readLong());
|
||||||
assertEquals(4, buffer.readLong());
|
assertEquals(4, buffer.readLong());
|
||||||
assertFalse(buffer.readable());
|
assertFalse(buffer.readable());
|
||||||
|
|
||||||
assertEquals(0, ChannelBuffers.wrapLong(null).capacity());
|
assertEquals(0, ChannelBuffers.copyLong(null).capacity());
|
||||||
assertEquals(0, ChannelBuffers.wrapLong(new long[0]).capacity());
|
assertEquals(0, ChannelBuffers.copyLong(new long[0]).capacity());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWrapSingleFloat() {
|
||||||
|
ChannelBuffer buffer = ChannelBuffers.copyFloat(42);
|
||||||
|
assertEquals(4, buffer.capacity());
|
||||||
|
assertEquals(42, buffer.readFloat(), 0.01);
|
||||||
|
assertFalse(buffer.readable());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWrapFloat() {
|
||||||
|
ChannelBuffer buffer = ChannelBuffers.copyFloat(1, 4);
|
||||||
|
assertEquals(8, buffer.capacity());
|
||||||
|
assertEquals(1, buffer.readFloat(), 0.01);
|
||||||
|
assertEquals(4, buffer.readFloat(), 0.01);
|
||||||
|
assertFalse(buffer.readable());
|
||||||
|
|
||||||
|
assertEquals(0, ChannelBuffers.copyFloat(null).capacity());
|
||||||
|
assertEquals(0, ChannelBuffers.copyFloat(new float[0]).capacity());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWrapSingleDouble() {
|
||||||
|
ChannelBuffer buffer = ChannelBuffers.copyDouble(42);
|
||||||
|
assertEquals(8, buffer.capacity());
|
||||||
|
assertEquals(42, buffer.readDouble(), 0.01);
|
||||||
|
assertFalse(buffer.readable());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWrapDouble() {
|
||||||
|
ChannelBuffer buffer = ChannelBuffers.copyDouble(1, 4);
|
||||||
|
assertEquals(16, buffer.capacity());
|
||||||
|
assertEquals(1, buffer.readDouble(), 0.01);
|
||||||
|
assertEquals(4, buffer.readDouble(), 0.01);
|
||||||
|
assertFalse(buffer.readable());
|
||||||
|
|
||||||
|
assertEquals(0, ChannelBuffers.copyDouble(null).capacity());
|
||||||
|
assertEquals(0, ChannelBuffers.copyDouble(new double[0]).capacity());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWrapBoolean() {
|
public void testWrapBoolean() {
|
||||||
ChannelBuffer buffer = ChannelBuffers.wrapBoolean(true, false);
|
ChannelBuffer buffer = ChannelBuffers.copyBoolean(true, false);
|
||||||
assertEquals(2, buffer.capacity());
|
assertEquals(2, buffer.capacity());
|
||||||
assertEquals(true, buffer.readBoolean());
|
assertEquals(true, buffer.readBoolean());
|
||||||
assertEquals(false, buffer.readBoolean());
|
assertEquals(false, buffer.readBoolean());
|
||||||
assertFalse(buffer.readable());
|
assertFalse(buffer.readable());
|
||||||
|
|
||||||
assertEquals(0, ChannelBuffers.wrapBoolean(null).capacity());
|
assertEquals(0, ChannelBuffers.copyBoolean(null).capacity());
|
||||||
assertEquals(0, ChannelBuffers.wrapBoolean(new boolean[0]).capacity());
|
assertEquals(0, ChannelBuffers.copyBoolean(new boolean[0]).capacity());
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user