/** * Creates a copy of the {@link java.io.InputStream InputStream} that is created using an {@link okhttp3.OkHttpClient OkHttpClient}. * * <p>You can access the input stream directly using {@link #withInputStream(net.dv8tion.jda.core.utils.IOConsumer) withInputStream(IOConsumer)} * which will have an open input stream available within the consumer scope. The stream will be closed once that method returns. * * @throws java.io.IOException * If an IO error occurs trying to read from the opened HTTP channel * * @return InputStream copy of the response body for this Attachment * * @since 3.4.0 */ public InputStream getInputStream() throws IOException { try (Response response = openConnection()) { // creates a copy in order to properly close the response InputStream in = Requester.getBody(response); return new ByteArrayInputStream(IOUtil.readFully(in)); } }
/** * Allows to access the InputStream that is available from the HTTP {@link okhttp3.Response Response} * to be used without having to copy it. * <br>Unlike {@link #getInputStream()} this does not return a full copy of the input stream. * Instead this method will provide the InputStream data in the specified consumer in which it is still accessible. * * <p><b>When this method returns the InputStream will be closed accordingly!</b> * * @param then * Not-null {@link net.dv8tion.jda.core.utils.IOConsumer IOConsumer} to accept the InputStream * * @throws java.lang.IllegalArgumentException * If the provided IOConsumer is {@code null} * @throws IOException * If an IOException occurs within the IOConsumer or while opening an HTTP channel * * @since 3.4.0 */ public void withInputStream(IOConsumer<InputStream> then) throws IOException { Checks.notNull(then, "Consumer"); try (Response response = openConnection()) { then.accept(Requester.getBody(response)); } }