use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.
the class BufferTest method testGetByte.
@Test
public void testGetByte() throws Exception {
int bytesLen = 100;
byte[] bytes = TestUtils.randomByteArray(bytesLen);
Buffer b = Buffer.buffer(bytes);
for (int i = 0; i < bytesLen; i++) {
assertEquals(bytes[i], b.getByte(i));
}
}
use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.
the class BufferTest method testAppendBufferWithOffsetAndLen.
@Test
public void testAppendBufferWithOffsetAndLen() throws Exception {
int bytesLen = 100;
byte[] bytes = TestUtils.randomByteArray(bytesLen);
Buffer src = Buffer.buffer(bytes);
int len = bytesLen - 2;
Buffer b = Buffer.buffer();
b.appendBuffer(src, 1, len);
assertEquals(b.length(), len);
byte[] copy = new byte[len];
System.arraycopy(bytes, 1, copy, 0, len);
assertTrue(TestUtils.byteArraysEqual(copy, b.getBytes()));
b.appendBuffer(src, 1, len);
assertEquals(b.length(), 2 * len);
assertNullPointerException(() -> b.appendBuffer(null, 1, len));
}
use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.
the class BufferTest method testGetOutOfBounds.
@Test
public void testGetOutOfBounds() throws Exception {
int bytesLen = 100;
byte[] bytes = TestUtils.randomByteArray(bytesLen);
Buffer b = Buffer.buffer(bytes);
assertIndexOutOfBoundsException(() -> b.getByte(bytesLen));
assertIndexOutOfBoundsException(() -> b.getByte(bytesLen + 1));
assertIndexOutOfBoundsException(() -> b.getByte(bytesLen + 100));
assertIndexOutOfBoundsException(() -> b.getByte(-1));
assertIndexOutOfBoundsException(() -> b.getByte(-100));
assertIndexOutOfBoundsException(() -> b.getInt(bytesLen));
assertIndexOutOfBoundsException(() -> b.getInt(bytesLen + 1));
assertIndexOutOfBoundsException(() -> b.getInt(bytesLen + 100));
assertIndexOutOfBoundsException(() -> b.getInt(-1));
assertIndexOutOfBoundsException(() -> b.getInt(-100));
assertIndexOutOfBoundsException(() -> b.getLong(bytesLen));
assertIndexOutOfBoundsException(() -> b.getLong(bytesLen + 1));
assertIndexOutOfBoundsException(() -> b.getLong(bytesLen + 100));
assertIndexOutOfBoundsException(() -> b.getLong(-1));
assertIndexOutOfBoundsException(() -> b.getLong(-100));
assertIndexOutOfBoundsException(() -> b.getFloat(bytesLen));
assertIndexOutOfBoundsException(() -> b.getFloat(bytesLen + 1));
assertIndexOutOfBoundsException(() -> b.getFloat(bytesLen + 100));
assertIndexOutOfBoundsException(() -> b.getFloat(-1));
assertIndexOutOfBoundsException(() -> b.getFloat(-100));
assertIndexOutOfBoundsException(() -> b.getDouble(bytesLen));
assertIndexOutOfBoundsException(() -> b.getDouble(bytesLen + 1));
assertIndexOutOfBoundsException(() -> b.getDouble(bytesLen + 100));
assertIndexOutOfBoundsException(() -> b.getDouble(-1));
assertIndexOutOfBoundsException(() -> b.getDouble(-100));
assertIndexOutOfBoundsException(() -> b.getShort(bytesLen));
assertIndexOutOfBoundsException(() -> b.getShort(bytesLen + 1));
assertIndexOutOfBoundsException(() -> b.getShort(bytesLen + 100));
assertIndexOutOfBoundsException(() -> b.getShort(-1));
assertIndexOutOfBoundsException(() -> b.getShort(-100));
assertIndexOutOfBoundsException(() -> b.getBytes(bytesLen + 1, bytesLen + 1));
assertIndexOutOfBoundsException(() -> b.getBytes(bytesLen + 100, bytesLen + 100));
assertIndexOutOfBoundsException(() -> b.getBytes(-1, -1));
assertIndexOutOfBoundsException(() -> b.getBytes(-100, -100));
assertIndexOutOfBoundsException(() -> b.getString(-1, bytesLen));
assertIndexOutOfBoundsException(() -> b.getString(0, bytesLen + 1));
assertIllegalArgumentException(() -> b.getString(2, 1));
assertIndexOutOfBoundsException(() -> b.getString(-1, bytesLen, "UTF-8"));
assertIndexOutOfBoundsException(() -> b.getString(0, bytesLen + 1, "UTF-8"));
assertIllegalArgumentException(() -> b.getString(2, 1, "UTF-8"));
}
use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.
the class BufferTest method testAppendShort.
@Test
public void testAppendShort() {
Buffer b = Buffer.buffer(TestUtils.randomByteArray(100));
b.appendShort(Short.MAX_VALUE);
assertEquals(102, b.length());
b.appendShortLE(Short.MAX_VALUE);
assertEquals(104, b.length());
}
use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.
the class BufferTest method testToJsonArray.
@Test
public void testToJsonArray() throws Exception {
JsonArray arr = new JsonArray();
arr.add("wibble");
arr.add(5);
arr.add(true);
Buffer buff = Buffer.buffer(arr.encode());
assertEquals(arr, buff.toJsonArray());
buff = Buffer.buffer(TestUtils.randomAlphaString(10));
try {
buff.toJsonObject();
fail();
} catch (DecodeException ignore) {
}
}
Aggregations