Mercurial Hosting > luan
comparison src/luan/lib/IoLib.java @ 143:fcb81fa2df0d
handle urls and java resources as files
git-svn-id: https://luan-java.googlecode.com/svn/trunk@144 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Fri, 13 Jun 2014 19:04:05 +0000 |
parents | 05f8c21160ef |
children | 2e92f0a6fcac |
comparison
equal
deleted
inserted
replaced
142:05f8c21160ef | 143:fcb81fa2df0d |
---|---|
35 | 35 |
36 public static final LuanFunction LOADER = new LuanFunction() { | 36 public static final LuanFunction LOADER = new LuanFunction() { |
37 @Override public Object call(LuanState luan,Object[] args) { | 37 @Override public Object call(LuanState luan,Object[] args) { |
38 LuanTable module = new LuanTable(); | 38 LuanTable module = new LuanTable(); |
39 try { | 39 try { |
40 add( module, "file", String.class ); | 40 add( module, "file", LuanState.class, String.class ); |
41 add( module, "java_resource_to_url", String.class ); | |
42 add( module, "url", String.class ); | |
43 add( module, "java_resource", String.class ); | |
44 add( module, "read_console_line", String.class ); | 41 add( module, "read_console_line", String.class ); |
45 | 42 |
46 LuanTable stdin = new LuanTable(); | 43 LuanTable stdin = new LuanTable(); |
47 stdin.put( "read_text", new LuanJavaFunction( | 44 stdin.put( "read_text", new LuanJavaFunction( |
48 IoLib.class.getMethod( "stdin_read_text" ), null | 45 IoLib.class.getMethod( "stdin_read_text" ), null |
87 } | 84 } |
88 | 85 |
89 public static LuanFunction stdin_read_blocks(Integer blockSize) throws IOException { | 86 public static LuanFunction stdin_read_blocks(Integer blockSize) throws IOException { |
90 int n = blockSize!=null ? blockSize : Utils.bufSize; | 87 int n = blockSize!=null ? blockSize : Utils.bufSize; |
91 return blocks(System.in,n); | 88 return blocks(System.in,n); |
92 } | |
93 | |
94 public static String java_resource_to_url(String path) { | |
95 URL url = ClassLoader.getSystemResource(path); | |
96 return url==null ? null : url.toString(); | |
97 } | |
98 | |
99 public static LuanTable java_resource(String path) throws IOException { | |
100 return url(java_resource_to_url(path)); | |
101 } | 89 } |
102 | 90 |
103 public static String read_console_line(String prompt) throws IOException { | 91 public static String read_console_line(String prompt) throws IOException { |
104 if( prompt==null ) | 92 if( prompt==null ) |
105 prompt = "> "; | 93 prompt = "> "; |
222 | 210 |
223 | 211 |
224 | 212 |
225 public static abstract class LuanIn { | 213 public static abstract class LuanIn { |
226 abstract InputStream inputStream() throws IOException; | 214 abstract InputStream inputStream() throws IOException; |
215 abstract String name(); | |
227 | 216 |
228 public String read_text() throws IOException { | 217 public String read_text() throws IOException { |
229 Reader in = new InputStreamReader(inputStream()); | 218 Reader in = new InputStreamReader(inputStream()); |
230 String s = Utils.readAll(in); | 219 String s = Utils.readAll(in); |
231 in.close(); | 220 in.close(); |
249 } | 238 } |
250 | 239 |
251 LuanTable table() { | 240 LuanTable table() { |
252 LuanTable tbl = new LuanTable(); | 241 LuanTable tbl = new LuanTable(); |
253 try { | 242 try { |
243 tbl.put( "name", new LuanJavaFunction( | |
244 LuanIn.class.getMethod( "name" ), this | |
245 ) ); | |
254 tbl.put( "read_text", new LuanJavaFunction( | 246 tbl.put( "read_text", new LuanJavaFunction( |
255 LuanIn.class.getMethod( "read_text" ), this | 247 LuanIn.class.getMethod( "read_text" ), this |
256 ) ); | 248 ) ); |
257 tbl.put( "read_binary", new LuanJavaFunction( | 249 tbl.put( "read_binary", new LuanJavaFunction( |
258 LuanIn.class.getMethod( "read_binary" ), this | 250 LuanIn.class.getMethod( "read_binary" ), this |
326 } | 318 } |
327 | 319 |
328 InputStream inputStream() throws IOException { | 320 InputStream inputStream() throws IOException { |
329 return url.openStream(); | 321 return url.openStream(); |
330 } | 322 } |
331 } | 323 |
332 | 324 String name() { |
333 public static LuanTable url(String s) throws MalformedURLException { | 325 return url.toString(); |
334 return new LuanUrl(s).table(); | 326 } |
335 } | 327 } |
336 | 328 |
337 public static final class LuanFile extends LuanIO { | 329 public static final class LuanFile extends LuanIO { |
338 private final File file; | 330 private final File file; |
339 | 331 |
346 } | 338 } |
347 | 339 |
348 OutputStream outputStream() throws IOException { | 340 OutputStream outputStream() throws IOException { |
349 return new FileOutputStream(file); | 341 return new FileOutputStream(file); |
350 } | 342 } |
351 } | 343 |
352 | 344 String name() { |
353 public static LuanTable file(String name) { | 345 return file.toString(); |
354 return new LuanFile(name).table(); | 346 } |
347 } | |
348 | |
349 public static LuanIn luanIo(LuanState luan,String name) throws LuanException { | |
350 if( Utils.isFile(name) ) | |
351 return new LuanFile(name); | |
352 String url = Utils.toUrl(name); | |
353 if( url == null ) | |
354 throw luan.exception( "file '"+name+"' not found" ); | |
355 try { | |
356 return new LuanUrl(url); | |
357 } catch(MalformedURLException e) { | |
358 throw new RuntimeException(e); | |
359 } | |
360 } | |
361 | |
362 public static LuanTable file(LuanState luan,String name) throws LuanException { | |
363 return luanIo(luan,name).table(); | |
355 } | 364 } |
356 | 365 |
357 public static final class LuanSocket extends LuanIO { | 366 public static final class LuanSocket extends LuanIO { |
358 private final Socket socket; | 367 private final Socket socket; |
359 | 368 |
369 return socket.getInputStream(); | 378 return socket.getInputStream(); |
370 } | 379 } |
371 | 380 |
372 OutputStream outputStream() throws IOException { | 381 OutputStream outputStream() throws IOException { |
373 return socket.getOutputStream(); | 382 return socket.getOutputStream(); |
383 } | |
384 | |
385 String name() { | |
386 return socket.toString(); | |
374 } | 387 } |
375 | 388 |
376 public LuanTable pickle_client(LuanState luan) throws IOException { | 389 public LuanTable pickle_client(LuanState luan) throws IOException { |
377 DataInputStream in = new DataInputStream(new BufferedInputStream(inputStream())); | 390 DataInputStream in = new DataInputStream(new BufferedInputStream(inputStream())); |
378 DataOutputStream out = new DataOutputStream(new BufferedOutputStream(outputStream())); | 391 DataOutputStream out = new DataOutputStream(new BufferedOutputStream(outputStream())); |