Skip to content

Commit

Permalink
Fix FileUtil.read when url is in a jar (classpath resources)
Browse files Browse the repository at this point in the history
  • Loading branch information
skerdudou committed Jun 12, 2024
1 parent c59cabf commit 0713441
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions vertigo-core/src/main/java/io/vertigo/core/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Pattern;

import io.vertigo.core.lang.Assertion;
Expand Down Expand Up @@ -65,16 +68,26 @@ public static String read(final URL url) {
Assertion.check().isNotNull(url);
//---
try {
final Path path = Paths.get(url.toURI());
final List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
return String.join("\r\n", lines);
final var uri = url.toURI();
if ("jar".equalsIgnoreCase(uri.getScheme())) {
try (final FileSystem fs = FileSystems.newFileSystem(url.toURI(), Map.of())) { // need to "open" the jar for Paths.get to work
return doRead(Paths.get(uri));
}
}
return doRead(Paths.get(uri));
} catch (final IOException | URISyntaxException e) {
throw WrappedException.wrap(e, "Error when reading file : '{0}'", url);
}
}

private static String doRead(final Path path) throws IOException {
final List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
return String.join("\r\n", lines);
}

/**
* Copie le contenu d'un flux d'entrée vers un fichier de sortie.
*
* @param in flux d'entrée
* @param file fichier de sortie
* @throws IOException Erreur d'entrée/sortie
Expand All @@ -88,6 +101,7 @@ public static void copy(final InputStream in, final File file) throws IOExceptio
* <p>
* This method returns the textual part of the filename after the last dot.
* There must be no directory separator after the dot.
*
* <pre>
* foo.txt --> "txt"
* a/b/c.jpg --> "jpg"
Expand All @@ -96,8 +110,8 @@ public static void copy(final InputStream in, final File file) throws IOExceptio
* </pre>
* <p>
* The output will be the same irrespective of the machine that the code is running on.
* @param fileName Nom du fichier
*
* @param fileName Nom du fichier
* @return the extension of the file or an empty string if none exists.
* (author Apache Commons IO 1.1)
*/
Expand Down Expand Up @@ -125,6 +139,7 @@ public static String getFileExtension(final String fileName) {

/**
* Replace "user.home" "user.dir" and "java.io.tmpdir" by system value.
*
* @param path PAth to translate
* @return translated path
*/
Expand All @@ -137,6 +152,7 @@ public static String translatePath(final String path) {

/**
* Check a filePath send by a user.
*
* @param userPath Path to check
*/
public static void checkUserPath(final String userPath) {
Expand All @@ -147,6 +163,7 @@ public static void checkUserPath(final String userPath) {

/**
* Check a filename send by a user.
*
* @param userFileName FileName to check
*/
public static void checkUserFileName(final String userFileName) {
Expand Down

0 comments on commit 0713441

Please sign in to comment.