Source:
private static byte[] serializeVersion3(LedgerMetadata metadata) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); writeHeader(os, 3); LedgerMetadataFormat.Builder builder = LedgerMetadataFormat.newBuilder(); builder.setQuorumSize(metadata.getWriteQuorumSize()) .setAckQuorumSize(metadata.getAckQuorumSize()) .setEnsembleSize(metadata.getEnsembleSize()) .setLength(metadata.getLength()) .setLastEntryId(metadata.getLastEntryId()); switch (metadata.getState()) { case CLOSED: builder.setState(LedgerMetadataFormat.State.CLOSED); break; case IN_RECOVERY: builder.setState(LedgerMetadataFormat.State.IN_RECOVERY); break; case OPEN: builder.setState(LedgerMetadataFormat.State.OPEN); break; default: checkArgument(false, String.format("Unknown state %s for protobuf serialization", metadata.getState())); break; } builder.setCtime(metadata.getCtime()); builder.setDigestType(apiToProtoDigestType(metadata.getDigestType())) .setPassword(ByteString.copyFrom(metadata.getPassword())); Map<String, byte[]> customMetadata = metadata.getCustomMetadata(); if (customMetadata.size() > 0) { LedgerMetadataFormat.cMetadataMapEntry.Builder cMetadataBuilder = LedgerMetadataFormat.cMetadataMapEntry.newBuilder(); for (Map.Entry<String, byte[]> entry : customMetadata.entrySet()) { cMetadataBuilder.setKey(entry.getKey()).setValue(ByteString.copyFrom(entry.getValue())); builder.addCustomMetadata(cMetadataBuilder.build()); } } for (Map.Entry<Long, ? extends List<BookieSocketAddress>> entry : metadata.getAllEnsembles().entrySet()) { LedgerMetadataFormat.Segment.Builder segmentBuilder = LedgerMetadataFormat.Segment.newBuilder(); segmentBuilder.setFirstEntryId(entry.getKey()); for (BookieSocketAddress addr : entry.getValue()) { segmentBuilder.addEnsembleMember(addr.toString()); } builder.addSegment(segmentBuilder.build()); } builder.build().writeDelimitedTo(os); return os.toByteArray(); }

Target:
private static byte[] serializeVersion3(LedgerMetadata metadata) throws IOException { try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { writeHeader(os, METADATA_FORMAT_VERSION_3); LedgerMetadataFormat.Builder builder = LedgerMetadataFormat.newBuilder(); builder.setQuorumSize(metadata.getWriteQuorumSize()) .setAckQuorumSize(metadata.getAckQuorumSize()) .setEnsembleSize(metadata.getEnsembleSize()) .setLength(metadata.getLength()) .setLastEntryId(metadata.getLastEntryId()); switch (metadata.getState()) { case CLOSED: builder.setState(LedgerMetadataFormat.State.CLOSED); break; case IN_RECOVERY: builder.setState(LedgerMetadataFormat.State.IN_RECOVERY); break; case OPEN: builder.setState(LedgerMetadataFormat.State.OPEN); break; default: checkArgument(false, String.format("Unknown state %s for protobuf serialization", metadata.getState())); break; } builder.setCtime(metadata.getCtime()); builder.setDigestType(apiToProtoDigestType(metadata.getDigestType())); serializePassword(metadata.getPassword(), builder); Map<String, byte[]> customMetadata = metadata.getCustomMetadata(); if (customMetadata.size() > 0) { LedgerMetadataFormat.cMetadataMapEntry.Builder cMetadataBuilder = LedgerMetadataFormat.cMetadataMapEntry.newBuilder(); for (Map.Entry<String, byte[]> entry : customMetadata.entrySet()) { cMetadataBuilder.setKey(entry.getKey()).setValue(ByteString.copyFrom(entry.getValue())); builder.addCustomMetadata(cMetadataBuilder.build()); } } for (Map.Entry<Long, ? extends List<BookieSocketAddress>> entry : metadata.getAllEnsembles().entrySet()) { LedgerMetadataFormat.Segment.Builder segmentBuilder = LedgerMetadataFormat.Segment.newBuilder(); segmentBuilder.setFirstEntryId(entry.getKey()); for (BookieSocketAddress addr : entry.getValue()) { segmentBuilder.addEnsembleMember(addr.toString()); } builder.addSegment(segmentBuilder.build()); } builder.build().writeDelimitedTo(os); return os.toByteArray(); } }