Skip to content

Commit

Permalink
Correctly open URLs for reading
Browse files Browse the repository at this point in the history
  • Loading branch information
pschichtel committed May 10, 2024
1 parent e3cef56 commit ff12c58
Showing 1 changed file with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.Reader;
import java.io.UncheckedIOException;
Expand All @@ -34,7 +35,6 @@
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.stream.Stream;

Expand All @@ -57,22 +57,26 @@ public static void write(File file, String content, Charset encoding) throws IOE
}
}

private static Reader urlToReader(URL url, Charset encoding) throws IOException {
return new BufferedReader(new InputStreamReader(url.openStream(), encoding));
}

public static String read(URL location, Charset encoding, Map<String, Object> properties) throws IOException, URISyntaxException {
try (Reader reader = new InterpolationFilterReader(Files.newBufferedReader(Paths.get(location.toURI()), encoding), properties)) {
try (Reader reader = new InterpolationFilterReader(urlToReader(location, encoding), properties)) {
return IOUtils.toString(reader);
}
}

public static String read(URL location, Charset encoding) throws IOException, URISyntaxException {
try (Reader reader = Files.newBufferedReader(Paths.get(location.toURI()), encoding)) {
try (Reader reader = urlToReader(location, encoding)) {
return IOUtils.toString(reader);
}
}

public static String[] read(final URL[] locations, final Charset encoding) throws IOException, URISyntaxException {
final String[] results = new String[locations.length];
for (int i = 0; i < locations.length; i++) {
try (Reader reader = Files.newBufferedReader(Paths.get(locations[i].toURI()), encoding)) {
try (Reader reader = urlToReader(locations[i], encoding)) {
results[i] = IOUtils.toString(reader);
}
}
Expand Down

0 comments on commit ff12c58

Please sign in to comment.