comparison src/luan/lib/IoLib.java @ 115:eacf6ce1b47d

add IoLib git-svn-id: https://luan-java.googlecode.com/svn/trunk@116 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Thu, 29 May 2014 09:26:44 +0000
parents
children 1ff1c32417eb
comparison
equal deleted inserted replaced
114:c599206448b9 115:eacf6ce1b47d
1 package luan.lib;
2
3 import java.io.OutputStream;
4 import java.io.PrintStream;
5 import java.io.Reader;
6 import java.io.Writer;
7 import java.io.FileReader;
8 import java.io.FileWriter;
9 import java.io.BufferedReader;
10 import java.io.File;
11 import java.io.FileOutputStream;
12 import java.io.InputStreamReader;
13 import java.io.IOException;
14 import luan.LuanState;
15 import luan.LuanTable;
16 import luan.LuanFunction;
17 import luan.LuanJavaFunction;
18 import luan.LuanException;
19
20
21 public final class IoLib {
22
23 public static final String NAME = "Io";
24
25 public static final LuanFunction LOADER = new LuanFunction() {
26 @Override public Object call(LuanState luan,Object[] args) {
27 LuanTable module = new LuanTable();
28 try {
29 add( module, "file", String.class );
30
31 LuanTable stdin = new LuanTable();
32 stdin.put( "read_text", new LuanJavaFunction(
33 IoLib.class.getMethod( "stdin_read_text" ), null
34 ) );
35 stdin.put( "read_binary", new LuanJavaFunction(
36 IoLib.class.getMethod( "stdin_read_binary" ), null
37 ) );
38 stdin.put( "read_lines", new LuanJavaFunction(
39 IoLib.class.getMethod( "stdin_read_lines" ), null
40 ) );
41 module.put( "stdin", stdin );
42 } catch(NoSuchMethodException e) {
43 throw new RuntimeException(e);
44 }
45 module.put( "stdout", writer(System.out) );
46 module.put( "stderr", writer(System.err) );
47 return module;
48 }
49 };
50
51 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException {
52 t.put( method, new LuanJavaFunction(IoLib.class.getMethod(method,parameterTypes),null) );
53 }
54
55 public static final class LuanFile {
56 private final File file;
57
58 private LuanFile(String name) {
59 this.file = new File(name);
60 }
61
62 public String read_text() throws IOException {
63 return Utils.read(file);
64 }
65
66 public byte[] read_binary() throws IOException {
67 return Utils.readAll(file);
68 }
69
70 public void write(LuanState luan,Object obj) throws LuanException, IOException {
71 if( obj instanceof String ) {
72 String s = (String)obj;
73 Utils.write(file,s);
74 return;
75 }
76 if( obj instanceof byte[] ) {
77 byte[] a = (byte[])obj;
78 Utils.writeAll(file,a);
79 return;
80 }
81 throw luan.JAVA.exception( "bad argument #1 to 'write' (string or binary expected)" );
82 }
83
84 public LuanTable text_writer() throws IOException {
85 return writer(new FileWriter(file));
86 }
87
88 public LuanTable binary_writer() throws IOException {
89 return binaryWriter(new FileOutputStream(file));
90 }
91
92 public LuanFunction read_lines() throws IOException {
93 return lines(new BufferedReader(new FileReader(file)));
94 }
95 }
96
97 public static LuanTable file(String name) {
98 LuanTable tbl = new LuanTable();
99 LuanFile file = new LuanFile(name);
100 try {
101 tbl.put( "read_text", new LuanJavaFunction(
102 LuanFile.class.getMethod( "read_text" ), file
103 ) );
104 tbl.put( "read_binary", new LuanJavaFunction(
105 LuanFile.class.getMethod( "read_binary" ), file
106 ) );
107 tbl.put( "write", new LuanJavaFunction(
108 LuanFile.class.getMethod( "write", LuanState.class, Object.class ), file
109 ) );
110 tbl.put( "text_writer", new LuanJavaFunction(
111 LuanFile.class.getMethod( "text_writer" ), file
112 ) );
113 tbl.put( "binary_writer", new LuanJavaFunction(
114 LuanFile.class.getMethod( "binary_writer" ), file
115 ) );
116 tbl.put( "read_lines", new LuanJavaFunction(
117 LuanFile.class.getMethod( "read_lines" ), file
118 ) );
119 } catch(NoSuchMethodException e) {
120 throw new RuntimeException(e);
121 }
122 return tbl;
123 }
124
125
126 public String stdin_read_text() throws IOException {
127 return Utils.readAll(new InputStreamReader(System.in));
128 }
129
130 public byte[] stdin_read_binary() throws IOException {
131 return Utils.readAll(System.in);
132 }
133
134 public LuanFunction stdin_read_lines() throws IOException {
135 return lines(new BufferedReader(new InputStreamReader(System.in)));
136 }
137
138
139 public interface LuanWriter {
140 public void write(LuanState luan,Object... args) throws LuanException, IOException;
141 public void close() throws IOException;
142 }
143
144 public static LuanTable writer(final PrintStream out) {
145 LuanWriter luanWriter = new LuanWriter() {
146
147 public void write(LuanState luan,Object... args) throws LuanException {
148 for( Object obj : args ) {
149 out.print( luan.JAVA.toString(obj) );
150 }
151 }
152
153 public void close() {
154 out.close();
155 }
156 };
157 return writer(luanWriter);
158 }
159
160 public static LuanTable writer(final Writer out) {
161 LuanWriter luanWriter = new LuanWriter() {
162
163 public void write(LuanState luan,Object... args) throws LuanException, IOException {
164 for( Object obj : args ) {
165 out.write( luan.JAVA.toString(obj) );
166 }
167 }
168
169 public void close() throws IOException {
170 out.close();
171 }
172 };
173 return writer(luanWriter);
174 }
175
176 private static LuanTable writer(LuanWriter luanWriter) {
177 LuanTable writer = new LuanTable();
178 try {
179 writer.put( "write", new LuanJavaFunction(
180 LuanWriter.class.getMethod( "write", LuanState.class, new Object[0].getClass() ), luanWriter
181 ) );
182 writer.put( "close", new LuanJavaFunction(
183 LuanWriter.class.getMethod( "close" ), luanWriter
184 ) );
185 } catch(NoSuchMethodException e) {
186 throw new RuntimeException(e);
187 }
188 return writer;
189 }
190
191
192 public static LuanTable binaryWriter(final OutputStream out) {
193 LuanTable writer = new LuanTable();
194 try {
195 writer.put( "write", new LuanJavaFunction(
196 OutputStream.class.getMethod( "write", new byte[0].getClass() ), out
197 ) );
198 writer.put( "close", new LuanJavaFunction(
199 OutputStream.class.getMethod( "close" ), out
200 ) );
201 } catch(NoSuchMethodException e) {
202 throw new RuntimeException(e);
203 }
204 return writer;
205 }
206
207 public static LuanFunction lines(final BufferedReader in) {
208 return new LuanFunction() {
209 public Object call(LuanState luan,Object[] args) throws LuanException {
210 try {
211 if( args.length==1 && "close".equals(args[0]) ) {
212 in.close();
213 return null;
214 }
215 String rtn = in.readLine();
216 if( rtn==null )
217 in.close();
218 return rtn;
219 } catch(IOException e) {
220 throw luan.JAVA.exception(e);
221 }
222 }
223 };
224 }
225
226 }