/** * Create an {@link EagerBodyReader}. * * @param framedBody the framed body * @param inputStream providing the body. The body is consumed immediately * @throws IOException if the inputStream throws */ public EagerBodyReader(FramedBody framedBody, @Nonnull InputStream inputStream) throws IOException { super(framedBody); this.rawInputStream = inputStream; this.rawBytes = framedBody.getBodyConsumer().consume(inputStream); }
/** * Create an {@link EagerBodyReader}. * * @param framedBody the framed body * @param inputStream providing the body. The body is consumed immediately * @throws IOException if the inputStream throws */ public EagerBodyReader(FramedBody framedBody, @Nonnull InputStream inputStream) throws IOException { super(framedBody); this.rawInputStream = inputStream; this.rawBytes = framedBody.getBodyConsumer().consume(inputStream); }
/** * @return the raw HTTP message's body as bytes. * <p> * This method does not unframe nor decode the body in case the body is encoded. * To get the decoded body, use * {@link #decodeBody()} or {@link #decodeBodyToString(Charset)}. * @throws IOException if an error occurs while consuming the message body */ public byte[] asRawBytes() throws IOException { return framedBody.getBodyConsumer().consume(asRawStream()); }
/** * @return the raw HTTP message's body as bytes. * <p> * This method does not unframe nor decode the body in case the body is encoded. * To get the decoded body, use * {@link #decodeBody()} or {@link #decodeBodyToString(Charset)}. * @throws IOException if an error occurs while consuming the message body */ public byte[] asRawBytes() throws IOException { return framedBody.getBodyConsumer().consume(asRawStream()); }
/** * Get a lazy stream of chunks if the message body is chunked, or empty otherwise. * <p> * The last chunk is always the empty chunk, so once the empty chunk is received, * trying to consume another chunk will result in an error. * * @return lazy stream of chunks if the message body is chunked, or empty otherwise. * @throws IOException if an error occurs while consuming the body */ public Optional<Iterator<ChunkedBodyContents.Chunk>> asChunkStream() throws IOException { BodyConsumer consumer = framedBody.getBodyConsumer(); if (consumer instanceof BodyConsumer.ChunkedBodyConsumer) { try { return Optional.of(((BodyConsumer.ChunkedBodyConsumer) consumer) .consumeLazily(asRawStream())); } catch (RuntimeException e) { Throwable cause = e.getCause(); if (cause instanceof IOException) { throw (IOException) cause; } throw e; } } else { return Optional.empty(); } }
/** * Read the raw HTTP message body, simultaneously writing it to the given output. * <p> * This method may not validate the full HTTP message before it starts writing it out. * To perform a full validation first, call {@link #eager()} to get an eager reader. * * @param out to write the HTTP body to * @param bufferSize size of the buffer to use for writing, if possible * @throws IOException if an error occurs while writing the message * @see BodyReader#writeDecodedTo(OutputStream, int) */ public void writeTo(OutputStream out, int bufferSize) throws IOException { framedBody.getBodyConsumer().consumeInto(asRawStream(), out, bufferSize); }
/** * Read the raw HTTP message body, simultaneously writing it to the given output. * <p> * This method may not validate the full HTTP message before it starts writing it out. * To perform a full validation first, call {@link #eager()} to get an eager reader. * * @param out to write the HTTP body to * @param bufferSize size of the buffer to use for writing, if possible * @throws IOException if an error occurs while writing the message * @see BodyReader#writeDecodedTo(OutputStream, int) */ public void writeTo(OutputStream out, int bufferSize) throws IOException { framedBody.getBodyConsumer().consumeInto(asRawStream(), out, bufferSize); }
/** * Get a lazy stream of chunks if the message body is chunked, or empty otherwise. * <p> * The last chunk is always the empty chunk, so once the empty chunk is received, * trying to consume another chunk will result in an error. * * @return lazy stream of chunks if the message body is chunked, or empty otherwise. * @throws IOException if an error occurs while consuming the body */ public Optional<Iterator<ChunkedBodyContents.Chunk>> asChunkStream() throws IOException { BodyConsumer consumer = framedBody.getBodyConsumer(); if (consumer instanceof BodyConsumer.ChunkedBodyConsumer) { try { return Optional.of(((BodyConsumer.ChunkedBodyConsumer) consumer) .consumeLazily(asRawStream())); } catch (RuntimeException e) { Throwable cause = e.getCause(); if (cause instanceof IOException) { throw (IOException) cause; } throw e; } } else { return Optional.empty(); } }
/** * Read the HTTP message body, simultaneously unframing and decoding it, * then writing the decoded body to the given output. * <p> * This method may not validate the full HTTP message before it starts writing it out. * To perform a full validation first, call {@link #eager()} to get an eager reader. * * @param out to write the unframed, decoded message body to * @param bufferSize size of the buffer to use for writing, if possible * @throws IOException if an error occurs while writing the message * @throws UnknownEncodingException if the body is encoded with an encoding that is unknown * by the {@link HttpBodyEncodingRegistry}. */ public void writeDecodedTo(OutputStream out, int bufferSize) throws IOException { DecodingOutputStream decodedStream = framedBody.getBodyDecoder().decoding(out); framedBody.getBodyConsumer().consumeDataInto(asRawStream(), decodedStream, bufferSize); decodedStream.finishDecoding(); }
/** * Read the HTTP message body, simultaneously unframing and decoding it, * then writing the decoded body to the given output. * <p> * This method may not validate the full HTTP message before it starts writing it out. * To perform a full validation first, call {@link #eager()} to get an eager reader. * * @param out to write the unframed, decoded message body to * @param bufferSize size of the buffer to use for writing, if possible * @throws IOException if an error occurs while writing the message * @throws UnknownEncodingException if the body is encoded with an encoding that is unknown * by the {@link HttpBodyEncodingRegistry}. */ public void writeDecodedTo(OutputStream out, int bufferSize) throws IOException { DecodingOutputStream decodedStream = framedBody.getBodyDecoder().decoding(out); framedBody.getBodyConsumer().consumeDataInto(asRawStream(), decodedStream, bufferSize); decodedStream.finishDecoding(); }