List of usage examples for io.netty.buffer ByteBufUtil getBytes
public static byte[] getBytes(ByteBuf buf)
From source file:org.elasticsearch.http.netty4.Netty4HttpServerTransportTests.java
License:Apache License
/** * Test that {@link Netty4HttpServerTransport} supports the "Expect: 100-continue" HTTP header *///from ww w . j a va 2s . c o m public void testExpectContinueHeader() throws Exception { try (Netty4HttpServerTransport transport = new Netty4HttpServerTransport(Settings.EMPTY, networkService, bigArrays, threadPool)) { transport.httpServerAdapter((request, channel, context) -> channel.sendResponse( new BytesRestResponse(OK, BytesRestResponse.TEXT_CONTENT_TYPE, new BytesArray("done")))); transport.start(); InetSocketTransportAddress remoteAddress = (InetSocketTransportAddress) randomFrom( transport.boundAddress().boundAddresses()); try (Netty4HttpClient client = new Netty4HttpClient()) { FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/"); HttpUtil.set100ContinueExpected(request, true); HttpUtil.setContentLength(request, 10); FullHttpResponse response = client.post(remoteAddress.address(), request); assertThat(response.status(), is(HttpResponseStatus.CONTINUE)); request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/", Unpooled.EMPTY_BUFFER); response = client.post(remoteAddress.address(), request); assertThat(response.status(), is(HttpResponseStatus.OK)); assertThat(new String(ByteBufUtil.getBytes(response.content()), StandardCharsets.UTF_8), is("done")); } } }
From source file:org.elasticsearch.http.nio.NioHttpServerTransportTests.java
License:Apache License
private void runExpectHeaderTest(final Settings settings, final String expectation, final int contentLength, final HttpResponseStatus expectedStatus) throws InterruptedException { final HttpServerTransport.Dispatcher dispatcher = new HttpServerTransport.Dispatcher() { @Override/*from w ww. j av a2 s. c o m*/ public void dispatchRequest(RestRequest request, RestChannel channel, ThreadContext threadContext) { channel.sendResponse( new BytesRestResponse(OK, BytesRestResponse.TEXT_CONTENT_TYPE, new BytesArray("done"))); } @Override public void dispatchBadRequest(RestRequest request, RestChannel channel, ThreadContext threadContext, Throwable cause) { throw new AssertionError(); } }; try (NioHttpServerTransport transport = new NioHttpServerTransport(settings, networkService, bigArrays, pageRecycler, threadPool, xContentRegistry(), dispatcher)) { transport.start(); final TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses()); try (NioHttpClient client = new NioHttpClient()) { final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/"); request.headers().set(HttpHeaderNames.EXPECT, expectation); HttpUtil.setContentLength(request, contentLength); final FullHttpResponse response = client.post(remoteAddress.address(), request); try { assertThat(response.status(), equalTo(expectedStatus)); if (expectedStatus.equals(HttpResponseStatus.CONTINUE)) { final FullHttpRequest continuationRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/", Unpooled.EMPTY_BUFFER); final FullHttpResponse continuationResponse = client.post(remoteAddress.address(), continuationRequest); try { assertThat(continuationResponse.status(), is(HttpResponseStatus.OK)); assertThat(new String(ByteBufUtil.getBytes(continuationResponse.content()), StandardCharsets.UTF_8), is("done")); } finally { continuationResponse.release(); } } } finally { response.release(); } } } }
From source file:org.redisson.tomcat.AttributeMessage.java
License:Apache License
protected byte[] toByteArray(Encoder encoder, Object value) throws IOException { if (value == null) { return null; }//from w w w . java 2 s . co m ByteBuf buf = encoder.encode(value); try { return ByteBufUtil.getBytes(buf); } finally { buf.release(); } }
From source file:org.wso2.carbon.http2.transport.util.HTTP2Decoder.java
License:Open Source License
@Override public int read(ByteBuffer dst) throws IOException { complete = false;/*from ww w .ja v a2s.co m*/ byte[] data = ByteBufUtil.getBytes(dataFrame.content()); dst.put(data); complete = true; return data.length; }
From source file:ratpack.stream.bytebuf.ByteBufStreams.java
License:Apache License
/** * Reduces the stream to a single {@code byte[]}. * <p>/*from w w w. j ava 2 s . c om*/ * This should only be used when it is known that the stream is small, * as this will effectively force the entire stream to be held in memory. * * @param publisher the byte stream * @return the bytes as a {@code byte[]} */ public static Promise<byte[]> toByteArray(Publisher<? extends ByteBuf> publisher) { return compose(publisher).map(byteBuf -> { byte[] bytes = ByteBufUtil.getBytes(byteBuf); byteBuf.release(); return bytes; }); }
From source file:ratpack.stream.bytebuf.ByteBufStreams.java
License:Apache License
/** * Converts the byte buf stream to a stream of {@code byte[]}. * * @param publisher the byte stream/* w w w . j a v a 2 s. c om*/ * @return a publisher of {@code byte[]} */ public static Publisher<byte[]> toByteArrays(Publisher<? extends ByteBuf> publisher) { return Streams.map(publisher, b -> { byte[] bytes = ByteBufUtil.getBytes(b); b.release(); return bytes; }); }