comparison 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
comparison
equal deleted inserted replaced
115:eacf6ce1b47d 116:1ff1c32417eb
1 package luan.lib; 1 package luan.lib;
2 2
3 import java.io.InputStream;
3 import java.io.OutputStream; 4 import java.io.OutputStream;
4 import java.io.PrintStream; 5 import java.io.PrintStream;
5 import java.io.Reader; 6 import java.io.Reader;
6 import java.io.Writer; 7 import java.io.Writer;
7 import java.io.FileReader; 8 import java.io.FileReader;
8 import java.io.FileWriter; 9 import java.io.FileWriter;
9 import java.io.BufferedReader; 10 import java.io.BufferedReader;
10 import java.io.File; 11 import java.io.File;
12 import java.io.FileInputStream;
11 import java.io.FileOutputStream; 13 import java.io.FileOutputStream;
12 import java.io.InputStreamReader; 14 import java.io.InputStreamReader;
13 import java.io.IOException; 15 import java.io.IOException;
16 import java.net.URL;
17 import java.net.MalformedURLException;
14 import luan.LuanState; 18 import luan.LuanState;
15 import luan.LuanTable; 19 import luan.LuanTable;
16 import luan.LuanFunction; 20 import luan.LuanFunction;
17 import luan.LuanJavaFunction; 21 import luan.LuanJavaFunction;
18 import luan.LuanException; 22 import luan.LuanException;
25 public static final LuanFunction LOADER = new LuanFunction() { 29 public static final LuanFunction LOADER = new LuanFunction() {
26 @Override public Object call(LuanState luan,Object[] args) { 30 @Override public Object call(LuanState luan,Object[] args) {
27 LuanTable module = new LuanTable(); 31 LuanTable module = new LuanTable();
28 try { 32 try {
29 add( module, "file", String.class ); 33 add( module, "file", String.class );
34 add( module, "java_resource_to_url", String.class );
35 add( module, "url", String.class );
36 add( module, "java_resource", String.class );
30 37
31 LuanTable stdin = new LuanTable(); 38 LuanTable stdin = new LuanTable();
32 stdin.put( "read_text", new LuanJavaFunction( 39 stdin.put( "read_text", new LuanJavaFunction(
33 IoLib.class.getMethod( "stdin_read_text" ), null 40 IoLib.class.getMethod( "stdin_read_text" ), null
34 ) ); 41 ) );
36 IoLib.class.getMethod( "stdin_read_binary" ), null 43 IoLib.class.getMethod( "stdin_read_binary" ), null
37 ) ); 44 ) );
38 stdin.put( "read_lines", new LuanJavaFunction( 45 stdin.put( "read_lines", new LuanJavaFunction(
39 IoLib.class.getMethod( "stdin_read_lines" ), null 46 IoLib.class.getMethod( "stdin_read_lines" ), null
40 ) ); 47 ) );
48 stdin.put( "read_blocks", new LuanJavaFunction(
49 IoLib.class.getMethod( "stdin_read_blocks", Integer.class ), null
50 ) );
41 module.put( "stdin", stdin ); 51 module.put( "stdin", stdin );
42 } catch(NoSuchMethodException e) { 52 } catch(NoSuchMethodException e) {
43 throw new RuntimeException(e); 53 throw new RuntimeException(e);
44 } 54 }
45 module.put( "stdout", writer(System.out) ); 55 module.put( "stdout", writer(System.out) );
89 return binaryWriter(new FileOutputStream(file)); 99 return binaryWriter(new FileOutputStream(file));
90 } 100 }
91 101
92 public LuanFunction read_lines() throws IOException { 102 public LuanFunction read_lines() throws IOException {
93 return lines(new BufferedReader(new FileReader(file))); 103 return lines(new BufferedReader(new FileReader(file)));
104 }
105
106 public LuanFunction read_blocks(Integer blockSize) throws IOException {
107 int n = blockSize!=null ? blockSize : Utils.bufSize;
108 return blocks(new FileInputStream(file),n);
94 } 109 }
95 } 110 }
96 111
97 public static LuanTable file(String name) { 112 public static LuanTable file(String name) {
98 LuanTable tbl = new LuanTable(); 113 LuanTable tbl = new LuanTable();
114 LuanFile.class.getMethod( "binary_writer" ), file 129 LuanFile.class.getMethod( "binary_writer" ), file
115 ) ); 130 ) );
116 tbl.put( "read_lines", new LuanJavaFunction( 131 tbl.put( "read_lines", new LuanJavaFunction(
117 LuanFile.class.getMethod( "read_lines" ), file 132 LuanFile.class.getMethod( "read_lines" ), file
118 ) ); 133 ) );
134 tbl.put( "read_blocks", new LuanJavaFunction(
135 LuanFile.class.getMethod( "read_blocks", Integer.class ), file
136 ) );
119 } catch(NoSuchMethodException e) { 137 } catch(NoSuchMethodException e) {
120 throw new RuntimeException(e); 138 throw new RuntimeException(e);
121 } 139 }
122 return tbl; 140 return tbl;
123 } 141 }
124 142
125 143
126 public String stdin_read_text() throws IOException { 144 public static final class LuanUrl {
145 private final URL url;
146
147 LuanUrl(String s) throws MalformedURLException {
148 this.url = new URL(s);
149 }
150
151 public String read_text() throws IOException {
152 Reader in = new InputStreamReader(url.openStream());
153 String s = Utils.readAll(in);
154 in.close();
155 return s;
156 }
157
158 public byte[] read_binary() throws IOException {
159 InputStream in = url.openStream();
160 byte[] a = Utils.readAll(in);
161 in.close();
162 return a;
163 }
164
165 public LuanFunction read_lines() throws IOException {
166 return lines(new BufferedReader(new InputStreamReader(url.openStream())));
167 }
168
169 public LuanFunction read_blocks(Integer blockSize) throws IOException {
170 int n = blockSize!=null ? blockSize : Utils.bufSize;
171 return blocks(url.openStream(),n);
172 }
173 }
174
175 public static LuanTable url(String s) throws MalformedURLException {
176 LuanTable tbl = new LuanTable();
177 LuanUrl url = new LuanUrl(s);
178 try {
179 tbl.put( "read_text", new LuanJavaFunction(
180 LuanUrl.class.getMethod( "read_text" ), url
181 ) );
182 tbl.put( "read_binary", new LuanJavaFunction(
183 LuanUrl.class.getMethod( "read_binary" ), url
184 ) );
185 tbl.put( "read_lines", new LuanJavaFunction(
186 LuanUrl.class.getMethod( "read_lines" ), url
187 ) );
188 tbl.put( "read_blocks", new LuanJavaFunction(
189 LuanUrl.class.getMethod( "read_blocks", Integer.class ), url
190 ) );
191 } catch(NoSuchMethodException e) {
192 throw new RuntimeException(e);
193 }
194 return tbl;
195 }
196
197
198
199 public static String stdin_read_text() throws IOException {
127 return Utils.readAll(new InputStreamReader(System.in)); 200 return Utils.readAll(new InputStreamReader(System.in));
128 } 201 }
129 202
130 public byte[] stdin_read_binary() throws IOException { 203 public static byte[] stdin_read_binary() throws IOException {
131 return Utils.readAll(System.in); 204 return Utils.readAll(System.in);
132 } 205 }
133 206
134 public LuanFunction stdin_read_lines() throws IOException { 207 public static LuanFunction stdin_read_lines() throws IOException {
135 return lines(new BufferedReader(new InputStreamReader(System.in))); 208 return lines(new BufferedReader(new InputStreamReader(System.in)));
209 }
210
211 public static LuanFunction stdin_read_blocks(Integer blockSize) throws IOException {
212 int n = blockSize!=null ? blockSize : Utils.bufSize;
213 return blocks(System.in,n);
214 }
215
216 public static String java_resource_to_url(String path) throws IOException {
217 URL url = ClassLoader.getSystemResource(path);
218 return url==null ? null : url.toString();
219 }
220
221 public static LuanTable java_resource(String path) throws IOException {
222 return url(java_resource_to_url(path));
136 } 223 }
137 224
138 225
139 public interface LuanWriter { 226 public interface LuanWriter {
140 public void write(LuanState luan,Object... args) throws LuanException, IOException; 227 public void write(LuanState luan,Object... args) throws LuanException, IOException;
202 throw new RuntimeException(e); 289 throw new RuntimeException(e);
203 } 290 }
204 return writer; 291 return writer;
205 } 292 }
206 293
207 public static LuanFunction lines(final BufferedReader in) { 294 static LuanFunction lines(final BufferedReader in) {
208 return new LuanFunction() { 295 return new LuanFunction() {
209 public Object call(LuanState luan,Object[] args) throws LuanException { 296 @Override public Object call(LuanState luan,Object[] args) throws LuanException {
210 try { 297 try {
211 if( args.length==1 && "close".equals(args[0]) ) { 298 if( args.length > 0 ) {
299 if( args.length > 1 || !"close".equals(args[0]) )
300 throw luan.JAVA.exception( "the only argument allowed is 'close'" );
212 in.close(); 301 in.close();
213 return null; 302 return null;
214 } 303 }
215 String rtn = in.readLine(); 304 String rtn = in.readLine();
216 if( rtn==null ) 305 if( rtn==null )
221 } 310 }
222 } 311 }
223 }; 312 };
224 } 313 }
225 314
315 static LuanFunction blocks(final InputStream in,final int blockSize) {
316 return new LuanFunction() {
317 final byte[] a = new byte[blockSize];
318
319 @Override public Object call(LuanState luan,Object[] args) throws LuanException {
320 try {
321 if( args.length > 0 ) {
322 if( args.length > 1 || !"close".equals(args[0]) )
323 throw luan.JAVA.exception( "the only argument allowed is 'close'" );
324 in.close();
325 return null;
326 }
327 if( in.read(a) == -1 ) {
328 in.close();
329 return null;
330 }
331 return a;
332 } catch(IOException e) {
333 throw luan.JAVA.exception(e);
334 }
335 }
336 };
337 }
338
226 } 339 }