comparison src/goodjava/rpc/RpcCon.java @ 1493:471ef3e6a84e

more io
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 03 May 2020 00:12:15 -0600
parents aaac1d29edea
children 91c167099462
comparison
equal deleted inserted replaced
1492:aaac1d29edea 1493:471ef3e6a84e
1 package goodjava.rpc; 1 package goodjava.rpc;
2 2
3 import java.io.InputStream; 3 import java.io.InputStream;
4 import java.io.BufferedOutputStream; 4 import java.io.BufferedOutputStream;
5 import java.io.IOException; 5 import java.io.IOException;
6 import java.io.EOFException;
7 import java.net.Socket; 6 import java.net.Socket;
8 import java.nio.charset.StandardCharsets;
9 import java.util.List; 7 import java.util.List;
10 import goodjava.parser.ParseException; 8 import goodjava.parser.ParseException;
11 import goodjava.json.JsonParser; 9 import goodjava.json.JsonParser;
12 import goodjava.json.JsonToString; 10 import goodjava.json.JsonToString;
13 import goodjava.io.BufferedInputStream; 11 import goodjava.io.BufferedInputStream;
14 import goodjava.io.DataInputStream; 12 import goodjava.io.DataInputStream;
15 import goodjava.io.DataOutputStream; 13 import goodjava.io.DataOutputStream;
14 import goodjava.io.IoUtils;
16 15
17 16
18 public class RpcCon { 17 public class RpcCon {
19 private final Socket socket; 18 private final Socket socket;
20 private final DataInputStream in; 19 private final DataInputStream in;
56 list.add(0,lenIn); 55 list.add(0,lenIn);
57 String json = new JsonToString().toString(list); 56 String json = new JsonToString().toString(list);
58 try { 57 try {
59 out.writeString(json); 58 out.writeString(json);
60 if( in != null ) { 59 if( in != null ) {
61 byte[] a = new byte[8192]; 60 long total = IoUtils.copyAll(in,out);
62 long total = 0;
63 int n;
64 while( (n=in.read(a)) != -1 ) {
65 out.write(a,0,n);
66 total += n;
67 }
68 if( total != lenIn ) { 61 if( total != lenIn ) {
69 close(); 62 close();
70 throw new RpcError("InputStream wrong length "+total+" when should be "+lenIn); 63 throw new RpcError("InputStream wrong length "+total+" when should be "+lenIn);
71 } 64 }
72 } 65 }