comparison src/luan/modules/IoLuan.java @ 1335:e0cf0d108a77

major cleanup
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 14 Feb 2019 03:10:45 -0700
parents c88b486a9511
children 0cceff521abb
comparison
equal deleted inserted replaced
1334:c88b486a9511 1335:e0cf0d108a77
87 } 87 }
88 }; 88 };
89 } 89 }
90 90
91 static LuanFunction lines(final BufferedReader in) { 91 static LuanFunction lines(final BufferedReader in) {
92 return new LuanFunction() { 92 return new LuanFunction(false) {
93 @Override public Object call(Luan luan,Object[] args) throws LuanException { 93 @Override public Object call(Object[] args) throws LuanException {
94 try { 94 try {
95 if( args.length > 0 ) { 95 if( args.length > 0 ) {
96 if( args.length > 1 || !"close".equals(args[0]) ) 96 if( args.length > 1 || !"close".equals(args[0]) )
97 throw new LuanException( "the only argument allowed is 'close'" ); 97 throw new LuanException( "the only argument allowed is 'close'" );
98 in.close(); 98 in.close();
108 } 108 }
109 }; 109 };
110 } 110 }
111 111
112 static LuanFunction blocks(final InputStream in,final int blockSize) { 112 static LuanFunction blocks(final InputStream in,final int blockSize) {
113 return new LuanFunction() { 113 return new LuanFunction(false) {
114 final byte[] a = new byte[blockSize]; 114 final byte[] a = new byte[blockSize];
115 115
116 @Override public Object call(Luan luan,Object[] args) throws LuanException { 116 @Override public Object call(Object[] args) throws LuanException {
117 try { 117 try {
118 if( args.length > 0 ) { 118 if( args.length > 0 ) {
119 if( args.length > 1 || !"close".equals(args[0]) ) 119 if( args.length > 1 || !"close".equals(args[0]) )
120 throw new LuanException( "the only argument allowed is 'close'" ); 120 throw new LuanException( "the only argument allowed is 'close'" );
121 in.close(); 121 in.close();
151 151
152 152
153 public static abstract class LuanIn { 153 public static abstract class LuanIn {
154 protected String charset = null; 154 protected String charset = null;
155 155
156 public abstract InputStream inputStream(Luan luan) throws IOException, LuanException; 156 public abstract InputStream inputStream() throws IOException, LuanException;
157 public abstract String to_string(); 157 public abstract String to_string();
158 public abstract String to_uri_string(); 158 public abstract String to_uri_string();
159 159
160 public Reader reader(Luan luan) throws IOException, LuanException { 160 public Reader reader() throws IOException, LuanException {
161 InputStream in = inputStream(luan); 161 InputStream in = inputStream();
162 return charset==null ? new InputStreamReader(in) : new InputStreamReader(in,charset); 162 return charset==null ? new InputStreamReader(in) : new InputStreamReader(in,charset);
163 } 163 }
164 164
165 public String read_text(Luan luan) throws IOException, LuanException { 165 public String read_text() throws IOException, LuanException {
166 Reader in = reader(luan); 166 Reader in = reader();
167 String s = Utils.readAll(in); 167 String s = Utils.readAll(in);
168 in.close(); 168 in.close();
169 return s; 169 return s;
170 } 170 }
171 171
172 public byte[] read_binary(Luan luan) throws IOException, LuanException { 172 public byte[] read_binary() throws IOException, LuanException {
173 InputStream in = inputStream(luan); 173 InputStream in = inputStream();
174 byte[] a = Utils.readAll(in); 174 byte[] a = Utils.readAll(in);
175 in.close(); 175 in.close();
176 return a; 176 return a;
177 } 177 }
178 178
179 public LuanFunction read_lines(Luan luan) throws IOException, LuanException { 179 public LuanFunction read_lines() throws IOException, LuanException {
180 return lines(new BufferedReader(reader(luan))); 180 return lines(new BufferedReader(reader()));
181 } 181 }
182 182
183 public LuanFunction read_blocks(Luan luan,Integer blockSize) throws IOException, LuanException { 183 public LuanFunction read_blocks(Integer blockSize) throws IOException, LuanException {
184 int n = blockSize!=null ? blockSize : Utils.bufSize; 184 int n = blockSize!=null ? blockSize : Utils.bufSize;
185 return blocks(inputStream(luan),n); 185 return blocks(inputStream(),n);
186 } 186 }
187 187
188 public boolean exists(Luan luan) throws IOException, LuanException { 188 public boolean exists() throws IOException, LuanException {
189 try { 189 try {
190 inputStream(luan).close(); 190 inputStream().close();
191 return true; 191 return true;
192 } catch(FileNotFoundException e) { 192 } catch(FileNotFoundException e) {
193 return false; 193 return false;
194 } catch(UnknownHostException e) { 194 } catch(UnknownHostException e) {
195 return false; 195 return false;
196 } 196 }
197 } 197 }
198 198
199 public long checksum(Luan luan) throws IOException, LuanException { 199 public long checksum() throws IOException, LuanException {
200 long cs = 0; 200 long cs = 0;
201 InputStream in = new BufferedInputStream(inputStream(luan)); 201 InputStream in = new BufferedInputStream(inputStream());
202 int c; 202 int c;
203 while( (c=in.read()) != -1 ) { 203 while( (c=in.read()) != -1 ) {
204 cs = 31 * cs + c; 204 cs = 31 * cs + c;
205 } 205 }
206 in.close(); 206 in.close();
216 } 216 }
217 } 217 }
218 218
219 public static final LuanIn defaultStdin = new LuanIn() { 219 public static final LuanIn defaultStdin = new LuanIn() {
220 220
221 @Override public InputStream inputStream(Luan luan) { 221 @Override public InputStream inputStream() {
222 return System.in; 222 return System.in;
223 } 223 }
224 224
225 @Override public String to_string() { 225 @Override public String to_string() {
226 return "<stdin>"; 226 return "<stdin>";
228 228
229 @Override public String to_uri_string() { 229 @Override public String to_uri_string() {
230 return "stdin:"; 230 return "stdin:";
231 } 231 }
232 232
233 @Override public String read_text(Luan luan) throws IOException { 233 @Override public String read_text() throws IOException {
234 return Utils.readAll(new InputStreamReader(System.in)); 234 return Utils.readAll(new InputStreamReader(System.in));
235 } 235 }
236 236
237 @Override public byte[] read_binary(Luan luan) throws IOException { 237 @Override public byte[] read_binary() throws IOException {
238 return Utils.readAll(System.in); 238 return Utils.readAll(System.in);
239 } 239 }
240 240
241 @Override public boolean exists(Luan luan) { 241 @Override public boolean exists() {
242 return true; 242 return true;
243 } 243 }
244 }; 244 };
245 245
246 public static abstract class LuanIO extends LuanIn { 246 public static abstract class LuanIO extends LuanIn {
249 private Writer writer() throws IOException { 249 private Writer writer() throws IOException {
250 OutputStream out = outputStream(); 250 OutputStream out = outputStream();
251 return charset==null ? new OutputStreamWriter(out) : new OutputStreamWriter(out,charset); 251 return charset==null ? new OutputStreamWriter(out) : new OutputStreamWriter(out,charset);
252 } 252 }
253 253
254 public void write(Luan luan,Object obj) throws LuanException, IOException { 254 public void write(Object obj) throws LuanException, IOException {
255 if( obj instanceof String ) { 255 if( obj instanceof String ) {
256 String s = (String)obj; 256 String s = (String)obj;
257 Writer out = writer(); 257 Writer out = writer();
258 out.write(s); 258 out.write(s);
259 out.close(); 259 out.close();
269 if( obj instanceof LuanTable ) { 269 if( obj instanceof LuanTable ) {
270 LuanTable t = (LuanTable)obj; 270 LuanTable t = (LuanTable)obj;
271 Object java = t.rawGet("java"); 271 Object java = t.rawGet("java");
272 if( java instanceof LuanIn ) { 272 if( java instanceof LuanIn ) {
273 LuanIn luanIn = (LuanIn)java; 273 LuanIn luanIn = (LuanIn)java;
274 InputStream in = luanIn.inputStream(luan); 274 InputStream in = luanIn.inputStream();
275 OutputStream out = outputStream(); 275 OutputStream out = outputStream();
276 Utils.copyAll(in,out); 276 Utils.copyAll(in,out);
277 out.close(); 277 out.close();
278 in.close(); 278 in.close();
279 return; 279 return;
288 288
289 public OutputStream binary_writer() throws IOException { 289 public OutputStream binary_writer() throws IOException {
290 return new BufferedOutputStream(outputStream()); 290 return new BufferedOutputStream(outputStream());
291 } 291 }
292 292
293 public void write_text(Luan luan,Object... args) throws LuanException, IOException { 293 public void write_text(Object... args) throws LuanException, IOException {
294 LuanWriter luanWriter = luanWriter(new BufferedWriter(writer())); 294 LuanWriter luanWriter = luanWriter(new BufferedWriter(writer()));
295 luanWriter.write(luan,args); 295 luanWriter.write(args);
296 luanWriter.close(); 296 luanWriter.close();
297 } 297 }
298 } 298 }
299 299
300 public static final LuanIO nullIO = new LuanIO() { 300 public static final LuanIO nullIO = new LuanIO() {
305 }; 305 };
306 private final OutputStream out = new OutputStream() { 306 private final OutputStream out = new OutputStream() {
307 @Override public void write(int b) {} 307 @Override public void write(int b) {}
308 }; 308 };
309 309
310 @Override public InputStream inputStream(Luan luan) { 310 @Override public InputStream inputStream() {
311 return in; 311 return in;
312 } 312 }
313 313
314 @Override OutputStream outputStream() { 314 @Override OutputStream outputStream() {
315 return out; 315 return out;
331 public LuanString(String s) throws LuanException { 331 public LuanString(String s) throws LuanException {
332 Utils.checkNotNull(s); 332 Utils.checkNotNull(s);
333 this.s = s; 333 this.s = s;
334 } 334 }
335 335
336 @Override public InputStream inputStream(Luan luan) { 336 @Override public InputStream inputStream() {
337 throw new UnsupportedOperationException(); 337 throw new UnsupportedOperationException();
338 } 338 }
339 339
340 @Override OutputStream outputStream() { 340 @Override OutputStream outputStream() {
341 throw new UnsupportedOperationException(); 341 throw new UnsupportedOperationException();
347 347
348 @Override public String to_uri_string() { 348 @Override public String to_uri_string() {
349 return "string:" + s; 349 return "string:" + s;
350 } 350 }
351 351
352 @Override public Reader reader(Luan luan) { 352 @Override public Reader reader() {
353 return new StringReader(s); 353 return new StringReader(s);
354 } 354 }
355 355
356 @Override public String read_text(Luan luan) { 356 @Override public String read_text() {
357 return s; 357 return s;
358 } 358 }
359 359
360 @Override public boolean exists(Luan luan) { 360 @Override public boolean exists() {
361 return true; 361 return true;
362 } 362 }
363 363
364 @Override public LuanWriter text_writer() { 364 @Override public LuanWriter text_writer() {
365 return new LuanWriter() { 365 return new LuanWriter() {
396 396
397 private LuanFile(File file) { 397 private LuanFile(File file) {
398 this.file = file; 398 this.file = file;
399 } 399 }
400 400
401 @Override public InputStream inputStream(Luan luan) throws IOException { 401 @Override public InputStream inputStream() throws IOException {
402 return new FileInputStream(file); 402 return new FileInputStream(file);
403 } 403 }
404 404
405 @Override OutputStream outputStream() throws IOException { 405 @Override OutputStream outputStream() throws IOException {
406 return new FileOutputStream(file); 406 return new FileOutputStream(file);
434 if( parent==null ) 434 if( parent==null )
435 parent = file.getCanonicalFile().getParentFile(); 435 parent = file.getCanonicalFile().getParentFile();
436 return new LuanFile(luan,parent); 436 return new LuanFile(luan,parent);
437 } 437 }
438 438
439 @Override public boolean exists(Luan luan) { 439 @Override public boolean exists() {
440 return file.exists(); 440 return file.exists();
441 } 441 }
442 442
443 public void rename_to(Object destObj) throws LuanException { 443 public void rename_to(Object destObj) throws LuanException {
444 File dest = objToFile(destObj); 444 File dest = objToFile(destObj);
544 throw new LuanException( "unrecognized options: "+map ); 544 throw new LuanException( "unrecognized options: "+map );
545 } 545 }
546 this.dir = dir; 546 this.dir = dir;
547 } 547 }
548 548
549 @Override public InputStream inputStream(Luan luan) throws IOException { 549 @Override public InputStream inputStream() throws IOException {
550 return proc.getInputStream(); 550 return proc.getInputStream();
551 } 551 }
552 552
553 @Override OutputStream outputStream() throws IOException { 553 @Override OutputStream outputStream() throws IOException {
554 return proc.getOutputStream(); 554 return proc.getOutputStream();
560 560
561 @Override public String to_uri_string() { 561 @Override public String to_uri_string() {
562 throw new UnsupportedOperationException(); 562 throw new UnsupportedOperationException();
563 } 563 }
564 564
565 @Override public boolean exists(Luan luan) { 565 @Override public boolean exists() {
566 return true; 566 return true;
567 } 567 }
568 568
569 public void wait_for() 569 public void wait_for()
570 throws IOException, LuanException 570 throws IOException, LuanException
581 err.close(); 581 err.close();
582 throw new LuanException(error); 582 throw new LuanException(error);
583 } 583 }
584 } 584 }
585 585
586 @Override public String read_text(Luan luan) throws IOException, LuanException { 586 @Override public String read_text() throws IOException, LuanException {
587 String s = super.read_text(luan); 587 String s = super.read_text();
588 wait_for(); 588 wait_for();
589 return s; 589 return s;
590 } 590 }
591 } 591 }
592 592
612 612
613 public LuanInput(InputStream in) { 613 public LuanInput(InputStream in) {
614 this.in = in; 614 this.in = in;
615 } 615 }
616 616
617 @Override public InputStream inputStream(Luan luan) { 617 @Override public InputStream inputStream() {
618 return in; 618 return in;
619 } 619 }
620 620
621 @Override public String to_string() { 621 @Override public String to_string() {
622 return "<input_stream>"; 622 return "<input_stream>";
624 624
625 @Override public String to_uri_string() { 625 @Override public String to_uri_string() {
626 throw new UnsupportedOperationException(); 626 throw new UnsupportedOperationException();
627 } 627 }
628 628
629 @Override public boolean exists(Luan luan) { 629 @Override public boolean exists() {
630 return true; 630 return true;
631 } 631 }
632 }; 632 };
633 633
634 634