diff --git a/src/main/java/io/github/sashirestela/cleverclient/client/JavaHttpClientAdapter.java b/src/main/java/io/github/sashirestela/cleverclient/client/JavaHttpClientAdapter.java index 6ff50ab..43e2554 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/client/JavaHttpClientAdapter.java +++ b/src/main/java/io/github/sashirestela/cleverclient/client/JavaHttpClientAdapter.java @@ -84,7 +84,7 @@ protected Object sendAsync(RequestData request) { @Override public void shutdown() { - this.httpClient.executor().ifPresent(executor -> { + httpClient.executor().ifPresent(executor -> { if (executor instanceof ExecutorService) { ((ExecutorService) executor).shutdown(); } diff --git a/src/main/java/io/github/sashirestela/cleverclient/client/OkHttpClientAdapter.java b/src/main/java/io/github/sashirestela/cleverclient/client/OkHttpClientAdapter.java index 99c2f3b..e2cb219 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/client/OkHttpClientAdapter.java +++ b/src/main/java/io/github/sashirestela/cleverclient/client/OkHttpClientAdapter.java @@ -23,11 +23,14 @@ import java.io.IOException; import java.io.InputStreamReader; import java.util.EnumMap; +import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Spliterator; import java.util.concurrent.CompletableFuture; import java.util.function.BiFunction; import java.util.stream.Stream; +import java.util.stream.StreamSupport; public class OkHttpClientAdapter extends HttpClientAdapter { @@ -187,13 +190,45 @@ private Object getResponseContent(ResponseBody responseBody, ReturnType returnTy try { if (returnType.isStream()) { BufferedReader reader = new BufferedReader(new InputStreamReader(responseBody.byteStream())); - return reader.lines().onClose(() -> { - try { - responseBody.close(); - } catch (Exception e) { - throw new CleverClientException(e); + return StreamSupport.stream(new Spliterator() { + + private final Iterator iterator = reader.lines().iterator(); + private boolean closed = false; + + @Override + public boolean tryAdvance(java.util.function.Consumer action) { + if (iterator.hasNext()) { + action.accept(iterator.next()); + return true; + } + if (!closed) { + try { + reader.close(); + responseBody.close(); + closed = true; + } catch (Exception e) { + throw new CleverClientException(e); + } + } + return false; + } + + @Override + public Spliterator trySplit() { + return null; } - }); + + @Override + public long estimateSize() { + return Long.MAX_VALUE; + } + + @Override + public int characteristics() { + return Spliterator.ORDERED; + } + + }, false); } else if (returnType.isInputStream()) { return responseBody.byteStream(); } else {