| 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.DescribedType; |
| 27 |
import org.apache.qpid.proton.amqp.Symbol; |
| 28 |
import org.apache.qpid.proton.codec.Data; |
| 29 |
|
| 30 |
class ArrayElement extends AbstractElement<Object[]> |
| 31 |
{ |
| 32 |
|
| 33 |
private final boolean _described; |
| 34 |
private final Data.DataType _arrayType; |
| 35 |
private ConstructorType _constructorType; |
| 36 |
private Element _first; |
| 37 |
|
| 38 |
|
| 39 |
static enum ConstructorType { TINY, SMALL, LARGE } |
| 40 |
|
| 41 |
|
| 42 |
static ConstructorType TINY = ConstructorType.TINY; |
| 43 |
static ConstructorType SMALL = ConstructorType.SMALL; |
| 44 |
static ConstructorType LARGE = ConstructorType.LARGE; |
| 45 |
|
| 46 |
ArrayElement(Element parent, Element prev, boolean described, Data.DataType type) |
| 47 |
{ |
| 48 |
super(parent, prev); |
| 49 |
_described = described; |
| 50 |
_arrayType = type; |
| 51 |
if(_arrayType == null) |
| 52 |
{ |
| 53 |
throw new NullPointerException("Array type cannot be null"); |
| 54 |
} |
| 55 |
else if(_arrayType == Data.DataType.DESCRIBED) |
| 56 |
{ |
| 57 |
throw new IllegalArgumentException("Array type cannot be DESCRIBED"); |
| 58 |
} |
| 59 |
switch(_arrayType) |
| 60 |
{ |
| 61 |
case UINT: |
| 62 |
case ULONG: |
| 63 |
case LIST: |
| 64 |
setConstructorType(TINY); |
| 65 |
break; |
| 66 |
default: |
| 67 |
setConstructorType(SMALL); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
ConstructorType constructorType() |
| 72 |
{ |
| 73 |
return _constructorType; |
| 74 |
} |
| 75 |
|
| 76 |
void setConstructorType(ConstructorType type) |
| 77 |
{ |
| 78 |
_constructorType = type; |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public int size() |
| 83 |
{ |
| 84 |
ConstructorType oldConstructorType; |
| 85 |
int bodySize; |
| 86 |
int count = 0; |
| 87 |
do |
| 88 |
{ |
| 89 |
bodySize = 1; // data type constructor |
| 90 |
oldConstructorType = _constructorType; |
| 91 |
Element element = _first; |
| 92 |
while(element != null) |
| 93 |
{ |
| 94 |
count++; |
| 95 |
bodySize += element.size(); |
| 96 |
element = element.next(); |
| 97 |
} |
| 98 |
} |
| 99 |
while (oldConstructorType != constructorType()); |
| 100 |
|
| 101 |
if(isDescribed()) |
| 102 |
{ |
| 103 |
bodySize++; // 00 instruction |
| 104 |
if(count != 0) |
| 105 |
{ |
| 106 |
count--; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
if(isElementOfArray()) |
| 111 |
{ |
| 112 |
ArrayElement parent = (ArrayElement)parent(); |
| 113 |
if(parent.constructorType()==SMALL) |
| 114 |
{ |
| 115 |
if(count<=255 && bodySize<=254) |
| 116 |
{ |
| 117 |
bodySize+=2; |
| 118 |
} |
| 119 |
else |
| 120 |
{ |
| 121 |
parent.setConstructorType(LARGE); |
| 122 |
bodySize+=8; |
| 123 |
} |
| 124 |
} |
| 125 |
else |
| 126 |
{ |
| 127 |
bodySize+=8; |
| 128 |
} |
| 129 |
} |
| 130 |
else |
| 131 |
{ |
| 132 |
|
| 133 |
if(count<=255 && bodySize<=254) |
| 134 |
{ |
| 135 |
bodySize+=3; |
| 136 |
} |
| 137 |
else |
| 138 |
{ |
| 139 |
bodySize+=9; |
| 140 |
} |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
return bodySize; |
| 146 |
} |
| 147 |
|
| 148 |
@Override |
| 149 |
public Object[] getValue() |
| 150 |
{ |
| 151 |
if(isDescribed()) |
| 152 |
{ |
| 153 |
DescribedType[] rVal = new DescribedType[(int) count()]; |
| 154 |
Object descriptor = _first == null ? null : _first.getValue(); |
| 155 |
Element element = _first == null ? null : _first.next(); |
| 156 |
int i = 0; |
| 157 |
while(element != null) |
| 158 |
{ |
| 159 |
rVal[i++] = new DescribedTypeImpl(descriptor, element.getValue()); |
| 160 |
element = element.next(); |
| 161 |
} |
| 162 |
return rVal; |
| 163 |
} |
| 164 |
else if(_arrayType == Data.DataType.SYMBOL) |
| 165 |
{ |
| 166 |
Symbol[] rVal = new Symbol[(int) count()]; |
| 167 |
SymbolElement element = (SymbolElement) _first; |
| 168 |
int i = 0; |
| 169 |
while (element!=null) |
| 170 |
{ |
| 171 |
rVal[i++] = element.getValue(); |
| 172 |
element = (SymbolElement) element.next(); |
| 173 |
} |
| 174 |
return rVal; |
| 175 |
} |
| 176 |
else |
| 177 |
{ |
| 178 |
Object[] rVal = new Object[(int) count()]; |
| 179 |
Element element = _first; |
| 180 |
int i = 0; |
| 181 |
while (element!=null) |
| 182 |
{ |
| 183 |
rVal[i++] = element.getValue(); |
| 184 |
element = element.next(); |
| 185 |
} |
| 186 |
return rVal; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
@Override |
| 191 |
public Data.DataType getDataType() |
| 192 |
{ |
| 193 |
return Data.DataType.ARRAY; |
| 194 |
} |
| 195 |
|
| 196 |
@Override |
| 197 |
public int encode(ByteBuffer b) |
| 198 |
{ |
| 199 |
int size = size(); |
| 200 |
|
| 201 |
final int count = (int) count(); |
| 202 |
|
| 203 |
if(b.remaining()>=size) |
| 204 |
{ |
| 205 |
if(!isElementOfArray()) |
| 206 |
{ |
| 207 |
if(size>257 || count >255) |
| 208 |
{ |
| 209 |
b.put((byte)0xf0); |
| 210 |
b.putInt(size-5); |
| 211 |
b.putInt(count); |
| 212 |
} |
| 213 |
else |
| 214 |
{ |
| 215 |
b.put((byte)0xe0); |
| 216 |
b.put((byte)(size-2)); |
| 217 |
b.put((byte)count); |
| 218 |
} |
| 219 |
} |
| 220 |
else |
| 221 |
{ |
| 222 |
ArrayElement parent = (ArrayElement)parent(); |
| 223 |
if(parent.constructorType()==SMALL) |
| 224 |
{ |
| 225 |
b.put((byte)(size-1)); |
| 226 |
b.put((byte)count); |
| 227 |
} |
| 228 |
else |
| 229 |
{ |
| 230 |
b.putInt(size-4); |
| 231 |
b.putInt(count); |
| 232 |
} |
| 233 |
} |
| 234 |
Element element = _first; |
| 235 |
if(isDescribed()) |
| 236 |
{ |
| 237 |
b.put((byte)0); |
| 238 |
if(element == null) |
| 239 |
{ |
| 240 |
b.put((byte)0x40); |
| 241 |
} |
| 242 |
else |
| 243 |
{ |
| 244 |
element.encode(b); |
| 245 |
element = element.next(); |
| 246 |
} |
| 247 |
} |
| 248 |
switch(_arrayType) |
| 249 |
{ |
| 250 |
case NULL: |
| 251 |
b.put((byte)0x40); |
| 252 |
break; |
| 253 |
case BOOL: |
| 254 |
b.put((byte)0x56); |
| 255 |
break; |
| 256 |
case UBYTE: |
| 257 |
b.put((byte)0x50); |
| 258 |
break; |
| 259 |
case BYTE: |
| 260 |
b.put((byte)0x51); |
| 261 |
break; |
| 262 |
case USHORT: |
| 263 |
b.put((byte)0x60); |
| 264 |
break; |
| 265 |
case SHORT: |
| 266 |
b.put((byte)0x61); |
| 267 |
break; |
| 268 |
case UINT: |
| 269 |
switch (constructorType()) |
| 270 |
{ |
| 271 |
case TINY: |
| 272 |
b.put((byte)0x43); |
| 273 |
break; |
| 274 |
case SMALL: |
| 275 |
b.put((byte)0x52); |
| 276 |
break; |
| 277 |
case LARGE: |
| 278 |
b.put((byte)0x70); |
| 279 |
break; |
| 280 |
} |
| 281 |
break; |
| 282 |
case INT: |
| 283 |
b.put(_constructorType == SMALL ? (byte)0x54 : (byte)0x71); |
| 284 |
break; |
| 285 |
case CHAR: |
| 286 |
b.put((byte)0x73); |
| 287 |
break; |
| 288 |
case ULONG: |
| 289 |
switch (constructorType()) |
| 290 |
{ |
| 291 |
case TINY: |
| 292 |
b.put((byte)0x44); |
| 293 |
break; |
| 294 |
case SMALL: |
| 295 |
b.put((byte)0x53); |
| 296 |
break; |
| 297 |
case LARGE: |
| 298 |
b.put((byte)0x80); |
| 299 |
break; |
| 300 |
} |
| 301 |
break; |
| 302 |
case LONG: |
| 303 |
b.put(_constructorType == SMALL ? (byte)0x55 : (byte)0x81); |
| 304 |
break; |
| 305 |
case TIMESTAMP: |
| 306 |
b.put((byte)0x83); |
| 307 |
break; |
| 308 |
case FLOAT: |
| 309 |
b.put((byte)0x72); |
| 310 |
break; |
| 311 |
case DOUBLE: |
| 312 |
b.put((byte)0x82); |
| 313 |
break; |
| 314 |
case DECIMAL32: |
| 315 |
b.put((byte)0x74); |
| 316 |
break; |
| 317 |
case DECIMAL64: |
| 318 |
b.put((byte)0x84); |
| 319 |
break; |
| 320 |
case DECIMAL128: |
| 321 |
b.put((byte)0x94); |
| 322 |
break; |
| 323 |
case UUID: |
| 324 |
b.put((byte)0x98); |
| 325 |
break; |
| 326 |
case BINARY: |
| 327 |
b.put(_constructorType == SMALL ? (byte)0xa0 : (byte)0xb0); |
| 328 |
break; |
| 329 |
case STRING: |
| 330 |
b.put(_constructorType == SMALL ? (byte)0xa1 : (byte)0xb1); |
| 331 |
break; |
| 332 |
case SYMBOL: |
| 333 |
b.put(_constructorType == SMALL ? (byte)0xa3 : (byte)0xb3); |
| 334 |
break; |
| 335 |
case ARRAY: |
| 336 |
b.put(_constructorType == SMALL ? (byte)0xe0 : (byte)0xf0); |
| 337 |
break; |
| 338 |
case LIST: |
| 339 |
b.put(_constructorType == TINY ? (byte)0x45 :_constructorType == SMALL ? (byte)0xc0 : (byte)0xd0); |
| 340 |
break; |
| 341 |
case MAP: |
| 342 |
b.put(_constructorType == SMALL ? (byte)0xc1 : (byte)0xd1); |
| 343 |
break; |
| 344 |
} |
| 345 |
while(element!=null) |
| 346 |
{ |
| 347 |
element.encode(b); |
| 348 |
element = element.next(); |
| 349 |
} |
| 350 |
return size; |
| 351 |
} |
| 352 |
else |
| 353 |
{ |
| 354 |
return 0; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
@Override |
| 359 |
public boolean canEnter() |
| 360 |
{ |
| 361 |
return true; |
| 362 |
} |
| 363 |
|
| 364 |
@Override |
| 365 |
public Element child() |
| 366 |
{ |
| 367 |
return _first; |
| 368 |
} |
| 369 |
|
| 370 |
@Override |
| 371 |
public void setChild(Element elt) |
| 372 |
{ |
| 373 |
_first = elt; |
| 374 |
} |
| 375 |
|
| 376 |
@Override |
| 377 |
public Element addChild(Element element) |
| 378 |
{ |
| 379 |
if(isDescribed() || element.getDataType() == _arrayType) |
| 380 |
{ |
| 381 |
_first = element; |
| 382 |
return element; |
| 383 |
} |
| 384 |
else |
| 385 |
{ |
| 386 |
Element replacement = coerce(element); |
| 387 |
if(replacement != null) |
| 388 |
{ |
| 389 |
_first = replacement; |
| 390 |
return replacement; |
| 391 |
} |
| 392 |
throw new IllegalArgumentException("Attempting to add instance of " + element.getDataType() + " to array of " + _arrayType); |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
private Element coerce(Element element) |
| 397 |
{ |
| 398 |
switch (_arrayType) |
| 399 |
{ |
| 400 |
case INT: |
| 401 |
int i; |
| 402 |
switch (element.getDataType()) |
| 403 |
{ |
| 404 |
case BYTE: |
| 405 |
i = ((ByteElement)element).getValue().intValue(); |
| 406 |
break; |
| 407 |
case SHORT: |
| 408 |
i = ((ShortElement)element).getValue().intValue(); |
| 409 |
break; |
| 410 |
case LONG: |
| 411 |
i = ((LongElement)element).getValue().intValue(); |
| 412 |
break; |
| 413 |
default: |
| 414 |
return null; |
| 415 |
} |
| 416 |
return new IntegerElement(element.parent(),element.prev(),i); |
| 417 |
|
| 418 |
case LONG: |
| 419 |
long l; |
| 420 |
switch (element.getDataType()) |
| 421 |
{ |
| 422 |
case BYTE: |
| 423 |
l = ((ByteElement)element).getValue().longValue(); |
| 424 |
break; |
| 425 |
case SHORT: |
| 426 |
l = ((ShortElement)element).getValue().longValue(); |
| 427 |
break; |
| 428 |
case INT: |
| 429 |
l = ((IntegerElement)element).getValue().longValue(); |
| 430 |
break; |
| 431 |
default: |
| 432 |
return null; |
| 433 |
} |
| 434 |
return new LongElement(element.parent(),element.prev(),l); |
| 435 |
} |
| 436 |
return null; |
| 437 |
} |
| 438 |
|
| 439 |
@Override |
| 440 |
public Element checkChild(Element element) |
| 441 |
{ |
| 442 |
if(element.getDataType() != _arrayType) |
| 443 |
{ |
| 444 |
Element replacement = coerce(element); |
| 445 |
if(replacement != null) |
| 446 |
{ |
| 447 |
return replacement; |
| 448 |
} |
| 449 |
throw new IllegalArgumentException("Attempting to add instance of " + element.getDataType() + " to array of " + _arrayType); |
| 450 |
} |
| 451 |
return element; |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
public long count() |
| 456 |
{ |
| 457 |
int count = 0; |
| 458 |
Element elt = _first; |
| 459 |
while(elt != null) |
| 460 |
{ |
| 461 |
count++; |
| 462 |
elt = elt.next(); |
| 463 |
} |
| 464 |
if(isDescribed() && count != 0) |
| 465 |
{ |
| 466 |
count--; |
| 467 |
} |
| 468 |
return count; |
| 469 |
} |
| 470 |
|
| 471 |
public boolean isDescribed() |
| 472 |
{ |
| 473 |
return _described; |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
public Data.DataType getArrayDataType() |
| 478 |
{ |
| 479 |
return _arrayType; |
| 480 |
} |
| 481 |
|
| 482 |
@Override |
| 483 |
String startSymbol() { |
| 484 |
return String.format("%s%s[", isDescribed() ? "D" : "", getArrayDataType()); |
| 485 |
} |
| 486 |
|
| 487 |
@Override |
| 488 |
String stopSymbol() { |
| 489 |
return "]"; |
| 490 |
} |
| 491 |
|
| 492 |
} |