Skip to content

Commit c04260e

Browse files
committedJan 23, 2021
PbObjectMapper can also handle buffers and streams
1 parent e1081a8 commit c04260e

File tree

2 files changed

+86
-9
lines changed

2 files changed

+86
-9
lines changed
 

‎src/main/java/com/rumpf/proto/mapper/PbObjectMapper.java

+86-7
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88

99
import java.io.ByteArrayOutputStream;
1010
import java.io.IOException;
11+
import java.io.InputStream;
12+
import java.io.OutputStream;
1113
import java.lang.reflect.Constructor;
1214
import java.lang.reflect.InvocationTargetException;
15+
import java.nio.ByteBuffer;
1316
import java.util.Arrays;
1417
import java.util.HashMap;
1518
import java.util.Map;
@@ -44,25 +47,102 @@ private void initMessageFields(Object object) {
4447
.forEach(mf -> messageFields.put(mf.getFieldNumber(), mf));
4548
}
4649

47-
public byte[] write(Object object) throws IOException {
48-
initMessageFields(object);
50+
////////////////////////////////////////////////////
51+
///// write operations /////
52+
////////////////////////////////////////////////////
4953

54+
public byte[] write(Object object) throws IOException {
5055
ByteArrayOutputStream os = new ByteArrayOutputStream();
5156
CodedOutputStream cos = CodedOutputStream.newInstance(os);
5257

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+
5386
for(MessageField field : messageFields.values()) {
5487
field.write(cos);
5588
}
5689

5790
cos.flush();
58-
return os.toByteArray();
5991
}
6092

93+
///////////////////////////////////////////////////
94+
///// read operations /////
95+
///////////////////////////////////////////////////
96+
6197
public Object read(byte[] data) throws IOException {
6298
return read(data, Object.class);
6399
}
64100

65101
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 {
66146
T object = createInstance(clazz);
67147

68148
if(object == null) {
@@ -71,13 +151,12 @@ public <T> T read(byte[] data, Class<T> clazz) throws IOException {
71151

72152
initMessageFields(object);
73153

74-
CodedInputStream cis = CodedInputStream.newInstance(data);
75-
76154
while (!cis.isAtEnd()) {
77155
int tag = cis.readTag();
156+
int fieldNumber = tag >> 3;
78157

79-
if(messageFields.containsKey(tag >> 3)) {
80-
messageFields.get(tag >> 3).read(cis);
158+
if(messageFields.containsKey(fieldNumber)) {
159+
messageFields.get(fieldNumber).read(cis);
81160
} else {
82161
cis.skipField(tag);
83162
}

‎src/test/java/com/rumpf/mapper/PbObjectMapperTest.java

-2
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,6 @@ public void writeObjectWithObjects() throws Exception {
132132
buffer.put((byte) 0x10); // tag
133133
buffer.put((byte) 0x00); // MOBILE
134134

135-
136-
137135
byte[] result = mapper.write(person);
138136

139137
Assertions.assertArrayEquals(expected, result);

0 commit comments

Comments
 (0)