comparison src/luan/lib/IoLib.java @ 125:0cd559a16758

add sockets git-svn-id: https://luan-java.googlecode.com/svn/trunk@126 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 06 Jun 2014 05:59:11 +0000
parents f537ff5e511d
children 486a0641bca4
comparison
equal deleted inserted replaced
124:f537ff5e511d 125:0cd559a16758
6 import java.io.Reader; 6 import java.io.Reader;
7 import java.io.Writer; 7 import java.io.Writer;
8 import java.io.FileReader; 8 import java.io.FileReader;
9 import java.io.FileWriter; 9 import java.io.FileWriter;
10 import java.io.BufferedReader; 10 import java.io.BufferedReader;
11 import java.io.BufferedWriter;
12 import java.io.BufferedOutputStream;
11 import java.io.File; 13 import java.io.File;
12 import java.io.FileInputStream; 14 import java.io.FileInputStream;
13 import java.io.FileOutputStream; 15 import java.io.FileOutputStream;
14 import java.io.InputStreamReader; 16 import java.io.InputStreamReader;
17 import java.io.OutputStreamWriter;
18 import java.io.ByteArrayInputStream;
15 import java.io.IOException; 19 import java.io.IOException;
16 import java.net.URL; 20 import java.net.URL;
21 import java.net.Socket;
22 import java.net.ServerSocket;
17 import java.net.MalformedURLException; 23 import java.net.MalformedURLException;
18 import luan.LuanState; 24 import luan.LuanState;
19 import luan.LuanTable; 25 import luan.LuanTable;
20 import luan.LuanFunction; 26 import luan.LuanFunction;
21 import luan.LuanJavaFunction; 27 import luan.LuanJavaFunction;
50 ) ); 56 ) );
51 stdin.put( "read_blocks", new LuanJavaFunction( 57 stdin.put( "read_blocks", new LuanJavaFunction(
52 IoLib.class.getMethod( "stdin_read_blocks", Integer.class ), null 58 IoLib.class.getMethod( "stdin_read_blocks", Integer.class ), null
53 ) ); 59 ) );
54 module.put( "stdin", stdin ); 60 module.put( "stdin", stdin );
61
62 add( module, "socket", String.class, Integer.TYPE );
63 add( module, "socket_server", Integer.TYPE );
55 } catch(NoSuchMethodException e) { 64 } catch(NoSuchMethodException e) {
56 throw new RuntimeException(e); 65 throw new RuntimeException(e);
57 } 66 }
58 module.put( "stdout", textWriter(System.out) ); 67 module.put( "stdout", textWriter(System.out) );
59 module.put( "stderr", textWriter(System.err) ); 68 module.put( "stderr", textWriter(System.err) );
62 }; 71 };
63 72
64 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { 73 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException {
65 t.put( method, new LuanJavaFunction(IoLib.class.getMethod(method,parameterTypes),null) ); 74 t.put( method, new LuanJavaFunction(IoLib.class.getMethod(method,parameterTypes),null) );
66 } 75 }
67
68 public static final class LuanFile {
69 private final File file;
70
71 private LuanFile(String name) {
72 this.file = new File(name);
73 }
74
75 public String read_text() throws IOException {
76 return Utils.read(file);
77 }
78
79 public byte[] read_binary() throws IOException {
80 return Utils.readAll(file);
81 }
82
83 public void write(LuanState luan,Object obj) throws LuanException, IOException {
84 if( obj instanceof String ) {
85 String s = (String)obj;
86 Utils.write(file,s);
87 return;
88 }
89 if( obj instanceof byte[] ) {
90 byte[] a = (byte[])obj;
91 Utils.writeAll(file,a);
92 return;
93 }
94 throw luan.JAVA.exception( "bad argument #1 to 'write' (string or binary expected)" );
95 }
96
97 public LuanTable text_writer() throws IOException {
98 return textWriter(new FileWriter(file));
99 }
100
101 public LuanTable binary_writer() throws IOException {
102 return binaryWriter(new FileOutputStream(file));
103 }
104
105 public LuanFunction read_lines() throws IOException {
106 return lines(new BufferedReader(new FileReader(file)));
107 }
108
109 public LuanFunction read_blocks(Integer blockSize) throws IOException {
110 int n = blockSize!=null ? blockSize : Utils.bufSize;
111 return blocks(new FileInputStream(file),n);
112 }
113 }
114
115 public static LuanTable file(String name) {
116 LuanTable tbl = new LuanTable();
117 LuanFile file = new LuanFile(name);
118 try {
119 tbl.put( "read_text", new LuanJavaFunction(
120 LuanFile.class.getMethod( "read_text" ), file
121 ) );
122 tbl.put( "read_binary", new LuanJavaFunction(
123 LuanFile.class.getMethod( "read_binary" ), file
124 ) );
125 tbl.put( "write", new LuanJavaFunction(
126 LuanFile.class.getMethod( "write", LuanState.class, Object.class ), file
127 ) );
128 tbl.put( "text_writer", new LuanJavaFunction(
129 LuanFile.class.getMethod( "text_writer" ), file
130 ) );
131 tbl.put( "binary_writer", new LuanJavaFunction(
132 LuanFile.class.getMethod( "binary_writer" ), file
133 ) );
134 tbl.put( "read_lines", new LuanJavaFunction(
135 LuanFile.class.getMethod( "read_lines" ), file
136 ) );
137 tbl.put( "read_blocks", new LuanJavaFunction(
138 LuanFile.class.getMethod( "read_blocks", Integer.class ), file
139 ) );
140 } catch(NoSuchMethodException e) {
141 throw new RuntimeException(e);
142 }
143 return tbl;
144 }
145
146
147 public static final class LuanUrl {
148 private final URL url;
149
150 LuanUrl(String s) throws MalformedURLException {
151 this.url = new URL(s);
152 }
153
154 public String read_text() throws IOException {
155 Reader in = new InputStreamReader(url.openStream());
156 String s = Utils.readAll(in);
157 in.close();
158 return s;
159 }
160
161 public byte[] read_binary() throws IOException {
162 InputStream in = url.openStream();
163 byte[] a = Utils.readAll(in);
164 in.close();
165 return a;
166 }
167
168 public LuanFunction read_lines() throws IOException {
169 return lines(new BufferedReader(new InputStreamReader(url.openStream())));
170 }
171
172 public LuanFunction read_blocks(Integer blockSize) throws IOException {
173 int n = blockSize!=null ? blockSize : Utils.bufSize;
174 return blocks(url.openStream(),n);
175 }
176 }
177
178 public static LuanTable url(String s) throws MalformedURLException {
179 LuanTable tbl = new LuanTable();
180 LuanUrl url = new LuanUrl(s);
181 try {
182 tbl.put( "read_text", new LuanJavaFunction(
183 LuanUrl.class.getMethod( "read_text" ), url
184 ) );
185 tbl.put( "read_binary", new LuanJavaFunction(
186 LuanUrl.class.getMethod( "read_binary" ), url
187 ) );
188 tbl.put( "read_lines", new LuanJavaFunction(
189 LuanUrl.class.getMethod( "read_lines" ), url
190 ) );
191 tbl.put( "read_blocks", new LuanJavaFunction(
192 LuanUrl.class.getMethod( "read_blocks", Integer.class ), url
193 ) );
194 } catch(NoSuchMethodException e) {
195 throw new RuntimeException(e);
196 }
197 return tbl;
198 }
199
200 76
201 77
202 public static String stdin_read_text() throws IOException { 78 public static String stdin_read_text() throws IOException {
203 return Utils.readAll(new InputStreamReader(System.in)); 79 return Utils.readAll(new InputStreamReader(System.in));
204 } 80 }
343 } 219 }
344 } 220 }
345 }; 221 };
346 } 222 }
347 223
224
225
226 public static abstract class LuanIn {
227 abstract InputStream inputStream() throws IOException;
228
229 public String read_text() throws IOException {
230 Reader in = new InputStreamReader(inputStream());
231 String s = Utils.readAll(in);
232 in.close();
233 return s;
234 }
235
236 public byte[] read_binary() throws IOException {
237 InputStream in = inputStream();
238 byte[] a = Utils.readAll(in);
239 in.close();
240 return a;
241 }
242
243 public LuanFunction read_lines() throws IOException {
244 return lines(new BufferedReader(new InputStreamReader(inputStream())));
245 }
246
247 public LuanFunction read_blocks(Integer blockSize) throws IOException {
248 int n = blockSize!=null ? blockSize : Utils.bufSize;
249 return blocks(inputStream(),n);
250 }
251
252 LuanTable table() {
253 LuanTable tbl = new LuanTable();
254 try {
255 tbl.put( "read_text", new LuanJavaFunction(
256 LuanIn.class.getMethod( "read_text" ), this
257 ) );
258 tbl.put( "read_binary", new LuanJavaFunction(
259 LuanIn.class.getMethod( "read_binary" ), this
260 ) );
261 tbl.put( "read_lines", new LuanJavaFunction(
262 LuanIn.class.getMethod( "read_lines" ), this
263 ) );
264 tbl.put( "read_blocks", new LuanJavaFunction(
265 LuanIn.class.getMethod( "read_blocks", Integer.class ), this
266 ) );
267 } catch(NoSuchMethodException e) {
268 throw new RuntimeException(e);
269 }
270 return tbl;
271 }
272 }
273
274 public static abstract class LuanIO extends LuanIn {
275 abstract OutputStream outputStream() throws IOException;
276
277 public void write(LuanState luan,Object obj) throws LuanException, IOException {
278 if( obj instanceof String ) {
279 String s = (String)obj;
280 Writer out = new OutputStreamWriter(outputStream());
281 out.write(s);
282 out.close();
283 return;
284 }
285 if( obj instanceof byte[] ) {
286 byte[] a = (byte[])obj;
287 OutputStream out = outputStream();
288 Utils.copyAll(new ByteArrayInputStream(a),out);
289 out.close();
290 return;
291 }
292 throw luan.JAVA.exception( "bad argument #1 to 'write' (string or binary expected)" );
293 }
294
295 public LuanTable text_writer() throws IOException {
296 return textWriter(new BufferedWriter(new OutputStreamWriter(outputStream())));
297 }
298
299 public LuanTable binary_writer() throws IOException {
300 return binaryWriter(new BufferedOutputStream(outputStream()));
301 }
302
303 LuanTable table() {
304 LuanTable tbl = super.table();
305 try {
306 tbl.put( "write", new LuanJavaFunction(
307 LuanIO.class.getMethod( "write", LuanState.class, Object.class ), this
308 ) );
309 tbl.put( "text_writer", new LuanJavaFunction(
310 LuanIO.class.getMethod( "text_writer" ), this
311 ) );
312 tbl.put( "binary_writer", new LuanJavaFunction(
313 LuanIO.class.getMethod( "binary_writer" ), this
314 ) );
315 } catch(NoSuchMethodException e) {
316 throw new RuntimeException(e);
317 }
318 return tbl;
319 }
320 }
321
322 public static final class LuanUrl extends LuanIn {
323 private final URL url;
324
325 public LuanUrl(String s) throws MalformedURLException {
326 this.url = new URL(s);
327 }
328
329 InputStream inputStream() throws IOException {
330 return url.openStream();
331 }
332 }
333
334 public static LuanTable url(String s) throws MalformedURLException {
335 return new LuanUrl(s).table();
336 }
337
338 public static final class LuanFile extends LuanIO {
339 private final File file;
340
341 public LuanFile(String name) {
342 this.file = new File(name);
343 }
344
345 InputStream inputStream() throws IOException {
346 return new FileInputStream(file);
347 }
348
349 OutputStream outputStream() throws IOException {
350 return new FileOutputStream(file);
351 }
352 }
353
354 public static LuanTable file(String name) {
355 return new LuanFile(name).table();
356 }
357
358 public static final class LuanSocket extends LuanIO {
359 private final Socket socket;
360
361 public LuanSocket(String host,int port) throws IOException {
362 this(new Socket(host,port));
363 }
364
365 public LuanSocket(Socket socket) throws IOException {
366 this.socket = socket;
367 }
368
369 InputStream inputStream() throws IOException {
370 return socket.getInputStream();
371 }
372
373 OutputStream outputStream() throws IOException {
374 return socket.getOutputStream();
375 }
376 }
377
378 public static LuanTable socket(String host,int port) throws IOException {
379 return new LuanSocket(host,port).table();
380 }
381
382 public static LuanFunction socket_server(int port) throws IOException {
383 final ServerSocket ss = new ServerSocket(port);
384 return new LuanFunction() {
385 @Override public Object call(LuanState luan,Object[] args) throws LuanException {
386 try {
387 if( args.length > 0 ) {
388 if( args.length > 1 || !"close".equals(args[0]) )
389 throw luan.JAVA.exception( "the only argument allowed is 'close'" );
390 ss.close();
391 return null;
392 }
393 return new LuanSocket(ss.accept()).table();
394 } catch(IOException e) {
395 throw luan.JAVA.exception(e);
396 }
397 }
398 };
399 }
348 } 400 }