Skip to content

Commit ad91a62

Browse files
authored
No longer close OutputStream that is passed into JsonSerializer (#2029)
* Do not close stream that is passed into JsonSerializer * Add changelog * Add JavaDoc to ISerializer * Wrap writer.flush with finally
1 parent 48c6733 commit ad91a62

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Fixes
6+
7+
- No longer close OutputStream that is passed into JsonSerializer (#2029)
8+
59
### Features
610

711
- Allow setting SDK info (name & version) in manifest ([#2016](https://github.com/getsentry/sentry-java/pull/2016))

sentry/src/main/java/io/sentry/ISerializer.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ public interface ISerializer {
1717

1818
<T> void serialize(@NotNull T entity, @NotNull Writer writer) throws IOException;
1919

20+
/**
21+
* Serializes an envelope
22+
*
23+
* @param envelope an envelope
24+
* @param outputStream which will not be closed automatically
25+
* @throws Exception an exception
26+
*/
2027
void serialize(@NotNull SentryEnvelope envelope, @NotNull OutputStream outputStream)
2128
throws Exception;
2229

sentry/src/main/java/io/sentry/JsonSerializer.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,24 @@ public <T> void serialize(@NotNull T entity, @NotNull Writer writer) throws IOEx
149149
writer.flush();
150150
}
151151

152+
/**
153+
* Serializes an envelope to an OutputStream
154+
*
155+
* @param envelope the envelope
156+
* @param outputStream will not be closed automatically
157+
* @throws Exception an exception
158+
*/
152159
@Override
153160
public void serialize(@NotNull SentryEnvelope envelope, @NotNull OutputStream outputStream)
154161
throws Exception {
155162
Objects.requireNonNull(envelope, "The SentryEnvelope object is required.");
156163
Objects.requireNonNull(outputStream, "The Stream object is required.");
157164

158-
try (final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
159-
final Writer writer =
160-
new BufferedWriter(new OutputStreamWriter(bufferedOutputStream, UTF_8))) {
165+
// we do not want to close these as we would also close the stream that was passed in
166+
final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
167+
final Writer writer = new BufferedWriter(new OutputStreamWriter(bufferedOutputStream, UTF_8));
161168

169+
try {
162170
envelope
163171
.getHeader()
164172
.serialize(new JsonObjectWriter(writer, options.getMaxDepth()), options.getLogger());
@@ -183,6 +191,7 @@ public void serialize(@NotNull SentryEnvelope envelope, @NotNull OutputStream ou
183191
.log(SentryLevel.ERROR, "Failed to create envelope item. Dropping it.", exception);
184192
}
185193
}
194+
} finally {
186195
writer.flush();
187196
}
188197
}

sentry/src/test/java/io/sentry/JsonSerializerTest.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.nhaarman.mockitokotlin2.any
44
import com.nhaarman.mockitokotlin2.check
55
import com.nhaarman.mockitokotlin2.eq
66
import com.nhaarman.mockitokotlin2.mock
7+
import com.nhaarman.mockitokotlin2.never
78
import com.nhaarman.mockitokotlin2.verify
89
import com.nhaarman.mockitokotlin2.whenever
910
import io.sentry.exception.SentryEnvelopeException
@@ -18,6 +19,7 @@ import java.io.ByteArrayOutputStream
1819
import java.io.IOException
1920
import java.io.InputStream
2021
import java.io.InputStreamReader
22+
import java.io.OutputStream
2123
import java.io.OutputStreamWriter
2224
import java.io.StringReader
2325
import java.io.StringWriter
@@ -862,6 +864,14 @@ class JsonSerializerTest {
862864
)
863865
}
864866

867+
@Test
868+
fun `json serializer does not close the stream that is passed in`() {
869+
val stream = mock<OutputStream>()
870+
JsonSerializer(SentryOptions()).serialize(SentryEnvelope.from(fixture.serializer, SentryEvent(), null), stream)
871+
872+
verify(stream, never()).close()
873+
}
874+
865875
private fun assertSessionData(expectedSession: Session?) {
866876
assertNotNull(expectedSession)
867877
assertEquals(UUID.fromString("c81d4e2e-bcf2-11e6-869b-7df92533d2db"), expectedSession.sessionId)

0 commit comments

Comments
 (0)