Search in sources :

Example 56 with Buffer

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

the class HTTPExamples method example48.

public void example48(HttpClient client) {
    client.getNow("some-uri", response -> {
        Buffer totalBuffer = Buffer.buffer();
        response.handler(buffer -> {
            System.out.println("Received a part of the response body: " + buffer.length());
            totalBuffer.appendBuffer(buffer);
        });
        response.endHandler(v -> {
            System.out.println("Total response body length is " + totalBuffer.length());
        });
    });
}
Also used : Buffer(io.vertx.core.buffer.Buffer)

Example 57 with Buffer

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

the class FileSystemExamples method asyncFileWrite.

public void asyncFileWrite() {
    Vertx vertx = Vertx.vertx();
    vertx.fileSystem().open("target/classes/hello.txt", new OpenOptions(), result -> {
        if (result.succeeded()) {
            AsyncFile file = result.result();
            Buffer buff = Buffer.buffer("foo");
            for (int i = 0; i < 5; i++) {
                file.write(buff, buff.length() * i, ar -> {
                    if (ar.succeeded()) {
                        System.out.println("Written ok!");
                    } else {
                        System.err.println("Failed to write: " + ar.cause());
                    }
                });
            }
        } else {
            System.err.println("Cannot open file " + result.cause());
        }
    });
}
Also used : OpenOptions(io.vertx.core.file.OpenOptions) Buffer(io.vertx.core.buffer.Buffer) AsyncFile(io.vertx.core.file.AsyncFile) Vertx(io.vertx.core.Vertx)

Example 58 with Buffer

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

the class EventBusTestBase method testSendBuffer.

@Test
public void testSendBuffer() {
    Buffer sent = TestUtils.randomBuffer(100);
    testSend(sent, (buffer) -> {
        assertEquals(sent, buffer);
        // Make sure it's copied
        assertFalse(sent == buffer);
    });
}
Also used : Buffer(io.vertx.core.buffer.Buffer) Test(org.junit.Test)

Example 59 with Buffer

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

the class DatagramTest method testSendReceiveLargePacket.

@Test
public void testSendReceiveLargePacket() {
    int packetSize = 10000;
    peer1 = vertx.createDatagramSocket(new DatagramSocketOptions().setSendBufferSize(packetSize));
    // OSX needs 16 more
    peer2 = vertx.createDatagramSocket(new DatagramSocketOptions().setReceiveBufferSize(packetSize + 16));
    peer2.exceptionHandler(t -> fail(t.getMessage()));
    peer2.listen(1234, "127.0.0.1", ar -> {
        assertTrue(ar.succeeded());
        Buffer buffer = TestUtils.randomBuffer(packetSize);
        peer2.handler(packet -> {
            assertEquals(buffer, packet.data());
            testComplete();
        });
        peer1.send(buffer, 1234, "127.0.0.1", ar2 -> assertTrue(ar2.succeeded()));
    });
    await();
}
Also used : DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) Buffer(io.vertx.core.buffer.Buffer) Test(org.junit.Test)

Example 60 with Buffer

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

the class DatagramTest method testBroadcast.

@Test
public void testBroadcast() {
    peer1 = vertx.createDatagramSocket(new DatagramSocketOptions().setBroadcast(true));
    peer2 = vertx.createDatagramSocket(new DatagramSocketOptions().setBroadcast(true));
    peer2.exceptionHandler(t -> fail(t.getMessage()));
    peer2.listen(1234, "0.0.0.0", ar1 -> {
        assertTrue(ar1.succeeded());
        Buffer buffer = TestUtils.randomBuffer(128);
        peer2.handler(packet -> {
            assertEquals(buffer, packet.data());
            testComplete();
        });
        peer1.send(buffer, 1234, "255.255.255.255", ar2 -> {
            assertTrue(ar2.succeeded());
        });
    });
    await();
}
Also used : DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) Buffer(io.vertx.core.buffer.Buffer) Test(org.junit.Test)

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