PageRenderTime 75ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/sk89q/skmcl/util/LauncherUtils.java

https://gitlab.com/Slind/skmclauncher
Java | 213 lines | 94 code | 19 blank | 100 comment | 17 complexity | 69af0e98171386911eb9f119699951ec MD5 | raw file
  1. /*
  2. * SK's Minecraft Launcher
  3. * Copyright (C) 2010, 2011 Albert Pham <http://www.sk89q.com>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. package com.sk89q.skmcl.util;
  19. import java.io.*;
  20. import java.util.logging.Logger;
  21. import static org.apache.commons.io.IOUtils.closeQuietly;
  22. /**
  23. * Various utility methods.
  24. */
  25. public final class LauncherUtils {
  26. private LauncherUtils() {
  27. }
  28. /**
  29. * Gets the value of an enum from a name. The name can be null, causing
  30. * the return value to be null.
  31. *
  32. * @param en enum
  33. * @param name name of item
  34. * @return value or null
  35. */
  36. public static <T extends Enum<T>> T valueOf(Class<T> en, String name) {
  37. if (name == null) return null;
  38. try {
  39. return Enum.valueOf(en, name);
  40. } catch (IllegalArgumentException e) {
  41. return null;
  42. }
  43. }
  44. /**
  45. * Gets the value of an enum from a name. The name can be null, causing
  46. * the return value to be null.
  47. *
  48. * @param en enum
  49. * @param name name of item, will be upper cased
  50. * @return value or null
  51. */
  52. public static <T extends Enum<T>> T uppercaseValueOf(Class<T> en, String name) {
  53. if (name == null) return null;
  54. return valueOf(en, name.toUpperCase());
  55. }
  56. /**
  57. * Get a filename's extension
  58. *
  59. * @param name filename
  60. * @return extension, or an empty string for no extension
  61. */
  62. public static String getExtension(String name) {
  63. int index = name.lastIndexOf('.');
  64. if (index == 0) {
  65. return "";
  66. } else {
  67. return name.substring(index + 1, name.length());
  68. }
  69. }
  70. /**
  71. * Get a filename's extension
  72. *
  73. * @param file file
  74. * @return extension, or an empty string for no extension
  75. */
  76. public static String getExtension(File file) {
  77. return getExtension(file.getName());
  78. }
  79. /**
  80. * Get a stack trace as a string.
  81. *
  82. * @param t exception
  83. * @return stack trace
  84. */
  85. public static String getStackTrace(Throwable t) {
  86. Writer result = new StringWriter();
  87. PrintWriter printWriter = new PrintWriter(result);
  88. t.printStackTrace(printWriter);
  89. return result.toString();
  90. }
  91. /**
  92. * Just consume and discard a stream.
  93. *
  94. * @param from stream to read
  95. */
  96. public static void consumeBlindly(InputStream from) {
  97. final InputStream in = from;
  98. Thread thread = new Thread(new Runnable() {
  99. @Override
  100. public void run() {
  101. byte[] buffer = new byte[1024];
  102. try {
  103. while (in.read(buffer) != -1) {
  104. }
  105. } catch (IOException e) {
  106. } finally {
  107. closeQuietly(in);
  108. }
  109. }
  110. });
  111. thread.setDaemon(true);
  112. thread.start();
  113. }
  114. /**
  115. * Given a path, get a File instance for the given directory. If the
  116. * directory does not exist, keep going up the path to find a directory
  117. * that does exist. If no directory can be found, return null.
  118. *
  119. * @param path path, or null to return null
  120. * @return File representing directory, or null
  121. */
  122. public static File getClosestDirectory(String path) {
  123. if (path == null) return null;
  124. File f = new File(path);
  125. while (!f.isDirectory()) {
  126. File parent = f.getParentFile();
  127. if (parent == null) {
  128. return null; // Path simply doesn't exist or we can't get a better one
  129. }
  130. f = parent;
  131. }
  132. return f;
  133. }
  134. /**
  135. * Check if the thread interruption flag is set.
  136. *
  137. * @throws InterruptedException thrown if set
  138. */
  139. public static void checkInterrupted() throws InterruptedException {
  140. if (Thread.interrupted()) {
  141. throw new InterruptedException();
  142. }
  143. }
  144. /**
  145. * Get a relative path to a base directory.
  146. *
  147. * @param base the base directory
  148. * @param path the path to make relative
  149. * @return the relativized path
  150. */
  151. public static String getRelative(File base, File path) {
  152. return base.toURI().relativize(path.toURI()).getPath();
  153. }
  154. /**
  155. * Read an {@link InputStream} to a string.
  156. *
  157. * @param is the input stream
  158. * @param encoding the encoding to read with
  159. * @return the string
  160. * @throws IOException on I/O error
  161. */
  162. public static String toString(InputStream is, String encoding) throws IOException {
  163. BufferedReader reader = new BufferedReader(
  164. new InputStreamReader(is, encoding));
  165. StringBuilder s = new StringBuilder();
  166. char[] buf = new char[1024];
  167. int len = 0;
  168. while ((len = reader.read(buf)) != -1) {
  169. s.append(buf, 0, len);
  170. }
  171. return s.toString();
  172. }
  173. /**
  174. * Get a logger for a given class.
  175. *
  176. * @param cls the class
  177. * @return a logger
  178. */
  179. public static Logger getLogger(Class<?> cls) {
  180. return Logger.getLogger(cls.getCanonicalName());
  181. }
  182. /**
  183. * Check whether the property {class}.{key} is true.
  184. *
  185. * @param cls the class
  186. * @param key the key
  187. * @return true if set
  188. */
  189. public static boolean hasSystemProperty(Class<?> cls, String key) {
  190. String name = cls.getCanonicalName() + "." + key;
  191. String value = System.getProperty(name, "false");
  192. return value.equalsIgnoreCase("true");
  193. }
  194. }