Skip to content

Commit

Permalink
Merge cf5e929 into 7ca9895
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanosiano authored Oct 19, 2023
2 parents 7ca9895 + cf5e929 commit 2e7efaf
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -292,26 +292,18 @@ private void reportAsSentryEvent(

private @NotNull ParseResult parseThreadDump(
final @NotNull ApplicationExitInfo exitInfo, final boolean isBackground) {
InputStream trace;
try {
trace = exitInfo.getTraceInputStream();
final byte[] dump;

try (final InputStream trace = exitInfo.getTraceInputStream()) {
if (trace == null) {
return new ParseResult(ParseResult.Type.NO_DUMP);
}
dump = getDumpBytes(trace);
} catch (Throwable e) {
options.getLogger().log(SentryLevel.WARNING, "Failed to read ANR thread dump", e);
return new ParseResult(ParseResult.Type.NO_DUMP);
}

byte[] dump = null;
try {
dump = getDumpBytes(trace);
} catch (Throwable e) {
options
.getLogger()
.log(SentryLevel.WARNING, "Failed to convert ANR thread dump to byte array", e);
}

try (final BufferedReader reader =
new BufferedReader(new InputStreamReader(new ByteArrayInputStream(dump)))) {
final Lines lines = Lines.readLines(reader);
Expand All @@ -330,16 +322,17 @@ private void reportAsSentryEvent(
}

private byte[] getDumpBytes(final @NotNull InputStream trace) throws IOException {
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
try (final ByteArrayOutputStream buffer = new ByteArrayOutputStream()) {

int nRead;
final byte[] data = new byte[1024];
int nRead;
final byte[] data = new byte[1024];

while ((nRead = trace.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
while ((nRead = trace.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}

return buffer.toByteArray();
return buffer.toByteArray();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.sentry.protocol.User;
import io.sentry.util.MapObjectWriter;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -142,10 +143,10 @@ public static SentryId captureEnvelope(final @NotNull byte[] envelopeData) {
final @NotNull IHub hub = HubAdapter.getInstance();
final @NotNull SentryOptions options = hub.getOptions();

try {
try (final InputStream envelopeInputStream = new ByteArrayInputStream(envelopeData)) {
final @NotNull ISerializer serializer = options.getSerializer();
final @Nullable SentryEnvelope envelope =
options.getEnvelopeReader().read(new ByteArrayInputStream(envelopeData));
options.getEnvelopeReader().read(envelopeInputStream);
if (envelope == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public AssetsModulesLoader(final @NotNull Context context, final @NotNull ILogge
protected Map<String, String> loadModules() {
final Map<String, String> modules = new TreeMap<>();

try {
final InputStream stream = context.getAssets().open(EXTERNAL_MODULES_FILENAME);
try (final InputStream stream = context.getAssets().open(EXTERNAL_MODULES_FILENAME)) {
return parseStream(stream);
} catch (FileNotFoundException e) {
logger.log(SentryLevel.INFO, "%s file was not found.", EXTERNAL_MODULES_FILENAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,11 @@ public void close() throws IOException {
options.getLogger().log(DEBUG, "Shutting down");
try {
httpclient.awaitShutdown(TimeValue.ofSeconds(1));
httpclient.close(CloseMode.GRACEFUL);
} catch (InterruptedException e) {
options.getLogger().log(DEBUG, "Thread interrupted while closing the connection.");
Thread.currentThread().interrupt();
} finally {
httpclient.close(CloseMode.GRACEFUL);
}
}

Expand Down
10 changes: 6 additions & 4 deletions sentry/src/main/java/io/sentry/EnvelopeReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,16 @@ public EnvelopeReader(@NotNull ISerializer serializer) {
private @Nullable SentryEnvelopeHeader deserializeEnvelopeHeader(
final @NotNull byte[] buffer, int offset, int length) {
String json = new String(buffer, offset, length, UTF_8);
StringReader reader = new StringReader(json);
return serializer.deserialize(reader, SentryEnvelopeHeader.class);
try (StringReader reader = new StringReader(json)) {
return serializer.deserialize(reader, SentryEnvelopeHeader.class);
}
}

private @Nullable SentryEnvelopeItemHeader deserializeEnvelopeItemHeader(
final @NotNull byte[] buffer, int offset, int length) {
String json = new String(buffer, offset, length, UTF_8);
StringReader reader = new StringReader(json);
return serializer.deserialize(reader, SentryEnvelopeItemHeader.class);
try (StringReader reader = new StringReader(json)) {
return serializer.deserialize(reader, SentryEnvelopeItemHeader.class);
}
}
}
9 changes: 8 additions & 1 deletion sentry/src/main/java/io/sentry/Hub.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.sentry.util.Pair;
import io.sentry.util.TracingUtils;
import java.io.Closeable;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -337,7 +338,13 @@ public void close() {
try {
for (Integration integration : options.getIntegrations()) {
if (integration instanceof Closeable) {
((Closeable) integration).close();
try {
((Closeable) integration).close();
} catch (IOException e) {
options
.getLogger()
.log(SentryLevel.WARNING, "Failed to close the integration {}.", integration, e);
}
}
}

Expand Down
6 changes: 2 additions & 4 deletions sentry/src/main/java/io/sentry/JsonSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ public JsonSerializer(@NotNull SentryOptions options) {
@NotNull Reader reader,
@NotNull Class<T> clazz,
@Nullable JsonDeserializer<R> elementDeserializer) {
try {
JsonObjectReader jsonObjectReader = new JsonObjectReader(reader);
try (JsonObjectReader jsonObjectReader = new JsonObjectReader(reader)) {
if (Collection.class.isAssignableFrom(clazz)) {
if (elementDeserializer == null) {
// if the object has no known deserializer we do best effort and deserialize it as map
Expand All @@ -147,8 +146,7 @@ public JsonSerializer(@NotNull SentryOptions options) {
@SuppressWarnings("unchecked")
@Override
public <T> @Nullable T deserialize(@NotNull Reader reader, @NotNull Class<T> clazz) {
try {
JsonObjectReader jsonObjectReader = new JsonObjectReader(reader);
try (JsonObjectReader jsonObjectReader = new JsonObjectReader(reader)) {
JsonDeserializer<?> deserializer = deserializersByClass.get(clazz);
if (deserializer != null) {
Object object = deserializer.deserialize(jsonObjectReader, options.getLogger());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public long skip(final long n) throws IOException {
@Override
public void close() throws IOException {
spanManager.finish(delegate);
super.close();
}

private static FileDescriptor getFileDescriptor(final @NotNull FileInputStream stream)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public void write(final byte @NotNull [] b, final int off, final int len) throws
@Override
public void close() throws IOException {
spanManager.finish(delegate);
super.close();
}

private static FileDescriptor getFileDescriptor(final @NotNull FileOutputStream stream)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,23 @@ public ResourcesDebugMetaLoader(final @NotNull ILogger logger) {

@Override
public @Nullable Properties loadDebugMeta() {
InputStream debugMetaStream = classLoader.getResourceAsStream(DEBUG_META_PROPERTIES_FILENAME);
if (debugMetaStream == null) {
logger.log(SentryLevel.INFO, "%s file was not found.", DEBUG_META_PROPERTIES_FILENAME);
} else {
try (final InputStream is = new BufferedInputStream(debugMetaStream)) {
final Properties properties = new Properties();
properties.load(is);
return properties;
} catch (IOException e) {
logger.log(SentryLevel.ERROR, e, "Failed to load %s", DEBUG_META_PROPERTIES_FILENAME);
} catch (RuntimeException e) {
logger.log(SentryLevel.ERROR, e, "%s file is malformed.", DEBUG_META_PROPERTIES_FILENAME);
try (InputStream debugMetaStream =
classLoader.getResourceAsStream(DEBUG_META_PROPERTIES_FILENAME)) {
if (debugMetaStream == null) {
logger.log(SentryLevel.INFO, "%s file was not found.", DEBUG_META_PROPERTIES_FILENAME);
} else {
try (final InputStream is = new BufferedInputStream(debugMetaStream)) {
final Properties properties = new Properties();
properties.load(is);
return properties;
} catch (IOException e) {
logger.log(SentryLevel.ERROR, e, "Failed to load %s", DEBUG_META_PROPERTIES_FILENAME);
} catch (RuntimeException e) {
logger.log(SentryLevel.ERROR, e, "%s file is malformed.", DEBUG_META_PROPERTIES_FILENAME);
}
}
} catch (IOException e) {
logger.log(SentryLevel.ERROR, e, "Failed to load %s", DEBUG_META_PROPERTIES_FILENAME);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import io.sentry.ILogger;
import io.sentry.SentryLevel;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.TreeMap;
Expand All @@ -28,9 +29,8 @@ public ResourcesModulesLoader(final @NotNull ILogger logger) {
@Override
protected Map<String, String> loadModules() {
final Map<String, String> modules = new TreeMap<>();
try {
final InputStream resourcesStream =
classLoader.getResourceAsStream(EXTERNAL_MODULES_FILENAME);
try (final InputStream resourcesStream =
classLoader.getResourceAsStream(EXTERNAL_MODULES_FILENAME)) {

if (resourcesStream == null) {
logger.log(SentryLevel.INFO, "%s file was not found.", EXTERNAL_MODULES_FILENAME);
Expand All @@ -40,6 +40,8 @@ protected Map<String, String> loadModules() {
return parseStream(resourcesStream);
} catch (SecurityException e) {
logger.log(SentryLevel.INFO, "Access to resources denied.", e);
} catch (IOException e) {
logger.log(SentryLevel.INFO, "Access to resources failed.", e);
}
return modules;
}
Expand Down

0 comments on commit 2e7efaf

Please sign in to comment.