Hello,
I have a protobuf message like this into a byte array:
1: { // META element
1: 2
2: 1
3: 1
4: { // CutOffTime element within META
1: 10
2: 3
}
5: 1
6: 40
}
2: 9836 // HOTEL element
3: 724 // MARKET element
We need to traverse this message and write it to a 'CodeOutputStream', but
changing the values of the 'cutoff' element to, for example to '4: { 1: 7,
2: 4 }'. I'm not able to do it, I need some help.
A basic algorithm that writes the same protobuf to another byte array but
without changing anything works. Here I try the special case of 'META' (key
= 1) that contains the 'cutoff' element.
Map<Integer, UnknownFieldSet.Field> rootFields =
UnknownFieldSet.parseFrom(document).asMap();
for (Map.Entry<Integer, UnknownFieldSet.Field> entry :
rootFields.entrySet()) {
if (entry.getKey() == 1) {
ByteString bs =
entry.getValue().getLengthDelimitedList().get(0);
output.writeByteArray(1, bs.toByteArray());
}
else {
entry.getValue().writeTo(entry.getKey(), output);
}
}
Now I try to go a bit further, trying to read the cuotff element, change
the values and rewrite them to the 'output'. And here is when I'm not able
to solve the problem. Below my try that does not work, it generates a byte
array
of 69 bytes instead of 73 (I'm losing 4 bytes):
Map<Integer, UnknownFieldSet.Field> rootFields =
UnknownFieldSet.parseFrom(document).asMap();
for (Map.Entry<Integer, UnknownFieldSet.Field> entry :
rootFields.entrySet()) {
if (entry.getKey() == 1) {
ByteString bs =
entry.getValue().getLengthDelimitedList().get(0);
Map<Integer, UnknownFieldSet.Field> ufs =
UnknownFieldSet.parseFrom(bs).asMap();
for (Map.Entry<Integer, UnknownFieldSet.Field> item :
ufs.entrySet()) {
if (item.getKey() == 4) {
Map<Integer, UnknownFieldSet.Field> cutoff =
UnknownFieldSet.parseFrom(item.getValue().getLengthDelimitedList().get(0)).asMap();
cutoff.put(1,
UnknownFieldSet.Field.newBuilder().addVarint(7).build()).writeTo(1, output);
cutoff.put(2,
UnknownFieldSet.Field.newBuilder().addVarint(4).build()).writeTo(1, output);
}
else {
item.getValue().writeTo(item.getKey(), output);
}
}
}
else {
entry.getValue().writeTo(entry.getKey(), output);
}
}
Any help would be really appreciated.
Thanks,
Joan.
--
You received this message because you are subscribed to the Google Groups
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/protobuf/844d51b7-649b-4fcc-8584-e4f139134fean%40googlegroups.com.