2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. 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
9 package org.opendaylight.controller.cluster.datastore.node.utils.stream;
11 import com.google.common.base.Preconditions;
12 import java.io.DataOutput;
13 import java.io.DataOutputStream;
14 import java.io.IOException;
15 import java.io.OutputStream;
16 import java.util.HashMap;
18 import org.opendaylight.yangtools.yang.common.QName;
21 * NormalizedNodeOutputStreamWriter will be used by distributed datastore to send normalized node in
23 * A stream writer wrapper around this class will write node objects to stream in recursive manner.
24 * for example - If you have a ContainerNode which has a two LeafNode as children, then
26 * {@link #startContainerNode(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier, int)},
28 * {@link #leafNode(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier, Object)} twice
29 * and then, {@link #endNode()} to end container node.
31 * Based on the each node, the node type is also written to the stream, that helps in reconstructing the object,
34 public class NormalizedNodeOutputStreamWriter extends AbstractNormalizedNodeDataOutput {
35 private final Map<String, Integer> stringCodeMap = new HashMap<>();
38 * @deprecated Use {@link #NormalizedNodeOutputStreamWriter(DataOutput)} instead.
41 public NormalizedNodeOutputStreamWriter(final OutputStream stream) throws IOException {
42 this((DataOutput) new DataOutputStream(Preconditions.checkNotNull(stream)));
46 * @deprecated Use {@link NormalizedNodeInputOutput#newDataOutput(DataOutput)} instead.
49 public NormalizedNodeOutputStreamWriter(final DataOutput output) {
54 protected final short streamVersion() {
55 return TokenTypes.LITHIUM_VERSION;
59 protected void writeQName(final QName qname) throws IOException {
60 writeString(qname.getLocalName());
61 writeString(qname.getNamespace().toString());
62 writeString(qname.getFormattedRevision());
66 protected void writeString(final String string) throws IOException {
68 final Integer value = stringCodeMap.get(string);
70 stringCodeMap.put(string, stringCodeMap.size());
71 writeByte(TokenTypes.IS_STRING_VALUE);
74 writeByte(TokenTypes.IS_CODE_VALUE);
78 writeByte(TokenTypes.IS_NULL_VALUE);