123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- package eu.tankernn.gameEngine.util;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.URL;
- import java.util.Arrays;
- import com.google.common.base.Charsets;
- import com.google.common.io.Resources;
- public class InternalFile {
- private static final String FILE_SEPARATOR = "/";
- private String path;
- private String name;
- public InternalFile(String path) throws FileNotFoundException {
- this.path = path;
- if (!path.startsWith("/") || path.isEmpty())
- this.path = FILE_SEPARATOR + path;
- String[] dirs = path.split(FILE_SEPARATOR);
- this.name = dirs[dirs.length - 1];
- try {
- getInputStream().close();
- } catch (FileNotFoundException e) {
- throw e;
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public InternalFile(String... paths) throws FileNotFoundException {
- this(String.join(FILE_SEPARATOR, paths));
- }
- public InternalFile(InternalFile file, String... subFiles) {
- this.path = file.path;
- for (String part : subFiles) {
- this.path += (FILE_SEPARATOR + part);
- }
- String[] dirs = path.split(FILE_SEPARATOR);
- this.name = dirs[dirs.length - 1];
- }
- public String getPath() {
- return path;
- }
- @Override
- public String toString() {
- return getPath();
- }
- @SuppressWarnings("resource")
- public InputStream getInputStream() throws FileNotFoundException {
- InputStream in = null;
- try {
- in = new FileInputStream(new File("." + path));
- } catch (FileNotFoundException ex) {
- in = InternalFile.class.getResourceAsStream(path);
- }
- if (in == null)
- throw new FileNotFoundException("Cannot find file " + path + " on classpath.");
- return in;
- }
- public BufferedReader getReader() throws IOException {
- try {
- InputStreamReader isr = new InputStreamReader(getInputStream());
- BufferedReader reader = new BufferedReader(isr);
- return reader;
- } catch (IOException e) {
- System.err.println("Couldn't get reader for " + path);
- throw e;
- }
- }
- public URL getURL() {
- return InternalFile.class.getResource(path);
- }
- public String readFile() throws IOException {
- return Resources.toString(getURL(), Charsets.UTF_8);
- }
- public String getName() {
- return name;
- }
- public static InternalFile[] fromFilenames(String dir, String[] filenames, String extension) {
- return Arrays.asList(filenames).stream().map(f -> {
- try {
- return new InternalFile(dir + FILE_SEPARATOR + f + "." + extension);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- return null;
- }
- }).toArray(size -> new InternalFile[size]);
- }
- @Override
- public boolean equals(Object obj) {
- if (!obj.getClass().equals(this.getClass())) {
- return false;
- } else {
- return this.getPath().equals(((InternalFile) obj).getPath());
- }
- }
- public String getExtension() {
- String[] split = getName().split("\\.");
- return split[split.length - 1];
- }
- }
|