OmniBuf
for Java 21.
Pure Java 21 library on Maven Central with a companion Buf plugin. Works with annotated records and POJOs, standard Protobuf Java messages, and generated codecs that inspect or splice fields directly inside byte arrays.
com.omnibuf:omnibuf-java:0.1.1Three ways to use OmniBuf in Java.
All three produce identical wire bytes in standard safe Java 21.
Annotated Java 21 records and POJOs
@OmniBufMessage and @OmniBufField map record components or class fields onto explicit wire numbers and WireKind overrides. OmniBufMapper caches field descriptors per Class<?> on first call.
import com.omnibuf.OmniBufMapper;
import com.omnibuf.annotation.OmniBufField;
import com.omnibuf.annotation.OmniBufMessage;
import com.omnibuf.annotation.WireKind;
@OmniBufMessage(name = "SensorReading")
public record SensorReading(
@OmniBufField(id = 1, name = "sensor_id") String sensorId,
@OmniBufField(id = 2, name = "delta_mk", wire = WireKind.SINT) int deltaMk,
@OmniBufField(id = 3, name = "healthy") boolean healthy
) {}
byte[] pkt = OmniBufMapper.marshal(new SensorReading("rack-12", -45, true));
SensorReading out = OmniBufMapper.unmarshal(pkt, SensorReading.class);Standard protobuf-java messages
Services with existing com.google.protobuf.Message builders and gRPC stubs pass them straight to OmniBufProtobuf.marshal and OmniBufProtobuf.unmarshal. Presence, oneof unions, maps, and Editions descriptors preserve exact semantics.
import com.omnibuf.OmniBufProtobuf;
import com.omnibuf.pb.bench.PeerUpdate;
byte[] pkt = OmniBufProtobuf.marshal(protoPeer);
PeerUpdate out = OmniBufProtobuf.unmarshal(pkt, PeerUpdate.getDefaultInstance());Generated classes with protoc-gen-omnibuf-java
protoc-gen-omnibuf-java emits mutable model classes, static <Msg>OmniBuf codecs, and Opaque<Msg> views over raw byte[] packets. Checking a nested string and splicing a 64-bit sequence field takes 294 ns end-to-end on OpenJDK 21.
version: v2
clean: true
plugins:
- protoc_builtin: java
out: src/main/gen
- local: ./bin/protoc-gen-omnibuf-java
out: src/main/gen$ buf lint
$ buf generateimport com.omnibuf.pb.bench.OmniBufPeerUpdate;
import com.omnibuf.pb.bench.OpaquePeerUpdate;
import com.omnibuf.pb.bench.PeerUpdateOmniBuf;
byte[] pkt = PeerUpdateOmniBuf.marshal(msg);
OpaquePeerUpdate view = new OpaquePeerUpdate(pkt);
String city = view.getLocation().getCity();
byte[] updated = view.setSequence(9_876_543_211L).marshal();Java 21 benchmarks.
11-field PeerUpdate workload on OpenJDK 21.0.13 (C2 JIT) across com.omnibuf, protobuf-java, FlatBuffers, Jackson JSON, and Jackson MessagePack. Shorter bars = Faster.
Serializing PeerUpdate into a fresh byte[] across generated OmniBuf, protobuf-java, FlatBuffers, and Jackson.
Decoding byte[] into a fresh PeerUpdate object and reading all 11 fields.
Reading a nested city string via FlatBuffers vtables or OpaquePeerUpdate vs full decode.
Checking a nested city string and splicing an updated sequence counter via OpaquePeerUpdate vs full decode and re-encode.
Serializing a 39 KiB packet with >16 KiB nested strings. Immutable protobuf-java reuses cached ByteString UTF-8 bytes and memoizedSerializedSize.
Growing a nested string field past 16 KiB ahead of 32 trailing Neighbor entries via Opaque splicing vs full decode and re-encode.
Benchmark Environment
Measured on Eclipse Temurin OpenJDK 21.0.13 after C2 JIT warmup on an Apple M1 Pro Mac. Every decode benchmark reads all 11 fields into a volatile checksum sink. Immutable protobuf-java instances cache UTF-8 ByteString slices and memoizedSerializedSize after the first pass, giving it an advantage on repeated encodes of unchanged 39 KiB messages.
Benchmark source lives in BenchmarkMain.java, with recorded output in testdata/benchmarks.json.
Java 21 questions.
Why does protobuf-java lead on the 39 KiB Large Embedded Message Encode test?
Immutable protobuf-java messages cache memoizedSerializedSize and UTF-8 ByteString slices. Repeated toByteArray() calls allocate the exact 39,227-byte array upfront and copy cached bytes in one pass (4.38 µs). Mutable OmniBuf classes never cache stale sizes, streaming UTF-8 into a pooled encoder and copying out in 5.04 µs. OmniBuf leads every other Java benchmark, up to about 8x faster on single-field reads.
Can OmniBufMapper reuse mutable POJO instances on decode?
Java 21 record types construct fresh instances via canonical constructors through OmniBufMapper.unmarshal(pkt, RecordClass.class). Mutable POJOs also accept OmniBufMapper.unmarshal(pkt, existingInstance) to clear fields and decode into an existing object.
What does OmniBufBlind output on raw packets?
OmniBufBlind.decodeBlind(pkt) returns a Map<String, Object> with synthetic field_1, field_2, ... keys on compact packets, or resolves schema field names when encoded with MarshalOptions.SELF_DESCRIBING.
import com.omnibuf.MarshalOptions;
import com.omnibuf.OmniBufBlind;
byte[] pkt = PeerUpdateOmniBuf.marshal(msg, MarshalOptions.SELF_DESCRIBING);
String json = OmniBufBlind.decodeBlindJson(pkt);