Mercurial Hosting > luan
comparison src/luan/modules/Utils.java @ 775:1a68fc55a80c
simplify dir structure
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 26 Aug 2016 14:36:40 -0600 |
parents | core/src/luan/modules/Utils.java@e44e98fe9de8 |
children | 836e00bf7ce2 |
comparison
equal
deleted
inserted
replaced
774:3e30cf310e56 | 775:1a68fc55a80c |
---|---|
1 package luan.modules; | |
2 | |
3 import java.io.Reader; | |
4 import java.io.IOException; | |
5 import java.io.ByteArrayOutputStream; | |
6 import java.io.InputStream; | |
7 import java.io.OutputStream; | |
8 import java.io.File; | |
9 import java.io.InputStreamReader; | |
10 import java.net.URL; | |
11 import java.net.MalformedURLException; | |
12 import luan.LuanException; | |
13 import luan.LuanTable; | |
14 import luan.LuanFunction; | |
15 | |
16 | |
17 public final class Utils { | |
18 private Utils() {} // never | |
19 | |
20 static final int bufSize = 8192; | |
21 | |
22 private static void checkNotNull(Object v,String expected,int pos) throws LuanException { | |
23 if( v == null ) | |
24 throw new LuanException("bad argument #"+pos+" ("+expected+" expected, got nil)"); | |
25 } | |
26 | |
27 public static void checkNotNull(String s,int pos) throws LuanException { | |
28 checkNotNull(s,"string",pos); | |
29 } | |
30 | |
31 public static void checkNotNull(String s) throws LuanException { | |
32 checkNotNull(s,1); | |
33 } | |
34 | |
35 public static void checkNotNull(byte[] b,int pos) throws LuanException { | |
36 checkNotNull(b,"binary",pos); | |
37 } | |
38 | |
39 public static void checkNotNull(byte[] b) throws LuanException { | |
40 checkNotNull(b,1); | |
41 } | |
42 | |
43 public static void checkNotNull(LuanTable t,int pos) throws LuanException { | |
44 checkNotNull(t,"table",pos); | |
45 } | |
46 | |
47 public static void checkNotNull(LuanTable t) throws LuanException { | |
48 checkNotNull(t,1); | |
49 } | |
50 | |
51 public static void checkNotNull(Number n,int pos) throws LuanException { | |
52 checkNotNull(n,"number",pos); | |
53 } | |
54 | |
55 public static void checkNotNull(Number n) throws LuanException { | |
56 checkNotNull(n,1); | |
57 } | |
58 | |
59 public static void checkNotNull(LuanFunction fn,int pos) throws LuanException { | |
60 checkNotNull(fn,"function",pos); | |
61 } | |
62 | |
63 public static void checkNotNull(LuanFunction fn) throws LuanException { | |
64 checkNotNull(fn,1); | |
65 } | |
66 | |
67 public static String readAll(Reader in) | |
68 throws IOException | |
69 { | |
70 char[] a = new char[bufSize]; | |
71 StringBuilder buf = new StringBuilder(); | |
72 int n; | |
73 while( (n=in.read(a)) != -1 ) { | |
74 buf.append(a,0,n); | |
75 } | |
76 return buf.toString(); | |
77 } | |
78 | |
79 public static void copyAll(InputStream in,OutputStream out) | |
80 throws IOException | |
81 { | |
82 byte[] a = new byte[bufSize]; | |
83 int n; | |
84 while( (n=in.read(a)) != -1 ) { | |
85 out.write(a,0,n); | |
86 } | |
87 } | |
88 | |
89 public static byte[] readAll(InputStream in) | |
90 throws IOException | |
91 { | |
92 ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
93 copyAll(in,out); | |
94 return out.toByteArray(); | |
95 } | |
96 /* | |
97 public static boolean exists(File file) { | |
98 try { | |
99 return file.exists() && file.getName().equals(file.getCanonicalFile().getName()); | |
100 } catch(IOException e) { | |
101 throw new RuntimeException(e); | |
102 } | |
103 } | |
104 */ | |
105 /* | |
106 private static File toFile(String path) { | |
107 if( path.contains("//") ) | |
108 return null; | |
109 File file = new File(path); | |
110 return file.exists() ? file : null; | |
111 } | |
112 | |
113 private static URL toUrl(String path) { | |
114 if( path.indexOf(':') == -1 ) | |
115 return null; | |
116 if( path.startsWith("classpath:") ) { | |
117 path = path.substring(10); | |
118 if( path.contains("//") ) | |
119 return null; | |
120 URL url; | |
121 if( !path.contains("#") ) { | |
122 url = ClassLoader.getSystemResource(path); | |
123 } else { | |
124 String[] a = path.split("#"); | |
125 url = ClassLoader.getSystemResource(a[0]); | |
126 if( url==null ) { | |
127 for( int i=1; i<a.length; i++ ) { | |
128 url = ClassLoader.getSystemResource(a[0]+"/"+a[i]); | |
129 if( url != null ) { | |
130 try { | |
131 url = new URL(url,"."); | |
132 } catch(MalformedURLException e) { | |
133 throw new RuntimeException(e); | |
134 } | |
135 break; | |
136 } | |
137 } | |
138 } | |
139 } | |
140 return url==null ? null : url; | |
141 } | |
142 try { | |
143 return new URL(path); | |
144 } catch(MalformedURLException e) {} | |
145 return null; | |
146 } | |
147 | |
148 static boolean exists(String path) { | |
149 return toFile(path)!=null || toUrl(path)!=null; | |
150 } | |
151 */ | |
152 | |
153 | |
154 | |
155 /* replace by uri"os:..." | |
156 | |
157 // process | |
158 | |
159 public static class ProcessException extends IOException { | |
160 private ProcessException(String msg) { | |
161 super(msg); | |
162 } | |
163 } | |
164 | |
165 public static void checkProcess(Process proc) | |
166 throws IOException | |
167 { | |
168 try { | |
169 proc.waitFor(); | |
170 } catch(InterruptedException e) { | |
171 throw new RuntimeException(e); | |
172 } | |
173 int exitVal = proc.exitValue(); | |
174 if( exitVal != 0 ) { | |
175 Reader err = new InputStreamReader(proc.getErrorStream()); | |
176 String error = readAll(err); | |
177 err.close(); | |
178 throw new ProcessException(error); | |
179 } | |
180 } | |
181 | |
182 public static String getOutput(Process proc) | |
183 throws IOException | |
184 { | |
185 Reader in = new InputStreamReader(proc.getInputStream()); | |
186 String s = readAll(in); | |
187 in.close(); | |
188 return s; | |
189 } | |
190 | |
191 public static String execProcess(String... cmd) | |
192 throws IOException | |
193 { | |
194 Process proc = Runtime.getRuntime().exec(cmd); | |
195 String s = getOutput(proc); | |
196 checkProcess(proc); | |
197 return s; | |
198 } | |
199 */ | |
200 } |