changeset 1493:471ef3e6a84e

more io
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 03 May 2020 00:12:15 -0600
parents aaac1d29edea
children 91c167099462
files src/goodjava/io/IoUtils.java src/goodjava/rpc/RpcCon.java src/goodjava/webserver/Connection.java src/luan/modules/IoLuan.java src/luan/modules/Utils.java
diffstat 5 files changed, 23 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/src/goodjava/io/IoUtils.java	Sat May 02 22:25:56 2020 -0600
+++ b/src/goodjava/io/IoUtils.java	Sun May 03 00:12:15 2020 -0600
@@ -1,6 +1,8 @@
 package goodjava.io;
 
 import java.io.File;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.io.IOException;
 import java.nio.file.Files;
 
@@ -33,4 +35,17 @@
 		Files.createLink( to.toPath(), from.toPath() );
 	}
 
+	public static long copyAll(InputStream in,OutputStream out)
+		throws IOException
+	{
+		long total = 0;
+		byte[] a = new byte[8192];
+		int n;
+		while( (n=in.read(a)) != -1 ) {
+			out.write(a,0,n);
+			total += n;
+		}
+		return total;
+	}
+
 }
\ No newline at end of file
--- a/src/goodjava/rpc/RpcCon.java	Sat May 02 22:25:56 2020 -0600
+++ b/src/goodjava/rpc/RpcCon.java	Sun May 03 00:12:15 2020 -0600
@@ -3,9 +3,7 @@
 import java.io.InputStream;
 import java.io.BufferedOutputStream;
 import java.io.IOException;
-import java.io.EOFException;
 import java.net.Socket;
-import java.nio.charset.StandardCharsets;
 import java.util.List;
 import goodjava.parser.ParseException;
 import goodjava.json.JsonParser;
@@ -13,6 +11,7 @@
 import goodjava.io.BufferedInputStream;
 import goodjava.io.DataInputStream;
 import goodjava.io.DataOutputStream;
+import goodjava.io.IoUtils;
 
 
 public class RpcCon {
@@ -58,13 +57,7 @@
 		try {
 			out.writeString(json);
 			if( in != null ) {
-				byte[] a = new byte[8192];
-				long total = 0;
-				int n;
-				while( (n=in.read(a)) != -1 ) {
-					out.write(a,0,n);
-					total += n;
-				}
+				long total = IoUtils.copyAll(in,out);
 				if( total != lenIn ) {
 					close();
 					throw new RpcError("InputStream wrong length "+total+" when should be "+lenIn);
--- a/src/goodjava/webserver/Connection.java	Sat May 02 22:25:56 2020 -0600
+++ b/src/goodjava/webserver/Connection.java	Sun May 03 00:12:15 2020 -0600
@@ -7,6 +7,7 @@
 import goodjava.logging.Logger;
 import goodjava.logging.LoggerFactory;
 import goodjava.parser.ParseException;
+import goodjava.io.IoUtils;
 
 
 final class Connection {
@@ -115,7 +116,7 @@
 	
 			OutputStream out = socket.getOutputStream();
 			out.write(header);
-			copyAll(response.body.content,out);
+			IoUtils.copyAll(response.body.content,out);
 			out.close();
 			socket.close();
 		} catch(IOException e) {
@@ -123,14 +124,4 @@
 		}
 	}
 
-	private static void copyAll(InputStream in,OutputStream out)
-		throws IOException
-	{
-		byte[] a = new byte[8192];
-		int n;
-		while( (n=in.read(a)) != -1 ) {
-			out.write(a,0,n);
-		}
-	}
-
 }
--- a/src/luan/modules/IoLuan.java	Sat May 02 22:25:56 2020 -0600
+++ b/src/luan/modules/IoLuan.java	Sun May 03 00:12:15 2020 -0600
@@ -273,7 +273,7 @@
 			if( obj instanceof byte[] ) {
 				byte[] a = (byte[])obj;
 				OutputStream out = outputStream();
-				Utils.copyAll(new ByteArrayInputStream(a),out);
+				out.write(a);
 				out.close();
 				return;
 			}
@@ -284,7 +284,7 @@
 					LuanIn luanIn = (LuanIn)java;
 					InputStream in = luanIn.inputStream();
 					OutputStream out = outputStream();
-					Utils.copyAll(in,out);
+					IoUtils.copyAll(in,out);
 					out.close();
 					in.close();
 					return;
--- a/src/luan/modules/Utils.java	Sat May 02 22:25:56 2020 -0600
+++ b/src/luan/modules/Utils.java	Sun May 03 00:12:15 2020 -0600
@@ -10,6 +10,7 @@
 import java.net.URL;
 import java.net.MalformedURLException;
 import java.util.Map;
+import goodjava.io.IoUtils;
 import luan.Luan;
 import luan.LuanException;
 import luan.LuanTable;
@@ -78,21 +79,11 @@
 		return buf.toString();
 	}
 
-	public static void copyAll(InputStream in,OutputStream out)
-		throws IOException
-	{
-		byte[] a = new byte[bufSize];
-		int n;
-		while( (n=in.read(a)) != -1 ) {
-			out.write(a,0,n);
-		}
-	}
-
 	public static byte[] readAll(InputStream in)
 		throws IOException
 	{
 		ByteArrayOutputStream out = new ByteArrayOutputStream();
-		copyAll(in,out);
+		IoUtils.copyAll(in,out);
 		return out.toByteArray();
 	}