Mercurial Hosting > luan
comparison core/src/luan/modules/IoLuan.java @ 314:7c4f52262213
add Io.schemes.string
git-svn-id: https://luan-java.googlecode.com/svn/trunk@315 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Thu, 25 Dec 2014 07:23:32 +0000 |
parents | 9e9bb358b2df |
children | cd2924a1052c |
comparison
equal
deleted
inserted
replaced
313:1f6d828986b9 | 314:7c4f52262213 |
---|---|
3 import java.io.InputStream; | 3 import java.io.InputStream; |
4 import java.io.OutputStream; | 4 import java.io.OutputStream; |
5 import java.io.PrintStream; | 5 import java.io.PrintStream; |
6 import java.io.Reader; | 6 import java.io.Reader; |
7 import java.io.Writer; | 7 import java.io.Writer; |
8 import java.io.StringReader; | |
8 import java.io.BufferedReader; | 9 import java.io.BufferedReader; |
9 import java.io.BufferedWriter; | 10 import java.io.BufferedWriter; |
10 import java.io.BufferedInputStream; | 11 import java.io.BufferedInputStream; |
11 import java.io.BufferedOutputStream; | 12 import java.io.BufferedOutputStream; |
12 import java.io.File; | 13 import java.io.File; |
163 | 164 |
164 public static abstract class LuanIn { | 165 public static abstract class LuanIn { |
165 abstract InputStream inputStream() throws IOException; | 166 abstract InputStream inputStream() throws IOException; |
166 public abstract String to_string(); | 167 public abstract String to_string(); |
167 | 168 |
169 public Reader reader() throws IOException { | |
170 return new InputStreamReader(inputStream()); | |
171 } | |
172 | |
168 public String read_text() throws IOException { | 173 public String read_text() throws IOException { |
169 Reader in = new InputStreamReader(inputStream()); | 174 Reader in = reader(); |
170 String s = Utils.readAll(in); | 175 String s = Utils.readAll(in); |
171 in.close(); | 176 in.close(); |
172 return s; | 177 return s; |
173 } | 178 } |
174 | 179 |
178 in.close(); | 183 in.close(); |
179 return a; | 184 return a; |
180 } | 185 } |
181 | 186 |
182 public LuanFunction read_lines() throws IOException { | 187 public LuanFunction read_lines() throws IOException { |
183 return lines(new BufferedReader(new InputStreamReader(inputStream()))); | 188 return lines(new BufferedReader(reader())); |
184 } | 189 } |
185 | 190 |
186 public LuanFunction read_blocks(Integer blockSize) throws IOException { | 191 public LuanFunction read_blocks(Integer blockSize) throws IOException { |
187 int n = blockSize!=null ? blockSize : Utils.bufSize; | 192 int n = blockSize!=null ? blockSize : Utils.bufSize; |
188 return blocks(inputStream(),n); | 193 return blocks(inputStream(),n); |
294 } | 299 } |
295 return tbl; | 300 return tbl; |
296 } | 301 } |
297 } | 302 } |
298 | 303 |
304 public static final class LuanString extends LuanIn { | |
305 private final String s; | |
306 | |
307 private LuanString(String s) { | |
308 this.s = s; | |
309 } | |
310 | |
311 @Override InputStream inputStream() throws IOException { | |
312 throw new UnsupportedOperationException(); | |
313 } | |
314 | |
315 @Override public String to_string() { | |
316 return "<string>"; | |
317 } | |
318 | |
319 @Override public Reader reader() { | |
320 return new StringReader(s); | |
321 } | |
322 | |
323 @Override public String read_text() { | |
324 return s; | |
325 } | |
326 | |
327 @Override public boolean exists() { | |
328 return true; | |
329 } | |
330 } | |
331 | |
299 public static final class LuanUrl extends LuanIn { | 332 public static final class LuanUrl extends LuanIn { |
300 private final URL url; | 333 private final URL url; |
301 | 334 |
302 private LuanUrl(URL url) { | 335 private LuanUrl(URL url) { |
303 this.url = url; | 336 this.url = url; |
418 } catch(NoSuchMethodException e) { | 451 } catch(NoSuchMethodException e) { |
419 throw new RuntimeException(e); | 452 throw new RuntimeException(e); |
420 } | 453 } |
421 return tbl; | 454 return tbl; |
422 } | 455 } |
456 } | |
457 | |
458 public static LuanTable string(LuanState luan,String s) throws LuanException { | |
459 Utils.checkNotNull(luan,s); | |
460 return new LuanString(s).table(); | |
423 } | 461 } |
424 | 462 |
425 public static LuanTable file(LuanState luan,String name) throws LuanException { | 463 public static LuanTable file(LuanState luan,String name) throws LuanException { |
426 File file = new File(name); | 464 File file = new File(name); |
427 return new LuanFile(file).table(); | 465 return new LuanFile(file).table(); |
480 } | 518 } |
481 | 519 |
482 public static LuanTable newSchemes() { | 520 public static LuanTable newSchemes() { |
483 LuanTable schemes = Luan.newTable(); | 521 LuanTable schemes = Luan.newTable(); |
484 try { | 522 try { |
523 add( schemes, "string", LuanState.class, String.class ); | |
485 add( schemes, "file", LuanState.class, String.class ); | 524 add( schemes, "file", LuanState.class, String.class ); |
486 add( schemes, "classpath", LuanState.class, String.class ); | 525 add( schemes, "classpath", LuanState.class, String.class ); |
487 add( schemes, "socket", LuanState.class, String.class ); | 526 add( schemes, "socket", LuanState.class, String.class ); |
488 add( schemes, "http", String.class ); | 527 add( schemes, "http", String.class ); |
489 add( schemes, "https", String.class ); | 528 add( schemes, "https", String.class ); |