Skip to content

Commit 545ea86

Browse files
authored
Added methods U.downloadUrl(url, fileName) and U.decompressGzip(source, target).
1 parent 031a211 commit 545ea86

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

src/main/java/com/github/underscore/lodash/U.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,16 @@
2828
import com.github.underscore.PredicateIndexed;
2929
import com.github.underscore.Tuple;
3030
import com.github.underscore.Underscore;
31+
import java.io.File;
32+
import java.io.FileInputStream;
33+
import java.io.FileOutputStream;
34+
import java.io.IOException;
35+
import java.net.URL;
36+
import java.nio.channels.Channels;
37+
import java.nio.channels.ReadableByteChannel;
3138
import java.nio.charset.StandardCharsets;
39+
import java.nio.file.Files;
40+
import java.nio.file.Paths;
3241
import java.util.ArrayList;
3342
import java.util.Arrays;
3443
import java.util.Collection;
@@ -48,6 +57,7 @@
4857
import java.util.function.Consumer;
4958
import java.util.function.Function;
5059
import java.util.function.Predicate;
60+
import java.util.zip.GZIPInputStream;
5161

5262
public class U<T> extends Underscore<T> {
5363
private static final int DEFAULT_TRUNC_LENGTH = 30;
@@ -1948,6 +1958,22 @@ public Map<String, Object> xmlMap() {
19481958
}
19491959
}
19501960

1961+
public static long downloadUrl(final String url, final String fileName) throws IOException {
1962+
final URL website = new URL(url);
1963+
try (ReadableByteChannel rbc = Channels.newChannel(website.openStream());
1964+
final FileOutputStream fos = new FileOutputStream(fileName)) {
1965+
return fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
1966+
}
1967+
}
1968+
1969+
public static void decompressGzip(final String sourceFileName, final String targetFileName)
1970+
throws IOException {
1971+
try (GZIPInputStream gis =
1972+
new GZIPInputStream(new FileInputStream(new File(sourceFileName)))) {
1973+
Files.copy(gis, Paths.get(targetFileName));
1974+
}
1975+
}
1976+
19511977
public static FetchResponse fetch(final String url) {
19521978
return fetch(url, null, null, DEFAULT_HEADER_FIELDS, null, null);
19531979
}
@@ -2156,8 +2182,14 @@ && nonNull(timeBetweenRetry)
21562182
UnsupportedOperationException saveException;
21572183
do {
21582184
try {
2159-
final FetchResponse fetchResponse = U.fetch(
2160-
url, method, body, headerFields, connectTimeout, readTimeout);
2185+
final FetchResponse fetchResponse =
2186+
U.fetch(
2187+
url,
2188+
method,
2189+
body,
2190+
headerFields,
2191+
connectTimeout,
2192+
readTimeout);
21612193
if (fetchResponse.getStatus() == 429) {
21622194
saveException = new UnsupportedOperationException("Too Many Requests");
21632195
} else {

src/test/java/com/github/underscore/lodash/LodashTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
import static org.junit.jupiter.api.Assertions.assertThrows;
3232

3333
import com.github.underscore.Tuple;
34+
35+
import java.io.IOException;
36+
import java.nio.file.Files;
37+
import java.nio.file.Paths;
3438
import java.util.ArrayList;
3539
import java.util.Arrays;
3640
import java.util.Collections;
@@ -492,6 +496,21 @@ void fetchGet() {
492496
.fetch();
493497
}
494498

499+
@Test
500+
void downloadUrl() throws IOException {
501+
long result =
502+
U.downloadUrl(
503+
"https://support.oneskyapp.com/hc/en-us/article_attachments/202761627/example_1.json", "test.json");
504+
assertEquals(65, result);
505+
}
506+
507+
@Test
508+
void decompressGzip() throws IOException {
509+
Files.deleteIfExists(Paths.get("file.txt"));
510+
U.decompressGzip("src/test/resources/file.gz", "file.txt");
511+
assertEquals(65, Files.readAllBytes(Paths.get("file.txt")).length);
512+
}
513+
495514
@Test
496515
void fetchGetWithTimeouts() {
497516
U.FetchResponse result =

src/test/resources/file.gz

90 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)