changeset 1492:aaac1d29edea

better io
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 02 May 2020 22:25:56 -0600
parents 491b355acef7
children 471ef3e6a84e
files src/goodjava/io/DataInputStream.java src/goodjava/io/DataOutputStream.java src/goodjava/rpc/RpcCon.java src/goodjava/rpc/RpcServer.java src/luan/impl/Compiled.java
diffstat 5 files changed, 66 insertions(+), 70 deletions(-) [+]
line wrap: on
line diff
diff -r 491b355acef7 -r aaac1d29edea src/goodjava/io/DataInputStream.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/io/DataInputStream.java	Sat May 02 22:25:56 2020 -0600
@@ -0,0 +1,20 @@
+package goodjava.io;
+
+import java.io.InputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+
+
+public class DataInputStream extends java.io.DataInputStream {
+
+	public DataInputStream(InputStream in) {
+		super(in);
+	}
+
+	public String readString() throws IOException {
+		int len = readInt();
+		byte[] a = new byte[len];
+		readFully(a);
+		return new String(a,StandardCharsets.UTF_8);
+	}
+}
diff -r 491b355acef7 -r aaac1d29edea src/goodjava/io/DataOutputStream.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/io/DataOutputStream.java	Sat May 02 22:25:56 2020 -0600
@@ -0,0 +1,19 @@
+package goodjava.io;
+
+import java.io.OutputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+
+
+public class DataOutputStream extends java.io.DataOutputStream {
+
+	public DataOutputStream(OutputStream out) {
+		super(out);
+	}
+
+	public void writeString(String s) throws IOException {
+		byte[] a = s.getBytes(StandardCharsets.UTF_8);
+		writeInt(a.length);
+		write(a);
+	}
+}
diff -r 491b355acef7 -r aaac1d29edea src/goodjava/rpc/RpcCon.java
--- a/src/goodjava/rpc/RpcCon.java	Sat May 02 21:09:17 2020 -0600
+++ b/src/goodjava/rpc/RpcCon.java	Sat May 02 22:25:56 2020 -0600
@@ -1,7 +1,7 @@
 package goodjava.rpc;
 
 import java.io.InputStream;
-import java.io.OutputStream;
+import java.io.BufferedOutputStream;
 import java.io.IOException;
 import java.io.EOFException;
 import java.net.Socket;
@@ -10,23 +10,25 @@
 import goodjava.parser.ParseException;
 import goodjava.json.JsonParser;
 import goodjava.json.JsonToString;
+import goodjava.io.BufferedInputStream;
+import goodjava.io.DataInputStream;
+import goodjava.io.DataOutputStream;
 
 
 public class RpcCon {
-	final Socket socket;
-	final InputStream in;
-	final OutputStream out;
+	private final Socket socket;
+	private final DataInputStream in;
+	private final DataOutputStream out;
 	InputStream inBinary = null;
 	long lenBinary = -1;
-	boolean readSome = false;
 
 	RpcCon(Socket socket)
 		throws RpcError
 	{
 		try {
 			this.socket = socket;
-			this.in = socket.getInputStream();
-			this.out = socket.getOutputStream();
+			this.in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
+			this.out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
 		} catch(IOException e) {
 			close();
 			throw new RpcError(e);
@@ -53,18 +55,10 @@
 		if( in != null )
 			list.add(0,lenIn);
 		String json = new JsonToString().toString(list);
-		byte[] aJson = json.getBytes(StandardCharsets.UTF_8);
-		int len = aJson.length;
-		byte[] a = new byte[4+len];
-        a[0] = (byte)(len >>> 24);
-        a[1] = (byte)(len >>> 16);
-        a[2] = (byte)(len >>>  8);
-        a[3] = (byte)(len >>>  0);
-		System.arraycopy(aJson,0,a,4,len);
 		try {
-			out.write(a);
+			out.writeString(json);
 			if( in != null ) {
-				a = new byte[8192];
+				byte[] a = new byte[8192];
 				long total = 0;
 				int n;
 				while( (n=in.read(a)) != -1 ) {
@@ -76,6 +70,7 @@
 					throw new RpcError("InputStream wrong length "+total+" when should be "+lenIn);
 				}
 			}
+			out.flush();
 		} catch(IOException e) {
 			close();
 			throw new RpcError(e);
@@ -91,17 +86,7 @@
 				inBinary = null;
 				lenBinary = -1;
 			}
-			readSome = false;
-			byte[] a = new byte[4];
-			readAll(a);
-			int len = 0;
-			for( byte b : a ) {
-				len <<= 8;
-				len |= b&0xFF;
-			}
-			a = new byte[len];
-			readAll(a);
-			String json = new String(a,StandardCharsets.UTF_8);
+			String json = in.readString();
 			List list = (List)JsonParser.parse(json);
 			if( list.get(0) instanceof Long ) {
 				lenBinary = (Long)list.remove(0);
@@ -117,16 +102,4 @@
 		}
 	}
 
-	private void readAll(final byte[] a) throws IOException {
-		int total = 0;
-		int n;
-		while( total < a.length ){
-			n = in.read( a, total, a.length-total );
-			if( n == -1 )
-				throw new EOFException();
-			readSome = true;
-			total += n;
-		}
-	}
-
 }
diff -r 491b355acef7 -r aaac1d29edea src/goodjava/rpc/RpcServer.java
--- a/src/goodjava/rpc/RpcServer.java	Sat May 02 21:09:17 2020 -0600
+++ b/src/goodjava/rpc/RpcServer.java	Sat May 02 22:25:56 2020 -0600
@@ -23,7 +23,7 @@
 			Object[] args = list.toArray();
 			return new RpcCall(inBinary,lenBinary,cmd,args);
 		} catch(RpcError e) {
-			if( !readSome && e.getCause() instanceof EOFException )
+			if( e.getCause() instanceof EOFException )
 				return null;
 			throw e;
 		}
