changeset 125:0cd559a16758

add sockets git-svn-id: https://luan-java.googlecode.com/svn/trunk@126 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 06 Jun 2014 05:59:11 +0000
parents f537ff5e511d
children 0149bdf98fd8
files src/luan/lib/BasicLib.java src/luan/lib/IoLib.java src/luan/lib/PackageLib.java src/luan/lib/Utils.java
diffstat 4 files changed, 187 insertions(+), 201 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/lib/BasicLib.java	Fri Jun 06 03:41:04 2014 +0000
+++ b/src/luan/lib/BasicLib.java	Fri Jun 06 05:59:11 2014 +0000
@@ -92,7 +92,7 @@
 
 	public static LuanFunction load_file(LuanState luan,String fileName) throws LuanException {
 		try {
-			String src = fileName==null ? Utils.readAll(new InputStreamReader(System.in)) : Utils.read(new File(fileName));
+			String src = fileName==null ? Utils.readAll(new InputStreamReader(System.in)) : new IoLib.LuanFile(fileName).read_text();
 			return load(luan,src,fileName,false);
 		} catch(IOException e) {
 			throw luan.JAVA.exception(e);
--- a/src/luan/lib/IoLib.java	Fri Jun 06 03:41:04 2014 +0000
+++ b/src/luan/lib/IoLib.java	Fri Jun 06 05:59:11 2014 +0000
@@ -8,12 +8,18 @@
 import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.BufferedOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.net.URL;
+import java.net.Socket;
+import java.net.ServerSocket;
 import java.net.MalformedURLException;
 import luan.LuanState;
 import luan.LuanTable;
@@ -52,6 +58,9 @@
 					IoLib.class.getMethod( "stdin_read_blocks", Integer.class ), null
 				) );
 				module.put( "stdin", stdin );
+
+				add( module, "socket", String.class, Integer.TYPE );
+				add( module, "socket_server", Integer.TYPE );
 			} catch(NoSuchMethodException e) {
 				throw new RuntimeException(e);
 			}
@@ -65,139 +74,6 @@
 		t.put( method, new LuanJavaFunction(IoLib.class.getMethod(method,parameterTypes),null) );
 	}
 
-	public static final class LuanFile {
-		private final File file;
-
-		private LuanFile(String name) {
-			this.file = new File(name);
-		}
-
-		public String read_text() throws IOException {
-			return Utils.read(file);
-		}
-
-		public byte[] read_binary() throws IOException {
-			return Utils.readAll(file);
-		}
-
-		public void write(LuanState luan,Object obj) throws LuanException, IOException {
-			if( obj instanceof String ) {
-				String s = (String)obj;
-				Utils.write(file,s);
-				return;
-			}
-			if( obj instanceof byte[] ) {
-				byte[] a = (byte[])obj;
-				Utils.writeAll(file,a);
-				return;
-			}
-			throw luan.JAVA.exception( "bad argument #1 to 'write' (string or binary expected)" );
-		}
-
-		public LuanTable text_writer() throws IOException {
-			return textWriter(new FileWriter(file));
-		}
-
-		public LuanTable binary_writer() throws IOException {
-			return binaryWriter(new FileOutputStream(file));
-		}
-
-		public LuanFunction read_lines() throws IOException {
-			return lines(new BufferedReader(new FileReader(file)));
-		}
-
-		public LuanFunction read_blocks(Integer blockSize) throws IOException {
-			int n = blockSize!=null ? blockSize : Utils.bufSize;
-			return blocks(new FileInputStream(file),n);
-		}
-	}
-
-	public static LuanTable file(String name) {
-		LuanTable tbl = new LuanTable();
-		LuanFile file = new LuanFile(name);
-		try {
-			tbl.put( "read_text", new LuanJavaFunction(
-				LuanFile.class.getMethod( "read_text" ), file
-			) );
-			tbl.put( "read_binary", new LuanJavaFunction(
-				LuanFile.class.getMethod( "read_binary" ), file
-			) );
-			tbl.put( "write", new LuanJavaFunction(
-				LuanFile.class.getMethod( "write", LuanState.class, Object.class ), file
-			) );
-			tbl.put( "text_writer", new LuanJavaFunction(
-				LuanFile.class.getMethod( "text_writer" ), file
-			) );
-			tbl.put( "binary_writer", new LuanJavaFunction(
-				LuanFile.class.getMethod( "binary_writer" ), file
-			) );
-			tbl.put( "read_lines", new LuanJavaFunction(
-				LuanFile.class.getMethod( "read_lines" ), file
-			) );
-			tbl.put( "read_blocks", new LuanJavaFunction(
-				LuanFile.class.getMethod( "read_blocks", Integer.class ), file
-			) );
-		} catch(NoSuchMethodException e) {
-			throw new RuntimeException(e);
-		}
-		return tbl;
-	}
-
-
-	public static final class LuanUrl {
-		private final URL url;
-
-		LuanUrl(String s) throws MalformedURLException {
-			this.url = new URL(s);
-		}
-
-		public String read_text() throws IOException {
-			Reader in = new InputStreamReader(url.openStream());
-			String s = Utils.readAll(in);
-			in.close();
-			return s;
-		}
-
-		public byte[] read_binary() throws IOException {
-			InputStream in = url.openStream();
-			byte[] a = Utils.readAll(in);
-			in.close();
-			return a;
-		}
-
-		public LuanFunction read_lines() throws IOException {
-			return lines(new BufferedReader(new InputStreamReader(url.openStream())));
-		}
-
-		public LuanFunction read_blocks(Integer blockSize) throws IOException {
-			int n = blockSize!=null ? blockSize : Utils.bufSize;
-			return blocks(url.openStream(),n);
-		}
-	}
-
-	public static LuanTable url(String s) throws MalformedURLException {
-		LuanTable tbl = new LuanTable();
-		LuanUrl url = new LuanUrl(s);
-		try {
-			tbl.put( "read_text", new LuanJavaFunction(
-				LuanUrl.class.getMethod( "read_text" ), url
-			) );
-			tbl.put( "read_binary", new LuanJavaFunction(
-				LuanUrl.class.getMethod( "read_binary" ), url
-			) );
-			tbl.put( "read_lines", new LuanJavaFunction(
-				LuanUrl.class.getMethod( "read_lines" ), url
-			) );
-			tbl.put( "read_blocks", new LuanJavaFunction(
-				LuanUrl.class.getMethod( "read_blocks", Integer.class ), url
-			) );
-		} catch(NoSuchMethodException e) {
-			throw new RuntimeException(e);
-		}
-		return tbl;
-	}
-
-
 
 	public static String stdin_read_text() throws IOException {
 		return Utils.readAll(new InputStreamReader(System.in));
@@ -345,4 +221,180 @@
 		};
 	}
 
