/** * 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 * @throws IOException if an error occurs while writing the message * @see BodyReader#writeDecodedTo(OutputStream) */ @Override public void writeTo(OutputStream out) throws IOException { writeTo(out, BodyConsumer.DEFAULT_BUFFER_SIZE); }
@Override public Optional<ChunkedBodyContents> asChunkedBodyContents() throws IOException { markConsumed(); return super.asChunkedBodyContents(); }
/** * Convert the HTTP message's body into a String. * <p> * The body is returned without being decoded (i.e. raw). * To get the decoded body, use * {@link #decodeBody()} or {@link #decodeBodyToString(Charset)}. * * @param charset text message's charset * @return String representing the raw HTTP message's body. * @throws IOException if an error occurs while consuming the message body */ public String asRawString(Charset charset) throws IOException { return new String(asRawBytes(), charset); }
/** * @return the body of the HTTP message as a {@link ChunkedBodyContents} if the body indeed used * the chunked transfer coding. If the body was not chunked, this method returns an empty value. * @throws IOException if an error occurs while consuming the message body */ public Optional<ChunkedBodyContents> asChunkedBodyContents() throws IOException { return framedBody.use( cl -> Optional.empty(), chunked -> Optional.of(chunked.getContents(asRawStream())), ct -> Optional.empty()); }
InputStream responseStream = response.getBody().map(b -> b.isChunked() ? b.asRawStream() : null) .orElseThrow(() -> new IllegalStateException("HTTP response does not contain a chunked body"));
private static void closeBodyOf(RawHttpResponse<?> response) { if (response != null) { response.getBody().ifPresent(b -> { try { b.close(); } catch (IOException e) { e.printStackTrace(); } }); } }
/** * 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 * @throws IOException if an error occurs while writing the message */ public void writeDecodedTo(OutputStream out) throws IOException { writeDecodedTo(out, BodyConsumer.DEFAULT_BUFFER_SIZE); }
response.getBody().get().eager() : null;
/** * Unframe and decode the HTTP message's body, then turn it into a String using the given charset. * * @param charset to use to convert the body into a String * @return the decoded message body as a String * @throws IOException if an error occurs while consuming the message body */ public String decodeBodyToString(Charset charset) throws IOException { return new String(decodeBody(), charset); }
/** * @return the body of the HTTP message as a {@link ChunkedBodyContents} if the body indeed used * the chunked transfer coding. If the body was not chunked, this method returns an empty value. * @throws IOException if an error occurs while consuming the message body */ public Optional<ChunkedBodyContents> asChunkedBodyContents() throws IOException { return framedBody.use( cl -> Optional.empty(), chunked -> Optional.of(chunked.getContents(asRawStream())), ct -> Optional.empty()); }
private static void closeBodyOf(RawHttpResponse<?> response) { if (response != null) { response.getBody().ifPresent(b -> { try { b.close(); } catch (IOException e) { e.printStackTrace(); } }); } }
/** * 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 * @throws IOException if an error occurs while writing the message */ public void writeDecodedTo(OutputStream out) throws IOException { writeDecodedTo(out, BodyConsumer.DEFAULT_BUFFER_SIZE); }
response.getBody().get().eager() : null;
/** * Unframe and decode the HTTP message's body, then turn it into a String using the given charset. * * @param charset to use to convert the body into a String * @return the decoded message body as a String * @throws IOException if an error occurs while consuming the message body */ public String decodeBodyToString(Charset charset) throws IOException { return new String(decodeBody(), charset); }
/** * @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()); }
/** * 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 * @throws IOException if an error occurs while writing the message * @see BodyReader#writeDecodedTo(OutputStream) */ @Override public void writeTo(OutputStream out) throws IOException { writeTo(out, BodyConsumer.DEFAULT_BUFFER_SIZE); }
/** * Ensure that this response is read eagerly, downloading the full body if necessary. * <p> * The returned object can be safely passed around after the connection used to receive * this response has been closed. * * @param keepAlive whether to keep the connection alive. If false, the {@link BodyReader} * associated with this response is closed after by the time this method returns. * @return this response, after eagerly downloading all of its contents. * @throws IOException if an error occurs while reading this response */ public EagerHttpResponse<Response> eagerly(boolean keepAlive) throws IOException { try { return EagerHttpResponse.from(this); } finally { if (!keepAlive) { getBody().ifPresent(b -> { try { b.close(); } catch (IOException e) { e.printStackTrace(); } }); } } }
/** * Convert the HTTP message's body into a String. * <p> * The body is returned without being decoded (i.e. raw). * To get the decoded body, use * {@link #decodeBody()} or {@link #decodeBodyToString(Charset)}. * * @param charset text message's charset * @return String representing the raw HTTP message's body. * @throws IOException if an error occurs while consuming the message body */ public String asRawString(Charset charset) throws IOException { return new String(asRawBytes(), charset); }
/** * Unframe and decode the HTTP message's body. * * @return the unframed, decoded message body * @throws IOException if an error occurs while consuming the message body */ public byte[] decodeBody() throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); writeDecodedTo(out, BodyConsumer.DEFAULT_BUFFER_SIZE); return out.toByteArray(); }
request.getBody().get().eager() : null;