| 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.codec.Data; |
| 27 |
|
| 28 |
class LongElement extends AtomicElement<Long> |
| 29 |
{ |
| 30 |
|
| 31 |
private final long _value; |
| 32 |
|
| 33 |
LongElement(Element parent, Element prev, long l) |
| 34 |
{ |
| 35 |
super(parent, prev); |
| 36 |
_value = l; |
| 37 |
} |
| 38 |
|
| 39 |
@Override |
| 40 |
public int size() |
| 41 |
{ |
| 42 |
if(isElementOfArray()) |
| 43 |
{ |
| 44 |
final ArrayElement parent = (ArrayElement) parent(); |
| 45 |
|
| 46 |
if(parent.constructorType() == ArrayElement.SMALL) |
| 47 |
{ |
| 48 |
if(-128l <= _value && _value <= 127l) |
| 49 |
{ |
| 50 |
return 1; |
| 51 |
} |
| 52 |
else |
| 53 |
{ |
| 54 |
parent.setConstructorType(ArrayElement.LARGE); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
return 8; |
| 59 |
|
| 60 |
} |
| 61 |
else |
| 62 |
{ |
| 63 |
return (-128l <= _value && _value <= 127l) ? 2 : 9; |
| 64 |
} |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public Long getValue() |
| 70 |
{ |
| 71 |
return _value; |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public Data.DataType getDataType() |
| 76 |
{ |
| 77 |
return Data.DataType.LONG; |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public int encode(ByteBuffer b) |
| 82 |
{ |
| 83 |
int size = size(); |
| 84 |
if(size > b.remaining()) |
| 85 |
{ |
| 86 |
return 0; |
| 87 |
} |
| 88 |
switch(size) |
| 89 |
{ |
| 90 |
case 2: |
| 91 |
b.put((byte)0x55); |
| 92 |
case 1: |
| 93 |
b.put((byte)_value); |
| 94 |
break; |
| 95 |
case 9: |
| 96 |
b.put((byte)0x81); |
| 97 |
case 8: |
| 98 |
b.putLong(_value); |
| 99 |
|
| 100 |
} |
| 101 |
return size; |
| 102 |
} |
| 103 |
} |