comparison core/src/luan/modules/IoLuan.java @ 578:60c549d43988

remove LuanState.exception()
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 14 Jul 2015 17:40:48 -0600
parents 7c3ad6db8ac3
children f050c30952c0
comparison
equal deleted inserted replaced
577:d7a85fbe15f1 578:60c549d43988
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 luan.exception( "the only argument allowed is 'close'" ); 125 throw new LuanException(luan, "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 luan.exception(e); 134 throw new LuanException(luan,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 luan.exception( "the only argument allowed is 'close'" ); 148 throw new LuanException(luan, "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 luan.exception(e); 158 throw new LuanException(luan,e);
159 } 159 }
160 } 160 }
161 }; 161 };
162 } 162 }
163 163
270 OutputStream out = outputStream(); 270 OutputStream out = outputStream();
271 Utils.copyAll(new ByteArrayInputStream(a),out); 271 Utils.copyAll(new ByteArrayInputStream(a),out);
272 out.close(); 272 out.close();
273 return; 273 return;
274 } 274 }
275 throw luan.exception( "bad argument #1 to 'write' (string or binary expected)" ); 275 throw new LuanException(luan, "bad argument #1 to 'write' (string or binary expected)" );
276 } 276 }
277 277
278 public LuanTable text_writer() throws IOException { 278 public LuanTable text_writer() throws IOException {
279 return textWriter(new BufferedWriter(new OutputStreamWriter(outputStream()))); 279 return textWriter(new BufferedWriter(new OutputStreamWriter(outputStream())));
280 } 280 }
583 } 583 }
584 584
585 public static LuanTable uri(LuanState luan,String name,Boolean addExtension) throws LuanException { 585 public static LuanTable uri(LuanState luan,String name,Boolean addExtension) throws LuanException {
586 int i = name.indexOf(':'); 586 int i = name.indexOf(':');
587 if( i == -1 ) 587 if( i == -1 )
588 throw luan.exception( "invalid Io.uri name '"+name+"', missing scheme" ); 588 throw new LuanException(luan, "invalid Io.uri name '"+name+"', missing scheme" );
589 String scheme = name.substring(0,i); 589 String scheme = name.substring(0,i);
590 String location = name.substring(i+1); 590 String location = name.substring(i+1);
591 LuanTable schemes = schemes(luan); 591 LuanTable schemes = schemes(luan);
592 LuanFunction opener = (LuanFunction)schemes.get(luan,scheme); 592 LuanFunction opener = (LuanFunction)schemes.get(luan,scheme);
593 if( opener == null ) 593 if( opener == null )
594 throw luan.exception( "invalid scheme '"+scheme+"' in '"+name+"'" ); 594 throw new LuanException(luan, "invalid scheme '"+scheme+"' in '"+name+"'" );
595 return (LuanTable)Luan.first(opener.call(luan,new Object[]{location,addExtension})); 595 return (LuanTable)Luan.first(opener.call(luan,new Object[]{location,addExtension}));
596 } 596 }
597 597
598 public static final class LuanSocket extends LuanIO { 598 public static final class LuanSocket extends LuanIO {
599 private final Socket socket; 599 private final Socket socket;
647 } 647 }
648 648
649 public static LuanTable socket(LuanState luan,String name) throws LuanException, IOException { 649 public static LuanTable socket(LuanState luan,String name) throws LuanException, IOException {
650 int i = name.indexOf(':'); 650 int i = name.indexOf(':');
651 if( i == -1 ) 651 if( i == -1 )
652 throw luan.exception( "invalid socket '"+name+"', format is: <host>:<port>" ); 652 throw new LuanException(luan, "invalid socket '"+name+"', format is: <host>:<port>" );
653 String host = name.substring(0,i); 653 String host = name.substring(0,i);
654 String portStr = name.substring(i+1); 654 String portStr = name.substring(i+1);
655 int port = Integer.parseInt(portStr); 655 int port = Integer.parseInt(portStr);
656 return new LuanSocket(host,port).table(); 656 return new LuanSocket(host,port).table();
657 } 657 }
661 return new LuanFunction() { 661 return new LuanFunction() {
662 @Override public Object call(LuanState luan,Object[] args) throws LuanException { 662 @Override public Object call(LuanState luan,Object[] args) throws LuanException {
663 try { 663 try {
664 if( args.length > 0 ) { 664 if( args.length > 0 ) {
665 if( args.length > 1 || !"close".equals(args[0]) ) 665 if( args.length > 1 || !"close".equals(args[0]) )
666 throw luan.exception( "the only argument allowed is 'close'" ); 666 throw new LuanException(luan, "the only argument allowed is 'close'" );
667 ss.close(); 667 ss.close();
668 return null; 668 return null;
669 } 669 }
670 return new LuanSocket(ss.accept()).table(); 670 return new LuanSocket(ss.accept()).table();
671 } catch(IOException e) { 671 } catch(IOException e) {
672 throw luan.exception(e); 672 throw new LuanException(luan,e);
673 } 673 }
674 } 674 }
675 }; 675 };
676 } 676 }
677 677