Skip to content

Commit bf7fe6b

Browse files
committed
PbObjectMapper with File support
1 parent c04260e commit bf7fe6b

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<groupId>org.junit.jupiter</groupId>
3434
<artifactId>junit-jupiter-api</artifactId>
3535
<version>5.7.0</version>
36+
<scope>test</scope>
3637
</dependency>
3738
<dependency>
3839
<groupId>org.junit.jupiter</groupId>

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

+13-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
import com.rumpf.proto.field.MessageField;
77
import com.rumpf.proto.field.MessageFieldFactory;
88

9-
import java.io.ByteArrayOutputStream;
10-
import java.io.IOException;
11-
import java.io.InputStream;
12-
import java.io.OutputStream;
9+
import java.io.*;
1310
import java.lang.reflect.Constructor;
1411
import java.lang.reflect.InvocationTargetException;
1512
import java.nio.ByteBuffer;
@@ -72,6 +69,10 @@ public void write(Object object, ByteBuffer buffer) throws IOException {
7269
write(object, CodedOutputStream.newInstance(buffer));
7370
}
7471

72+
public void write(Object object, File file) throws IOException {
73+
write(object, new FileOutputStream(file));
74+
}
75+
7576
public void write(Object object, OutputStream output) throws IOException {
7677
write(object, CodedOutputStream.newInstance(output));
7778
}
@@ -118,6 +119,14 @@ public <T> T read(ByteBuffer buf, Class<T> clazz) throws IOException {
118119
return read(CodedInputStream.newInstance(buf), clazz);
119120
}
120121

122+
public Object read(File source) throws IOException {
123+
return read(source, Object.class);
124+
}
125+
126+
public <T> T read(File source, Class<T> clazz) throws IOException {
127+
return read(new FileInputStream(source), clazz);
128+
}
129+
121130
public Object read(InputStream input) throws IOException {
122131
return read(input, Object.class);
123132
}

0 commit comments

Comments
 (0)