Debian Codegraph @

proton-j/src/main/java/org/apache/qpid/proton/codec/impl/SymbolElement.java

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21
22 package org.apache.qpid.proton.codec.impl;
23
24 import java.nio.ByteBuffer;
25 import java.nio.charset.Charset;
26
27 import org.apache.qpid.proton.amqp.Binary;
28 import org.apache.qpid.proton.amqp.Symbol;
29 import org.apache.qpid.proton.codec.Data;
30
31 class SymbolElement extends AtomicElement<Symbol>
32 {
33
34 private static final Charset ASCII = Charset.forName("US-ASCII");
35 private final Symbol _value;
36
37 SymbolElement(Element parent, Element prev, Symbol s)
38 {
39 super(parent, prev);
40 _value = s;
41 }
42
43 @Override
44 public int size()
45 {
46 final int length = _value.length();
47
48 if(isElementOfArray())
49 {
50 final ArrayElement parent = (ArrayElement) parent();
51
52 if(parent.constructorType() == ArrayElement.SMALL)
53 {
54 if(length > 255)
55 {
56 parent.setConstructorType(ArrayElement.LARGE);
57 return 4+length;
58 }
59 else
60 {
61 return 1+length;
62 }
63 }
64 else
65 {
66 return 4+length;
67 }
68 }
69 else
70 {
71 if(length >255)
72 {
73 return 5 + length;
74 }
75 else
76 {
77 return 2 + length;
78 }
79 }
80 }
81
82 @Override
83 public Symbol getValue()
84 {
85 return _value;
86 }
87
88 @Override
89 public Data.DataType getDataType()
90 {
91 return Data.DataType.SYMBOL;
92 }
93
94 @Override
95 public int encode(ByteBuffer b)
96 {
97 int size = size();
98 if(b.remaining()<size)
99 {
100 return 0;
101 }
102 if(isElementOfArray())
103 {
104 final ArrayElement parent = (ArrayElement) parent();
105
106 if(parent.constructorType() == ArrayElement.SMALL)
107 {
108 b.put((byte)_value.length());
109 }
110 else
111 {
112 b.putInt(_value.length());
113 }
114 }
115 else if(_value.length()<=255)
116 {
117 b.put((byte)0xa3);
118 b.put((byte)_value.length());
119 }
120 else
121 {
122 b.put((byte)0xb3);
123 b.put((byte)_value.length());
124 }
125 b.put(_value.toString().getBytes(ASCII));
126 return size;
127
128 }
129 }