Skip to content

Commit f8769d7

Browse files
authored
Support downloading bytes to memory, closes #7 (#11)
1 parent 5a21c37 commit f8769d7

File tree

2 files changed

+66
-8
lines changed

2 files changed

+66
-8
lines changed

download-utils/src/java11/java/net/minecraftforge/util/download/DownloadUtils.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,20 +98,49 @@ private static InputStream connect(String address) throws IOException, Interrupt
9898
}
9999

100100
/**
101-
* Downloads a string from the given URL, effectively acting as {@code curl}.
101+
* Downloads raw bytes from the given URL.
102102
*
103103
* @param url The URL to download from
104-
* @return The downloaded string
104+
* @return The downloaded bytes, stored in memory
105105
* @throws IOException If the download failed
106106
*/
107-
public static String downloadString(String url) throws IOException {
107+
public static byte[] downloadBytes(String url) throws IOException {
108108
try (InputStream stream = connect(url)) {
109-
return new String(stream.readAllBytes(), StandardCharsets.UTF_8);
109+
return stream.readAllBytes();
110110
} catch (Exception e) {
111111
throw new DownloadFailedException(url, e);
112112
}
113113
}
114114

115+
/**
116+
* Downloads raw bytes from the given URL.
117+
* <p>Returns {@code null} on failure.</p>
118+
*
119+
* @param url The URL to download from
120+
* @return The downloaded bytes, stored in memory, or {@code null} if the download failed
121+
*/
122+
public static byte @Nullable [] tryDownloadBytes(boolean silent, String url) {
123+
try {
124+
return downloadBytes(url);
125+
} catch (IOException e) {
126+
if (!silent) {
127+
e.printStackTrace(Log.WARN);
128+
}
129+
return null;
130+
}
131+
}
132+
133+
/**
134+
* Downloads a string from the given URL, effectively acting as {@code curl}.
135+
*
136+
* @param url The URL to download from
137+
* @return The downloaded string
138+
* @throws IOException If the download failed
139+
*/
140+
public static String downloadString(String url) throws IOException {
141+
return new String(downloadBytes(url), StandardCharsets.UTF_8);
142+
}
143+
115144
/**
116145
* Downloads a string from the given URL, effectively acting as {@code curl}.
117146
* <p>Returns {@code null} on failure.</p>

download-utils/src/main/java/net/minecraftforge/util/download/DownloadUtils.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ private static <R, E extends Throwable> R sneak(Throwable t) throws E {
9393
}
9494

9595
/**
96-
* Downloads a string from the given URL, effectively acting as {@code curl}.
96+
* Downloads raw bytes from the given URL.
9797
*
9898
* @param url The URL to download from
99-
* @return The downloaded string
99+
* @return The downloaded bytes, stored in memory
100100
* @throws IOException If the download failed
101101
*/
102-
public static String downloadString(String url) throws IOException {
102+
public static byte[] downloadBytes(String url) throws IOException {
103103
try (InputStream stream = connect(url);
104104
ByteArrayOutputStream out = new ByteArrayOutputStream()
105105
) {
@@ -110,12 +110,41 @@ public static String downloadString(String url) throws IOException {
110110
out.write(buf, 0, n);
111111
}
112112

113-
return new String(out.toByteArray(), StandardCharsets.UTF_8);
113+
return out.toByteArray();
114114
} catch (Exception e) {
115115
throw new DownloadFailedException(url, e);
116116
}
117117
}
118118

119+
/**
120+
* Downloads raw bytes from the given URL.
121+
* <p>Returns {@code null} on failure.</p>
122+
*
123+
* @param url The URL to download from
124+
* @return The downloaded bytes, stored in memory, or {@code null} if the download failed
125+
*/
126+
public static byte @Nullable [] tryDownloadBytes(boolean silent, String url) {
127+
try {
128+
return downloadBytes(url);
129+
} catch (IOException e) {
130+
if (!silent) {
131+
e.printStackTrace(Log.WARN);
132+
}
133+
return null;
134+
}
135+
}
136+
137+
/**
138+
* Downloads a string from the given URL, effectively acting as {@code curl}.
139+
*
140+
* @param url The URL to download from
141+
* @return The downloaded string
142+
* @throws IOException If the download failed
143+
*/
144+
public static String downloadString(String url) throws IOException {
145+
return new String(downloadBytes(url), StandardCharsets.UTF_8);
146+
}
147+
119148
/**
120149
* Downloads a string from the given URL, effectively acting as {@code curl}.
121150
* <p>Returns {@code null} on failure.</p>

0 commit comments

Comments
 (0)