InternalFile.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package eu.tankernn.gameEngine.util;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.InputStreamReader;
  9. import java.net.URL;
  10. import java.util.Arrays;
  11. import com.google.common.base.Charsets;
  12. import com.google.common.io.Resources;
  13. public class InternalFile {
  14. private static final String FILE_SEPARATOR = "/";
  15. private String path;
  16. private String name;
  17. public InternalFile(String path) throws FileNotFoundException {
  18. this.path = path;
  19. if (!path.startsWith("/") || path.isEmpty())
  20. this.path = FILE_SEPARATOR + path;
  21. String[] dirs = path.split(FILE_SEPARATOR);
  22. this.name = dirs[dirs.length - 1];
  23. try {
  24. getInputStream().close();
  25. } catch (FileNotFoundException e) {
  26. throw e;
  27. } catch (IOException e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. public InternalFile(String... paths) throws FileNotFoundException {
  32. this(String.join(FILE_SEPARATOR, paths));
  33. }
  34. public InternalFile(InternalFile file, String... subFiles) {
  35. this.path = file.path;
  36. for (String part : subFiles) {
  37. this.path += (FILE_SEPARATOR + part);
  38. }
  39. String[] dirs = path.split(FILE_SEPARATOR);
  40. this.name = dirs[dirs.length - 1];
  41. }
  42. public String getPath() {
  43. return path;
  44. }
  45. @Override
  46. public String toString() {
  47. return getPath();
  48. }
  49. @SuppressWarnings("resource")
  50. public InputStream getInputStream() throws FileNotFoundException {
  51. InputStream in = null;
  52. try {
  53. in = new FileInputStream(new File("." + path));
  54. } catch (FileNotFoundException ex) {
  55. in = InternalFile.class.getResourceAsStream(path);
  56. }
  57. if (in == null)
  58. throw new FileNotFoundException("Cannot find file " + path + " on classpath.");
  59. return in;
  60. }
  61. public BufferedReader getReader() throws IOException {
  62. try {
  63. InputStreamReader isr = new InputStreamReader(getInputStream());
  64. BufferedReader reader = new BufferedReader(isr);
  65. return reader;
  66. } catch (IOException e) {
  67. System.err.println("Couldn't get reader for " + path);
  68. throw e;
  69. }
  70. }
  71. public URL getURL() {
  72. return InternalFile.class.getResource(path);
  73. }
  74. public String readFile() throws IOException {
  75. return Resources.toString(getURL(), Charsets.UTF_8);
  76. }
  77. public String getName() {
  78. return name;
  79. }
  80. public static InternalFile[] fromFilenames(String dir, String[] filenames, String extension) {
  81. return Arrays.asList(filenames).stream().map(f -> {
  82. try {
  83. return new InternalFile(dir + FILE_SEPARATOR + f + "." + extension);
  84. } catch (FileNotFoundException e) {
  85. e.printStackTrace();
  86. return null;
  87. }
  88. }).toArray(size -> new InternalFile[size]);
  89. }
  90. @Override
  91. public boolean equals(Object obj) {
  92. if (!obj.getClass().equals(this.getClass())) {
  93. return false;
  94. } else {
  95. return this.getPath().equals(((InternalFile) obj).getPath());
  96. }
  97. }
  98. public String getExtension() {
  99. String[] split = getName().split("\\.");
  100. return split[split.length - 1];
  101. }
  102. }