Skip to content

Commit

Permalink
Java Proto Lite: Serialize repeated enums without allocating
Browse files Browse the repository at this point in the history
Serialize repeated enums without allocating

PiperOrigin-RevId: 628915124
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Apr 29, 2024
1 parent b3e7a00 commit de6aba9
Showing 1 changed file with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,38 @@ private void writeDoubleListInternal(int fieldNumber, List<Double> value, boolea
@Override
public void writeEnumList(int fieldNumber, List<Integer> value, boolean packed)
throws IOException {
if (value instanceof IntArrayList) {
writeEnumListInternal(fieldNumber, (IntArrayList) value, packed);
} else {
writeEnumListInternal(fieldNumber, value, packed);
}
}

private void writeEnumListInternal(int fieldNumber, IntArrayList value, boolean packed)
throws IOException {
if (packed) {
output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED);

// Compute and write the length of the data.
int dataSize = 0;
for (int i = 0; i < value.size(); ++i) {
dataSize += CodedOutputStream.computeEnumSizeNoTag(value.getInt(i));
}
output.writeUInt32NoTag(dataSize);

// Write the data itself, without any tags.
for (int i = 0; i < value.size(); ++i) {
output.writeEnumNoTag(value.getInt(i));
}
} else {
for (int i = 0; i < value.size(); ++i) {
output.writeEnum(fieldNumber, value.getInt(i));
}
}
}

private void writeEnumListInternal(int fieldNumber, List<Integer> value, boolean packed)
throws IOException {
if (packed) {
output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED);

Expand Down

0 comments on commit de6aba9

Please sign in to comment.