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