/** * Create a new {@link ChunkedBody} to encode the contents of the given stream. * <p> * The stream is read lazily, so it shouldn't be closed until this body is consumed. * * @param stream content to encode * @param contentType Content-Type of the stream contents * @param chunkLength the length of each chunk */ public ChunkedBody(InputStream stream, @Nullable String contentType, int chunkLength) { this(stream, contentType, chunkLength, defaultChunkedBodyDecoder(), new HttpMetadataParser(RawHttpOptions.defaultInstance())); }
/** * Create a new {@link ChunkedBody} to encode the contents of the given stream. * <p> * The stream is read lazily, so it shouldn't be closed until this body is consumed. * * @param stream content to encode * @param contentType Content-Type of the stream contents * @param chunkLength the length of each chunk * @param bodyDecoder decoder capable of decoding the body. The last encoding must be "chunked". * @param metadataParser metadata parser (chunked body may contain metadata) * @throws IllegalArgumentException if the bodyDecoder's last encoding is not "chunked" */ public ChunkedBody(InputStream stream, @Nullable String contentType, int chunkLength, BodyDecoder bodyDecoder, HttpMetadataParser metadataParser) { super(contentType, bodyDecoder); this.stream = stream; this.chunkLength = chunkLength; this.metadataParser = metadataParser; validateEncodings(bodyDecoder.getEncodings()); }
@Override public LazyBodyReader toBodyReader() { return new LazyBodyReader(new FramedBody.Chunked(getBodyDecoder(), metadataParser), new InputStreamChunkEncoder(stream, chunkLength)); }
@Test public void replacingBodyWithChunkedEncodedMessage() throws IOException { InputStream stream = new ByteArrayInputStream("Hello RawHTTTP".getBytes()); int chunkSize = 4; RawHttp http = new RawHttp(); RawHttpRequest request = http.parseRequest("POST http://example.com/hello"); RawHttpRequest requestWithBody = request.withBody( new ChunkedBody(stream, "text/plain", chunkSize)); System.out.println(requestWithBody.eagerly()); }
@Override public LazyBodyReader toBodyReader() { return new LazyBodyReader(new FramedBody.Chunked(getBodyDecoder(), metadataParser), new InputStreamChunkEncoder(stream, chunkLength)); }
/** * Create a new {@link ChunkedBody} to encode the contents of the given stream. * <p> * The stream is read lazily, so it shouldn't be closed until this body is consumed. * * @param stream content to encode * @param contentType Content-Type of the stream contents * @param chunkLength the length of each chunk */ public ChunkedBody(InputStream stream, @Nullable String contentType, int chunkLength) { this(stream, contentType, chunkLength, defaultChunkedBodyDecoder(), new HttpMetadataParser(RawHttpOptions.defaultInstance())); }
/** * Create a new {@link ChunkedBody} to encode the contents of the given stream. * <p> * The stream is read lazily, so it shouldn't be closed until this body is consumed. * * @param stream content to encode * @param contentType Content-Type of the stream contents * @param chunkLength the length of each chunk * @param bodyDecoder decoder capable of decoding the body. The last encoding must be "chunked". * @param metadataParser metadata parser (chunked body may contain metadata) * @throws IllegalArgumentException if the bodyDecoder's last encoding is not "chunked" */ public ChunkedBody(InputStream stream, @Nullable String contentType, int chunkLength, BodyDecoder bodyDecoder, HttpMetadataParser metadataParser) { super(contentType, bodyDecoder); this.stream = stream; this.chunkLength = chunkLength; this.metadataParser = metadataParser; validateEncodings(bodyDecoder.getEncodings()); }
/** * Create a new {@link ChunkedBody} to encode the contents of the given stream. * <p> * The stream is read lazily, so it shouldn't be closed until this body is consumed. * <p> * A default chunk size of 4096 bytes is used. * * @param stream content to encode */ public ChunkedBody(InputStream stream) { this(stream, null, 4096, defaultChunkedBodyDecoder(), new HttpMetadataParser(RawHttpOptions.defaultInstance())); }
/** * Create a new {@link ChunkedBody} to encode the contents of the given stream. * <p> * The stream is read lazily, so it shouldn't be closed until this body is consumed. * <p> * A default chunk size of 4096 bytes is used. * * @param stream content to encode */ public ChunkedBody(InputStream stream) { this(stream, null, 4096, defaultChunkedBodyDecoder(), new HttpMetadataParser(RawHttpOptions.defaultInstance())); }