Mercurial Hosting > luan
annotate 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 |
rev | line source |
---|---|
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1120
diff
changeset
|
1 package goodjava.rpc; |
1118 | 2 |
3 import java.io.InputStream; | |
1492 | 4 import java.io.BufferedOutputStream; |
1118 | 5 import java.io.IOException; |
6 import java.io.EOFException; | |
7 import java.net.Socket; | |
8 import java.nio.charset.StandardCharsets; | |
9 import java.util.List; | |
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1120
diff
changeset
|
10 import goodjava.parser.ParseException; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1120
diff
changeset
|
11 import goodjava.json.JsonParser; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1120
diff
changeset
|
12 import goodjava.json.JsonToString; |
1492 | 13 import goodjava.io.BufferedInputStream; |
14 import goodjava.io.DataInputStream; | |
15 import goodjava.io.DataOutputStream; | |
1118 | 16 |
17 | |
18 public class RpcCon { | |
1492 | 19 private final Socket socket; |
20 private final DataInputStream in; | |
21 private final DataOutputStream out; | |
1118 | 22 InputStream inBinary = null; |
23 long lenBinary = -1; | |
24 | |
1119 | 25 RpcCon(Socket socket) |
26 throws RpcError | |
27 { | |
28 try { | |
29 this.socket = socket; | |
1492 | 30 this.in = new DataInputStream(new BufferedInputStream(socket.getInputStream())); |
31 this.out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); | |
1119 | 32 } catch(IOException e) { |
33 close(); | |
34 throw new RpcError(e); | |
35 } | |
1118 | 36 } |
37 | |
38 public void close() | |
1119 | 39 throws RpcError |
1118 | 40 { |
1119 | 41 try { |
42 socket.close(); | |
43 } catch(IOException e) { | |
44 throw new RpcError(e); | |
45 } | |
1118 | 46 } |
47 | |
48 public boolean isClosed() { | |
49 return socket.isClosed(); | |
50 } | |
51 | |
52 void write(InputStream in,long lenIn,List list) | |
1119 | 53 throws RpcError |
1118 | 54 { |
55 if( in != null ) | |
56 list.add(0,lenIn); | |
1419
59fd2e8b1b9d
stringify and json_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
57 String json = new JsonToString().toString(list); |
1119 | 58 try { |
1492 | 59 out.writeString(json); |
1119 | 60 if( in != null ) { |
1492 | 61 byte[] a = new byte[8192]; |
1119 | 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 ) { | |
69 close(); | |
70 throw new RpcError("InputStream wrong length "+total+" when should be "+lenIn); | |
71 } | |
1118 | 72 } |
1492 | 73 out.flush(); |
1119 | 74 } catch(IOException e) { |
75 close(); | |
76 throw new RpcError(e); | |
1118 | 77 } |
78 } | |
79 | |
80 List readJson() | |
1119 | 81 throws RpcError |
1118 | 82 { |
1119 | 83 try { |
84 if( inBinary != null ) { | |
85 inBinary.close(); | |
86 inBinary = null; | |
87 lenBinary = -1; | |
88 } | |
1492 | 89 String json = in.readString(); |
1119 | 90 List list = (List)JsonParser.parse(json); |
91 if( list.get(0) instanceof Long ) { | |
92 lenBinary = (Long)list.remove(0); | |
93 inBinary = new FixedLengthInputStream(in,lenBinary); | |
94 } | |
95 return list; | |
96 } catch(IOException e) { | |
97 close(); | |
98 throw new RpcError(e); | |
99 } catch(ParseException e) { | |
100 close(); | |
101 throw new RpcError(e); | |
1118 | 102 } |
103 } | |
104 | |
105 } |