You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileUtils.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.util.io;
  23. import java.io.IOException;
  24. import java.net.URI;
  25. import java.net.URISyntaxException;
  26. import java.net.URL;
  27. import java.nio.file.CopyOption;
  28. import java.nio.file.DirectoryStream;
  29. import java.nio.file.FileSystem;
  30. import java.nio.file.FileSystems;
  31. import java.nio.file.Files;
  32. import java.nio.file.Path;
  33. import java.nio.file.Paths;
  34. import java.nio.file.StandardCopyOption;
  35. import java.security.CodeSource;
  36. import java.security.ProtectionDomain;
  37. import java.util.HashMap;
  38. import java.util.Map;
  39. /**
  40. * Utility class to deal with files.
  41. */
  42. public final class FileUtils {
  43. private FileUtils() {
  44. // Shouldn't be instansiated.
  45. }
  46. /**
  47. * Checks whether this application has been run from a jar or not.
  48. *
  49. * @param clazz Class used to locate the application
  50. *
  51. * @return true if the application was launched from a jar, false otherwise
  52. *
  53. * @throws IllegalStateException If the application path cannot be determined for any reason
  54. */
  55. public static boolean isRunningFromJar(final Class<?> clazz) throws IllegalStateException {
  56. return getApplicationPath(clazz).getFileName().endsWith(".jar");
  57. }
  58. /**
  59. * Returns the base location for the specified class, this is either the a jar or a folder
  60. * depending on how the application was launched.
  61. *
  62. * This can fail for a number of reasons, it is not wholly reliable.
  63. *
  64. * @param clazz Class used to locate the application
  65. *
  66. * @return A {@link Path} to application location
  67. *
  68. * @throws IllegalStateException If the application path cannot be determined for any reason
  69. */
  70. public static Path getApplicationPath(final Class<?> clazz) throws IllegalStateException {
  71. final ProtectionDomain pd;
  72. try {
  73. pd = clazz.getProtectionDomain();
  74. } catch (SecurityException ex) {
  75. throw new IllegalStateException("Unable to get protection domain", ex);
  76. }
  77. final CodeSource cs = pd.getCodeSource();
  78. if (cs == null) {
  79. throw new IllegalStateException("Unable to get the code source");
  80. }
  81. final URI uri;
  82. try {
  83. uri = cs.getLocation().toURI();
  84. } catch (URISyntaxException ex) {
  85. throw new IllegalStateException("Unable to convert location to URI", ex);
  86. }
  87. return Paths.get(uri);
  88. }
  89. /**
  90. * Recursively copies the resources specified by the given URL to the destination folder.
  91. * Existing files will be replaced.
  92. *
  93. * @param source The resource to be copied (file or folder).
  94. * @param destination The destination folder to copy the resource to.
  95. * @throws IOException If the resource couldn't be copied.
  96. */
  97. public static void copyResources(final URL source, final Path destination) throws IOException {
  98. try {
  99. final String path = source.toURI().toString();
  100. final int index = path.indexOf("!/");
  101. if (index > -1) {
  102. final String file = path.substring(0, index);
  103. final Map<String, String> env = new HashMap<>();
  104. try (final FileSystem fs = FileSystems.newFileSystem(URI.create(file), env)) {
  105. copyRecursively(fs.getPath(path.substring(index + 2)), destination,
  106. StandardCopyOption.REPLACE_EXISTING);
  107. }
  108. } else {
  109. copyRecursively(Paths.get(source.toURI()), destination,
  110. StandardCopyOption.REPLACE_EXISTING);
  111. }
  112. } catch (URISyntaxException ex) {
  113. throw new IOException("Unable to get source URI", ex);
  114. }
  115. }
  116. /**
  117. * Recursively copies one path to another. Once complete, a deep copy of the source file or
  118. * folder will be present in the destination directory.
  119. *
  120. * @param source The path that should be copied.
  121. * @param destination The directory to place copied files in.
  122. * @param options Options to use when copying.
  123. * @throws IOException If any files couldn't be copied.
  124. */
  125. public static void copyRecursively(final Path source, final Path destination,
  126. final CopyOption... options) throws IOException {
  127. final Path destinationPath = destination.resolve(source.getFileName().toString());
  128. if (Files.isDirectory(source)) {
  129. Files.createDirectories(destinationPath);
  130. try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(source)) {
  131. for (Path path : directoryStream) {
  132. copyRecursively(path, destinationPath, options);
  133. }
  134. }
  135. } else {
  136. Files.copy(source, destinationPath, options);
  137. }
  138. }
  139. }