8
8
9
9
import java .io .ByteArrayOutputStream ;
10
10
import java .io .IOException ;
11
+ import java .io .InputStream ;
12
+ import java .io .OutputStream ;
11
13
import java .lang .reflect .Constructor ;
12
14
import java .lang .reflect .InvocationTargetException ;
15
+ import java .nio .ByteBuffer ;
13
16
import java .util .Arrays ;
14
17
import java .util .HashMap ;
15
18
import java .util .Map ;
@@ -44,25 +47,102 @@ private void initMessageFields(Object object) {
44
47
.forEach (mf -> messageFields .put (mf .getFieldNumber (), mf ));
45
48
}
46
49
47
- public byte [] write (Object object ) throws IOException {
48
- initMessageFields (object );
50
+ ////////////////////////////////////////////////////
51
+ ///// write operations /////
52
+ ////////////////////////////////////////////////////
49
53
54
+ public byte [] write (Object object ) throws IOException {
50
55
ByteArrayOutputStream os = new ByteArrayOutputStream ();
51
56
CodedOutputStream cos = CodedOutputStream .newInstance (os );
52
57
58
+ write (object , cos );
59
+
60
+ return os .toByteArray ();
61
+ }
62
+
63
+ public void write (Object object , byte [] flatArray ) throws IOException {
64
+ write (object , CodedOutputStream .newInstance (flatArray ));
65
+ }
66
+
67
+ public void write (Object object , byte [] flatArray , int offset , int length ) throws IOException {
68
+ write (object , CodedOutputStream .newInstance (flatArray , offset , length ));
69
+ }
70
+
71
+ public void write (Object object , ByteBuffer buffer ) throws IOException {
72
+ write (object , CodedOutputStream .newInstance (buffer ));
73
+ }
74
+
75
+ public void write (Object object , OutputStream output ) throws IOException {
76
+ write (object , CodedOutputStream .newInstance (output ));
77
+ }
78
+
79
+ public void write (Object object , OutputStream output , int bufferSize ) throws IOException {
80
+ write (object , CodedOutputStream .newInstance (output , bufferSize ));
81
+ }
82
+
83
+ private void write (Object object , CodedOutputStream cos ) throws IOException {
84
+ initMessageFields (object );
85
+
53
86
for (MessageField field : messageFields .values ()) {
54
87
field .write (cos );
55
88
}
56
89
57
90
cos .flush ();
58
- return os .toByteArray ();
59
91
}
60
92
93
+ ///////////////////////////////////////////////////
94
+ ///// read operations /////
95
+ ///////////////////////////////////////////////////
96
+
61
97
public Object read (byte [] data ) throws IOException {
62
98
return read (data , Object .class );
63
99
}
64
100
65
101
public <T > T read (byte [] data , Class <T > clazz ) throws IOException {
102
+ return read (CodedInputStream .newInstance (data ), clazz );
103
+ }
104
+
105
+ public Object read (byte [] data , int off , int len ) throws IOException {
106
+ return read (data , off , len , Object .class );
107
+ }
108
+
109
+ public <T > T read (byte [] data , int off , int len , Class <T > clazz ) throws IOException {
110
+ return read (CodedInputStream .newInstance (data , off , len ), clazz );
111
+ }
112
+
113
+ public Object read (ByteBuffer buf ) throws IOException {
114
+ return read (buf , Object .class );
115
+ }
116
+
117
+ public <T > T read (ByteBuffer buf , Class <T > clazz ) throws IOException {
118
+ return read (CodedInputStream .newInstance (buf ), clazz );
119
+ }
120
+
121
+ public Object read (InputStream input ) throws IOException {
122
+ return read (input , Object .class );
123
+ }
124
+
125
+ public <T > T read (InputStream input , Class <T > clazz ) throws IOException {
126
+ return read (CodedInputStream .newInstance (input ), clazz );
127
+ }
128
+
129
+ public Object read (InputStream input , int buffersize ) throws IOException {
130
+ return read (input , buffersize , Object .class );
131
+ }
132
+
133
+ public <T > T read (InputStream input , int buffersize , Class <T > clazz ) throws IOException {
134
+ return read (CodedInputStream .newInstance (input , buffersize ), clazz );
135
+ }
136
+
137
+ public Object read (Iterable <ByteBuffer > input ) throws IOException {
138
+ return read (input , Object .class );
139
+ }
140
+
141
+ public <T > T read (Iterable <ByteBuffer > input , Class <T > clazz ) throws IOException {
142
+ return read (CodedInputStream .newInstance (input ), clazz );
143
+ }
144
+
145
+ private <T > T read (CodedInputStream cis , Class <T > clazz ) throws IOException {
66
146
T object = createInstance (clazz );
67
147
68
148
if (object == null ) {
@@ -71,13 +151,12 @@ public <T> T read(byte[] data, Class<T> clazz) throws IOException {
71
151
72
152
initMessageFields (object );
73
153
74
- CodedInputStream cis = CodedInputStream .newInstance (data );
75
-
76
154
while (!cis .isAtEnd ()) {
77
155
int tag = cis .readTag ();
156
+ int fieldNumber = tag >> 3 ;
78
157
79
- if (messageFields .containsKey (tag >> 3 )) {
80
- messageFields .get (tag >> 3 ).read (cis );
158
+ if (messageFields .containsKey (fieldNumber )) {
159
+ messageFields .get (fieldNumber ).read (cis );
81
160
} else {
82
161
cis .skipField (tag );
83
162
}
0 commit comments