Search in sources :

Example 86 with Buffer

use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.

the class BufferTest method testAppendByte.

@Test
public void testAppendByte() throws Exception {
    int bytesLen = 100;
    byte[] bytes = TestUtils.randomByteArray(bytesLen);
    Buffer b = Buffer.buffer();
    for (int i = 0; i < bytesLen; i++) {
        b.appendByte(bytes[i]);
    }
    assertEquals(b.length(), bytes.length);
    assertTrue(TestUtils.byteArraysEqual(bytes, b.getBytes()));
    for (int i = 0; i < bytesLen; i++) {
        b.appendByte(bytes[i]);
    }
    assertEquals(b.length(), 2 * bytes.length);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 87 with Buffer

use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.

the class BufferTest method testSetBytesBuffer.

private void testSetBytesBuffer(Buffer buff) throws Exception {
    Buffer b = TestUtils.randomBuffer(100);
    buff.setBuffer(50, b);
    byte[] b2 = buff.getBytes(50, 150);
    assertEquals(b, Buffer.buffer(b2));
    byte[] b3 = TestUtils.randomByteArray(100);
    buff.setBytes(50, b3);
    byte[] b4 = buff.getBytes(50, 150);
    assertEquals(Buffer.buffer(b3), Buffer.buffer(b4));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ByteBuffer(java.nio.ByteBuffer)

Example 88 with Buffer

use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.

the class BufferTest method testGetBytes2.

@Test
public void testGetBytes2() throws Exception {
    byte[] bytes = TestUtils.randomByteArray(100);
    Buffer b = Buffer.buffer(bytes);
    byte[] sub = new byte[bytes.length / 2];
    System.arraycopy(bytes, bytes.length / 4, sub, 0, bytes.length / 2);
    assertTrue(TestUtils.byteArraysEqual(sub, b.getBytes(bytes.length / 4, bytes.length / 4 + bytes.length / 2)));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 89 with Buffer

use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.

the class Http2ConnectionBase method onPingAckRead.

@Override
public synchronized void onPingAckRead(ChannelHandlerContext ctx, ByteBuf data) {
    Handler<AsyncResult<Buffer>> handler = pongHandlers.poll();
    if (handler != null) {
        context.executeFromIO(() -> {
            Buffer buff = Buffer.buffer(safeBuffer(data, ctx.alloc()));
            handler.handle(Future.succeededFuture(buff));
        });
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer) AsyncResult(io.vertx.core.AsyncResult)

Example 90 with Buffer

use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.

the class WebSocketImplBase method writePartialMessage.

/**
   * Splits the provided buffer into multiple frames (which do not exceed the maximum web socket frame size)
   * and writes them in order to the socket.
   */
private void writePartialMessage(FrameType frameType, Buffer data, int offset) {
    int end = offset + maxWebSocketFrameSize;
    boolean isFinal;
    if (end >= data.length()) {
        end = data.length();
        isFinal = true;
    } else {
        isFinal = false;
    }
    Buffer slice = data.slice(offset, end);
    WebSocketFrame frame;
    if (offset == 0 || !supportsContinuation) {
        frame = new WebSocketFrameImpl(frameType, slice.getByteBuf(), isFinal);
    } else {
        frame = WebSocketFrame.continuationFrame(slice, isFinal);
    }
    writeFrame(frame);
    int newOffset = offset + maxWebSocketFrameSize;
    if (!isFinal) {
        writePartialMessage(frameType, data, newOffset);
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer) WebSocketFrameImpl(io.vertx.core.http.impl.ws.WebSocketFrameImpl) WebSocketFrame(io.vertx.core.http.WebSocketFrame)

Aggregations

Buffer (io.vertx.core.buffer.Buffer)1052 Test (org.junit.Test)604 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)256 Future (io.vertx.core.Future)253 AtomicReference (java.util.concurrent.atomic.AtomicReference)237 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)235 Handler (io.vertx.core.Handler)230 Vertx (io.vertx.core.Vertx)185 TestUtils (io.vertx.test.core.TestUtils)177 JsonObject (io.vertx.core.json.JsonObject)172 TimeUnit (java.util.concurrent.TimeUnit)161 CountDownLatch (java.util.concurrent.CountDownLatch)153 List (java.util.List)152 Consumer (java.util.function.Consumer)149 Function (java.util.function.Function)142 ReadStream (io.vertx.core.streams.ReadStream)139 Context (io.vertx.core.Context)135 ArrayList (java.util.ArrayList)132 MultiMap (io.vertx.core.MultiMap)130 Assume (org.junit.Assume)130