comparison src/luan/lib/IoLib.java @ 145:90f38a5d0e0a

add Os.File git-svn-id: https://luan-java.googlecode.com/svn/trunk@146 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 15 Jun 2014 10:02:16 +0000
parents 2e92f0a6fcac
children cc3a0578edac
comparison
equal deleted inserted replaced
144:2e92f0a6fcac 145:90f38a5d0e0a
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", LuanState.class, String.class ); 40 add( module, "File", LuanState.class, String.class );
41 add( module, "read_console_line", String.class ); 41 add( module, "read_console_line", String.class );
42 42
43 LuanTable stdin = new LuanTable(); 43 LuanTable stdin = new LuanTable();
44 stdin.put( "read_text", new LuanJavaFunction( 44 stdin.put( "read_text", new LuanJavaFunction(
45 IoLib.class.getMethod( "stdin_read_text" ), null 45 IoLib.class.getMethod( "stdin_read_text" ), null
53 stdin.put( "read_blocks", new LuanJavaFunction( 53 stdin.put( "read_blocks", new LuanJavaFunction(
54 IoLib.class.getMethod( "stdin_read_blocks", Integer.class ), null 54 IoLib.class.getMethod( "stdin_read_blocks", Integer.class ), null
55 ) ); 55 ) );
56 module.put( "stdin", stdin ); 56 module.put( "stdin", stdin );
57 57
58 add( module, "socket", String.class, Integer.TYPE ); 58 add( module, "Socket", String.class, Integer.TYPE );
59 add( module, "socket_server", Integer.TYPE ); 59 add( module, "socket_server", Integer.TYPE );
60 } catch(NoSuchMethodException e) { 60 } catch(NoSuchMethodException e) {
61 throw new RuntimeException(e); 61 throw new RuntimeException(e);
62 } 62 }
63 module.put( "stdout", textWriter(System.out) ); 63 module.put( "stdout", textWriter(System.out) );
328 328
329 public static final class LuanFile extends LuanIO { 329 public static final class LuanFile extends LuanIO {
330 private final File file; 330 private final File file;
331 331
332 public LuanFile(String name) { 332 public LuanFile(String name) {
333 this.file = new File(name); 333 this(new File(name));
334 }
335
336 public LuanFile(File file) {
337 this.file = file;
334 } 338 }
335 339
336 @Override InputStream inputStream() throws IOException { 340 @Override InputStream inputStream() throws IOException {
337 return new FileInputStream(file); 341 return new FileInputStream(file);
338 } 342 }
341 return new FileOutputStream(file); 345 return new FileOutputStream(file);
342 } 346 }
343 347
344 @Override public String name() { 348 @Override public String name() {
345 return file.toString(); 349 return file.toString();
350 }
351
352 public LuanTable os_file() {
353 return new OsLib.LuanFile(file).table();
354 }
355
356 @Override LuanTable table() {
357 LuanTable tbl = super.table();
358 try {
359 tbl.put( "os_file", new LuanJavaFunction(
360 LuanFile.class.getMethod( "os_file" ), this
361 ) );
362 } catch(NoSuchMethodException e) {
363 throw new RuntimeException(e);
364 }
365 return tbl;
346 } 366 }
347 } 367 }
348 368
349 public static LuanIn luanIo(LuanState luan,String name) throws LuanException { 369 public static LuanIn luanIo(LuanState luan,String name) throws LuanException {
350 if( Utils.isFile(name) ) 370 if( Utils.isFile(name) )
357 } catch(MalformedURLException e) { 377 } catch(MalformedURLException e) {
358 throw new RuntimeException(e); 378 throw new RuntimeException(e);
359 } 379 }
360 } 380 }
361 381
362 public static LuanTable file(LuanState luan,String name) throws LuanException { 382 public static LuanTable File(LuanState luan,String name) throws LuanException {
363 return luanIo(luan,name).table(); 383 return luanIo(luan,name).table();
364 } 384 }
365 385
366 public static final class LuanSocket extends LuanIO { 386 public static final class LuanSocket extends LuanIO {
367 private final Socket socket; 387 private final Socket socket;
384 404
385 @Override public String name() { 405 @Override public String name() {
386 return socket.toString(); 406 return socket.toString();
387 } 407 }
388 408
389 public LuanTable pickle_client(LuanState luan) throws IOException { 409 public LuanTable Pickle_client(LuanState luan) throws IOException {
390 DataInputStream in = new DataInputStream(new BufferedInputStream(inputStream())); 410 DataInputStream in = new DataInputStream(new BufferedInputStream(inputStream()));
391 DataOutputStream out = new DataOutputStream(new BufferedOutputStream(outputStream())); 411 DataOutputStream out = new DataOutputStream(new BufferedOutputStream(outputStream()));
392 return new PickleClient(luan,in,out).table(); 412 return new PickleClient(luan,in,out).table();
393 } 413 }
394 414
399 } 419 }
400 420
401 @Override LuanTable table() { 421 @Override LuanTable table() {
402 LuanTable tbl = super.table(); 422 LuanTable tbl = super.table();
403 try { 423 try {
404 tbl.put( "pickle_client", new LuanJavaFunction( 424 tbl.put( "Pickle_client", new LuanJavaFunction(
405 LuanSocket.class.getMethod( "pickle_client", LuanState.class ), this 425 LuanSocket.class.getMethod( "Pickle_client", LuanState.class ), this
406 ) ); 426 ) );
407 tbl.put( "run_pickle_server", new LuanJavaFunction( 427 tbl.put( "run_pickle_server", new LuanJavaFunction(
408 LuanSocket.class.getMethod( "run_pickle_server", LuanState.class ), this 428 LuanSocket.class.getMethod( "run_pickle_server", LuanState.class ), this
409 ) ); 429 ) );
410 } catch(NoSuchMethodException e) { 430 } catch(NoSuchMethodException e) {
412 } 432 }
413 return tbl; 433 return tbl;
414 } 434 }
415 } 435 }
416 436
417 public static LuanTable socket(String host,int port) throws IOException { 437 public static LuanTable Socket(String host,int port) throws IOException {
418 return new LuanSocket(host,port).table(); 438 return new LuanSocket(host,port).table();
419 } 439 }
420 440
421 public static LuanFunction socket_server(int port) throws IOException { 441 public static LuanFunction socket_server(int port) throws IOException {
422 final ServerSocket ss = new ServerSocket(port); 442 final ServerSocket ss = new ServerSocket(port);