Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

No longer close OutputStream that is passed into JsonSerializer #2029

Merged
merged 4 commits into from May 17, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Fixes

- No longer close OutputStream that is passed into JsonSerializer (#2029)

### Features

- Allow setting SDK info (name & version) in manifest ([#2016](https://github.com/getsentry/sentry-java/pull/2016))
Expand Down
7 changes: 7 additions & 0 deletions sentry/src/main/java/io/sentry/ISerializer.java
Expand Up @@ -17,6 +17,13 @@ public interface ISerializer {

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

/**
* Serializes an envelope
*
* @param envelope an envelope
* @param outputStream which will not be closed automatically
* @throws Exception an exception
*/
void serialize(@NotNull SentryEnvelope envelope, @NotNull OutputStream outputStream)
throws Exception;

Expand Down
62 changes: 34 additions & 28 deletions sentry/src/main/java/io/sentry/JsonSerializer.java
Expand Up @@ -149,42 +149,48 @@ public <T> void serialize(@NotNull T entity, @NotNull Writer writer) throws IOEx
writer.flush();
}

/**
* Serializes an envelope to an OutputStream
*
* @param envelope the envelope
* @param outputStream will not be closed automatically
* @throws Exception an exception
*/
@Override
public void serialize(@NotNull SentryEnvelope envelope, @NotNull OutputStream outputStream)
throws Exception {
Objects.requireNonNull(envelope, "The SentryEnvelope object is required.");
Objects.requireNonNull(outputStream, "The Stream object is required.");

try (final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
final Writer writer =
new BufferedWriter(new OutputStreamWriter(bufferedOutputStream, UTF_8))) {

envelope
.getHeader()
.serialize(new JsonObjectWriter(writer, options.getMaxDepth()), options.getLogger());
writer.write("\n");

for (final SentryEnvelopeItem item : envelope.getItems()) {
try {
// When this throws we don't write anything and continue with the next item.
final byte[] data = item.getData();

item.getHeader()
.serialize(new JsonObjectWriter(writer, options.getMaxDepth()), options.getLogger());
writer.write("\n");
writer.flush();

outputStream.write(data);

writer.write("\n");
} catch (Exception exception) {
options
.getLogger()
.log(SentryLevel.ERROR, "Failed to create envelope item. Dropping it.", exception);
}
// we do not want to close these as we would also close the stream that was passed in
final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
final Writer writer = new BufferedWriter(new OutputStreamWriter(bufferedOutputStream, UTF_8));
adinauer marked this conversation as resolved.
Show resolved Hide resolved

envelope
.getHeader()
.serialize(new JsonObjectWriter(writer, options.getMaxDepth()), options.getLogger());
writer.write("\n");

for (final SentryEnvelopeItem item : envelope.getItems()) {
try {
// When this throws we don't write anything and continue with the next item.
final byte[] data = item.getData();

item.getHeader()
.serialize(new JsonObjectWriter(writer, options.getMaxDepth()), options.getLogger());
writer.write("\n");
writer.flush();

outputStream.write(data);

writer.write("\n");
} catch (Exception exception) {
options
.getLogger()
.log(SentryLevel.ERROR, "Failed to create envelope item. Dropping it.", exception);
}
writer.flush();
}
writer.flush();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be likely in the finally block?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only moved the code around. There's a catch block that only logs but doesn't throw out right before though.

}

@Override
Expand Down
10 changes: 10 additions & 0 deletions sentry/src/test/java/io/sentry/JsonSerializerTest.kt
Expand Up @@ -4,6 +4,7 @@ import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.check
import com.nhaarman.mockitokotlin2.eq
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.never
import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.whenever
import io.sentry.exception.SentryEnvelopeException
Expand All @@ -18,6 +19,7 @@ import java.io.ByteArrayOutputStream
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
import java.io.OutputStream
import java.io.OutputStreamWriter
import java.io.StringReader
import java.io.StringWriter
Expand Down Expand Up @@ -862,6 +864,14 @@ class JsonSerializerTest {
)
}

@Test
fun `json serializer does not close the stream that is passed in`() {
val stream = mock<OutputStream>()
JsonSerializer(SentryOptions()).serialize(SentryEnvelope.from(fixture.serializer, SentryEvent(), null), stream)

verify(stream, never()).close()
}

private fun assertSessionData(expectedSession: Session?) {
assertNotNull(expectedSession)
assertEquals(UUID.fromString("c81d4e2e-bcf2-11e6-869b-7df92533d2db"), expectedSession.sessionId)
Expand Down