From 818a2428ef166eaa97fd923c536a43bb80a7f77c Mon Sep 17 00:00:00 2001 From: iProdigy Date: Tue, 26 Sep 2023 00:30:47 -0700 Subject: [PATCH 1/3] perf: avoid writing full body to an intermediate string --- .../main/java/feign/moshi/MoshiDecoder.java | 22 +++++-------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/moshi/src/main/java/feign/moshi/MoshiDecoder.java b/moshi/src/main/java/feign/moshi/MoshiDecoder.java index 82b2f8503..02b76f689 100644 --- a/moshi/src/main/java/feign/moshi/MoshiDecoder.java +++ b/moshi/src/main/java/feign/moshi/MoshiDecoder.java @@ -13,19 +13,17 @@ */ package feign.moshi; -import com.google.common.io.CharStreams; import com.squareup.moshi.JsonAdapter; import com.squareup.moshi.JsonDataException; -import com.squareup.moshi.JsonEncodingException; import com.squareup.moshi.Moshi; import feign.Response; import feign.Util; import feign.codec.Decoder; +import okio.BufferedSource; +import okio.Okio; + import java.io.IOException; -import java.io.Reader; import java.lang.reflect.Type; -import static feign.Util.UTF_8; -import static feign.Util.ensureClosed; public class MoshiDecoder implements Decoder { private final Moshi moshi; @@ -42,7 +40,6 @@ public MoshiDecoder(Iterable> adapters) { this(MoshiFactory.create(adapters)); } - @Override public Object decode(Response response, Type type) throws IOException { JsonAdapter jsonAdapter = moshi.adapter(type); @@ -52,23 +49,14 @@ public Object decode(Response response, Type type) throws IOException { if (response.body() == null) return null; - Reader reader = response.body().asReader(UTF_8); - - try { - return parseJson(jsonAdapter, reader); + try (BufferedSource source = Okio.buffer(Okio.source(response.body().asInputStream()))) { + return jsonAdapter.fromJson(source); } catch (JsonDataException e) { if (e.getCause() != null && e.getCause() instanceof IOException) { throw (IOException) e.getCause(); } throw e; - } finally { - ensureClosed(reader); } } - private Object parseJson(JsonAdapter jsonAdapter, Reader reader) throws IOException { - String targetString = CharStreams.toString(reader); - return jsonAdapter.fromJson(targetString); - } } - From 5507e523a189a1d6e7339e63f32d15215e5186c9 Mon Sep 17 00:00:00 2001 From: iProdigy Date: Tue, 26 Sep 2023 01:13:47 -0700 Subject: [PATCH 2/3] fix: eagerly return null for empty bodies --- moshi/src/main/java/feign/moshi/MoshiDecoder.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/moshi/src/main/java/feign/moshi/MoshiDecoder.java b/moshi/src/main/java/feign/moshi/MoshiDecoder.java index 02b76f689..23c7fe855 100644 --- a/moshi/src/main/java/feign/moshi/MoshiDecoder.java +++ b/moshi/src/main/java/feign/moshi/MoshiDecoder.java @@ -21,7 +21,6 @@ import feign.codec.Decoder; import okio.BufferedSource; import okio.Okio; - import java.io.IOException; import java.lang.reflect.Type; @@ -50,6 +49,9 @@ public Object decode(Response response, Type type) throws IOException { return null; try (BufferedSource source = Okio.buffer(Okio.source(response.body().asInputStream()))) { + if (source.exhausted()) { + return null; // empty body + } return jsonAdapter.fromJson(source); } catch (JsonDataException e) { if (e.getCause() != null && e.getCause() instanceof IOException) { From f3cd4d8e0b6d3575094d7af040879318bc417838 Mon Sep 17 00:00:00 2001 From: iProdigy Date: Tue, 26 Sep 2023 14:32:33 -0700 Subject: [PATCH 3/3] chore(moshi): remove guava dependency --- moshi/pom.xml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/moshi/pom.xml b/moshi/pom.xml index a97784d2c..e674a1237 100644 --- a/moshi/pom.xml +++ b/moshi/pom.xml @@ -42,12 +42,6 @@ moshi - - com.google.guava - guava - ${guava.version} - - ${project.groupId} feign-core