comparison core/src/luan/modules/IoLuan.java @ 646:cdc70de628b5

simplify LuanException
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 29 Mar 2016 19:58:39 -0600
parents 8bd98da6991a
children ca169567ce07
comparison
equal deleted inserted replaced
645:859c0dedc8b6 646:cdc70de628b5
120 return new LuanFunction() { 120 return new LuanFunction() {
121 @Override public Object call(LuanState luan,Object[] args) throws LuanException { 121 @Override public Object call(LuanState luan,Object[] args) throws LuanException {
122 try { 122 try {
123 if( args.length > 0 ) { 123 if( args.length > 0 ) {
124 if( args.length > 1 || !"close".equals(args[0]) ) 124 if( args.length > 1 || !"close".equals(args[0]) )
125 throw new LuanException(luan, "the only argument allowed is 'close'" ); 125 throw new LuanException( "the only argument allowed is 'close'" );
126 in.close(); 126 in.close();
127 return null; 127 return null;
128 } 128 }
129 String rtn = in.readLine(); 129 String rtn = in.readLine();
130 if( rtn==null ) 130 if( rtn==null )
131 in.close(); 131 in.close();
132 return rtn; 132 return rtn;
133 } catch(IOException e) { 133 } catch(IOException e) {
134 throw new LuanException(luan,e); 134 throw new LuanException(e);
135 } 135 }
136 } 136 }
137 }; 137 };
138 } 138 }
139 139
143 143
144 @Override public Object call(LuanState luan,Object[] args) throws LuanException { 144 @Override public Object call(LuanState luan,Object[] args) throws LuanException {
145 try { 145 try {
146 if( args.length > 0 ) { 146 if( args.length > 0 ) {
147 if( args.length > 1 || !"close".equals(args[0]) ) 147 if( args.length > 1 || !"close".equals(args[0]) )
148 throw new LuanException(luan, "the only argument allowed is 'close'" ); 148 throw new LuanException( "the only argument allowed is 'close'" );
149 in.close(); 149 in.close();
150 return null; 150 return null;
151 } 151 }
152 if( in.read(a) == -1 ) { 152 if( in.read(a) == -1 ) {
153 in.close(); 153 in.close();
154 return null; 154 return null;
155 } 155 }
156 return a; 156 return a;
157 } catch(IOException e) { 157 } catch(IOException e) {
158 throw new LuanException(luan,e); 158 throw new LuanException(e);
159 } 159 }
160 } 160 }
161 }; 161 };
162 } 162 }
163 163
256 }; 256 };
257 257
258 public static abstract class LuanIO extends LuanIn { 258 public static abstract class LuanIO extends LuanIn {
259 abstract OutputStream outputStream() throws IOException; 259 abstract OutputStream outputStream() throws IOException;
260 260
261 public void write(LuanState luan,Object obj) throws LuanException, IOException { 261 public void write(Object obj) throws LuanException, IOException {
262 if( obj instanceof String ) { 262 if( obj instanceof String ) {
263 String s = (String)obj; 263 String s = (String)obj;
264 Writer out = new OutputStreamWriter(outputStream()); 264 Writer out = new OutputStreamWriter(outputStream());
265 out.write(s); 265 out.write(s);
266 out.close(); 266 out.close();
271 OutputStream out = outputStream(); 271 OutputStream out = outputStream();
272 Utils.copyAll(new ByteArrayInputStream(a),out); 272 Utils.copyAll(new ByteArrayInputStream(a),out);
273 out.close(); 273 out.close();
274 return; 274 return;
275 } 275 }
276 throw new LuanException(luan, "bad argument #1 to 'write' (string or binary expected)" ); 276 throw new LuanException( "bad argument #1 to 'write' (string or binary expected)" );
277 } 277 }
278 278
279 public LuanTable text_writer() throws IOException { 279 public LuanTable text_writer() throws IOException {
280 return textWriter(new BufferedWriter(new OutputStreamWriter(outputStream()))); 280 return textWriter(new BufferedWriter(new OutputStreamWriter(outputStream())));
281 } 281 }
286 286
287 @Override public LuanTable table() { 287 @Override public LuanTable table() {
288 LuanTable tbl = super.table(); 288 LuanTable tbl = super.table();
289 try { 289 try {
290 tbl.rawPut( "write", new LuanJavaFunction( 290 tbl.rawPut( "write", new LuanJavaFunction(
291 LuanIO.class.getMethod( "write", LuanState.class, Object.class ), this 291 LuanIO.class.getMethod( "write", Object.class ), this
292 ) ); 292 ) );
293 tbl.rawPut( "text_writer", new LuanJavaFunction( 293 tbl.rawPut( "text_writer", new LuanJavaFunction(
294 LuanIO.class.getMethod( "text_writer" ), this 294 LuanIO.class.getMethod( "text_writer" ), this
295 ) ); 295 ) );
296 tbl.rawPut( "binary_writer", new LuanJavaFunction( 296 tbl.rawPut( "binary_writer", new LuanJavaFunction(
514 514
515 public static LuanTable null_() { 515 public static LuanTable null_() {
516 return nullIO.table(); 516 return nullIO.table();
517 } 517 }
518 518
519 public static LuanTable string(LuanState luan,String s) throws LuanException { 519 public static LuanTable string(String s) throws LuanException {
520 Utils.checkNotNull(luan,s); 520 Utils.checkNotNull(s);
521 return new LuanString(s).table(); 521 return new LuanString(s).table();
522 } 522 }
523 523
524 public static LuanTable file(LuanState luan,String name,Boolean addExtension) throws LuanException { 524 public static LuanTable file(LuanState luan,String name,Boolean addExtension) throws LuanException {
525 if( addExtension != null && addExtension ) 525 if( addExtension != null && addExtension )
586 586
587 public static LuanTable newSchemes() { 587 public static LuanTable newSchemes() {
588 LuanTable schemes = new LuanTable(); 588 LuanTable schemes = new LuanTable();
589 try { 589 try {
590 schemes.rawPut( "null", new LuanJavaFunction(IoLuan.class.getMethod("null_"),null) ); 590 schemes.rawPut( "null", new LuanJavaFunction(IoLuan.class.getMethod("null_"),null) );
591 add( schemes, "string", LuanState.class, String.class ); 591 add( schemes, "string", String.class );
592 add( schemes, "file", LuanState.class, String.class, Boolean.class ); 592 add( schemes, "file", LuanState.class, String.class, Boolean.class );
593 add( schemes, "classpath", LuanState.class, String.class, Boolean.class ); 593 add( schemes, "classpath", LuanState.class, String.class, Boolean.class );
594 add( schemes, "socket", LuanState.class, String.class ); 594 add( schemes, "socket", String.class );
595 add( schemes, "http", String.class, Boolean.class ); 595 add( schemes, "http", String.class, Boolean.class );
596 add( schemes, "https", String.class, Boolean.class ); 596 add( schemes, "https", String.class, Boolean.class );
597 add( schemes, "luan", LuanState.class, String.class, Boolean.class ); 597 add( schemes, "luan", LuanState.class, String.class, Boolean.class );
598 add( schemes, "stdin", LuanState.class ); 598 add( schemes, "stdin", LuanState.class );
599 } catch(NoSuchMethodException e) { 599 } catch(NoSuchMethodException e) {
613 } 613 }
614 614
615 public static LuanTable uri(LuanState luan,String name,Boolean addExtension) throws LuanException { 615 public static LuanTable uri(LuanState luan,String name,Boolean addExtension) throws LuanException {
616 int i = name.indexOf(':'); 616 int i = name.indexOf(':');
617 if( i == -1 ) 617 if( i == -1 )
618 throw new LuanException(luan, "invalid Io.uri name '"+name+"', missing scheme" ); 618 throw new LuanException( "invalid Io.uri name '"+name+"', missing scheme" );
619 String scheme = name.substring(0,i); 619 String scheme = name.substring(0,i);
620 String location = name.substring(i+1); 620 String location = name.substring(i+1);
621 LuanTable schemes = schemes(luan); 621 LuanTable schemes = schemes(luan);
622 LuanFunction opener = (LuanFunction)schemes.get(luan,scheme); 622 LuanFunction opener = (LuanFunction)schemes.get(luan,scheme);
623 if( opener == null ) 623 if( opener == null )
624 throw new LuanException(luan, "invalid scheme '"+scheme+"' in '"+name+"'" ); 624 throw new LuanException( "invalid scheme '"+scheme+"' in '"+name+"'" );
625 return (LuanTable)Luan.first(opener.call(luan,new Object[]{location,addExtension})); 625 return (LuanTable)Luan.first(opener.call(luan,new Object[]{location,addExtension}));
626 } 626 }
627 627
628 public static final class LuanSocket extends LuanIO { 628 public static final class LuanSocket extends LuanIO {
629 private final Socket socket; 629 private final Socket socket;
647 @Override public String to_string() { 647 @Override public String to_string() {
648 return socket.toString(); 648 return socket.toString();
649 } 649 }
650 } 650 }
651 651
652 public static LuanTable socket(LuanState luan,String name) throws LuanException, IOException { 652 public static LuanTable socket(String name) throws LuanException, IOException {
653 int i = name.indexOf(':'); 653 int i = name.indexOf(':');
654 if( i == -1 ) 654 if( i == -1 )
655 throw new LuanException(luan, "invalid socket '"+name+"', format is: <host>:<port>" ); 655 throw new LuanException( "invalid socket '"+name+"', format is: <host>:<port>" );
656 String host = name.substring(0,i); 656 String host = name.substring(0,i);
657 String portStr = name.substring(i+1); 657 String portStr = name.substring(i+1);
658 int port = Integer.parseInt(portStr); 658 int port = Integer.parseInt(portStr);
659 return new LuanSocket(host,port).table(); 659 return new LuanSocket(host,port).table();
660 } 660 }
664 return new LuanFunction() { 664 return new LuanFunction() {
665 @Override public Object call(LuanState luan,Object[] args) throws LuanException { 665 @Override public Object call(LuanState luan,Object[] args) throws LuanException {
666 try { 666 try {
667 if( args.length > 0 ) { 667 if( args.length > 0 ) {
668 if( args.length > 1 || !"close".equals(args[0]) ) 668 if( args.length > 1 || !"close".equals(args[0]) )
669 throw new LuanException(luan, "the only argument allowed is 'close'" ); 669 throw new LuanException( "the only argument allowed is 'close'" );
670 ss.close(); 670 ss.close();
671 return null; 671 return null;
672 } 672 }
673 return new LuanSocket(ss.accept()).table(); 673 return new LuanSocket(ss.accept()).table();
674 } catch(IOException e) { 674 } catch(IOException e) {
675 throw new LuanException(luan,e); 675 throw new LuanException(e);
676 } 676 }
677 } 677 }
678 }; 678 };
679 } 679 }
680 680