Mercurial Hosting > luan
comparison core/src/luan/modules/IoLuan.java @ 265:454a486d9c19
allow IO on files that don't exist
git-svn-id: https://luan-java.googlecode.com/svn/trunk@266 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Wed, 29 Oct 2014 16:26:42 +0000 |
parents | 9e0d4452e649 |
children | 1507149fc447 |
comparison
equal
deleted
inserted
replaced
264:9e0d4452e649 | 265:454a486d9c19 |
---|---|
16 import java.io.OutputStreamWriter; | 16 import java.io.OutputStreamWriter; |
17 import java.io.ByteArrayInputStream; | 17 import java.io.ByteArrayInputStream; |
18 import java.io.DataInputStream; | 18 import java.io.DataInputStream; |
19 import java.io.DataOutputStream; | 19 import java.io.DataOutputStream; |
20 import java.io.IOException; | 20 import java.io.IOException; |
21 import java.io.FileNotFoundException; | |
21 import java.net.URL; | 22 import java.net.URL; |
22 import java.net.Socket; | 23 import java.net.Socket; |
23 import java.net.ServerSocket; | 24 import java.net.ServerSocket; |
24 import java.net.MalformedURLException; | 25 import java.net.MalformedURLException; |
25 import java.util.List; | 26 import java.util.List; |
208 | 209 |
209 public LuanFunction loader(LuanState luan,String name) throws LuanException { | 210 public LuanFunction loader(LuanState luan,String name) throws LuanException { |
210 try { | 211 try { |
211 String src = read_text(); | 212 String src = read_text(); |
212 return BasicLuan.load(luan,src,name,null,false); | 213 return BasicLuan.load(luan,src,name,null,false); |
214 } catch(FileNotFoundException e) { | |
215 return null; | |
213 } catch(IOException e) { | 216 } catch(IOException e) { |
214 throw luan.exception(e); | 217 throw luan.exception(e); |
218 } | |
219 } | |
220 | |
221 public boolean exists() throws IOException { | |
222 try { | |
223 inputStream().close(); | |
224 return true; | |
225 } catch(FileNotFoundException e) { | |
226 return false; | |
215 } | 227 } |
216 } | 228 } |
217 | 229 |
218 LuanTable table() { | 230 LuanTable table() { |
219 LuanTable tbl = Luan.newTable(); | 231 LuanTable tbl = Luan.newTable(); |
234 LuanIn.class.getMethod( "read_blocks", Integer.class ), this | 246 LuanIn.class.getMethod( "read_blocks", Integer.class ), this |
235 ) ); | 247 ) ); |
236 tbl.put( "loader", new LuanJavaFunction( | 248 tbl.put( "loader", new LuanJavaFunction( |
237 LuanIn.class.getMethod( "loader", LuanState.class, String.class ), this | 249 LuanIn.class.getMethod( "loader", LuanState.class, String.class ), this |
238 ) ); | 250 ) ); |
251 tbl.put( "exists", new LuanJavaFunction( | |
252 LuanIn.class.getMethod( "exists" ), this | |
253 ) ); | |
239 } catch(NoSuchMethodException e) { | 254 } catch(NoSuchMethodException e) { |
240 throw new RuntimeException(e); | 255 throw new RuntimeException(e); |
241 } | 256 } |
242 return tbl; | 257 return tbl; |
243 } | 258 } |
257 return Utils.readAll(new InputStreamReader(System.in)); | 272 return Utils.readAll(new InputStreamReader(System.in)); |
258 } | 273 } |
259 | 274 |
260 @Override public byte[] read_binary() throws IOException { | 275 @Override public byte[] read_binary() throws IOException { |
261 return Utils.readAll(System.in); | 276 return Utils.readAll(System.in); |
277 } | |
278 | |
279 @Override public boolean exists() { | |
280 return true; | |
262 } | 281 } |
263 }; | 282 }; |
264 | 283 |
265 public static abstract class LuanIO extends LuanIn { | 284 public static abstract class LuanIO extends LuanIn { |
266 abstract OutputStream outputStream() throws IOException; | 285 abstract OutputStream outputStream() throws IOException; |
363 list.add(new LuanFile(luan,f).table()); | 382 list.add(new LuanFile(luan,f).table()); |
364 } | 383 } |
365 return list; | 384 return list; |
366 } | 385 } |
367 | 386 |
368 public boolean exists() { | 387 @Override public boolean exists() { |
369 return file.exists(); | 388 return file.exists(); |
370 } | 389 } |
371 | 390 |
372 @Override LuanTable table() { | 391 @Override LuanTable table() { |
373 LuanTable tbl = super.table(); | 392 LuanTable tbl = super.table(); |
374 try { | 393 try { |
375 tbl.put( "name", new LuanJavaFunction( | 394 tbl.put( "name", new LuanJavaFunction( |
376 File.class.getMethod( "getName" ), file | 395 File.class.getMethod( "getName" ), file |
377 ) ); | 396 ) ); |
378 tbl.put( "exists", new LuanJavaFunction( | |
379 LuanFile.class.getMethod( "exists" ), this | |
380 ) ); | |
381 tbl.put( "is_directory", new LuanJavaFunction( | 397 tbl.put( "is_directory", new LuanJavaFunction( |
382 File.class.getMethod( "isDirectory" ), file | 398 File.class.getMethod( "isDirectory" ), file |
383 ) ); | 399 ) ); |
384 tbl.put( "is_file", new LuanJavaFunction( | 400 tbl.put( "is_file", new LuanJavaFunction( |
385 File.class.getMethod( "isFile" ), file | 401 File.class.getMethod( "isFile" ), file |
411 | 427 |
412 public static LuanTable file(LuanState luan,String name,Boolean loading) throws LuanException { | 428 public static LuanTable file(LuanState luan,String name,Boolean loading) throws LuanException { |
413 if( Boolean.TRUE.equals(loading) ) | 429 if( Boolean.TRUE.equals(loading) ) |
414 name += ".luan"; | 430 name += ".luan"; |
415 File file = new File(name); | 431 File file = new File(name); |
416 if( !file.exists() ) | |
417 return null; | |
418 return new LuanFile(file).table(); | 432 return new LuanFile(file).table(); |
419 } | 433 } |
420 | 434 |
421 public static LuanTable classpath(LuanState luan,String name,Boolean loading) throws LuanException { | 435 public static LuanTable classpath(LuanState luan,String name,Boolean loading) throws LuanException { |
422 if( name.contains("//") ) | 436 if( name.contains("//") ) |
502 | 516 |
503 public static LuanTable luan(LuanState luan,String path,Boolean loading) throws LuanException { | 517 public static LuanTable luan(LuanState luan,String path,Boolean loading) throws LuanException { |
504 return classpath( luan, "luan/modules/" + path, loading ); | 518 return classpath( luan, "luan/modules/" + path, loading ); |
505 } | 519 } |
506 | 520 |
521 public static LuanTable stdin(LuanState luan) throws LuanException { | |
522 LuanTable io = (LuanTable)PackageLuan.loaded(luan).get("luan:Io"); | |
523 return (LuanTable)io.get("stdin"); | |
524 } | |
525 | |
507 private static LuanTable newProtocols() { | 526 private static LuanTable newProtocols() { |
508 LuanTable protocols = Luan.newTable(); | 527 LuanTable protocols = Luan.newTable(); |
509 try { | 528 try { |
510 add( protocols, "file", LuanState.class, String.class, Boolean.class ); | 529 add( protocols, "file", LuanState.class, String.class, Boolean.class ); |
511 add( protocols, "classpath", LuanState.class, String.class, Boolean.class ); | 530 add( protocols, "classpath", LuanState.class, String.class, Boolean.class ); |
514 add( protocols, "https", String.class, Boolean.class ); | 533 add( protocols, "https", String.class, Boolean.class ); |
515 protocols.put( "class", new LuanJavaFunction( | 534 protocols.put( "class", new LuanJavaFunction( |
516 IoLuan.class.getMethod( "_class", LuanState.class, String.class, Boolean.class ), null | 535 IoLuan.class.getMethod( "_class", LuanState.class, String.class, Boolean.class ), null |
517 ) ); | 536 ) ); |
518 add( protocols, "luan", LuanState.class, String.class, Boolean.class ); | 537 add( protocols, "luan", LuanState.class, String.class, Boolean.class ); |
538 add( protocols, "stdin", LuanState.class ); | |
519 } catch(NoSuchMethodException e) { | 539 } catch(NoSuchMethodException e) { |
520 throw new RuntimeException(e); | 540 throw new RuntimeException(e); |
521 } | 541 } |
522 return protocols; | 542 return protocols; |
523 } | 543 } |