Skip to content

Commit 99dbf73

Browse files
authored
Reliable channel: close resources in finally block. (#572)
* Reliable channel: close resources in finally block. * change logging to warning when closing resources
1 parent d76bcf9 commit 99dbf73

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

core/src/main/java/com/microsoft/applicationinsights/internal/channel/common/PartialSuccessHandler.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,27 +114,36 @@ List<String> generateOriginalItems(TransmissionHandlerArgs args) {
114114

115115
if (args.getTransmission().getWebContentEncodingType() == "gzip") {
116116

117+
GZIPInputStream gis = null;
118+
BufferedReader bufferedReader = null;
119+
117120
try {
118-
GZIPInputStream gis = new GZIPInputStream(
121+
gis = new GZIPInputStream(
119122
new ByteArrayInputStream(args.getTransmission().getContent()));
120-
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(gis));
123+
bufferedReader = new BufferedReader(new InputStreamReader(gis));
121124
String line;
122125
while ((line = bufferedReader.readLine()) != null) {
123126
originalItems.add(line);
124127
}
128+
} catch (IOException ex) {
129+
InternalLogger.INSTANCE.error("IOException: Error while reading the GZIP stream.%nStack Trace:%n%s", ExceptionUtils.getStackTrace(ex));
130+
} catch (Throwable t) {
131+
InternalLogger.INSTANCE.error("Error while reading the GZIP stream.%nStack Trace:%n%s", ExceptionUtils.getStackTrace(t));
132+
} finally {
125133
if (gis != null) {
126-
gis.close();
134+
try {
135+
gis.close();
136+
} catch (IOException ex){
137+
InternalLogger.INSTANCE.warn("Error while closing the GZIP stream.%nStack Trace:%n%s", ExceptionUtils.getStackTrace(ex));
138+
}
127139
}
128140
if (bufferedReader != null) {
129-
bufferedReader.close();
141+
try {
142+
bufferedReader.close();
143+
} catch (IOException ex){
144+
InternalLogger.INSTANCE.warn("Error while closing the buffered reader.%nStack Trace:%n%s", ExceptionUtils.getStackTrace(ex));
145+
}
130146
}
131-
} catch (IOException e1) {
132-
InternalLogger.INSTANCE.error("IOException: Error while reading the GZIP stream.%nStack Trace:%n%s",
133-
ExceptionUtils.getStackTrace(e1));
134-
} catch (Throwable t) {
135-
InternalLogger.INSTANCE.error("Error while reading the GZIP stream.%nStack Trace:%n%s",
136-
ExceptionUtils.getStackTrace(t));
137-
} finally {
138147
}
139148
} else {
140149
for (String s : new String(args.getTransmission().getContent()).split("\r\n")) {

0 commit comments

Comments
 (0)