+
+
+	public static abstract class LuanIn {
+		abstract InputStream inputStream() throws IOException;
+
+		public String read_text() throws IOException {
+			Reader in = new InputStreamReader(inputStream());
+			String s = Utils.readAll(in);
+			in.close();
+			return s;
+		}
+
+		public byte[] read_binary() throws IOException {
+			InputStream in = inputStream();
+			byte[] a = Utils.readAll(in);
+			in.close();
+			return a;
+		}
+
+		public LuanFunction read_lines() throws IOException {
+			return lines(new BufferedReader(new InputStreamReader(inputStream())));
+		}
+
+		public LuanFunction read_blocks(Integer blockSize) throws IOException {
+			int n = blockSize!=null ? blockSize : Utils.bufSize;
+			return blocks(inputStream(),n);
+		}
+
+		LuanTable table() {
+			LuanTable tbl = new LuanTable();
+			try {
+				tbl.put( "read_text", new LuanJavaFunction(
+					LuanIn.class.getMethod( "read_text" ), this
+				) );
+				tbl.put( "read_binary", new LuanJavaFunction(
+					LuanIn.class.getMethod( "read_binary" ), this
+				) );
+				tbl.put( "read_lines", new LuanJavaFunction(
+					LuanIn.class.getMethod( "read_lines" ), this
+				) );
+				tbl.put( "read_blocks", new LuanJavaFunction(
+					LuanIn.class.getMethod( "read_blocks", Integer.class ), this
+				) );
+			} catch(NoSuchMethodException e) {
+				throw new RuntimeException(e);
+			}
+			return tbl;
+		}
+	}
+
+	public static abstract class LuanIO extends LuanIn {
+		abstract OutputStream outputStream() throws IOException;
+
+		public void write(LuanState luan,Object obj) throws LuanException, IOException {
+			if( obj instanceof String ) {
+				String s = (String)obj;
+				Writer out = new OutputStreamWriter(outputStream());
+				out.write(s);
+				out.close();
+				return;
+			}
+			if( obj instanceof byte[] ) {
+				byte[] a = (byte[])obj;
+				OutputStream out = outputStream();
+				Utils.copyAll(new ByteArrayInputStream(a),out);
+				out.close();
+				return;
+			}
+			throw luan.JAVA.exception( "bad argument #1 to 'write' (string or binary expected)" );
+		}
+
+		public LuanTable text_writer() throws IOException {
+			return textWriter(new BufferedWriter(new OutputStreamWriter(outputStream())));
+		}
+
+		public LuanTable binary_writer() throws IOException {
+			return binaryWriter(new BufferedOutputStream(outputStream()));
+		}
+
+		LuanTable table() {
+			LuanTable tbl = super.table();
+			try {
+				tbl.put( "write", new LuanJavaFunction(
+					LuanIO.class.getMethod( "write", LuanState.class, Object.class ), this
+				) );
+				tbl.put( "text_writer", new LuanJavaFunction(
+					LuanIO.class.getMethod( "text_writer" ), this
+				) );
+				tbl.put( "binary_writer", new LuanJavaFunction(
+					LuanIO.class.getMethod( "binary_writer" ), this
+				) );
+			} catch(NoSuchMethodException e) {
+				throw new RuntimeException(e);
+			}
+			return tbl;
+		}
+	}
+
+	public static final class LuanUrl extends LuanIn {
+		private final URL url;
+
+		public LuanUrl(String s) throws MalformedURLException {
+			this.url = new URL(s);
+		}
+
+		InputStream inputStream() throws IOException {
+			return url.openStream();
+		}
+	}
+
+	public static LuanTable url(String s) throws MalformedURLException {
+		return new LuanUrl(s).table();
+	}
+
+	public static final class LuanFile extends LuanIO {
+		private final File file;
+
+		public LuanFile(String name) {
+			this.file = new File(name);
+		}
+
+		InputStream inputStream() throws IOException {
+			return new FileInputStream(file);
+		}
+
+		OutputStream outputStream() throws IOException {
+			return new FileOutputStream(file);
+		}
+	}
+
+	public static LuanTable file(String name) {
+		return new LuanFile(name).table();
+	}
+
+	public static final class LuanSocket extends LuanIO {
+		private final Socket socket;
+
+		public LuanSocket(String host,int port) throws IOException {
+			this(new Socket(host,port));
+		}
+
+		public LuanSocket(Socket socket) throws IOException {
+			this.socket = socket;
+		}
+
+		InputStream inputStream() throws IOException {
+			return socket.getInputStream();
+		}
+
+		OutputStream outputStream() throws IOException {
+			return socket.getOutputStream();
+		}
+	}
+
+	public static LuanTable socket(String host,int port) throws IOException {
+		return new LuanSocket(host,port).table();
+	}
+
+	public static LuanFunction socket_server(int port) throws IOException {
+		final ServerSocket ss = new ServerSocket(port);
+		return new LuanFunction() {
+			@Override public Object call(LuanState luan,Object[] args) throws LuanException {
+				try {
+					if( args.length > 0 ) {
+						if( args.length > 1 || !"close".equals(args[0]) )
+							throw luan.JAVA.exception( "the only argument allowed is 'close'" );
+						ss.close();
+						return null;
+					}
+					return new LuanSocket(ss.accept()).table();
+				} catch(IOException e) {
+					throw luan.JAVA.exception(e);
+				}
+			}
+		};
+	}
 }
