Skip to content

Commit

Permalink
Simplify body closing in response converters.
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton committed Jan 17, 2016
1 parent 6269b91 commit a58a031
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import com.google.gson.TypeAdapter;
import java.io.IOException;
import java.io.Reader;
import okhttp3.ResponseBody;

final class GsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
Expand All @@ -28,16 +27,10 @@ final class GsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
}

@Override public T convert(ResponseBody value) throws IOException {
Reader reader = value.charStream();
try {
return adapter.fromJson(reader);
return adapter.fromJson(value.charStream());
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ignored) {
}
}
value.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import com.fasterxml.jackson.databind.ObjectReader;
import java.io.IOException;
import java.io.Reader;
import okhttp3.ResponseBody;

final class JacksonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
Expand All @@ -28,16 +27,10 @@ final class JacksonResponseBodyConverter<T> implements Converter<ResponseBody, T
}

@Override public T convert(ResponseBody value) throws IOException {
Reader reader = value.charStream();
try {
return adapter.readValue(reader);
return adapter.readValue(value.charStream());
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ignored) {
}
}
value.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.squareup.moshi.JsonAdapter;
import java.io.IOException;
import okhttp3.ResponseBody;
import okio.BufferedSource;

final class MoshiResponseBodyConverter<T> implements Converter<ResponseBody, T> {
private final JsonAdapter<T> adapter;
Expand All @@ -28,16 +27,10 @@ final class MoshiResponseBodyConverter<T> implements Converter<ResponseBody, T>
}

@Override public T convert(ResponseBody value) throws IOException {
BufferedSource source = value.source();
try {
return adapter.fromJson(source);
return adapter.fromJson(value.source());
} finally {
if (source != null) {
try {
source.close();
} catch (IOException ignored) {
}
}
value.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.google.protobuf.MessageLite;
import com.google.protobuf.Parser;
import java.io.IOException;
import java.io.InputStream;
import okhttp3.ResponseBody;

final class ProtoResponseBodyConverter<T extends MessageLite>
Expand All @@ -31,18 +30,12 @@ final class ProtoResponseBodyConverter<T extends MessageLite>
}

@Override public T convert(ResponseBody value) throws IOException {
InputStream is = value.byteStream();
try {
return parser.parseFrom(is);
return parser.parseFrom(value.byteStream());
} catch (InvalidProtocolBufferException e) {
throw new RuntimeException(e); // Despite extending IOException, this is data mismatch.
} finally {
if (is != null) {
try {
is.close();
} catch (IOException ignored) {
}
}
value.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package retrofit2;

import java.io.IOException;
import java.io.InputStream;
import okhttp3.ResponseBody;
import org.simpleframework.xml.Serializer;

Expand All @@ -32,9 +31,8 @@ final class SimpleXmlResponseBodyConverter<T> implements Converter<ResponseBody,
}

@Override public T convert(ResponseBody value) throws IOException {
InputStream is = value.byteStream();
try {
T read = serializer.read(cls, is, strict);
T read = serializer.read(cls, value.byteStream(), strict);
if (read == null) {
throw new IllegalStateException("Could not deserialize body as " + cls);
}
Expand All @@ -44,10 +42,7 @@ final class SimpleXmlResponseBodyConverter<T> implements Converter<ResponseBody,
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
is.close();
} catch (IOException ignored) {
}
value.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.squareup.wire.ProtoAdapter;
import java.io.IOException;
import okhttp3.ResponseBody;
import okio.BufferedSource;

final class WireResponseBodyConverter<T extends Message<T, ?>>
implements Converter<ResponseBody, T> {
Expand All @@ -30,17 +29,10 @@ final class WireResponseBodyConverter<T extends Message<T, ?>>
}

@Override public T convert(ResponseBody value) throws IOException {
BufferedSource source = null;
try {
source = value.source();
return adapter.decode(source);
return adapter.decode(value.source());
} finally {
if (source != null) {
try {
source.close();
} catch (IOException ignored) {
}
}
value.close();
}
}
}

0 comments on commit a58a031

Please sign in to comment.