summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/Util.java
diff options
context:
space:
mode:
authorGravatar Thog2016-09-06 12:11:50 +0200
committerGravatar Thog2016-09-06 12:11:50 +0200
commite0e3141619cecd54c087d964654e6c35511c48f9 (patch)
tree9d7750f6abe26b4ca08cc9ffb516222569ebfd0b /src/main/java/cuchaz/enigma/Util.java
parentAvoid Engima converter detecting <init> and <clinit> as matchable token (diff)
downloadenigma-fork-e0e3141619cecd54c087d964654e6c35511c48f9.tar.gz
enigma-fork-e0e3141619cecd54c087d964654e6c35511c48f9.tar.xz
enigma-fork-e0e3141619cecd54c087d964654e6c35511c48f9.zip
A little bit of clean up
Diffstat (limited to 'src/main/java/cuchaz/enigma/Util.java')
-rw-r--r--src/main/java/cuchaz/enigma/Util.java99
1 files changed, 0 insertions, 99 deletions
diff --git a/src/main/java/cuchaz/enigma/Util.java b/src/main/java/cuchaz/enigma/Util.java
deleted file mode 100644
index 1bcdb9e..0000000
--- a/src/main/java/cuchaz/enigma/Util.java
+++ /dev/null
@@ -1,99 +0,0 @@
1/*******************************************************************************
2 * Copyright (c) 2015 Jeff Martin.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the GNU Lesser General Public
5 * License v3.0 which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/lgpl.html
7 * <p>
8 * Contributors:
9 * Jeff Martin - initial API and implementation
10 ******************************************************************************/
11package cuchaz.enigma;
12
13import com.google.common.io.CharStreams;
14
15import java.awt.Desktop;
16import java.io.*;
17import java.net.URI;
18import java.net.URISyntaxException;
19import java.util.Arrays;
20import java.util.jar.JarFile;
21
22import javassist.CannotCompileException;
23import javassist.CtClass;
24import javassist.bytecode.Descriptor;
25
26public class Util {
27
28 public static int combineHashesOrdered(Object... objs) {
29 return combineHashesOrdered(Arrays.asList(objs));
30 }
31
32 public static int combineHashesOrdered(Iterable<Object> objs) {
33 final int prime = 67;
34 int result = 1;
35 for (Object obj : objs) {
36 result *= prime;
37 if (obj != null) {
38 result += obj.hashCode();
39 }
40 }
41 return result;
42 }
43
44 public static void closeQuietly(Closeable closeable) {
45 if (closeable != null) {
46 try {
47 closeable.close();
48 } catch (IOException ex) {
49 // just ignore any further exceptions
50 }
51 }
52 }
53
54 public static void closeQuietly(JarFile jarFile) {
55 // silly library should implement Closeable...
56 if (jarFile != null) {
57 try {
58 jarFile.close();
59 } catch (IOException ex) {
60 // just ignore any further exceptions
61 }
62 }
63 }
64
65 public static String readStreamToString(InputStream in) throws IOException {
66 return CharStreams.toString(new InputStreamReader(in, "UTF-8"));
67 }
68
69 public static String readResourceToString(String path) throws IOException {
70 InputStream in = Util.class.getResourceAsStream(path);
71 if (in == null) {
72 throw new IllegalArgumentException("Resource not found! " + path);
73 }
74 return readStreamToString(in);
75 }
76
77 public static void openUrl(String url) {
78 if (Desktop.isDesktopSupported()) {
79 Desktop desktop = Desktop.getDesktop();
80 try {
81 desktop.browse(new URI(url));
82 } catch (IOException ex) {
83 throw new Error(ex);
84 } catch (URISyntaxException ex) {
85 throw new IllegalArgumentException(ex);
86 }
87 }
88 }
89
90 public static void writeClass(CtClass c) {
91 String name = Descriptor.toJavaName(c.getName());
92 File file = new File(name + ".class");
93 try (FileOutputStream out = new FileOutputStream(file)) {
94 out.write(c.toBytecode());
95 } catch (IOException | CannotCompileException ex) {
96 throw new Error(ex);
97 }
98 }
99}