Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly open URLs for reading #762

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,24 +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)) {
results[i] = IOUtils.toString(reader);
}
results[i] = read(locations[i], encoding);
}
return results;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,31 @@
import org.junit.jupiter.api.Test;

import java.io.File;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;

class FileUtilsTest {
@Test
void test_jar_url_read() {
// the assumption here is, that junit's @Test class is within a jar on the classpath
URL resourceUrl = Test.class.getResource("/" + Test.class.getName().replace('.', '/') + ".class");
final Charset encoding = StandardCharsets.US_ASCII;
final String magic = encoding.decode(ByteBuffer.wrap(new byte[]{(byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE})).toString();

String singlePlainContent = Assertions.assertDoesNotThrow(() -> FileUtils.read(resourceUrl, encoding));
Assertions.assertEquals(magic, singlePlainContent.substring(0, 4));

String[] multipleContents = Assertions.assertDoesNotThrow(() -> FileUtils.read(new URL[]{ resourceUrl, resourceUrl }, encoding));
Assertions.assertEquals(2, multipleContents.length);
Assertions.assertEquals(magic, multipleContents[0].substring(0, 4));
Assertions.assertEquals(magic, multipleContents[1].substring(0, 4));

String interpolatedContent = Assertions.assertDoesNotThrow(() -> FileUtils.read(resourceUrl, encoding, new HashMap<>()));
Assertions.assertEquals(magic, interpolatedContent.substring(0, 4));
}

@Test
void test_read_first_lines() throws Exception {
Expand Down