summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/Util.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/cuchaz/enigma/Util.java')
-rw-r--r--src/cuchaz/enigma/Util.java107
1 files changed, 38 insertions, 69 deletions
diff --git a/src/cuchaz/enigma/Util.java b/src/cuchaz/enigma/Util.java
index 678de54..7f04bda 100644
--- a/src/cuchaz/enigma/Util.java
+++ b/src/cuchaz/enigma/Util.java
@@ -28,108 +28,77 @@ import javassist.bytecode.Descriptor;
28 28
29import com.google.common.io.CharStreams; 29import com.google.common.io.CharStreams;
30 30
31 31public class Util {
32public class Util 32
33{ 33 public static int combineHashesOrdered(Object... objs) {
34 public static int combineHashesOrdered( Object ... objs ) 34 return combineHashesOrdered(Arrays.asList(objs));
35 {
36 return combineHashesOrdered( Arrays.asList( objs ) );
37 } 35 }
38 36
39 public static int combineHashesOrdered( Iterable<Object> objs ) 37 public static int combineHashesOrdered(Iterable<Object> objs) {
40 {
41 final int prime = 67; 38 final int prime = 67;
42 int result = 1; 39 int result = 1;
43 for( Object obj : objs ) 40 for (Object obj : objs) {
44 {
45 result *= prime; 41 result *= prime;
46 if( obj != null ) 42 if (obj != null) {
47 {
48 result += obj.hashCode(); 43 result += obj.hashCode();
49 } 44 }
50 } 45 }
51 return result; 46 return result;
52 } 47 }
53 48
54 public static void closeQuietly( Closeable closeable ) 49 public static void closeQuietly(Closeable closeable) {
55 { 50 if (closeable != null) {
56 if( closeable != null ) 51 try {
57 {
58 try
59 {
60 closeable.close(); 52 closeable.close();
61 } 53 } catch (IOException ex) {
62 catch( IOException ex )
63 {
64 // just ignore any further exceptions 54 // just ignore any further exceptions
65 } 55 }
66 } 56 }
67 } 57 }
68 58
69 public static void closeQuietly( JarFile jarFile ) 59 public static void closeQuietly(JarFile jarFile) {
70 {
71 // silly library should implement Closeable... 60 // silly library should implement Closeable...
72 if( jarFile != null ) 61 if (jarFile != null) {
73 { 62 try {
74 try
75 {
76 jarFile.close(); 63 jarFile.close();
77 } 64 } catch (IOException ex) {
78 catch( IOException ex )
79 {
80 // just ignore any further exceptions 65 // just ignore any further exceptions
81 } 66 }
82 } 67 }
83 } 68 }
84 69
85 public static String readStreamToString( InputStream in ) 70 public static String readStreamToString(InputStream in) throws IOException {
86 throws IOException 71 return CharStreams.toString(new InputStreamReader(in, "UTF-8"));
87 {
88 return CharStreams.toString( new InputStreamReader( in, "UTF-8" ) );
89 } 72 }
90 73
91 public static String readResourceToString( String path ) 74 public static String readResourceToString(String path) throws IOException {
92 throws IOException 75 InputStream in = Util.class.getResourceAsStream(path);
93 { 76 if (in == null) {
94 InputStream in = Util.class.getResourceAsStream( path ); 77 throw new IllegalArgumentException("Resource not found! " + path);
95 if( in == null )
96 {
97 throw new IllegalArgumentException( "Resource not found! " + path );
98 } 78 }
99 return readStreamToString( in ); 79 return readStreamToString(in);
100 } 80 }
101 81
102 public static void openUrl( String url ) 82 public static void openUrl(String url) {
103 { 83 if (Desktop.isDesktopSupported()) {
104 if( Desktop.isDesktopSupported() )
105 {
106 Desktop desktop = Desktop.getDesktop(); 84 Desktop desktop = Desktop.getDesktop();
107 try 85 try {
108 { 86 desktop.browse(new URI(url));
109 desktop.browse( new URI( url ) ); 87 } catch (IOException ex) {
110 } 88 throw new Error(ex);
111 catch( IOException ex ) 89 } catch (URISyntaxException ex) {
112 { 90 throw new IllegalArgumentException(ex);
113 throw new Error( ex );
114 }
115 catch( URISyntaxException ex )
116 {
117 throw new IllegalArgumentException( ex );
118 } 91 }
119 } 92 }
120 } 93 }
121 94
122 public static void writeClass( CtClass c ) 95 public static void writeClass(CtClass c) {
123 { 96 String name = Descriptor.toJavaName(c.getName());
124 String name = Descriptor.toJavaName( c.getName() ); 97 File file = new File(name + ".class");
125 File file = new File( name + ".class" ); 98 try (FileOutputStream out = new FileOutputStream(file)) {
126 try( FileOutputStream out = new FileOutputStream( file ) ) 99 out.write(c.toBytecode());
127 { 100 } catch (IOException | CannotCompileException ex) {
128 out.write( c.toBytecode() ); 101 throw new Error(ex);
129 }
130 catch( IOException | CannotCompileException ex )
131 {
132 throw new Error( ex );
133 } 102 }
134 } 103 }
135} 104}