use of io.netty.buffer.ByteBufAllocator in project reactor-netty by reactor.
the class HttpServerTests method doTestSendFileAsync.
private void doTestSendFileAsync(int chunk) throws IOException, URISyntaxException {
Path largeFile = Paths.get(getClass().getResource("/largeFile.txt").toURI());
Path tempFile = Files.createTempFile(largeFile.getParent(), "temp", ".txt");
tempFile.toFile().deleteOnExit();
byte[] fileBytes = Files.readAllBytes(largeFile);
for (int i = 0; i < 1000; i++) {
Files.write(tempFile, fileBytes, StandardOpenOption.APPEND);
}
ByteBufAllocator allocator = ByteBufAllocator.DEFAULT;
AsynchronousFileChannel channel = AsynchronousFileChannel.open(tempFile, StandardOpenOption.READ);
Flux<ByteBuf> content = Flux.create(fluxSink -> {
fluxSink.onDispose(() -> {
try {
if (channel != null) {
channel.close();
}
} catch (IOException ignored) {
}
});
ByteBuffer buf = ByteBuffer.allocate(chunk);
channel.read(buf, 0, buf, new TestCompletionHandler(channel, fluxSink, allocator, chunk));
});
NettyContext context = HttpServer.create(opt -> opt.host("localhost")).newHandler((req, resp) -> resp.sendByteArray(req.receive().aggregate().asByteArray())).block();
byte[] response = HttpClient.create(opt -> opt.connectAddress(() -> context.address())).request(HttpMethod.POST, "/", req -> req.send(content).then()).flatMap(res -> res.receive().aggregate().asByteArray()).block();
assertThat(response).isEqualTo(Files.readAllBytes(tempFile));
context.dispose();
}
use of io.netty.buffer.ByteBufAllocator in project pravega by pravega.
the class DirectMemoryCache method createBuffers.
/**
* Creates all the {@link DirectMemoryBuffer} instances for this {@link DirectMemoryCache} instance.
*/
@GuardedBy("availableBufferIds")
private void createBuffers() {
ByteBufAllocator allocator = createAllocator();
for (int i = 0; i < this.buffers.length; i++) {
this.unallocatedBufferIds.addLast(i);
this.buffers[i] = new DirectMemoryBuffer(i, allocator, this.layout);
}
}
use of io.netty.buffer.ByteBufAllocator in project ribbon by Netflix.
the class LoadBalancingHttpClient method createRxClient.
@Override
protected HttpClient<I, O> createRxClient(Server server) {
HttpClientBuilder<I, O> clientBuilder;
if (requestIdProvider != null) {
clientBuilder = RxContexts.<I, O>newHttpClientBuilder(server.getHost(), server.getPort(), requestIdProvider, RxContexts.DEFAULT_CORRELATOR, pipelineConfigurator);
} else {
clientBuilder = RxContexts.<I, O>newHttpClientBuilder(server.getHost(), server.getPort(), RxContexts.DEFAULT_CORRELATOR, pipelineConfigurator);
}
Integer connectTimeout = getProperty(IClientConfigKey.Keys.ConnectTimeout, null, DefaultClientConfigImpl.DEFAULT_CONNECT_TIMEOUT);
Integer readTimeout = getProperty(IClientConfigKey.Keys.ReadTimeout, null, DefaultClientConfigImpl.DEFAULT_READ_TIMEOUT);
Boolean followRedirect = getProperty(IClientConfigKey.Keys.FollowRedirects, null, null);
HttpClientConfig.Builder builder = new HttpClientConfig.Builder().readTimeout(readTimeout, TimeUnit.MILLISECONDS);
if (followRedirect != null) {
builder.setFollowRedirect(followRedirect);
}
clientBuilder.channelOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeout).config(builder.build());
if (isPoolEnabled()) {
clientBuilder.withConnectionPoolLimitStrategy(poolStrategy).withIdleConnectionsTimeoutMillis(idleConnectionEvictionMills).withPoolIdleCleanupScheduler(poolCleanerScheduler);
} else {
clientBuilder.withNoConnectionPooling();
}
if (sslContextFactory != null) {
try {
SSLEngineFactory myFactory = new DefaultFactories.SSLContextBasedFactory(sslContextFactory.getSSLContext()) {
@Override
public SSLEngine createSSLEngine(ByteBufAllocator allocator) {
SSLEngine myEngine = super.createSSLEngine(allocator);
myEngine.setUseClientMode(true);
return myEngine;
}
};
clientBuilder.withSslEngineFactory(myFactory);
} catch (ClientSslSocketFactoryException e) {
throw new RuntimeException(e);
}
}
return clientBuilder.build();
}
use of io.netty.buffer.ByteBufAllocator in project grpc-java by grpc.
the class AltsTsiFrameProtectorTest method parserHeader_frameOkFragment.
@Test
public void parserHeader_frameOkFragment() throws GeneralSecurityException {
ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;
List<Object> out = new ArrayList<>();
FakeChannelCrypter crypter = new FakeChannelCrypter();
AltsTsiFrameProtector.Unprotector unprotector = new AltsTsiFrameProtector.Unprotector(crypter, alloc);
ByteBuf in = getDirectBuffer(AltsTsiFrameProtector.getHeaderBytes() + FakeChannelCrypter.getTagBytes(), ref);
in.writeIntLE(FRAME_MIN_SIZE);
in.writeIntLE(6);
ByteBuf in1 = in.readSlice(AltsTsiFrameProtector.getHeaderBytes() - 1);
ByteBuf in2 = in.readSlice(1);
unprotector.unprotect(in1, out, alloc);
assertThat(in1.readableBytes()).isEqualTo(0);
unprotector.unprotect(in2, out, alloc);
assertThat(in2.readableBytes()).isEqualTo(0);
unprotector.destroy();
}
use of io.netty.buffer.ByteBufAllocator in project grpc-java by grpc.
the class AltsTsiFrameProtectorTest method parseFrame_oneFrameNoFragment.
@Test
public void parseFrame_oneFrameNoFragment() throws GeneralSecurityException {
int payloadBytes = 1024;
ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;
List<Object> out = new ArrayList<>();
FakeChannelCrypter crypter = new FakeChannelCrypter();
AltsTsiFrameProtector.Unprotector unprotector = new AltsTsiFrameProtector.Unprotector(crypter, alloc);
ByteBuf plain = getRandom(payloadBytes, ref);
ByteBuf outFrame = getDirectBuffer(AltsTsiFrameProtector.getHeaderBytes() + payloadBytes + FakeChannelCrypter.getTagBytes(), ref);
outFrame.writeIntLE(AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes + FakeChannelCrypter.getTagBytes());
outFrame.writeIntLE(6);
List<ByteBuf> framePlain = Collections.singletonList(plain);
ByteBuf frameOut = writeSlice(outFrame, payloadBytes + FakeChannelCrypter.getTagBytes());
crypter.encrypt(frameOut, framePlain);
plain.readerIndex(0);
unprotector.unprotect(outFrame, out, alloc);
assertThat(outFrame.readableBytes()).isEqualTo(0);
assertThat(out.size()).isEqualTo(1);
ByteBuf out1 = ref((ByteBuf) out.get(0));
assertThat(out1).isEqualTo(plain);
unprotector.destroy();
}
Aggregations