Skip to content

Commit 7b121c1

Browse files
committed
Provide readAllBytes(InputStream) helper to other modules
1 parent 676cf7c commit 7b121c1

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

utils/src/main/java/datadog/instrument/glue/Glue.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@
77
package datadog.instrument.glue;
88

99
import static java.nio.charset.StandardCharsets.UTF_16BE;
10+
import static java.util.Objects.requireNonNull;
1011

1112
import datadog.instrument.utils.JVM;
12-
import java.io.ByteArrayOutputStream;
1313
import java.io.InputStream;
1414
import java.util.MissingResourceException;
15-
import java.util.Objects;
1615

1716
/** Methods for loading instrumentation glue bytecode from string literals and resource files. */
1817
public final class Glue {
19-
2018
private static final String GLUE_RESOURCE_PREFIX = "/datadog/instrument/glue/";
2119

2220
private static final int BUFFER_SIZE = 8192;
@@ -41,22 +39,10 @@ public static byte[] unpackBytecode(String bytecode) {
4139
* @return the glue bytecode
4240
* @throws MissingResourceException if the bytecode cannot be read
4341
*/
44-
@SuppressWarnings({"Since15"})
4542
public static byte[] loadBytecode(Class<?> host, String glueName) {
4643
String glueResource = GLUE_RESOURCE_PREFIX + glueName;
47-
try (InputStream is = Objects.requireNonNull(host.getResourceAsStream(glueResource))) {
48-
if (JVM.atLeastJava(9)) {
49-
return is.readAllBytes();
50-
} else {
51-
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
52-
int bytesRead;
53-
byte[] buf = new byte[BUFFER_SIZE];
54-
while ((bytesRead = is.read(buf, 0, BUFFER_SIZE)) != -1) {
55-
os.write(buf, 0, bytesRead);
56-
}
57-
return os.toByteArray();
58-
}
59-
}
44+
try (InputStream is = requireNonNull(host.getResourceAsStream(glueResource))) {
45+
return JVM.readAllBytes(is);
6046
} catch (Throwable e) {
6147
String detail = "Cannot load " + glueResource + " from " + host + ": " + e;
6248
throw new MissingResourceException(detail, host.getName(), glueResource);

utils/src/main/java/datadog/instrument/utils/JVM.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77
package datadog.instrument.utils;
88

9-
/** Provides information about the JVM; used to select the best injection approach. */
9+
import java.io.ByteArrayOutputStream;
10+
import java.io.IOException;
11+
import java.io.InputStream;
12+
13+
/** Assorted JVM helpers; uses the best approach for the current JVM. */
1014
public final class JVM {
1115
private static final int JAVA_VERSION = getMajorJavaVersion();
1216

@@ -53,4 +57,29 @@ private static int parseMajorJavaVersion(String str) {
5357
}
5458
return value;
5559
}
60+
61+
private static final int BUFFER_SIZE = 8192;
62+
63+
/**
64+
* Reads all remaining bytes from the given input stream.
65+
*
66+
* @param is the input stream
67+
* @return a byte array containing the bytes read from the input stream
68+
* @throws IOException if an I/O error occurs
69+
*/
70+
@SuppressWarnings("Since15")
71+
public static byte[] readAllBytes(InputStream is) throws IOException {
72+
if (JVM.atLeastJava(9)) {
73+
return is.readAllBytes();
74+
} else {
75+
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
76+
int bytesRead;
77+
byte[] buf = new byte[BUFFER_SIZE];
78+
while ((bytesRead = is.read(buf, 0, BUFFER_SIZE)) != -1) {
79+
os.write(buf, 0, bytesRead);
80+
}
81+
return os.toByteArray();
82+
}
83+
}
84+
}
5685
}

0 commit comments

Comments
 (0)