Mercurial Hosting > luan
comparison core/src/luan/modules/IoLuan.java @ 261:715c4a6e1169
simplify Io.stdin
git-svn-id: https://luan-java.googlecode.com/svn/trunk@262 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Tue, 28 Oct 2014 23:36:29 +0000 |
parents | f1f7d8c7e94e |
children | 3ccc5205d04d |
comparison
equal
deleted
inserted
replaced
260:f1f7d8c7e94e | 261:715c4a6e1169 |
---|---|
42 | 42 |
43 module.put( "protocols", newProtocols() ); | 43 module.put( "protocols", newProtocols() ); |
44 | 44 |
45 add( module, "get", LuanState.class, String.class ); | 45 add( module, "get", LuanState.class, String.class ); |
46 | 46 |
47 LuanTable stdin = Luan.newTable(); | 47 module.put( "stdin", stdin.table() ); |
48 stdin.put( "read_text", new LuanJavaFunction( | |
49 IoLuan.class.getMethod( "stdin_read_text" ), null | |
50 ) ); | |
51 stdin.put( "read_binary", new LuanJavaFunction( | |
52 IoLuan.class.getMethod( "stdin_read_binary" ), null | |
53 ) ); | |
54 stdin.put( "read_lines", new LuanJavaFunction( | |
55 IoLuan.class.getMethod( "stdin_read_lines" ), null | |
56 ) ); | |
57 stdin.put( "read_blocks", new LuanJavaFunction( | |
58 IoLuan.class.getMethod( "stdin_read_blocks", Integer.class ), null | |
59 ) ); | |
60 module.put( "stdin", stdin ); | |
61 | 48 |
62 add( module, "Socket", String.class, Integer.TYPE ); | 49 add( module, "Socket", String.class, Integer.TYPE ); |
63 add( module, "socket_server", Integer.TYPE ); | 50 add( module, "socket_server", Integer.TYPE ); |
64 } catch(NoSuchMethodException e) { | 51 } catch(NoSuchMethodException e) { |
65 throw new RuntimeException(e); | 52 throw new RuntimeException(e); |
72 | 59 |
73 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { | 60 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { |
74 t.put( method, new LuanJavaFunction(IoLuan.class.getMethod(method,parameterTypes),null) ); | 61 t.put( method, new LuanJavaFunction(IoLuan.class.getMethod(method,parameterTypes),null) ); |
75 } | 62 } |
76 | 63 |
77 | |
78 public static String stdin_read_text() throws IOException { | |
79 return Utils.readAll(new InputStreamReader(System.in)); | |
80 } | |
81 | |
82 public static byte[] stdin_read_binary() throws IOException { | |
83 return Utils.readAll(System.in); | |
84 } | |
85 | |
86 public static LuanFunction stdin_read_lines() throws IOException { | |
87 return lines(new BufferedReader(new InputStreamReader(System.in))); | |
88 } | |
89 | |
90 public static LuanFunction stdin_read_blocks(Integer blockSize) throws IOException { | |
91 int n = blockSize!=null ? blockSize : Utils.bufSize; | |
92 return blocks(System.in,n); | |
93 } | |
94 | 64 |
95 public static String read_console_line(String prompt) throws IOException { | 65 public static String read_console_line(String prompt) throws IOException { |
96 if( prompt==null ) | 66 if( prompt==null ) |
97 prompt = "> "; | 67 prompt = "> "; |
98 return System.console().readLine(prompt); | 68 return System.console().readLine(prompt); |
263 throw new RuntimeException(e); | 233 throw new RuntimeException(e); |
264 } | 234 } |
265 return tbl; | 235 return tbl; |
266 } | 236 } |
267 } | 237 } |
238 | |
239 private static final LuanIn stdin = new LuanIn() { | |
240 | |
241 @Override InputStream inputStream() { | |
242 return System.in; | |
243 } | |
244 | |
245 @Override public String to_string() { | |
246 return "<stdin>"; | |
247 } | |
248 | |
249 @Override public String read_text() throws IOException { | |
250 return Utils.readAll(new InputStreamReader(System.in)); | |
251 } | |
252 | |
253 @Override public byte[] read_binary() throws IOException { | |
254 return Utils.readAll(System.in); | |
255 } | |
256 }; | |
268 | 257 |
269 public static abstract class LuanIO extends LuanIn { | 258 public static abstract class LuanIO extends LuanIn { |
270 abstract OutputStream outputStream() throws IOException; | 259 abstract OutputStream outputStream() throws IOException; |
271 | 260 |
272 public void write(LuanState luan,Object obj) throws LuanException, IOException { | 261 public void write(LuanState luan,Object obj) throws LuanException, IOException { |