List of usage examples for io.netty.buffer ByteBufUtil getBytes
public static byte[] getBytes(ByteBuf buf)
From source file:blazingcache.client.CacheEntry.java
License:Apache License
public byte[] getSerializedData() { // copy data from Direct Memory to Heap return ByteBufUtil.getBytes(buf); }
From source file:com.heliosapm.streams.metrics.aggregation.StreamedMetricAggregation.java
License:Apache License
/** * Returns this aggregation as a byte array * @return a byte array/* w w w . j a v a2 s. c om*/ */ public byte[] toByteArray() { final ByteBuf b = BufferManager.getInstance().buffer(size == -1 ? 128 : size); try { b.writeByte(sticky ? 1 : 0); b.writeByte(doubleType ? 1 : 0); b.writeLong(createTime); b.writeLong(period); b.writeByte(periodUnit.ordinal()); values.position(0); b.writeBytes(values); b.writeByte(tags.size()); BufferManager.writeUTF(metricName, b); for (Map.Entry<String, String> entry : tags.entrySet()) { BufferManager.writeUTF(entry.getKey(), b); BufferManager.writeUTF(entry.getValue(), b); } return ByteBufUtil.getBytes(b); } finally { try { b.release(); } catch (Exception x) { /* No Op */} } }
From source file:com.linecorp.armeria.client.encoding.ZlibStreamDecoder.java
License:Apache License
private byte[] fetchDecoderOutput() { CompositeByteBuf decoded = Unpooled.compositeBuffer(); for (;;) {/*w w w .j a va2 s. co m*/ ByteBuf buf = decoder.readInbound(); if (buf == null) { break; } if (!buf.isReadable()) { buf.release(); continue; } decoded.addComponent(true, buf); } byte[] ret = ByteBufUtil.getBytes(decoded); decoded.release(); return ret; }
From source file:com.linecorp.armeria.client.http.SimpleHttpClientCodec.java
License:Apache License
@Override public <T> T decodeResponse(ServiceInvocationContext ctx, ByteBuf content, Object originalResponse) throws Exception { if (!(originalResponse instanceof FullHttpResponse)) { throw new IllegalStateException( "HTTP client can only be used when session protocol is HTTP: " + ctx.scheme().uriText()); }/*from w w w. j a v a 2 s . c o m*/ FullHttpResponse httpResponse = (FullHttpResponse) originalResponse; byte[] body = content.readableBytes() == 0 ? EMPTY : ByteBufUtil.getBytes(content); @SuppressWarnings("unchecked") // Guaranteed by SimpleHttpClient interface. T response = (T) new SimpleHttpResponse(httpResponse.status(), httpResponse.headers(), body); return response; }
From source file:com.linecorp.armeria.client.http.SimpleHttpClientCodecTest.java
License:Apache License
@Test public void encodeRequestWithBody() { SimpleHttpRequest request = SimpleHttpRequestBuilder.forGet("/foo?q=foo&bar=baz") .content("lorem ipsum foo bar", StandardCharsets.UTF_8).header(HttpHeaderNames.ORIGIN, "localhost") .build();/* www . j a v a2 s .c o m*/ EncodeResult result = codec.encodeRequest(channel, SCHEME.sessionProtocol(), EXECUTE_METHOD, new Object[] { request }); assertTrue(result.isSuccess()); assertEquals(SCHEME, result.encodedScheme().get()); assertEquals("/foo", result.encodedPath().get()); assertEquals("www.github.com", result.encodedHost().get()); FullHttpRequest fullHttpRequest = (FullHttpRequest) result.content(); assertEquals(HttpVersion.HTTP_1_1, fullHttpRequest.protocolVersion()); assertEquals("/foo?q=foo&bar=baz", fullHttpRequest.uri()); assertEquals(HttpMethod.GET, fullHttpRequest.method()); assertEquals("localhost", fullHttpRequest.headers().get(HttpHeaderNames.ORIGIN)); assertEquals("lorem ipsum foo bar", new String(ByteBufUtil.getBytes(fullHttpRequest.content()), StandardCharsets.UTF_8)); }
From source file:com.linecorp.armeria.common.http.HttpData.java
License:Apache License
static HttpData of(ByteBuf buf) { return of(ByteBufUtil.getBytes(buf)); }
From source file:com.linecorp.armeria.common.HttpData.java
License:Apache License
/** * Converts the specified Netty {@link ByteBuf} into an {@link HttpData}. Unlike {@link #of(byte[])}, this * method makes a copy of the {@link ByteBuf}. * * @return a new {@link HttpData}. {@link #EMPTY_DATA} if the readable bytes of {@code buf} is 0. *///from www . j av a 2 s .c o m static HttpData of(ByteBuf buf) { requireNonNull(buf, "buf"); if (!buf.isReadable()) { return EMPTY_DATA; } return of(ByteBufUtil.getBytes(buf)); }
From source file:com.linecorp.armeria.internal.grpc.ArmeriaMessageFramer.java
License:Apache License
private ByteBuf writeCompressed(ByteBuf message) throws IOException { final CompositeByteBuf compressed = alloc.compositeBuffer(); try (OutputStream compressingStream = compressor.compress(new ByteBufOutputStream(compressed))) { compressingStream.write(ByteBufUtil.getBytes(message)); } finally {/*from w ww. jav a 2 s . co m*/ message.release(); } return write(compressed, true); }
From source file:com.linecorp.armeria.internal.grpc.GrpcMessageMarshallerTest.java
License:Apache License
@Test public void serializeRequest() throws Exception { ByteBuf serialized = marshaller.serializeRequest(GrpcTestUtil.REQUEST_MESSAGE); assertThat(ByteBufUtil.getBytes(serialized)).containsExactly(GrpcTestUtil.REQUEST_MESSAGE.toByteArray()); serialized.release();// ww w . j a v a2s . c om }
From source file:com.linecorp.armeria.internal.grpc.GrpcMessageMarshallerTest.java
License:Apache License
@Test public void serializeResponse() throws Exception { ByteBuf serialized = marshaller.serializeResponse(GrpcTestUtil.RESPONSE_MESSAGE); assertThat(ByteBufUtil.getBytes(serialized)).containsExactly(GrpcTestUtil.RESPONSE_MESSAGE.toByteArray()); serialized.release();/*from w ww .j ava 2s . c o m*/ }