Search in sources :

Example 86 with PipedOutputStream

use of java.io.PipedOutputStream in project activemq-artemis by apache.

the class LargeMessageBufferTest method testStreamData.

@Test
public void testStreamData() throws Exception {
    final LargeMessageControllerImpl outBuffer = new LargeMessageControllerImpl(new FakeConsumerInternal(), 1024 * 11 + 123, 1000);
    final PipedOutputStream output = new PipedOutputStream();
    final PipedInputStream input = new PipedInputStream(output);
    final AtomicInteger errors = new AtomicInteger(0);
    // Done reading 3 elements
    final CountDownLatch done1 = new CountDownLatch(1);
    // Done with the thread
    final CountDownLatch done2 = new CountDownLatch(1);
    final AtomicInteger count = new AtomicInteger(0);
    final AtomicInteger totalBytes = new AtomicInteger(0);
    Thread treader = new Thread("treader") {

        @Override
        public void run() {
            try {
                byte[] line = new byte[1024];
                int dataRead = 0;
                while (dataRead >= 0) {
                    dataRead = input.read(line);
                    if (dataRead > 0) {
                        totalBytes.addAndGet(dataRead);
                        if (count.incrementAndGet() == 3) {
                            done1.countDown();
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                errors.incrementAndGet();
            } finally {
                done1.countDown();
                done2.countDown();
            }
        }
    };
    treader.setDaemon(true);
    treader.start();
    for (int i = 0; i < 3; i++) {
        outBuffer.addPacket(new byte[1024], 1, true);
    }
    outBuffer.setOutputStream(output);
    final CountDownLatch waiting = new CountDownLatch(1);
    Thread twaiter = new Thread("twaiter") {

        @Override
        public void run() {
            try {
                outBuffer.waitCompletion(0);
                waiting.countDown();
            } catch (Exception e) {
                e.printStackTrace();
                errors.incrementAndGet();
            }
        }
    };
    twaiter.setDaemon(true);
    twaiter.start();
    Assert.assertTrue(done1.await(10, TimeUnit.SECONDS));
    Assert.assertEquals(3, count.get());
    Assert.assertEquals(1024 * 3, totalBytes.get());
    for (int i = 0; i < 8; i++) {
        outBuffer.addPacket(new byte[1024], 1, true);
    }
    Assert.assertEquals(1, waiting.getCount());
    outBuffer.addPacket(new byte[123], 1, false);
    Assert.assertTrue(done2.await(10, TimeUnit.SECONDS));
    Assert.assertTrue(waiting.await(10, TimeUnit.SECONDS));
    Assert.assertEquals(12, count.get());
    Assert.assertEquals(1024 * 11 + 123, totalBytes.get());
    treader.join();
    twaiter.join();
    Assert.assertEquals(0, errors.get());
    input.close();
}
Also used : LargeMessageControllerImpl(org.apache.activemq.artemis.core.client.impl.LargeMessageControllerImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) IOException(java.io.IOException) Test(org.junit.Test)

Example 87 with PipedOutputStream

use of java.io.PipedOutputStream in project new-cloud by xie-summer.

the class ProtocolTest method buildACommand.

@Test
public void buildACommand() throws IOException {
    PipedInputStream pis = new PipedInputStream();
    BufferedInputStream bis = new BufferedInputStream(pis);
    PipedOutputStream pos = new PipedOutputStream(pis);
    RedisOutputStream ros = new RedisOutputStream(pos);
    Protocol.sendCommand(ros, Protocol.Command.GET, "SOMEKEY".getBytes(Protocol.CHARSET));
    ros.flush();
    pos.close();
    String expectedCommand = "*2\r\n$3\r\nGET\r\n$7\r\nSOMEKEY\r\n";
    int b;
    StringBuilder sb = new StringBuilder();
    while ((b = bis.read()) != -1) {
        sb.append((char) b);
    }
    assertEquals(expectedCommand, sb.toString());
}
Also used : BufferedInputStream(java.io.BufferedInputStream) RedisOutputStream(redis.clients.util.RedisOutputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) Test(org.junit.Test)

Example 88 with PipedOutputStream

use of java.io.PipedOutputStream in project cachecloud by sohutv.

the class ProtocolTest method buildACommand.

@Test
public void buildACommand() throws IOException {
    PipedInputStream pis = new PipedInputStream();
    BufferedInputStream bis = new BufferedInputStream(pis);
    PipedOutputStream pos = new PipedOutputStream(pis);
    RedisOutputStream ros = new RedisOutputStream(pos);
    Protocol.sendCommand(ros, Protocol.Command.GET, "SOMEKEY".getBytes(Protocol.CHARSET));
    ros.flush();
    pos.close();
    String expectedCommand = "*2\r\n$3\r\nGET\r\n$7\r\nSOMEKEY\r\n";
    int b;
    StringBuilder sb = new StringBuilder();
    while ((b = bis.read()) != -1) {
        sb.append((char) b);
    }
    assertEquals(expectedCommand, sb.toString());
}
Also used : BufferedInputStream(java.io.BufferedInputStream) RedisOutputStream(redis.clients.util.RedisOutputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) Test(org.junit.Test)

Example 89 with PipedOutputStream

use of java.io.PipedOutputStream in project java-chassis by ServiceComb.

the class DownloadSchema method slowInputStream.

@ApiResponses({ @ApiResponse(code = 200, response = File.class, message = "") })
@GetMapping(path = "/slowInputStream")
public ResponseEntity<InputStream> slowInputStream() throws IOException {
    PipedInputStream in = new PipedInputStream();
    PipedOutputStream out = new PipedOutputStream();
    in.connect(out);
    slowInputStreamThread = new Thread(() -> {
        Thread.currentThread().setName("download thread");
        byte[] bytes = "1".getBytes();
        for (; ; ) {
            try {
                out.write(bytes);
                out.flush();
                Thread.sleep(1000);
            } catch (Throwable e) {
                break;
            }
        }
        try {
            out.close();
        } catch (final IOException ioe) {
        // ignore
        }
    });
    slowInputStreamThread.start();
    ResponseEntity<InputStream> responseEntity = ResponseEntity.ok().header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE).header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=slowInputStream.txt").body(in);
    return responseEntity;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PipedInputStream(java.io.PipedInputStream) InputStream(java.io.InputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) GetMapping(org.springframework.web.bind.annotation.GetMapping) ApiResponses(io.swagger.annotations.ApiResponses)

Example 90 with PipedOutputStream

use of java.io.PipedOutputStream in project nanohttpd by NanoHttpd.

the class HttpKeepAliveTest method testManyRequests.

/**
 * Issue the given request many times to check whether an error occurs. For
 * this test, a small stack size is used, since a stack overflow is among
 * the possible errors.
 *
 * @param request
 *            The request to issue
 * @param expected
 *            The expected response
 */
public void testManyRequests(final String request, final String[] expected) throws Exception {
    Runnable r = new Runnable() {

        @Override
        public void run() {
            try {
                PipedOutputStream requestStream = new PipedOutputStream();
                PipedInputStream inputStream = new PipedInputStream(requestStream);
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                DefaultTempFileManager tempFileManager = new DefaultTempFileManager();
                try {
                    HTTPSession session = HttpKeepAliveTest.this.testServer.createSession(tempFileManager, inputStream, outputStream);
                    for (int i = 0; i < 2048; i++) {
                        requestStream.write(request.getBytes());
                        requestStream.flush();
                        outputStream.reset();
                        session.execute();
                        assertResponse(outputStream, expected);
                    }
                    // Finally, try "Connection: Close"
                    String closeReq = request.replaceAll("HTTP/1.1", "HTTP/1.1\r\nConnection: Close");
                    expected[3] = "Connection: close";
                    requestStream.write(closeReq.getBytes());
                    outputStream.reset();
                    requestStream.flush();
                    // SocketException:
                    try {
                        session.execute();
                    } catch (java.net.SocketException se) {
                        junit.framework.Assert.assertEquals(se.getMessage(), "NanoHttpd Shutdown");
                    }
                    assertResponse(outputStream, expected);
                } finally {
                    tempFileManager.clear();
                }
            } catch (Throwable t) {
                HttpKeepAliveTest.this.error = t;
            }
        }
    };
    Thread t = new Thread(null, r, "Request Thread", 1 << 17);
    t.start();
    t.join();
    if (this.error != null) {
        fail("" + this.error);
        this.error.printStackTrace();
    }
}
Also used : DefaultTempFileManager(org.nanohttpd.protocols.http.tempfiles.DefaultTempFileManager) HTTPSession(org.nanohttpd.protocols.http.HTTPSession) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

PipedOutputStream (java.io.PipedOutputStream)221 PipedInputStream (java.io.PipedInputStream)199 IOException (java.io.IOException)89 Test (org.junit.Test)54 InputStream (java.io.InputStream)30 OutputStream (java.io.OutputStream)23 BinaryDecoder (co.cask.cdap.common.io.BinaryDecoder)21 BinaryEncoder (co.cask.cdap.common.io.BinaryEncoder)21 PrintStream (java.io.PrintStream)21 ByteArrayOutputStream (java.io.ByteArrayOutputStream)19 ReflectionDatumReader (co.cask.cdap.internal.io.ReflectionDatumReader)17 TypeToken (com.google.common.reflect.TypeToken)17 InputStreamReader (java.io.InputStreamReader)16 DataInputStream (java.io.DataInputStream)14 DataOutputStream (java.io.DataOutputStream)14 BufferedReader (java.io.BufferedReader)13 Before (org.junit.Before)12 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ExecutorService (java.util.concurrent.ExecutorService)9 ArrayList (java.util.ArrayList)7