Skip to content

Commit 1e71c33

Browse files
committed
implement Utils.closeQuietly()
1 parent d8ecba7 commit 1e71c33

File tree

4 files changed

+34
-12
lines changed

4 files changed

+34
-12
lines changed

src/AndroidClient/android/src/main/java/net/servicestack/client/JsonServiceClient.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public HttpURLConnection createRequest(String requestUrl, String httpMethod, byt
176176
DataOutputStream wr = new DataOutputStream(req.getOutputStream());
177177
wr.write(requestBody);
178178
wr.flush();
179-
wr.close();
179+
Utils.closeQuietly(wr);
180180
}
181181

182182
return req;
@@ -391,10 +391,10 @@ public <TResponse> TResponse send(String requestUrl, String httpMethod, byte[] r
391391
else {
392392
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
393393
TResponse response = resClass != null
394-
? (TResponse) getGson().fromJson(reader, resClass)
395-
: (TResponse) getGson().fromJson(reader, resType);
394+
? (TResponse) getGson().fromJson(reader, resClass)
395+
: (TResponse) getGson().fromJson(reader, resType);
396396

397-
reader.close();
397+
Utils.closeQuietly(reader);
398398
return response;
399399
}
400400
} catch (IOException e) {

src/AndroidClient/android/src/main/java/net/servicestack/client/Utils.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.io.BufferedInputStream;
1414
import java.io.BufferedReader;
1515
import java.io.ByteArrayOutputStream;
16+
import java.io.Closeable;
1617
import java.io.IOException;
1718
import java.io.InputStream;
1819
import java.io.InputStreamReader;
@@ -524,7 +525,7 @@ public static String readToEnd(InputStream stream, final String charsetName) thr
524525
}
525526

526527
String text = sb.toString();
527-
reader.close();
528+
closeQuietly(reader);
528529
return text;
529530
}
530531

@@ -557,7 +558,7 @@ public static byte[] readBytesToEnd(InputStream stream) throws IOException {
557558
}
558559
return bytes.toByteArray();
559560
} finally {
560-
bufferedStream.close();
561+
closeQuietly(bufferedStream);
561562
}
562563
}
563564

@@ -912,4 +913,14 @@ public static String unescapeHtml(String html){
912913
.replace("&#39;","\'")
913914
.replace("&quot;","\"");
914915
}
916+
917+
public static void closeQuietly(Closeable closeable) {
918+
if (closeable != null) {
919+
try {
920+
closeable.close();
921+
} catch (RuntimeException rethrown) {
922+
throw rethrown;
923+
} catch (Exception ignored) {}
924+
}
925+
}
915926
}

src/AndroidClient/client/src/main/java/net/servicestack/client/JsonServiceClient.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public HttpURLConnection createRequest(String requestUrl, String httpMethod, byt
176176
DataOutputStream wr = new DataOutputStream(req.getOutputStream());
177177
wr.write(requestBody);
178178
wr.flush();
179-
wr.close();
179+
Utils.closeQuietly(wr);
180180
}
181181

182182
return req;
@@ -391,10 +391,10 @@ public <TResponse> TResponse send(String requestUrl, String httpMethod, byte[] r
391391
else {
392392
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
393393
TResponse response = resClass != null
394-
? (TResponse) getGson().fromJson(reader, resClass)
395-
: (TResponse) getGson().fromJson(reader, resType);
394+
? (TResponse) getGson().fromJson(reader, resClass)
395+
: (TResponse) getGson().fromJson(reader, resType);
396396

397-
reader.close();
397+
Utils.closeQuietly(reader);
398398
return response;
399399
}
400400
} catch (IOException e) {

src/AndroidClient/client/src/main/java/net/servicestack/client/Utils.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.io.BufferedInputStream;
1414
import java.io.BufferedReader;
1515
import java.io.ByteArrayOutputStream;
16+
import java.io.Closeable;
1617
import java.io.IOException;
1718
import java.io.InputStream;
1819
import java.io.InputStreamReader;
@@ -524,7 +525,7 @@ public static String readToEnd(InputStream stream, final String charsetName) thr
524525
}
525526

526527
String text = sb.toString();
527-
reader.close();
528+
closeQuietly(reader);
528529
return text;
529530
}
530531

@@ -557,7 +558,7 @@ public static byte[] readBytesToEnd(InputStream stream) throws IOException {
557558
}
558559
return bytes.toByteArray();
559560
} finally {
560-
bufferedStream.close();
561+
closeQuietly(bufferedStream);
561562
}
562563
}
563564

@@ -912,4 +913,14 @@ public static String unescapeHtml(String html){
912913
.replace("&#39;","\'")
913914
.replace("&quot;","\"");
914915
}
916+
917+
public static void closeQuietly(Closeable closeable) {
918+
if (closeable != null) {
919+
try {
920+
closeable.close();
921+
} catch (RuntimeException rethrown) {
922+
throw rethrown;
923+
} catch (Exception ignored) {}
924+
}
925+
}
915926
}

0 commit comments

Comments
 (0)