X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2Fpersisted%2FAbstractVersionedShardDataTreeSnapshot.java;h=7ebb0055f2da2c1fcbbd9f8843ca2abc0c65e560;hb=90735c91ce3390a731afc446b4e7314c544ab9d6;hp=0d92eac586c023444bf95ed6b7dd34394ae477cc;hpb=b70b396725749d3fd6ca761f02f4b630f6f4f1ce;p=controller.git diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/persisted/AbstractVersionedShardDataTreeSnapshot.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/persisted/AbstractVersionedShardDataTreeSnapshot.java index 0d92eac586..7ebb0055f2 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/persisted/AbstractVersionedShardDataTreeSnapshot.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/persisted/AbstractVersionedShardDataTreeSnapshot.java @@ -8,12 +8,9 @@ package org.opendaylight.controller.cluster.datastore.persisted; import com.google.common.base.Verify; -import java.io.ByteArrayOutputStream; -import java.io.DataInputStream; -import java.io.DataOutputStream; import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; +import java.io.ObjectInput; +import java.io.ObjectOutput; import java.util.Optional; import javax.annotation.Nonnull; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; @@ -29,13 +26,14 @@ import org.slf4j.LoggerFactory; abstract class AbstractVersionedShardDataTreeSnapshot extends ShardDataTreeSnapshot { private static final Logger LOG = LoggerFactory.getLogger(AbstractVersionedShardDataTreeSnapshot.class); - static ShardDataTreeSnapshot deserialize(final DataInputStream is) throws IOException { - final PayloadVersion version = PayloadVersion.readFrom(is); + @SuppressWarnings("checkstyle:FallThrough") + static ShardDataTreeSnapshot versionedDeserialize(final ObjectInput in) throws IOException { + final PayloadVersion version = PayloadVersion.readFrom(in); switch (version) { case BORON: // Boron snapshots use Java Serialization - try (final ObjectInputStream ois = new ObjectInputStream(is)) { - return (ShardDataTreeSnapshot) ois.readObject(); + try { + return (ShardDataTreeSnapshot) in.readObject(); } catch (ClassNotFoundException e) { LOG.error("Failed to serialize data tree snapshot", e); throw new IOException("Snapshot failed to deserialize", e); @@ -44,6 +42,8 @@ abstract class AbstractVersionedShardDataTreeSnapshot extends ShardDataTreeSnaps case TEST_PAST_VERSION: // These versions are never returned and this code is effectively dead break; + default: + throw new IOException("Invalid payload version in snapshot"); } // Not included as default in above switch to ensure we get warnings when new versions are added @@ -60,42 +60,37 @@ abstract class AbstractVersionedShardDataTreeSnapshot extends ShardDataTreeSnaps * * @return The root node. */ - abstract @Nonnull NormalizedNode rootNode(); + @Nonnull + abstract NormalizedNode rootNode(); /** * Return the snapshot payload version. Implementations of this method should return a constant. * * @return Snapshot payload version */ - abstract @Nonnull PayloadVersion version(); + @Nonnull + abstract PayloadVersion version(); - private void versionedSerialize(final DataOutputStream dos, final PayloadVersion version) throws IOException { + private void versionedSerialize(final ObjectOutput out, final PayloadVersion version) throws IOException { switch (version) { case BORON: // Boron snapshots use Java Serialization - try (ObjectOutputStream oos = new ObjectOutputStream(dos)) { - oos.writeObject(this); - } + out.writeObject(this); return; case TEST_FUTURE_VERSION: case TEST_PAST_VERSION: break; - + default: + throw new IOException("Invalid payload version in snapshot"); } throw new IOException("Encountered unhandled version" + version); } @Override - public final byte[] serialize() throws IOException { - try (final ByteArrayOutputStream bos = new ByteArrayOutputStream()) { - try (final DataOutputStream dos = new DataOutputStream(bos)) { - final PayloadVersion version = version(); - version.writeTo(dos); - versionedSerialize(dos, version); - } - - return bos.toByteArray(); - } + public void serialize(final ObjectOutput out) throws IOException { + final PayloadVersion version = version(); + version.writeTo(out); + versionedSerialize(out, version); } }