| 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.Binary; |
| 27 |
import org.apache.qpid.proton.codec.Data; |
| 28 |
|
| 29 |
class BinaryElement extends AtomicElement<Binary> |
| 30 |
{ |
| 31 |
|
| 32 |
private final Binary _value; |
| 33 |
|
| 34 |
BinaryElement(Element parent, Element prev, Binary b) |
| 35 |
{ |
| 36 |
super(parent, prev); |
| 37 |
byte[] data = new byte[b.getLength()]; |
| 38 |
System.arraycopy(b.getArray(),b.getArrayOffset(),data,0,b.getLength()); |
| 39 |
_value = new Binary(data); |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
public int size() |
| 44 |
{ |
| 45 |
final int length = _value.getLength(); |
| 46 |
|
| 47 |
if(isElementOfArray()) |
| 48 |
{ |
| 49 |
final ArrayElement parent = (ArrayElement) parent(); |
| 50 |
|
| 51 |
if(parent.constructorType() == ArrayElement.SMALL) |
| 52 |
{ |
| 53 |
if(length > 255) |
| 54 |
{ |
| 55 |
parent.setConstructorType(ArrayElement.LARGE); |
| 56 |
return 4+length; |
| 57 |
} |
| 58 |
else |
| 59 |
{ |
| 60 |
return 1+length; |
| 61 |
} |
| 62 |
} |
| 63 |
else |
| 64 |
{ |
| 65 |
return 4+length; |
| 66 |
} |
| 67 |
} |
| 68 |
else |
| 69 |
{ |
| 70 |
if(length >255) |
| 71 |
{ |
| 72 |
return 5 + length; |
| 73 |
} |
| 74 |
else |
| 75 |
{ |
| 76 |
return 2 + length; |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public Binary getValue() |
| 83 |
{ |
| 84 |
return _value; |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public Data.DataType getDataType() |
| 89 |
{ |
| 90 |
return Data.DataType.BINARY; |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public int encode(ByteBuffer b) |
| 95 |
{ |
| 96 |
int size = size(); |
| 97 |
if(b.remaining()<size) |
| 98 |
{ |
| 99 |
return 0; |
| 100 |
} |
| 101 |
if(isElementOfArray()) |
| 102 |
{ |
| 103 |
final ArrayElement parent = (ArrayElement) parent(); |
| 104 |
|
| 105 |
if(parent.constructorType() == ArrayElement.SMALL) |
| 106 |
{ |
| 107 |
b.put((byte)_value.getLength()); |
| 108 |
} |
| 109 |
else |
| 110 |
{ |
| 111 |
b.putInt(_value.getLength()); |
| 112 |
} |
| 113 |
} |
| 114 |
else if(_value.getLength()<=255) |
| 115 |
{ |
| 116 |
b.put((byte)0xa0); |
| 117 |
b.put((byte)_value.getLength()); |
| 118 |
} |
| 119 |
else |
| 120 |
{ |
| 121 |
b.put((byte)0xb0); |
| 122 |
b.putInt(_value.getLength()); |
| 123 |
} |
| 124 |
b.put(_value.getArray(),_value.getArrayOffset(),_value.getLength()); |
| 125 |
return size; |
| 126 |
|
| 127 |
} |
| 128 |
} |