Skip to content

Commit 6d9bd2b

Browse files
Merge 2c17141 into 4e29063
2 parents 4e29063 + 2c17141 commit 6d9bd2b

4 files changed

Lines changed: 59 additions & 42 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- (Internal) Extract Android Profiler and Measurements for Hybrid SDKs ([#3016](https://github.com/getsentry/sentry-java/pull/3016))
88
- Ensure DSN uses http/https protocol ([#3044](https://github.com/getsentry/sentry-java/pull/3044))
9+
- (Internal) Add `readBytesFromFile` for use in Hybrid SDKs ([#3052](https://github.com/getsentry/sentry-java/pull/3052))
910

1011
### Features
1112

sentry/api/sentry.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4365,6 +4365,7 @@ public final class io/sentry/util/ExceptionUtils {
43654365
public final class io/sentry/util/FileUtils {
43664366
public fun <init> ()V
43674367
public static fun deleteRecursively (Ljava/io/File;)Z
4368+
public static fun readBytesFromFile (Ljava/lang/String;J)[B
43684369
public static fun readText (Ljava/io/File;)Ljava/lang/String;
43694370
}
43704371

sentry/src/main/java/io/sentry/SentryEnvelopeItem.java

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.sentry;
22

3+
import static io.sentry.util.FileUtils.readBytesFromFile;
34
import static io.sentry.vendor.Base64.NO_PADDING;
45
import static io.sentry.vendor.Base64.NO_WRAP;
56

@@ -304,48 +305,6 @@ private static void ensureAttachmentSizeLimit(
304305
return new SentryEnvelopeItem(itemHeader, () -> cachedItem.getBytes());
305306
}
306307

307-
private static byte[] readBytesFromFile(String pathname, long maxFileLength)
308-
throws SentryEnvelopeException {
309-
try {
310-
File file = new File(pathname);
311-
312-
if (!file.isFile()) {
313-
throw new SentryEnvelopeException(
314-
String.format(
315-
"Reading the item %s failed, because the file located at the path is not a file.",
316-
pathname));
317-
}
318-
319-
if (!file.canRead()) {
320-
throw new SentryEnvelopeException(
321-
String.format("Reading the item %s failed, because can't read the file.", pathname));
322-
}
323-
324-
if (file.length() > maxFileLength) {
325-
throw new SentryEnvelopeException(
326-
String.format(
327-
"Dropping item, because its size located at '%s' with %d bytes is bigger "
328-
+ "than the maximum allowed size of %d bytes.",
329-
pathname, file.length(), maxFileLength));
330-
}
331-
332-
try (FileInputStream fileInputStream = new FileInputStream(pathname);
333-
BufferedInputStream inputStream = new BufferedInputStream(fileInputStream);
334-
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
335-
byte[] bytes = new byte[1024];
336-
int length;
337-
int offset = 0;
338-
while ((length = inputStream.read(bytes)) != -1) {
339-
outputStream.write(bytes, offset, length);
340-
}
341-
return outputStream.toByteArray();
342-
}
343-
} catch (IOException | SecurityException exception) {
344-
throw new SentryEnvelopeException(
345-
String.format("Reading the item %s failed.\n%s", pathname, exception.getMessage()));
346-
}
347-
}
348-
349308
public static @NotNull SentryEnvelopeItem fromClientReport(
350309
final @NotNull ISerializer serializer, final @NotNull ClientReport clientReport)
351310
throws IOException {

sentry/src/main/java/io/sentry/util/FileUtils.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package io.sentry.util;
22

3+
import java.io.BufferedInputStream;
34
import java.io.BufferedReader;
5+
import java.io.ByteArrayOutputStream;
46
import java.io.File;
7+
import java.io.FileInputStream;
58
import java.io.FileReader;
69
import java.io.IOException;
710
import org.jetbrains.annotations.ApiStatus;
@@ -60,4 +63,57 @@ public static boolean deleteRecursively(@Nullable File file) {
6063
}
6164
return contentBuilder.toString();
6265
}
66+
67+
/**
68+
* Reads the content of a path into a byte array. If the path is does not exists,
69+
* it's not a file, can't be read or is larger than max size allowed IOException
70+
* is thrown. Do not use with large files, as the byte array is kept in memory!
71+
*
72+
* @param pathname file to read
73+
* @return a byte array containing all the content of the file
74+
* @throws IOException In case of error reading the file
75+
*/
76+
public static byte[] readBytesFromFile(String pathname, long maxFileLength)
77+
throws IOException, SecurityException {
78+
File file = new File(pathname);
79+
80+
if (!file.exists()) {
81+
throw new IOException(
82+
String.format(
83+
"File '%s' doesn't exists",
84+
file.getName()));
85+
}
86+
87+
if (!file.isFile()) {
88+
throw new IOException(
89+
String.format(
90+
"Reading path %s failed, because it's not a file.",
91+
pathname));
92+
}
93+
94+
if (!file.canRead()) {
95+
throw new IOException(
96+
String.format("Reading the item %s failed, because can't read the file.", pathname));
97+
}
98+
99+
if (file.length() > maxFileLength) {
100+
throw new IOException(
101+
String.format(
102+
"Reading file failed, because size located at '%s' with %d bytes is bigger "
103+
+ "than the maximum allowed size of %d bytes.",
104+
pathname, file.length(), maxFileLength));
105+
}
106+
107+
try (FileInputStream fileInputStream = new FileInputStream(pathname);
108+
BufferedInputStream inputStream = new BufferedInputStream(fileInputStream);
109+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
110+
byte[] bytes = new byte[1024];
111+
int length;
112+
int offset = 0;
113+
while ((length = inputStream.read(bytes)) != -1) {
114+
outputStream.write(bytes, offset, length);
115+
}
116+
return outputStream.toByteArray();
117+
}
118+
}
63119
}

0 commit comments

Comments
 (0)