@Override public ByteBufHttpData duplicate() { return new ByteBufHttpData(buf.duplicate(), endOfStream); }
@Override public ByteBufHttpData retainedDuplicate() { return new ByteBufHttpData(buf.retainedDuplicate(), endOfStream); }
@Override public ByteBufHttpData replace(ByteBuf content) { requireNonNull(content, "content"); content.touch(); return new ByteBufHttpData(content, endOfStream); }
@Override public ByteBufHttpData copy() { return new ByteBufHttpData(buf.copy(), endOfStream); }
/** * Converts a {@link DataBuffer} into an {@link HttpData}. */ HttpData toHttpData(DataBuffer dataBuffer) { if (dataBuffer instanceof NettyDataBuffer) { return new ByteBufHttpData(((NettyDataBuffer) dataBuffer).getNativeBuffer(), false); } if (dataBuffer instanceof DefaultDataBuffer) { return new ByteBufHttpData( Unpooled.wrappedBuffer(((DefaultDataBuffer) dataBuffer).getNativeBuffer()), false); } return new ByteBufHttpData(Unpooled.wrappedBuffer(dataBuffer.asByteBuffer()), false); }
private HttpData serializeTrailersAsMessage(HttpHeaders trailers) { final ByteBuf serialized = ctx.alloc().buffer(); boolean success = false; try { serialized.writeByte(TRAILERS_FRAME_HEADER); // Skip, we'll set this after serializing the headers. serialized.writeInt(0); for (Map.Entry<AsciiString, String> trailer : trailers) { encodeHeader(trailer.getKey(), trailer.getValue(), serialized); } final int messageSize = serialized.readableBytes() - 5; serialized.setInt(1, messageSize); success = true; } finally { if (!success) { serialized.release(); } } return new ByteBufHttpData(serialized, true); }
@Override public void messageRead(ByteBufOrStream message) { // We know there is only one message in total, so don't bother with checking endOfStream // We also know that we don't support compression, so this is always a ByteBuffer. final HttpData unframedContent = new ByteBufHttpData(message.buf(), true); unframedHeaders.setInt(HttpHeaderNames.CONTENT_LENGTH, unframedContent.length()); res.complete(HttpResponse.of(unframedHeaders, unframedContent)); }
content = new ByteBufHttpData(merged, true); } else { final byte[] merged = new byte[contentLength];
final boolean endOfStream = nextOffset == end; if (readBytes > 0) { if (!res.tryWrite(new ByteBufHttpData(buf, endOfStream))) { close(in); return;
/** * Writes out a payload message. * * @param message the message to be written out. Ownership is taken by {@link ArmeriaMessageFramer}. * * @return a {@link ByteBufHttpData} with the framed payload. Ownership is passed to caller. */ public ByteBufHttpData writePayload(ByteBuf message) { verifyNotClosed(); final boolean compressed = messageCompression && compressor != Codec.Identity.NONE; final int messageLength = message.readableBytes(); try { final ByteBuf buf; if (messageLength != 0 && compressed) { buf = writeCompressed(message); } else { buf = writeUncompressed(message); } return new ByteBufHttpData(buf, false); } catch (IOException | RuntimeException e) { // IOException will not be thrown, since sink#deliverFrame doesn't throw. throw Status.INTERNAL .withDescription("Failed to frame message") .withCause(e) .asRuntimeException(); } }
private static HttpData encodeSuccess(ServiceRequestContext ctx, RpcResponse reply, SerializationFormat serializationFormat, String methodName, int seqId, TBase<?, ?> result) { final ByteBuf buf = ctx.alloc().buffer(128); boolean success = false; try { final TTransport transport = new TByteBufTransport(buf); final TProtocol outProto = ThriftProtocolFactories.get(serializationFormat).getProtocol(transport); final TMessage header = new TMessage(methodName, TMessageType.REPLY, seqId); outProto.writeMessageBegin(header); result.write(outProto); outProto.writeMessageEnd(); ctx.logBuilder().responseContent(reply, new ThriftReply(header, result)); final HttpData encoded = new ByteBufHttpData(buf, false); success = true; return encoded; } catch (TException e) { throw new Error(e); // Should never reach here. } finally { if (!success) { buf.release(); } } }
res.tryWrite(new ByteBufHttpData(data.retain(), endOfStream)); } catch (Throwable t) { res.close(t);
@Test public void usingDefaultDataBufferFactory_ByteBufHttpData() { final DataBufferFactoryWrapper<?> wrapper = new DataBufferFactoryWrapper<>(new DefaultDataBufferFactory()); final ByteBufHttpData httpData1 = new ByteBufHttpData(Unpooled.wrappedBuffer("abc".getBytes()), false); final DataBuffer buffer = wrapper.toDataBuffer(httpData1); assertThat(buffer).isInstanceOf(DefaultDataBuffer.class); assertThat(httpData1.refCnt()).isZero(); assertThat(buffer.asByteBuffer()).isEqualTo(ByteBuffer.wrap("abc".getBytes())); final HttpData httpData2 = wrapper.toHttpData(buffer); assertThat(httpData2).isInstanceOf(ByteBufHttpData.class); assertThat(((ByteBufHttpData) httpData2).refCnt()).isOne(); assertThat(ByteBufUtil.getBytes(((ByteBufHttpData) httpData2).content())).isEqualTo("abc".getBytes()); }
@Test public void usingNettyDataBufferFactory_ByteBufHttpData() { final DataBufferFactoryWrapper<?> wrapper = new DataBufferFactoryWrapper<>(new NettyDataBufferFactory(UnpooledByteBufAllocator.DEFAULT)); final ByteBufHttpData httpData1 = new ByteBufHttpData(Unpooled.wrappedBuffer("abc".getBytes()), false); final DataBuffer buffer = wrapper.toDataBuffer(httpData1); assertThat(buffer).isInstanceOf(NettyDataBuffer.class); assertThat(((NettyDataBuffer) buffer).getNativeBuffer().refCnt()).isOne(); final HttpData httpData2 = wrapper.toHttpData(buffer); assertThat(httpData2).isInstanceOf(ByteBufHttpData.class); assertThat(((ByteBufHttpData) httpData2).content()) .isEqualTo(((NettyDataBuffer) buffer).getNativeBuffer()); assertThat(((ByteBufHttpData) httpData2).refCnt()).isOne(); }
: new ByteBufHttpData(buf, true), attrs.lastModifiedMillis()) .date(isDateEnabled())
final HttpData encoded = new ByteBufHttpData(buf, false); success = true; return encoded;
req.write(new ByteBufHttpData(data.retain(), false));
req.write(new ByteBufHttpData(data.retain(), endOfStream)); } catch (Throwable t) { req.close(t);
private HttpData serializeTrailersAsMessage(HttpHeaders trailers) { final ByteBuf serialized = ctx.alloc().buffer(); boolean success = false; try { serialized.writeByte(TRAILERS_FRAME_HEADER); // Skip, we'll set this after serializing the headers. serialized.writeInt(0); for (Map.Entry<AsciiString, String> trailer : trailers) { encodeHeader(trailer.getKey(), trailer.getValue(), serialized); } final int messageSize = serialized.readableBytes() - 5; serialized.setInt(1, messageSize); success = true; } finally { if (!success) { serialized.release(); } } return new ByteBufHttpData(serialized, true); }
@Override public void messageRead(ByteBufOrStream message) { // We know there is only one message in total, so don't bother with checking endOfStream // We also know that we don't support compression, so this is always a ByteBuffer. final HttpData unframedContent = new ByteBufHttpData(message.buf(), true); unframedHeaders.setInt(HttpHeaderNames.CONTENT_LENGTH, unframedContent.length()); res.complete(HttpResponse.of(unframedHeaders, unframedContent)); }