001// Generated by delombok at Mon Jan 02 16:27:27 UTC 2023 002package io.cui.tools.io; 003 004import static io.cui.tools.base.Preconditions.checkArgument; 005import static io.cui.tools.string.MoreStrings.isEmpty; 006import static java.util.Objects.requireNonNull; 007import java.io.BufferedInputStream; 008import java.io.IOException; 009import java.io.InputStream; 010import java.io.StringWriter; 011import java.nio.charset.Charset; 012import java.nio.charset.StandardCharsets; 013import java.nio.file.Files; 014import java.nio.file.Path; 015import java.nio.file.StandardCopyOption; 016 017/** 018 * Utility class for dealing with generic / classpath related file access. 019 * 020 * @author Oliver Wolff 021 */ 022public final class FileLoaderUtility { 023 /** 024 * Returns an implementation of {@link FileLoader} matching to the given path. 025 * 026 * @param pathName must not be null or empty. 027 * @return a configured implementation of {@link FileLoader}. In case the pathName is prefixed 028 * with {@link FileTypePrefix#CLASSPATH} it returns a {@link ClassPathLoader}. If 029 * prefixed with 030 * {@link FileTypePrefix#URL} it returns a {@link UrlLoader}. Otherwise, it returns a 031 * {@link FileSystemLoader}. 032 */ 033 public static FileLoader getLoaderForPath(final String pathName) { 034 if (isEmpty(pathName)) { 035 throw new IllegalArgumentException("pathName must not be null nor empty"); 036 } 037 if (pathName.startsWith(FileTypePrefix.CLASSPATH.getPrefix())) { 038 return new ClassPathLoader(pathName); 039 } 040 if (pathName.startsWith(FileTypePrefix.URL.getPrefix())) { 041 return new UrlLoader(pathName); 042 } 043 return new FileSystemLoader(pathName); 044 } 045 046 // owolff: See hint Caution: Security-Impact 047 /** 048 * Helper class that copies the content of a {@link FileLoader} to the temp-folder and 049 * references it 050 * <h2>Caution: Security-Impact</h2> 051 * Creating a temp-file might introduce a security issue. Never ever use this location for 052 * sensitive information that might be of interest for an attacker 053 * 054 * @param source must not be null and represent an accessible file, saying 055 * {@link FileLoader#isReadable()} 056 * @param markDeleteOnExit if <code>true</code> the file will be marked to delete on Exit. 057 * @return a reference on a file copied in the temp folder 058 * @throws IOException 059 */ 060 @SuppressWarnings("java:S5443") 061 public static Path copyFileToTemp(final FileLoader source, final boolean markDeleteOnExit) throws IOException { 062 checkArgument(null != source, "Attribute with name source must not be null"); 063 checkArgument(source.isReadable(), "Source must be readable"); 064 final Path target = Files.createTempFile(source.getFileName().getNamePart(), source.getFileName().getSuffix()); 065 try (InputStream inputStream = source.inputStream()) { 066 Files.copy(new BufferedInputStream(inputStream), target, StandardCopyOption.REPLACE_EXISTING); 067 } 068 if (markDeleteOnExit) { 069 target.toFile().deleteOnExit(); 070 } 071 return target; 072 } 073 074 /** 075 * Convenience method for reading the content from a given {@link FileLoader} into a String 076 * 077 * @param fileLoader must not be null 078 * @param charset must not be null 079 * @return The String content of the File represented by the given {@link FileLoader} 080 * @throws IOException 081 */ 082 public static String toString(final FileLoader fileLoader, final Charset charset) throws IOException { 083 requireNonNull(fileLoader); 084 final StringWriter writer = new StringWriter(); 085 try (InputStream inputStream = fileLoader.inputStream()) { 086 IOStreams.copy(inputStream, writer, charset); 087 } 088 return writer.toString(); 089 } 090 091 /** 092 * Convenience method for reading the content from a given {@link FileLoader} into a String read 093 * as UTF-8 String 094 * 095 * @param fileLoader must not be null 096 * @return The String content of the File represented by the given {@link FileLoader} 097 * @throws IOException 098 */ 099 public static String toString(final FileLoader fileLoader) throws IOException { 100 return toString(fileLoader, StandardCharsets.UTF_8); 101 } 102 103 /** 104 * Convenience method for reading the content from a given {@link FileLoader} into a String read 105 * as UTF-8 String 106 * 107 * @param fileLoader must not be null 108 * @return The String content of the File represented by the given {@link FileLoader} 109 * @throws IllegalArgumentException masking the actual {@link IOException} 110 * @throws NullPointerException if <code>fileLoader</code> is null 111 */ 112 public static String toStringUnchecked(final FileLoader fileLoader) { 113 requireNonNull(fileLoader); 114 try { 115 return toString(fileLoader); 116 } catch (final IOException | IllegalStateException e) { 117 throw new IllegalArgumentException("Unable to read from Path " + fileLoader.getFileName(), e); 118 } 119 } 120 121 @java.lang.SuppressWarnings("all") 122 @lombok.Generated 123 private FileLoaderUtility() { 124 } 125}