| 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.UnsignedInteger; |
| 27 |
import org.apache.qpid.proton.codec.Data; |
| 28 |
|
| 29 |
class UnsignedIntegerElement extends AtomicElement<UnsignedInteger> |
| 30 |
{ |
| 31 |
|
| 32 |
private final UnsignedInteger _value; |
| 33 |
|
| 34 |
UnsignedIntegerElement(Element parent, Element prev, UnsignedInteger i) |
| 35 |
{ |
| 36 |
super(parent, prev); |
| 37 |
_value = i; |
| 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.intValue() == 0) |
| 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(0 <= _value.intValue() && _value.intValue() <= 255) |
| 61 |
{ |
| 62 |
return 1; |
| 63 |
} |
| 64 |
else |
| 65 |
{ |
| 66 |
parent.setConstructorType(ArrayElement.LARGE); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
return 4; |
| 71 |
|
| 72 |
} |
| 73 |
else |
| 74 |
{ |
| 75 |
return 0 == _value.intValue() ? 1 : (1 <= _value.intValue() && _value.intValue() <= 255) ? 2 : 5; |
| 76 |
} |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public UnsignedInteger getValue() |
| 82 |
{ |
| 83 |
return _value; |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public Data.DataType getDataType() |
| 88 |
{ |
| 89 |
return Data.DataType.UINT; |
| 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.intValue()); |
| 106 |
} |
| 107 |
else |
| 108 |
{ |
| 109 |
b.put((byte)0x43); |
| 110 |
} |
| 111 |
break; |
| 112 |
case 2: |
| 113 |
b.put((byte)0x52); |
| 114 |
b.put((byte)_value.intValue()); |
| 115 |
break; |
| 116 |
case 5: |
| 117 |
b.put((byte)0x70); |
| 118 |
case 4: |
| 119 |
b.putInt(_value.intValue()); |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
return size; |
| 124 |
} |
| 125 |
} |