Mercurial Hosting > luan
diff src/luan/lib/IoLib.java @ 116:1ff1c32417eb
more IoLib work and added init.luan
git-svn-id: https://luan-java.googlecode.com/svn/trunk@117 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Fri, 30 May 2014 04:55:37 +0000 |
parents | eacf6ce1b47d |
children | 735708619119 |
line wrap: on
line diff
--- a/src/luan/lib/IoLib.java Thu May 29 09:26:44 2014 +0000 +++ b/src/luan/lib/IoLib.java Fri May 30 04:55:37 2014 +0000 @@ -1,5 +1,6 @@ package luan.lib; +import java.io.InputStream; import java.io.OutputStream; import java.io.PrintStream; import java.io.Reader; @@ -8,9 +9,12 @@ import java.io.FileWriter; import java.io.BufferedReader; import java.io.File; +import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.IOException; +import java.net.URL; +import java.net.MalformedURLException; import luan.LuanState; import luan.LuanTable; import luan.LuanFunction; @@ -27,6 +31,9 @@ LuanTable module = new LuanTable(); try { add( module, "file", String.class ); + add( module, "java_resource_to_url", String.class ); + add( module, "url", String.class ); + add( module, "java_resource", String.class ); LuanTable stdin = new LuanTable(); stdin.put( "read_text", new LuanJavaFunction( @@ -38,6 +45,9 @@ stdin.put( "read_lines", new LuanJavaFunction( IoLib.class.getMethod( "stdin_read_lines" ), null ) ); + stdin.put( "read_blocks", new LuanJavaFunction( + IoLib.class.getMethod( "stdin_read_blocks", Integer.class ), null + ) ); module.put( "stdin", stdin ); } catch(NoSuchMethodException e) { throw new RuntimeException(e); @@ -92,6 +102,11 @@ public LuanFunction read_lines() throws IOException { return lines(new BufferedReader(new FileReader(file))); } + + public LuanFunction read_blocks(Integer blockSize) throws IOException { + int n = blockSize!=null ? blockSize : Utils.bufSize; + return blocks(new FileInputStream(file),n); + } } public static LuanTable file(String name) { @@ -116,6 +131,9 @@ tbl.put( "read_lines", new LuanJavaFunction( LuanFile.class.getMethod( "read_lines" ), file ) ); + tbl.put( "read_blocks", new LuanJavaFunction( + LuanFile.class.getMethod( "read_blocks", Integer.class ), file + ) ); } catch(NoSuchMethodException e) { throw new RuntimeException(e); } @@ -123,18 +141,87 @@ } - public String stdin_read_text() throws IOException { + public static final class LuanUrl { + private final URL url; + + LuanUrl(String s) throws MalformedURLException { + this.url = new URL(s); + } + + public String read_text() throws IOException { + Reader in = new InputStreamReader(url.openStream()); + String s = Utils.readAll(in); + in.close(); + return s; + } + + public byte[] read_binary() throws IOException { + InputStream in = url.openStream(); + byte[] a = Utils.readAll(in); + in.close(); + return a; + } + + public LuanFunction read_lines() throws IOException { + return lines(new BufferedReader(new InputStreamReader(url.openStream()))); + } + + public LuanFunction read_blocks(Integer blockSize) throws IOException { + int n = blockSize!=null ? blockSize : Utils.bufSize; + return blocks(url.openStream(),n); + } + } + + public static LuanTable url(String s) throws MalformedURLException { + LuanTable tbl = new LuanTable(); + LuanUrl url = new LuanUrl(s); + try { + tbl.put( "read_text", new LuanJavaFunction( + LuanUrl.class.getMethod( "read_text" ), url + ) ); + tbl.put( "read_binary", new LuanJavaFunction( + LuanUrl.class.getMethod( "read_binary" ), url + ) ); + tbl.put( "read_lines", new LuanJavaFunction( + LuanUrl.class.getMethod( "read_lines" ), url + ) ); + tbl.put( "read_blocks", new LuanJavaFunction( + LuanUrl.class.getMethod( "read_blocks", Integer.class ), url + ) ); + } catch(NoSuchMethodException e) { + throw new RuntimeException(e); + } + return tbl; + } + + + + public static String stdin_read_text() throws IOException { return Utils.readAll(new InputStreamReader(System.in)); } - public byte[] stdin_read_binary() throws IOException { + public static byte[] stdin_read_binary() throws IOException { return Utils.readAll(System.in); } - public LuanFunction stdin_read_lines() throws IOException { + public static LuanFunction stdin_read_lines() throws IOException { return lines(new BufferedReader(new InputStreamReader(System.in))); } + public static LuanFunction stdin_read_blocks(Integer blockSize) throws IOException { + int n = blockSize!=null ? blockSize : Utils.bufSize; + return blocks(System.in,n); + } + + public static String java_resource_to_url(String path) throws IOException { + URL url = ClassLoader.getSystemResource(path); + return url==null ? null : url.toString(); + } + + public static LuanTable java_resource(String path) throws IOException { + return url(java_resource_to_url(path)); + } + public interface LuanWriter { public void write(LuanState luan,Object... args) throws LuanException, IOException; @@ -204,11 +291,13 @@ return writer; } - public static LuanFunction lines(final BufferedReader in) { + static LuanFunction lines(final BufferedReader in) { return new LuanFunction() { - public Object call(LuanState luan,Object[] args) throws LuanException { + @Override public Object call(LuanState luan,Object[] args) throws LuanException { try { - if( args.length==1 && "close".equals(args[0]) ) { + if( args.length > 0 ) { + if( args.length > 1 || !"close".equals(args[0]) ) + throw luan.JAVA.exception( "the only argument allowed is 'close'" ); in.close(); return null; } @@ -223,4 +312,28 @@ }; } + static LuanFunction blocks(final InputStream in,final int blockSize) { + return new LuanFunction() { + final byte[] a = new byte[blockSize]; + + @Override public Object call(LuanState luan,Object[] args) throws LuanException { + try { + if( args.length > 0 ) { + if( args.length > 1 || !"close".equals(args[0]) ) + throw luan.JAVA.exception( "the only argument allowed is 'close'" ); + in.close(); + return null; + } + if( in.read(a) == -1 ) { + in.close(); + return null; + } + return a; + } catch(IOException e) { + throw luan.JAVA.exception(e); + } + } + }; + } + }