diff -r 491b355acef7 -r aaac1d29edea src/luan/impl/Compiled.java
--- a/src/luan/impl/Compiled.java	Sat May 02 21:09:17 2020 -0600
+++ b/src/luan/impl/Compiled.java	Sat May 02 22:25:56 2020 -0600
@@ -1,13 +1,12 @@
 package luan.impl;
 
 import java.io.OutputStream;
+import java.io.BufferedOutputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.StringWriter;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
-import java.io.DataInputStream;
-import java.io.DataOutputStream;
 import java.io.IOException;
 import java.net.URI;
 import java.util.Collections;
@@ -23,6 +22,9 @@
 import javax.tools.JavaFileManager;
 import javax.tools.StandardJavaFileManager;
 import javax.tools.ForwardingJavaFileManager;
+import goodjava.io.BufferedInputStream;
+import goodjava.io.DataInputStream;
+import goodjava.io.DataOutputStream;
 import goodjava.logging.Logger;
 import goodjava.logging.LoggerFactory;
 
@@ -125,13 +127,13 @@
 				}
 			};
 			return cl.loadClass(className);
-		} catch(ClassNotFoundException e) {
-			throw new RuntimeException(e);
-		}
+		} catch(ClassNotFoundException e) {
+			throw new RuntimeException(e);
+		}
 	}
 
 
-	private static final int VERSION = 2;
+	private static final int VERSION = 3;
 	private static final File tmpDir;
 	static {
 		File f = new File(System.getProperty("java.io.tmpdir"));
@@ -141,33 +143,15 @@
 			throw new RuntimeException();
 	}
 
-	static void writeLongString(DataOutputStream out,String s) throws IOException {
-		int n = s.length()/0xFFFF;
-		out.writeInt(n+1);
-		for( int i=0; i<n; i++ ) {
-			out.writeUTF( s.substring(i*0xFFFF,(i+1)*0xFFFF) );
-		}
-		out.writeUTF( s.substring(n*0xFFFF) );
-	}
-
-	static String readLongString(DataInputStream in) throws IOException {
-		StringBuilder sb = new StringBuilder();
-		int n = in.readInt();
-		for( int i=0; i<n; i++ ) {
-			sb.append( in.readUTF() );
-		}
-		return sb.toString();
-	}
-
 	static Compiled load(String fileName,String key) {
 		try {
 			File f = new File(tmpDir,fileName);
 			if( !f.exists() )
 				return null;
-			DataInputStream in = new DataInputStream(new FileInputStream(f));
+			DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(f)));
 			if( in.readInt() != VERSION )
 				return null;
-			if( !readLongString(in).equals(key) )
+			if( !in.readString().equals(key) )
 				return null;
 			String className = in.readUTF();
 			int n = in.readInt();
@@ -190,16 +174,16 @@
 	void save(String fileName,String key) {
 		try {
 			File f = new File(tmpDir,fileName);
-			DataOutputStream out = new DataOutputStream(new FileOutputStream(f));
+			DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(f)));
 			out.writeInt(VERSION);
-			writeLongString(out,key);
+			out.writeString(key);
 			out.writeUTF(className);
 			out.writeInt(map.size());
 			for( Map.Entry<String,byte[]> entry : map.entrySet() ) {
 				out.writeUTF( entry.getKey() );
 				byte[] a = entry.getValue();
 				out.writeInt(a.length);
-				out.write(a,0,a.length);
+				out.write(a);
 			}
 			out.close();
 		} catch(IOException e) {