Example usage for java.io ByteArrayOutputStream wait

List of usage examples for java.io ByteArrayOutputStream wait

Introduction

In this page you can find the example usage for java.io ByteArrayOutputStream wait.

Prototype

public final void wait() throws InterruptedException 

Source Link

Document

Causes the current thread to wait until it is awakened, typically by being notified or interrupted.

Usage

From source file:org.commonjava.vertx.vabr.util.VertXInputStreamTest.java

@Test
//    @Ignore/* w  ww  .  j a v a 2  s  . c o m*/
public void readViaHttpHandler() throws InterruptedException {
    final ByteArrayOutputStream result = new ByteArrayOutputStream();

    final Vertx v = new DefaultVertx();
    final HttpServer server = v.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {
        @Override
        public void handle(final HttpServerRequest request) {
            request.pause();

            new Thread(new Runnable() {
                @Override
                public void run() {
                    logger.info("GOT IT");
                    final VertXInputStream stream = new VertXInputStream(request);
                    try {
                        IOUtils.copy(stream, result);

                        logger.info("READ DONE");
                        synchronized (result) {
                            result.notifyAll();
                        }
                    } catch (final IOException e) {
                        throw new RuntimeException("Failed to read stream: " + e.getMessage(), e);
                    }
                }
            }, "server-request").start();
        }
    }).listen(port, "localhost");

    final HttpClient client = v.createHttpClient().setHost("localhost").setPort(port);
    final HttpClientRequest put = client.put("/put", new Handler<HttpClientResponse>() {
        @Override
        public void handle(final HttpClientResponse response) {
            logger.info("Response: {} {}", response.statusCode(), response.statusMessage());
        }
    });

    final ByteArrayOutputStream check = new ByteArrayOutputStream();
    final Random rand = new Random();

    // 4Mb file...
    final byte[] txfr = new byte[4];
    for (int i = 0; i < 1048576; i++) {
        rand.nextBytes(txfr);
        check.write(txfr, 0, txfr.length);
    }

    put.setChunked(true).write(new Buffer(check.toByteArray())).end();

    logger.info("SENT: {}", check.toByteArray().length);

    synchronized (result) {
        result.wait();
    }

    final byte[] checkedArry = check.toByteArray();
    final byte[] resultArry = result.toByteArray();
    assertThat(checkedArry.length, equalTo(resultArry.length));

    boolean match = true;
    for (int i = 0; i < checkedArry.length; i++) {
        if (resultArry[i] != checkedArry[i]) {
            logger.error("Index {} mismatch! Was: {}, expected: {}", i, resultArry[i], checkedArry[i]);
            match = false;
        }
    }

    assertThat("Byte arrays do not match.", match, equalTo(true));

    server.close();
    client.close();
}