diff src/goodjava/rpc/RpcCon.java @ 1492:aaac1d29edea

better io
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 02 May 2020 22:25:56 -0600
parents 59fd2e8b1b9d
children 471ef3e6a84e
line wrap: on
line diff
--- 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;
-		}
-	}
-
 }