Example usage for java.io ByteArrayOutputStream notifyAll

List of usage examples for java.io ByteArrayOutputStream notifyAll

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public final native void notifyAll();

Source Link

Document

Wakes up all threads that are waiting on this object's monitor.

Usage

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

@Test
//    @Ignore/*from w w w  .  ja  v  a2 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();
}