comparison src/luan/lib/IoLib.java @ 130:0594c132888b

cleanup git-svn-id: https://luan-java.googlecode.com/svn/trunk@131 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Tue, 10 Jun 2014 02:43:40 +0000
parents 486a0641bca4
children 3119326260ea
comparison
equal deleted inserted replaced
129:486a0641bca4 130:0594c132888b
119 public static LuanTable textWriter(final PrintStream out) { 119 public static LuanTable textWriter(final PrintStream out) {
120 LuanWriter luanWriter = new LuanWriter() { 120 LuanWriter luanWriter = new LuanWriter() {
121 121
122 public void write(LuanState luan,Object... args) throws LuanException { 122 public void write(LuanState luan,Object... args) throws LuanException {
123 for( Object obj : args ) { 123 for( Object obj : args ) {
124 out.print( luan.JAVA.toString(obj) ); 124 out.print( luan.toString(obj) );
125 } 125 }
126 } 126 }
127 127
128 public void close() { 128 public void close() {
129 out.close(); 129 out.close();
135 public static LuanTable textWriter(final Writer out) { 135 public static LuanTable textWriter(final Writer out) {
136 LuanWriter luanWriter = new LuanWriter() { 136 LuanWriter luanWriter = new LuanWriter() {
137 137
138 public void write(LuanState luan,Object... args) throws LuanException, IOException { 138 public void write(LuanState luan,Object... args) throws LuanException, IOException {
139 for( Object obj : args ) { 139 for( Object obj : args ) {
140 out.write( luan.JAVA.toString(obj) ); 140 out.write( luan.toString(obj) );
141 } 141 }
142 } 142 }
143 143
144 public void close() throws IOException { 144 public void close() throws IOException {
145 out.close(); 145 out.close();
183 return new LuanFunction() { 183 return new LuanFunction() {
184 @Override public Object call(LuanState luan,Object[] args) throws LuanException { 184 @Override public Object call(LuanState luan,Object[] args) throws LuanException {
185 try { 185 try {
186 if( args.length > 0 ) { 186 if( args.length > 0 ) {
187 if( args.length > 1 || !"close".equals(args[0]) ) 187 if( args.length > 1 || !"close".equals(args[0]) )
188 throw luan.JAVA.exception( "the only argument allowed is 'close'" ); 188 throw luan.exception( "the only argument allowed is 'close'" );
189 in.close(); 189 in.close();
190 return null; 190 return null;
191 } 191 }
192 String rtn = in.readLine(); 192 String rtn = in.readLine();
193 if( rtn==null ) 193 if( rtn==null )
194 in.close(); 194 in.close();
195 return rtn; 195 return rtn;
196 } catch(IOException e) { 196 } catch(IOException e) {
197 throw luan.JAVA.exception(e); 197 throw luan.exception(e);
198 } 198 }
199 } 199 }
200 }; 200 };
201 } 201 }
202 202
206 206
207 @Override public Object call(LuanState luan,Object[] args) throws LuanException { 207 @Override public Object call(LuanState luan,Object[] args) throws LuanException {
208 try { 208 try {
209 if( args.length > 0 ) { 209 if( args.length > 0 ) {
210 if( args.length > 1 || !"close".equals(args[0]) ) 210 if( args.length > 1 || !"close".equals(args[0]) )
211 throw luan.JAVA.exception( "the only argument allowed is 'close'" ); 211 throw luan.exception( "the only argument allowed is 'close'" );
212 in.close(); 212 in.close();
213 return null; 213 return null;
214 } 214 }
215 if( in.read(a) == -1 ) { 215 if( in.read(a) == -1 ) {
216 in.close(); 216 in.close();
217 return null; 217 return null;
218 } 218 }
219 return a; 219 return a;
220 } catch(IOException e) { 220 } catch(IOException e) {
221 throw luan.JAVA.exception(e); 221 throw luan.exception(e);
222 } 222 }
223 } 223 }
224 }; 224 };
225 } 225 }
226 226
290 OutputStream out = outputStream(); 290 OutputStream out = outputStream();
291 Utils.copyAll(new ByteArrayInputStream(a),out); 291 Utils.copyAll(new ByteArrayInputStream(a),out);
292 out.close(); 292 out.close();
293 return; 293 return;
294 } 294 }
295 throw luan.JAVA.exception( "bad argument #1 to 'write' (string or binary expected)" ); 295 throw luan.exception( "bad argument #1 to 'write' (string or binary expected)" );
296 } 296 }
297 297
298 public LuanTable text_writer() throws IOException { 298 public LuanTable text_writer() throws IOException {
299 return textWriter(new BufferedWriter(new OutputStreamWriter(outputStream()))); 299 return textWriter(new BufferedWriter(new OutputStreamWriter(outputStream())));
300 } 300 }
414 return new LuanFunction() { 414 return new LuanFunction() {
415 @Override public Object call(LuanState luan,Object[] args) throws LuanException { 415 @Override public Object call(LuanState luan,Object[] args) throws LuanException {
416 try { 416 try {
417 if( args.length > 0 ) { 417 if( args.length > 0 ) {
418 if( args.length > 1 || !"close".equals(args[0]) ) 418 if( args.length > 1 || !"close".equals(args[0]) )
419 throw luan.JAVA.exception( "the only argument allowed is 'close'" ); 419 throw luan.exception( "the only argument allowed is 'close'" );
420 ss.close(); 420 ss.close();
421 return null; 421 return null;
422 } 422 }
423 return new LuanSocket(ss.accept()).table(); 423 return new LuanSocket(ss.accept()).table();
424 } catch(IOException e) { 424 } catch(IOException e) {
425 throw luan.JAVA.exception(e); 425 throw luan.exception(e);
426 } 426 }
427 } 427 }
428 }; 428 };
429 } 429 }
430 } 430 }