Mercurial Hosting > luan
comparison 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 |
comparison
equal
deleted
inserted
replaced
1491:491b355acef7 | 1492:aaac1d29edea |
---|---|
1 package goodjava.rpc; | 1 package goodjava.rpc; |
2 | 2 |
3 import java.io.InputStream; | 3 import java.io.InputStream; |
4 import java.io.OutputStream; | 4 import java.io.BufferedOutputStream; |
5 import java.io.IOException; | 5 import java.io.IOException; |
6 import java.io.EOFException; | 6 import java.io.EOFException; |
7 import java.net.Socket; | 7 import java.net.Socket; |
8 import java.nio.charset.StandardCharsets; | 8 import java.nio.charset.StandardCharsets; |
9 import java.util.List; | 9 import java.util.List; |
10 import goodjava.parser.ParseException; | 10 import goodjava.parser.ParseException; |
11 import goodjava.json.JsonParser; | 11 import goodjava.json.JsonParser; |
12 import goodjava.json.JsonToString; | 12 import goodjava.json.JsonToString; |
13 import goodjava.io.BufferedInputStream; | |
14 import goodjava.io.DataInputStream; | |
15 import goodjava.io.DataOutputStream; | |
13 | 16 |
14 | 17 |
15 public class RpcCon { | 18 public class RpcCon { |
16 final Socket socket; | 19 private final Socket socket; |
17 final InputStream in; | 20 private final DataInputStream in; |
18 final OutputStream out; | 21 private final DataOutputStream out; |
19 InputStream inBinary = null; | 22 InputStream inBinary = null; |
20 long lenBinary = -1; | 23 long lenBinary = -1; |
21 boolean readSome = false; | |
22 | 24 |
23 RpcCon(Socket socket) | 25 RpcCon(Socket socket) |
24 throws RpcError | 26 throws RpcError |
25 { | 27 { |
26 try { | 28 try { |
27 this.socket = socket; | 29 this.socket = socket; |
28 this.in = socket.getInputStream(); | 30 this.in = new DataInputStream(new BufferedInputStream(socket.getInputStream())); |
29 this.out = socket.getOutputStream(); | 31 this.out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); |
30 } catch(IOException e) { | 32 } catch(IOException e) { |
31 close(); | 33 close(); |
32 throw new RpcError(e); | 34 throw new RpcError(e); |
33 } | 35 } |
34 } | 36 } |
51 throws RpcError | 53 throws RpcError |
52 { | 54 { |
53 if( in != null ) | 55 if( in != null ) |
54 list.add(0,lenIn); | 56 list.add(0,lenIn); |
55 String json = new JsonToString().toString(list); | 57 String json = new JsonToString().toString(list); |
56 byte[] aJson = json.getBytes(StandardCharsets.UTF_8); | |
57 int len = aJson.length; | |
58 byte[] a = new byte[4+len]; | |
59 a[0] = (byte)(len >>> 24); | |
60 a[1] = (byte)(len >>> 16); | |
61 a[2] = (byte)(len >>> 8); | |
62 a[3] = (byte)(len >>> 0); | |
63 System.arraycopy(aJson,0,a,4,len); | |
64 try { | 58 try { |
65 out.write(a); | 59 out.writeString(json); |
66 if( in != null ) { | 60 if( in != null ) { |
67 a = new byte[8192]; | 61 byte[] a = new byte[8192]; |
68 long total = 0; | 62 long total = 0; |
69 int n; | 63 int n; |
70 while( (n=in.read(a)) != -1 ) { | 64 while( (n=in.read(a)) != -1 ) { |
71 out.write(a,0,n); | 65 out.write(a,0,n); |
72 total += n; | 66 total += n; |
74 if( total != lenIn ) { | 68 if( total != lenIn ) { |
75 close(); | 69 close(); |
76 throw new RpcError("InputStream wrong length "+total+" when should be "+lenIn); | 70 throw new RpcError("InputStream wrong length "+total+" when should be "+lenIn); |
77 } | 71 } |
78 } | 72 } |
73 out.flush(); | |
79 } catch(IOException e) { | 74 } catch(IOException e) { |
80 close(); | 75 close(); |
81 throw new RpcError(e); | 76 throw new RpcError(e); |
82 } | 77 } |
83 } | 78 } |
89 if( inBinary != null ) { | 84 if( inBinary != null ) { |
90 inBinary.close(); | 85 inBinary.close(); |
91 inBinary = null; | 86 inBinary = null; |
92 lenBinary = -1; | 87 lenBinary = -1; |
93 } | 88 } |
94 readSome = false; | 89 String json = in.readString(); |
95 byte[] a = new byte[4]; | |
96 readAll(a); | |
97 int len = 0; | |
98 for( byte b : a ) { | |
99 len <<= 8; | |
100 len |= b&0xFF; | |
101 } | |
102 a = new byte[len]; | |
103 readAll(a); | |
104 String json = new String(a,StandardCharsets.UTF_8); | |
105 List list = (List)JsonParser.parse(json); | 90 List list = (List)JsonParser.parse(json); |
106 if( list.get(0) instanceof Long ) { | 91 if( list.get(0) instanceof Long ) { |
107 lenBinary = (Long)list.remove(0); | 92 lenBinary = (Long)list.remove(0); |
108 inBinary = new FixedLengthInputStream(in,lenBinary); | 93 inBinary = new FixedLengthInputStream(in,lenBinary); |
109 } | 94 } |
115 close(); | 100 close(); |
116 throw new RpcError(e); | 101 throw new RpcError(e); |
117 } | 102 } |
118 } | 103 } |
119 | 104 |
120 private void readAll(final byte[] a) throws IOException { | |
121 int total = 0; | |
122 int n; | |
123 while( total < a.length ){ | |
124 n = in.read( a, total, a.length-total ); | |
125 if( n == -1 ) | |
126 throw new EOFException(); | |
127 readSome = true; | |
128 total += n; | |
129 } | |
130 } | |
131 | |
132 } | 105 } |