Thanks! A simple example to do that in the docs would be very useful.
This is what I did and it works exactly as I wanted it to. Code
in red below, shows the changes:
public class TestPerson {
public static void main(String[] args) throws IOException {
Person p1 = Person.newBuilder()
.setField1("52114768")
.setField2("54741573")
.setField3("36702831")
.setField4("1477556")
.setField5("44421078")
.setField6("48688590")
.setField7("21657641")
.setField8("51566604")
.setField9("19799113")
.setField10("31045088")
.setField11("51944937")
.setField12("18035997")
.setField13("47693781")
.setField14("44247784")
.setField15("61035138")
.setField16("62112467")
.setField17("44173609")
.setField18("45795251")
.setField19("38467797")
.setField20("18354530")
.build();
FileOutputStream fos = new FileOutputStream(args[0]);
fos.write(p1.toByteArray());
fos.close();
Person person = Person.parseFrom(new FileInputStream(args[0]));
System.out.println("Read into " + person.getClass().getName() + " from " + args[0]);
System.out.println(person);
System.out.println("Bytes length " + person.getSerializedSize());
System.out.println();
PersonLite personLite = PersonLite.parseFrom(new FileInputStream(args[0]));
System.out.println("Read into " + personLite.getClass().getName() + " from " + args[0]);
System.out.println(personLite);
System.out.println("Bytes length " + personLite.getSerializedSize());
System.out.println();
Builder personLiteBuilder2 = PersonLite.newBuilder().mergeFrom(new FileInputStream(args[0]));
personLiteBuilder2.setUnknownFields(UnknownFieldSet.getDefaultInstance());
PersonLite personLite2 = personLiteBuilder2.build();
System.out.println("Read into " + personLite2.getClass().getName() + " from " + args[0]);
System.out.println(personLite2);
System.out.println("Bytes length " + personLite2.getSerializedSize());
}
}
Output:Read into com.javaforu.demo.protobuf.DemoProtos$Person from D:\Dump\protobuf\in_out\person1.datfield_1: "52114768"field_2: "54741573"field_3: "36702831"field_4: "1477556"field_5: "44421078"field_6: "48688590"field_7: "21657641"field_8: "51566604"field_9: "19799113"field_10: "31045088"field_11: "51944937"field_12: "18035997"field_13: "47693781"field_14: "44247784"field_15: "61035138"field_16: "62112467"field_17: "44173609"field_18: "45795251"field_19: "38467797"field_20: "18354530"Bytes length 204Read into com.javaforu.demo.protobuf.DemoProtos$PersonLite from D:\Dump\protobuf\in_out\person1.datfield_1: "52114768"field_5: "44421078"field_10: "31045088"field_15: "61035138"field_20: "18354530"2: "54741573"3: "36702831"4: "1477556"6: "48688590"7: "21657641"8: "51566604"9: "19799113"11: "51944937"12: "18035997"13: "47693781"14: "44247784"16: "62112467"17: "44173609"18: "45795251"19: "38467797"Bytes length 204Read into com.javaforu.demo.protobuf.DemoProtos$PersonLite from D:\Dump\protobuf\in_out\person1.datfield_1: "52114768"field_5: "44421078"field_10: "31045088"field_15: "61035138"field_20: "18354530"Bytes length 51Ashwin (
http://www.ashwinjayaprakash.com)