--- a/src/luan/lib/PackageLib.java	Fri Jun 06 03:41:04 2014 +0000
+++ b/src/luan/lib/PackageLib.java	Fri Jun 06 05:59:11 2014 +0000
@@ -118,8 +118,7 @@
 		@Override public Object call(LuanState luan,Object[] args) throws LuanException {
 			String urlStr = (String)args[1];
 			try {
-				URL url = new URL(urlStr);
-				String src = Utils.read(url);
+				String src = new IoLib.LuanUrl(urlStr).read_text();
 				LuanFunction fn = BasicLib.load(luan,src,urlStr,false);
 				return fn.call(luan,args);
 			} catch(IOException e) {
--- a/src/luan/lib/Utils.java	Fri Jun 06 03:41:04 2014 +0000
+++ b/src/luan/lib/Utils.java	Fri Jun 06 05:59:11 2014 +0000
@@ -1,22 +1,12 @@
 package luan.lib;
 
-import java.io.File;
-import java.io.FileReader;
-import java.io.FileWriter;
-import java.io.InputStreamReader;
 import java.io.Reader;
-import java.io.Writer;
 import java.io.IOException;
-import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.net.URL;
 import luan.LuanState;
 import luan.LuanException;
-import luan.LuanElement;
 
 
 public final class Utils {
@@ -41,24 +31,6 @@
 		return buf.toString();
 	}
 
-	public static String read(File file)
-		throws IOException
-	{
-		Reader in = new FileReader(file);
-		String s = readAll(in);
-		in.close();
-		return s;
-	}
-
-	public static String read(URL url)
-		throws IOException
-	{
-		Reader in = new InputStreamReader(url.openStream());
-		String s = readAll(in);
-		in.close();
-		return s;
-	}
-
 	public static void copyAll(InputStream in,OutputStream out)
 		throws IOException
 	{
@@ -69,43 +41,6 @@
 		}
 	}
 
-	public static byte[] readAll(File file)
-		throws IOException
-	{
-		int len = (int)file.length();
-		ByteArrayOutputStream out = new ByteArrayOutputStream(len) {
-			public byte[] toByteArray() {
-				return buf;
-			}
-		};
-		FileInputStream in = new FileInputStream(file);
-		copyAll(in,out);
-		in.close();
-		return out.toByteArray();
-	}
-
-	public static void write(File file,String s)
-		throws IOException
-	{
-		Writer out = new FileWriter(file);
-		out.write(s);
-		out.close();
-	}
-
-	public static void writeAll(byte[] a,OutputStream out)
-		throws IOException
-	{
-		copyAll(new ByteArrayInputStream(a),out);
-	}
-
-	public static void writeAll(File file,byte[] a)
-		throws IOException
-	{
-		FileOutputStream fos = new FileOutputStream(file);
-		writeAll(a,fos);
-		fos.close();
-	}
-
 	public static byte[] readAll(InputStream in)
 		throws IOException
 	{