private <T> void encodeOptional(DataOutputStream os, Optional<T> ot, Codec<T> codec) throws IOException { if (ot.isPresent()) { os.writeBoolean(true); codec.encode(os, ot.get()); } else { os.writeBoolean(false); } }
private <T> Optional<T> decodeOptional(DataInputStream is, Codec<T> codec) throws IOException { return is.readBoolean() ? Optional.of(codec.decode(is)) : Optional.empty(); }