diff options
| author | 2015-05-21 23:30:00 +0100 | |
|---|---|---|
| committer | 2015-05-21 23:30:00 +0100 | |
| commit | e3f452250e51b7271f3989c7dfd12e4422934942 (patch) | |
| tree | 5aa482f9a6e21eb318a3e23e7d8274d77c73faf6 /src/cuchaz/enigma/Util.java | |
| download | enigma-fork-e3f452250e51b7271f3989c7dfd12e4422934942.tar.gz enigma-fork-e3f452250e51b7271f3989c7dfd12e4422934942.tar.xz enigma-fork-e3f452250e51b7271f3989c7dfd12e4422934942.zip | |
Support Gradle alongside SSJB
This makes builds faster, simpler and better automated but still keeps
Cuchaz happy. :)
Diffstat (limited to 'src/cuchaz/enigma/Util.java')
| -rw-r--r-- | src/cuchaz/enigma/Util.java | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/Util.java b/src/cuchaz/enigma/Util.java new file mode 100644 index 0000000..c7e509f --- /dev/null +++ b/src/cuchaz/enigma/Util.java | |||
| @@ -0,0 +1,104 @@ | |||
| 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 | * | ||
| 8 | * Contributors: | ||
| 9 | * Jeff Martin - initial API and implementation | ||
| 10 | ******************************************************************************/ | ||
| 11 | package cuchaz.enigma; | ||
| 12 | |||
| 13 | import java.awt.Desktop; | ||
| 14 | import java.io.Closeable; | ||
| 15 | import java.io.File; | ||
| 16 | import java.io.FileOutputStream; | ||
| 17 | import java.io.IOException; | ||
| 18 | import java.io.InputStream; | ||
| 19 | import java.io.InputStreamReader; | ||
| 20 | import java.net.URI; | ||
| 21 | import java.net.URISyntaxException; | ||
| 22 | import java.util.Arrays; | ||
| 23 | import java.util.jar.JarFile; | ||
| 24 | |||
| 25 | import javassist.CannotCompileException; | ||
| 26 | import javassist.CtClass; | ||
| 27 | import javassist.bytecode.Descriptor; | ||
| 28 | |||
| 29 | import com.google.common.io.CharStreams; | ||
| 30 | |||
| 31 | public class Util { | ||
| 32 | |||
| 33 | public static int combineHashesOrdered(Object... objs) { | ||
| 34 | return combineHashesOrdered(Arrays.asList(objs)); | ||
| 35 | } | ||
| 36 | |||
| 37 | public static int combineHashesOrdered(Iterable<Object> objs) { | ||
| 38 | final int prime = 67; | ||
| 39 | int result = 1; | ||
| 40 | for (Object obj : objs) { | ||
| 41 | result *= prime; | ||
| 42 | if (obj != null) { | ||
| 43 | result += obj.hashCode(); | ||
| 44 | } | ||
| 45 | } | ||
| 46 | return result; | ||
| 47 | } | ||
| 48 | |||
| 49 | public static void closeQuietly(Closeable closeable) { | ||
| 50 | if (closeable != null) { | ||
| 51 | try { | ||
| 52 | closeable.close(); | ||
| 53 | } catch (IOException ex) { | ||
| 54 | // just ignore any further exceptions | ||
| 55 | } | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | public static void closeQuietly(JarFile jarFile) { | ||
| 60 | // silly library should implement Closeable... | ||
| 61 | if (jarFile != null) { | ||
| 62 | try { | ||
| 63 | jarFile.close(); | ||
| 64 | } catch (IOException ex) { | ||
| 65 | // just ignore any further exceptions | ||
| 66 | } | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | public static String readStreamToString(InputStream in) throws IOException { | ||
| 71 | return CharStreams.toString(new InputStreamReader(in, "UTF-8")); | ||
| 72 | } | ||
| 73 | |||
| 74 | public static String readResourceToString(String path) throws IOException { | ||
| 75 | InputStream in = Util.class.getResourceAsStream(path); | ||
| 76 | if (in == null) { | ||
| 77 | throw new IllegalArgumentException("Resource not found! " + path); | ||
| 78 | } | ||
| 79 | return readStreamToString(in); | ||
| 80 | } | ||
| 81 | |||
| 82 | public static void openUrl(String url) { | ||
| 83 | if (Desktop.isDesktopSupported()) { | ||
| 84 | Desktop desktop = Desktop.getDesktop(); | ||
| 85 | try { | ||
| 86 | desktop.browse(new URI(url)); | ||
| 87 | } catch (IOException ex) { | ||
| 88 | throw new Error(ex); | ||
| 89 | } catch (URISyntaxException ex) { | ||
| 90 | throw new IllegalArgumentException(ex); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | public static void writeClass(CtClass c) { | ||
| 96 | String name = Descriptor.toJavaName(c.getName()); | ||
| 97 | File file = new File(name + ".class"); | ||
| 98 | try (FileOutputStream out = new FileOutputStream(file)) { | ||
| 99 | out.write(c.toBytecode()); | ||
| 100 | } catch (IOException | CannotCompileException ex) { | ||
| 101 | throw new Error(ex); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | } | ||