/** * Decodes this data into a {@link String}. * * @param charset the {@link Charset} to use for decoding this data * * @return the decoded {@link String} */ default String toString(Charset charset) { requireNonNull(charset, "charset"); return new String(array(), offset(), length(), charset); }
/** * Converts an {@link HttpData} into a {@link DataBuffer}. */ DataBuffer toDataBuffer(HttpData httpData) { requireNonNull(httpData, "httpData"); if (!(httpData instanceof ByteBufHttpData)) { return delegate.wrap( ByteBuffer.wrap(httpData.array(), httpData.offset(), httpData.length())); } return converter.apply((ByteBufHttpData) httpData); }
@Override public int doRead(ApplicationBufferHandler handler) throws IOException { if (!isNeedToRead()) { // Read only once. return -1; } read = true; final int readableBytes = content.length(); handler.setByteBuffer(ByteBuffer.wrap(content.array(), content.offset(), readableBytes)); return readableBytes; }
/** * Returns a new {@link InputStream} that is sourced from this data. */ default InputStream toInputStream() { return new FastByteArrayInputStream(array(), offset(), length()); }
@Override void onHttpData(HttpData data) { responseDataBuffer.write(data.array(), data.offset(), data.length()); }
@Override public int doRead(ByteChunk chunk, Request request) throws IOException { if (read || content.isEmpty()) { // Read only once. return -1; } read = true; final int readableBytes = content.length(); chunk.setBytes(content.array(), content.offset(), readableBytes); return readableBytes; } }
public int doRead(ByteChunk chunk) { if (!isNeedToRead()) { // Read only once. return -1; } read = true; final int readableBytes = content.length(); chunk.setBytes(content.array(), content.offset(), readableBytes); return readableBytes; }
buf = ((ByteBufHolder) data).content(); } else { buf = Unpooled.wrappedBuffer(data.array(), data.offset(), dataLength);
@Override void onHttpData(HttpData data) { if (!responseCalled) { safeOnResponse(Okio.buffer(new ForwardingSource(pipeBuffer.source()) { @Override public long read(Buffer sink, long byteCount) throws IOException { request(1); return super.read(sink, byteCount); } })); responseCalled = true; } pipeBuffer.write(data.array(), data.offset(), data.length()); }
@Override public HttpData decode(HttpData obj) { if (obj instanceof ByteBufHolder) { decoder.writeInbound(((ByteBufHolder) obj).content()); } else { final ByteBuf compressed = Unpooled.wrappedBuffer(obj.array(), obj.offset(), obj.length()); decoder.writeInbound(compressed); } return HttpData.of(fetchDecoderOutput()); }
protected final ByteBuf toByteBuf(HttpData data) { if (data instanceof ByteBufHolder) { return ((ByteBufHolder) data).content(); } final ByteBuf buf = channel().alloc().directBuffer(data.length(), data.length()); buf.writeBytes(data.array(), data.offset(), data.length()); return buf; } }
private ChannelFuture doWriteSplitData(int id, HttpData data, boolean endStream) { try { int offset = data.offset(); int remaining = data.length(); ChannelFuture lastFuture; for (;;) { // Ensure an HttpContent does not exceed the maximum length of a cleartext TLS record. final int chunkSize = Math.min(MAX_TLS_DATA_LENGTH, remaining); lastFuture = write(id, new DefaultHttpContent(dataChunk(data, offset, chunkSize)), false); remaining -= chunkSize; if (remaining == 0) { break; } offset += chunkSize; } if (endStream) { lastFuture = write(id, LastHttpContent.EMPTY_LAST_CONTENT, true); } ch.flush(); return lastFuture; } finally { ReferenceCountUtil.safeRelease(data); } }
assert encodedStream != null; try { encodingStream.write(data.array(), data.offset(), data.length()); encodingStream.flush(); return HttpData.of(encodedStream.toByteArray());
} else { message = ctx.alloc().buffer(content.length()); message.writeBytes(content.array(), content.offset(), content.length());
private void invoke(THttpService service) throws Exception { invoke0(service, HttpData.of(out.getArray(), 0, out.length()), promise); final HttpData res = promise.get(); in.reset(res.array(), res.offset(), res.length()); }
private static void fillRequest( ServiceRequestContext ctx, AggregatedHttpMessage aReq, Request jReq) { jReq.setDispatcherType(DispatcherType.REQUEST); jReq.setAsyncSupported(false, "armeria"); jReq.setSecure(ctx.sessionProtocol().isTls()); jReq.setMetaData(toRequestMetadata(ctx, aReq)); final HttpData content = aReq.content(); if (!content.isEmpty()) { jReq.getHttpInput().addContent(new Content(ByteBuffer.wrap( content.array(), content.offset(), content.length()))); } jReq.getHttpInput().eof(); }
} else { buf = ctx.alloc().buffer(content.length()); buf.writeBytes(content.array(), content.offset(), content.length());
@Test public void testMultipleInheritance() throws Exception { final NameService.Client client1 = new NameService.Client.Factory().getClient(inProto, outProto); client1.send_removeMiddle(new Name(BAZ, BAR, FOO)); assertThat(out.length()).isGreaterThan(0); final HttpData req1 = HttpData.of(out.getArray(), 0, out.length()); out = new TMemoryBuffer(128); outProto = ThriftProtocolFactories.get(defaultSerializationFormat).getProtocol(out); final NameSortService.Client client2 = new NameSortService.Client.Factory().getClient(inProto, outProto); client2.send_sort(Arrays.asList(NAME_C, NAME_B, NAME_A)); assertThat(out.length()).isGreaterThan(0); final HttpData req2 = HttpData.of(out.getArray(), 0, out.length()); final THttpService service = THttpService.of( (UberNameService) (names, callback) -> callback.onComplete( names.stream().sorted().collect(toImmutableList())), defaultSerializationFormat); invoke0(service, req1, promise); invoke0(service, req2, promise2); final HttpData res1 = promise.get(); final HttpData res2 = promise2.get(); in.reset(res1.array(), res1.offset(), res1.length()); assertThat(client1.recv_removeMiddle()).isEqualTo(new Name(BAZ, null, FOO)); in.reset(res2.array(), res2.offset(), res2.length()); assertThat(client2.recv_sort()).containsExactly(NAME_A, NAME_B, NAME_C); }
new TMemoryInputTransport(content.array(), content.offset(), content.length()); final TProtocol inputProtocol = protocolFactory.getProtocol(inputTransport);