Debian Codegraph @

proton-j/src/main/java/org/apache/qpid/proton/codec/impl/UnsignedLongElement.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
26 import org.apache.qpid.proton.amqp.UnsignedLong;
27 import org.apache.qpid.proton.codec.Data;
28
29 class UnsignedLongElement extends AtomicElement<UnsignedLong>
30 {
31
32 private final UnsignedLong _value;
33
34 UnsignedLongElement(Element parent, Element prev, UnsignedLong ul)
35 {
36 super(parent, prev);
37 _value = ul;
38 }
39
40 @Override
41 public int size()
42 {
43 if(isElementOfArray())
44 {
45 final ArrayElement parent = (ArrayElement) parent();
46 if(parent.constructorType() == ArrayElement.TINY)
47 {
48 if(_value.longValue() == 0l)
49 {
50 return 0;
51 }
52 else
53 {
54 parent.setConstructorType(ArrayElement.SMALL);
55 }
56 }
57
58 if(parent.constructorType() == ArrayElement.SMALL)
59 {
60 if(0l <= _value.longValue() && _value.longValue() <= 255l)
61 {
62 return 1;
63 }
64 else
65 {
66 parent.setConstructorType(ArrayElement.LARGE);
67 }
68 }
69
70 return 8;
71
72 }
73 else
74 {
75 return 0l == _value.longValue() ? 1 : (1l <= _value.longValue() && _value.longValue() <= 255l) ? 2 : 9;
76 }
77
78 }
79
80 @Override
81 public UnsignedLong getValue()
82 {
83 return _value;
84 }
85
86 @Override
87 public Data.DataType getDataType()
88 {
89 return Data.DataType.ULONG;
90 }
91
92 @Override
93 public int encode(ByteBuffer b)
94 {
95 int size = size();
96 if(size > b.remaining())
97 {
98 return 0;
99 }
100 switch(size)
101 {
102 case 1:
103 if(isElementOfArray())
104 {
105 b.put((byte)_value.longValue());
106 }
107 else
108 {
109 b.put((byte)0x44);
110 }
111 break;
112 case 2:
113 b.put((byte)0x53);
114 b.put((byte)_value.longValue());
115 break;
116 case 9:
117 b.put((byte)0x80);
118 case 8:
119 b.putLong(_value.longValue());
120
121 }
122
123 return size;
124 }
125 }