summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/Util.java
diff options
context:
space:
mode:
authorGravatar lclc982016-06-30 00:49:21 +1000
committerGravatar GitHub2016-06-30 00:49:21 +1000
commit4be005617b3b8c3578cca07c5d085d12916f0d1d (patch)
treedb163431f38703e26da417ef05eaea2b27a498b9 /src/main/java/cuchaz/enigma/Util.java
parentSome small changes to fix idea importing (diff)
downloadenigma-fork-4be005617b3b8c3578cca07c5d085d12916f0d1d.tar.gz
enigma-fork-4be005617b3b8c3578cca07c5d085d12916f0d1d.tar.xz
enigma-fork-4be005617b3b8c3578cca07c5d085d12916f0d1d.zip
Json format (#2)
* Added new format * Fixed bug * Updated Version
Diffstat (limited to 'src/main/java/cuchaz/enigma/Util.java')
-rw-r--r--src/main/java/cuchaz/enigma/Util.java99
1 files changed, 99 insertions, 0 deletions
diff --git a/src/main/java/cuchaz/enigma/Util.java b/src/main/java/cuchaz/enigma/Util.java
new file mode 100644
index 0000000..1bcdb9e
--- /dev/null
+++ b/src/main/java/cuchaz/enigma/Util.java
@@ -0,0 +1,99 @@
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}