package net.knarcraft.ffmpegconverter.utility; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * A helper class for dealing with files */ @SuppressWarnings("unused") public final class FileHelper { private FileHelper() { } /** * Gets a buffered reader for an internal file * * @param file
The name of the file to get a buffered reader for (start with a '/'). The file should reside in * the resources directory.
* @returnA buffered read for reading the file
* @throws FileNotFoundExceptionIf unable to get an input stream for the given file
*/ @NotNull public static BufferedReader getBufferedReaderForInternalFile(@NotNull String file) throws FileNotFoundException { InputStream inputStream = getInputStreamForInternalFile(file); if (inputStream == null) { throw new FileNotFoundException("Unable to read the given file"); } return getBufferedReaderFromInputStream(inputStream); } /** * Gets an input stream from a string pointing to an internal file * *This is used for getting an input stream for reading a file contained within the compiled .jar file. The file * should be in the resources directory, and the file path should start with a forward slash ("/") character.
* * @param fileThe file to read
* @returnAn input stream for the file
*/ @Nullable public static InputStream getInputStreamForInternalFile(@NotNull String file) { return FileHelper.class.getResourceAsStream(file); } /** * Gets a buffered reader from a string pointing to a file * * @param fileThe file to read
* @returnA buffered reader reading the file
* @throws FileNotFoundExceptionIf the given file does not exist
*/ @NotNull public static BufferedReader getBufferedReaderFromString(@NotNull String file) throws FileNotFoundException { FileInputStream fileInputStream = new FileInputStream(file); return getBufferedReaderFromInputStream(fileInputStream); } /** * Gets a buffered reader given an input stream * * @param inputStreamThe input stream to read
* @returnA buffered reader reading the input stream
*/ @NotNull public static BufferedReader getBufferedReaderFromInputStream(@NotNull InputStream inputStream) { InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8); return new BufferedReader(inputStreamReader); } /** * Gets a buffered writer from a string pointing to a file * * @param fileThe file to write to
* @returnA buffered writer writing to the file
* @throws FileNotFoundExceptionIf the file does not exist
*/ @NotNull public static BufferedWriter getBufferedWriterFromString(@NotNull String file) throws FileNotFoundException { FileOutputStream fileOutputStream = new FileOutputStream(file); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8); return new BufferedWriter(outputStreamWriter); } /** * Reads key/value pairs from a buffered reader * * @param bufferedReaderThe buffered reader to read
* @param separatorThe separator separating a key from a value
* @returnA map containing the read pairs
* @throws IOExceptionIf unable to read from the stream
*/ @NotNull public static MapThe buffered reader to read
* @returnA list of the read strings
* @throws IOExceptionIf unable to read from the stream
*/ @NotNull public static ListThe string to remove the BOM from
* @returnA string guaranteed without a BOM
*/ private static @NotNull String removeUTF8BOM(@NotNull String string) { String UTF8_BOM = "\uFEFF"; if (string.startsWith(UTF8_BOM)) { string = string.substring(1); } return string; } }