Search in sources :

Example 26 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project spring-boot by spring-projects.

the class HttpTunnelPayload method assignTo.

/**
	 * Assign this payload to the given {@link HttpOutputMessage}.
	 * @param message the message to assign this payload to
	 * @throws IOException in case of I/O errors
	 */
public void assignTo(HttpOutputMessage message) throws IOException {
    Assert.notNull(message, "Message must not be null");
    HttpHeaders headers = message.getHeaders();
    headers.setContentLength(this.data.remaining());
    headers.add(SEQ_HEADER, Long.toString(getSequence()));
    headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    WritableByteChannel body = Channels.newChannel(message.getBody());
    while (this.data.hasRemaining()) {
        body.write(this.data);
    }
    body.close();
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) WritableByteChannel(java.nio.channels.WritableByteChannel)

Example 27 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project spring-boot by spring-projects.

the class HttpTunnelConnectionTests method closeTunnelChangesIsOpen.

@Test
public void closeTunnelChangesIsOpen() throws Exception {
    this.requestFactory.willRespondAfterDelay(1000, HttpStatus.GONE);
    WritableByteChannel channel = openTunnel(false);
    assertThat(channel.isOpen()).isTrue();
    channel.close();
    assertThat(channel.isOpen()).isFalse();
}
Also used : WritableByteChannel(java.nio.channels.WritableByteChannel) Test(org.junit.Test)

Example 28 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project spring-boot by spring-projects.

the class HttpTunnelPayloadForwarderTests method forwardOutOfSequence.

@Test
public void forwardOutOfSequence() throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    WritableByteChannel channel = Channels.newChannel(out);
    HttpTunnelPayloadForwarder forwarder = new HttpTunnelPayloadForwarder(channel);
    forwarder.forward(payload(3, "o"));
    forwarder.forward(payload(2, "ll"));
    forwarder.forward(payload(1, "he"));
    assertThat(out.toByteArray()).isEqualTo("hello".getBytes());
}
Also used : WritableByteChannel(java.nio.channels.WritableByteChannel) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 29 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project spring-boot by spring-projects.

the class HttpTunnelPayloadForwarderTests method forwardInSequence.

@Test
public void forwardInSequence() throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    WritableByteChannel channel = Channels.newChannel(out);
    HttpTunnelPayloadForwarder forwarder = new HttpTunnelPayloadForwarder(channel);
    forwarder.forward(payload(1, "he"));
    forwarder.forward(payload(2, "ll"));
    forwarder.forward(payload(3, "o"));
    assertThat(out.toByteArray()).isEqualTo("hello".getBytes());
}
Also used : WritableByteChannel(java.nio.channels.WritableByteChannel) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 30 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project spring-boot by spring-projects.

the class HttpTunnelPayloadForwarderTests method overflow.

@Test
public void overflow() throws Exception {
    WritableByteChannel channel = Channels.newChannel(new ByteArrayOutputStream());
    HttpTunnelPayloadForwarder forwarder = new HttpTunnelPayloadForwarder(channel);
    this.thrown.expect(IllegalStateException.class);
    this.thrown.expectMessage("Too many messages queued");
    for (int i = 2; i < 130; i++) {
        forwarder.forward(payload(i, "data" + i));
    }
}
Also used : WritableByteChannel(java.nio.channels.WritableByteChannel) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

WritableByteChannel (java.nio.channels.WritableByteChannel)111 ByteBuffer (java.nio.ByteBuffer)32 ByteArrayOutputStream (java.io.ByteArrayOutputStream)28 ReadableByteChannel (java.nio.channels.ReadableByteChannel)27 FileOutputStream (java.io.FileOutputStream)23 Test (org.testng.annotations.Test)19 ByteArrayInputStream (java.io.ByteArrayInputStream)16 Test (org.junit.Test)15 IOException (java.io.IOException)14 File (java.io.File)13 OutputStream (java.io.OutputStream)12 DbusEventGenerator (com.linkedin.databus.core.test.DbusEventGenerator)10 Vector (java.util.Vector)10 FileInputStream (java.io.FileInputStream)6 Writer (java.io.Writer)6 DbusEventIterator (com.linkedin.databus.core.DbusEventBuffer.DbusEventIterator)4 PhysicalPartition (com.linkedin.databus.core.data_model.PhysicalPartition)4 DbusEventsStatisticsCollector (com.linkedin.databus.core.monitoring.mbean.DbusEventsStatisticsCollector)4 ClosedChannelException (java.nio.channels.ClosedChannelException)4 Map (java.util.Map)4