2 * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.datastore.node.utils.stream;
10 import static java.util.Objects.requireNonNull;
12 import java.io.DataInput;
13 import java.io.IOException;
15 final class VersionedNormalizedNodeDataInput extends ForwardingNormalizedNodeDataInput {
16 private DataInput input;
17 private NormalizedNodeDataInput delegate;
19 VersionedNormalizedNodeDataInput(final DataInput input) {
20 this.input = requireNonNull(input);
24 NormalizedNodeDataInput delegate() throws IOException {
25 if (delegate != null) {
29 final byte marker = input.readByte();
30 if (marker != TokenTypes.SIGNATURE_MARKER) {
31 throw defunct("Invalid signature marker: %d", marker);
34 final short version = input.readShort();
35 final NormalizedNodeDataInput ret;
37 case TokenTypes.LITHIUM_VERSION:
38 ret = new LithiumNormalizedNodeInputStreamReader(input);
40 case TokenTypes.NEON_SR2_VERSION:
41 ret = new NeonSR2NormalizedNodeInputStreamReader(input);
44 throw defunct("Unhandled stream version %s", version);
51 private InvalidNormalizedNodeStreamException defunct(final String format, final Object... args) {
52 final InvalidNormalizedNodeStreamException ret = new InvalidNormalizedNodeStreamException(
53 String.format(format, args));
54 // Make sure the stream is not touched
55 setDelegate(new ForwardingNormalizedNodeDataInput() {
57 NormalizedNodeDataInput delegate() throws IOException {
58 throw new InvalidNormalizedNodeStreamException("Stream is not usable", ret);
64 private void setDelegate(final NormalizedNodeDataInput delegate) {
65 this.delegate = requireNonNull(delegate);