Remove InputStream constructors
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / stream / NormalizedNodeOutputStreamWriter.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.datastore.node.utils.stream;
10
11 import java.io.DataOutput;
12 import java.io.IOException;
13 import java.util.HashMap;
14 import java.util.Map;
15 import org.opendaylight.yangtools.yang.common.QName;
16
17 /**
18  * NormalizedNodeOutputStreamWriter will be used by distributed datastore to send normalized node in
19  * a stream.
20  * A stream writer wrapper around this class will write node objects to stream in recursive manner.
21  * for example - If you have a ContainerNode which has a two LeafNode as children, then
22  * you will first call
23  * {@link #startContainerNode(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier, int)},
24  * then will call
25  * {@link #leafNode(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier, Object)} twice
26  * and then, {@link #endNode()} to end container node.
27  *
28  * Based on the each node, the node type is also written to the stream, that helps in reconstructing the object,
29  * while reading.
30  */
31 public class NormalizedNodeOutputStreamWriter extends AbstractNormalizedNodeDataOutput {
32     private final Map<String, Integer> stringCodeMap = new HashMap<>();
33
34     /**
35      * @deprecated Use {@link NormalizedNodeInputOutput#newDataOutput(DataOutput)} instead.
36      */
37     @Deprecated
38     public NormalizedNodeOutputStreamWriter(final DataOutput output) {
39         super(output);
40     }
41
42     @Override
43     protected final short streamVersion() {
44         return TokenTypes.LITHIUM_VERSION;
45     }
46
47     @Override
48     protected void writeQName(final QName qname) throws IOException {
49         writeString(qname.getLocalName());
50         writeString(qname.getNamespace().toString());
51         writeString(qname.getFormattedRevision());
52     }
53
54     @Override
55     protected void writeString(final String string) throws IOException {
56         if (string != null) {
57             final Integer value = stringCodeMap.get(string);
58             if (value == null) {
59                 stringCodeMap.put(string, stringCodeMap.size());
60                 writeByte(TokenTypes.IS_STRING_VALUE);
61                 writeUTF(string);
62             } else {
63                 writeByte(TokenTypes.IS_CODE_VALUE);
64                 writeInt(value);
65             }
66         } else {
67             writeByte(TokenTypes.IS_NULL_VALUE);
68         }
69     }
70 }