changeset 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 1f6d828986b9
children 6e11eace1091
files core/src/luan/modules/IoLuan.java
diffstat 1 files changed, 41 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/core/src/luan/modules/IoLuan.java	Thu Dec 25 06:14:59 2014 +0000
+++ b/core/src/luan/modules/IoLuan.java	Thu Dec 25 07:23:32 2014 +0000
@@ -5,6 +5,7 @@
 import java.io.PrintStream;
 import java.io.Reader;
 import java.io.Writer;
+import java.io.StringReader;
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.BufferedInputStream;
@@ -165,8 +166,12 @@
 		abstract InputStream inputStream() throws IOException;
 		public abstract String to_string();
 
+		public Reader reader() throws IOException {
+			return new InputStreamReader(inputStream());
+		}
+
 		public String read_text() throws IOException {
-			Reader in = new InputStreamReader(inputStream());
+			Reader in = reader();
 			String s = Utils.readAll(in);
 			in.close();
 			return s;
@@ -180,7 +185,7 @@
 		}
 
 		public LuanFunction read_lines() throws IOException {
-			return lines(new BufferedReader(new InputStreamReader(inputStream())));
+			return lines(new BufferedReader(reader()));
 		}
 
 		public LuanFunction read_blocks(Integer blockSize) throws IOException {
@@ -296,6 +301,34 @@
 		}
 	}
 
+	public static final class LuanString extends LuanIn {
+		private final String s;
+
+		private LuanString(String s) {
+			this.s = s;
+		}
+
+		@Override InputStream inputStream() throws IOException {
+			throw new UnsupportedOperationException();
+		}
+
+		@Override public String to_string() {
+			return "<string>";
+		}
+
+		@Override public Reader reader() {
+			return new StringReader(s);
+		}
+
+		@Override public String read_text() {
+			return s;
+		}
+
+		@Override public boolean exists() {
+			return true;
+		}
+	}
+
 	public static final class LuanUrl extends LuanIn {
 		private final URL url;
 
@@ -422,6 +455,11 @@
 		}
 	}
 
+	public static LuanTable string(LuanState luan,String s) throws LuanException {
+		Utils.checkNotNull(luan,s);
+		return new LuanString(s).table();
+	}
+
 	public static LuanTable file(LuanState luan,String name) throws LuanException {
 		File file = new File(name);
 		return new LuanFile(file).table();
@@ -482,6 +520,7 @@
 	public static LuanTable newSchemes() {
 		LuanTable schemes = Luan.newTable();
 		try {
+			add( schemes, "string", LuanState.class, String.class );
 			add( schemes, "file", LuanState.class, String.class );
 			add( schemes, "classpath", LuanState.class, String.class );
 			add( schemes, "socket", LuanState.class, String.class );