| 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 |
package org.apache.qpid.proton.codec; |
| 22 |
|
| 23 |
import java.nio.ByteBuffer; |
| 24 |
import java.util.Date; |
| 25 |
import java.util.HashMap; |
| 26 |
import java.util.List; |
| 27 |
import java.util.Map; |
| 28 |
import java.util.UUID; |
| 29 |
|
| 30 |
import org.apache.qpid.proton.amqp.Binary; |
| 31 |
import org.apache.qpid.proton.amqp.Decimal128; |
| 32 |
import org.apache.qpid.proton.amqp.Decimal32; |
| 33 |
import org.apache.qpid.proton.amqp.Decimal64; |
| 34 |
import org.apache.qpid.proton.amqp.DescribedType; |
| 35 |
import org.apache.qpid.proton.amqp.Symbol; |
| 36 |
import org.apache.qpid.proton.amqp.UnsignedByte; |
| 37 |
import org.apache.qpid.proton.amqp.UnsignedInteger; |
| 38 |
import org.apache.qpid.proton.amqp.UnsignedLong; |
| 39 |
import org.apache.qpid.proton.amqp.UnsignedShort; |
| 40 |
|
| 41 |
public final class EncoderImpl implements ByteBufferEncoder |
| 42 |
{ |
| 43 |
private static final byte DESCRIBED_TYPE_OP = (byte)0; |
| 44 |
|
| 45 |
private WritableBuffer _buffer; |
| 46 |
|
| 47 |
private final DecoderImpl _decoder; |
| 48 |
private final Map<Class, AMQPType> _typeRegistry = new HashMap<Class, AMQPType>(); |
| 49 |
private Map<Object, AMQPType> _describedDescriptorRegistry = new HashMap<Object, AMQPType>(); |
| 50 |
private Map<Class, AMQPType> _describedTypesClassRegistry = new HashMap<Class, AMQPType>(); |
| 51 |
|
| 52 |
private final NullType _nullType; |
| 53 |
private final BooleanType _booleanType; |
| 54 |
private final ByteType _byteType; |
| 55 |
private final UnsignedByteType _unsignedByteType; |
| 56 |
private final ShortType _shortType; |
| 57 |
private final UnsignedShortType _unsignedShortType; |
| 58 |
private final IntegerType _integerType; |
| 59 |
private final UnsignedIntegerType _unsignedIntegerType; |
| 60 |
private final LongType _longType; |
| 61 |
private final UnsignedLongType _unsignedLongType; |
| 62 |
private final BigIntegerType _bigIntegerType; |
| 63 |
|
| 64 |
private final CharacterType _characterType; |
| 65 |
private final FloatType _floatType; |
| 66 |
private final DoubleType _doubleType; |
| 67 |
private final TimestampType _timestampType; |
| 68 |
private final UUIDType _uuidType; |
| 69 |
|
| 70 |
private final Decimal32Type _decimal32Type; |
| 71 |
private final Decimal64Type _decimal64Type; |
| 72 |
private final Decimal128Type _decimal128Type; |
| 73 |
|
| 74 |
private final BinaryType _binaryType; |
| 75 |
private final SymbolType _symbolType; |
| 76 |
private final StringType _stringType; |
| 77 |
|
| 78 |
private final ListType _listType; |
| 79 |
private final MapType _mapType; |
| 80 |
|
| 81 |
private final ArrayType _arrayType; |
| 82 |
|
| 83 |
public EncoderImpl(DecoderImpl decoder) |
| 84 |
{ |
| 85 |
_decoder = decoder; |
| 86 |
_nullType = new NullType(this, decoder); |
| 87 |
_booleanType = new BooleanType(this, decoder); |
| 88 |
_byteType = new ByteType(this, decoder); |
| 89 |
_unsignedByteType = new UnsignedByteType(this, decoder); |
| 90 |
_shortType = new ShortType(this, decoder); |
| 91 |
_unsignedShortType = new UnsignedShortType(this, decoder); |
| 92 |
_integerType = new IntegerType(this, decoder); |
| 93 |
_unsignedIntegerType = new UnsignedIntegerType(this, decoder); |
| 94 |
_longType = new LongType(this, decoder); |
| 95 |
_unsignedLongType = new UnsignedLongType(this, decoder); |
| 96 |
_bigIntegerType = new BigIntegerType(this, decoder); |
| 97 |
|
| 98 |
_characterType = new CharacterType(this, decoder); |
| 99 |
_floatType = new FloatType(this, decoder); |
| 100 |
_doubleType = new DoubleType(this, decoder); |
| 101 |
_timestampType = new TimestampType(this, decoder); |
| 102 |
_uuidType = new UUIDType(this, decoder); |
| 103 |
|
| 104 |
_decimal32Type = new Decimal32Type(this, decoder); |
| 105 |
_decimal64Type = new Decimal64Type(this, decoder); |
| 106 |
_decimal128Type = new Decimal128Type(this, decoder); |
| 107 |
|
| 108 |
|
| 109 |
_binaryType = new BinaryType(this, decoder); |
| 110 |
_symbolType = new SymbolType(this, decoder); |
| 111 |
_stringType = new StringType(this, decoder); |
| 112 |
|
| 113 |
_listType = new ListType(this, decoder); |
| 114 |
_mapType = new MapType(this, decoder); |
| 115 |
|
| 116 |
_arrayType = new ArrayType(this, |
| 117 |
decoder, |
| 118 |
_booleanType, |
| 119 |
_byteType, |
| 120 |
_shortType, |
| 121 |
_integerType, |
| 122 |
_longType, |
| 123 |
_floatType, |
| 124 |
_doubleType, |
| 125 |
_characterType); |
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
public void setByteBuffer(final ByteBuffer buf) |
| 130 |
{ |
| 131 |
_buffer = new WritableBuffer.ByteBufferWrapper(buf); |
| 132 |
} |
| 133 |
|
| 134 |
public void setByteBuffer(final WritableBuffer buf) |
| 135 |
{ |
| 136 |
_buffer = buf; |
| 137 |
} |
| 138 |
|
| 139 |
public WritableBuffer getBuffer() |
| 140 |
{ |
| 141 |
return _buffer; |
| 142 |
} |
| 143 |
|
| 144 |
public DecoderImpl getDecoder() |
| 145 |
{ |
| 146 |
return _decoder; |
| 147 |
} |
| 148 |
|
| 149 |
@Override |
| 150 |
public AMQPType getType(final Object element) |
| 151 |
{ |
| 152 |
return getTypeFromClass(element == null ? Void.class : element.getClass(), element); |
| 153 |
} |
| 154 |
|
| 155 |
public AMQPType getTypeFromClass(final Class clazz) |
| 156 |
{ |
| 157 |
return getTypeFromClass(clazz, null); |
| 158 |
} |
| 159 |
|
| 160 |
private AMQPType<?> getTypeFromClass(final Class<?> clazz, final Object instance) |
| 161 |
{ |
| 162 |
AMQPType<?> amqpType = _typeRegistry.get(clazz); |
| 163 |
if(amqpType == null) |
| 164 |
{ |
| 165 |
amqpType = deduceTypeFromClass(clazz, instance); |
| 166 |
} |
| 167 |
|
| 168 |
return amqpType; |
| 169 |
} |
| 170 |
|
| 171 |
private AMQPType<?> deduceTypeFromClass(final Class<?> clazz, final Object instance) { |
| 172 |
AMQPType<?> amqpType = null; |
| 173 |
|
| 174 |
if(clazz.isArray()) |
| 175 |
{ |
| 176 |
amqpType = _arrayType; |
| 177 |
} |
| 178 |
else |
| 179 |
{ |
| 180 |
if(List.class.isAssignableFrom(clazz)) |
| 181 |
{ |
| 182 |
amqpType = _listType; |
| 183 |
} |
| 184 |
else if(Map.class.isAssignableFrom(clazz)) |
| 185 |
{ |
| 186 |
amqpType = _mapType; |
| 187 |
} |
| 188 |
else if(DescribedType.class.isAssignableFrom(clazz)) |
| 189 |
{ |
| 190 |
amqpType = _describedTypesClassRegistry.get(clazz); |
| 191 |
if(amqpType == null && instance != null) |
| 192 |
{ |
| 193 |
Object descriptor = ((DescribedType) instance).getDescriptor(); |
| 194 |
amqpType = _describedDescriptorRegistry.get(descriptor); |
| 195 |
if(amqpType == null) |
| 196 |
{ |
| 197 |
amqpType = new DynamicDescribedType(this, descriptor); |
| 198 |
_describedDescriptorRegistry.put(descriptor, amqpType); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
return amqpType; |
| 203 |
} |
| 204 |
} |
| 205 |
_typeRegistry.put(clazz, amqpType); |
| 206 |
|
| 207 |
return amqpType; |
| 208 |
} |
| 209 |
|
| 210 |
@Override |
| 211 |
public <V> void register(AMQPType<V> type) |
| 212 |
{ |
| 213 |
register(type.getTypeClass(), type); |
| 214 |
} |
| 215 |
|
| 216 |
<T> void register(Class<T> clazz, AMQPType<T> type) |
| 217 |
{ |
| 218 |
_typeRegistry.put(clazz, type); |
| 219 |
} |
| 220 |
|
| 221 |
public void registerDescribedType(Class clazz, Object descriptor) |
| 222 |
{ |
| 223 |
AMQPType<?> type = _describedDescriptorRegistry.get(descriptor); |
| 224 |
if(type == null) |
| 225 |
{ |
| 226 |
type = new DynamicDescribedType(this, descriptor); |
| 227 |
_describedDescriptorRegistry.put(descriptor, type); |
| 228 |
} |
| 229 |
_describedTypesClassRegistry.put(clazz, type); |
| 230 |
} |
| 231 |
|
| 232 |
@Override |
| 233 |
public void writeNull() |
| 234 |
{ |
| 235 |
_buffer.put(EncodingCodes.NULL); |
| 236 |
} |
| 237 |
|
| 238 |
@Override |
| 239 |
public void writeBoolean(final boolean bool) |
| 240 |
{ |
| 241 |
if (bool) |
| 242 |
{ |
| 243 |
_buffer.put(EncodingCodes.BOOLEAN_TRUE); |
| 244 |
} |
| 245 |
else |
| 246 |
{ |
| 247 |
_buffer.put(EncodingCodes.BOOLEAN_FALSE); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
@Override |
| 252 |
public void writeBoolean(final Boolean bool) |
| 253 |
{ |
| 254 |
if(bool == null) |
| 255 |
{ |
| 256 |
writeNull(); |
| 257 |
} |
| 258 |
else if (Boolean.TRUE.equals(bool)) |
| 259 |
{ |
| 260 |
_buffer.put(EncodingCodes.BOOLEAN_TRUE); |
| 261 |
} |
| 262 |
else |
| 263 |
{ |
| 264 |
_buffer.put(EncodingCodes.BOOLEAN_FALSE); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
@Override |
| 269 |
public void writeUnsignedByte(final UnsignedByte ubyte) |
| 270 |
{ |
| 271 |
if(ubyte == null) |
| 272 |
{ |
| 273 |
writeNull(); |
| 274 |
} |
| 275 |
else |
| 276 |
{ |
| 277 |
_unsignedByteType.fastWrite(this, ubyte); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
@Override |
| 282 |
public void writeUnsignedShort(final UnsignedShort ushort) |
| 283 |
{ |
| 284 |
if(ushort == null) |
| 285 |
{ |
| 286 |
writeNull(); |
| 287 |
} |
| 288 |
else |
| 289 |
{ |
| 290 |
_unsignedShortType.fastWrite(this, ushort); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
@Override |
| 295 |
public void writeUnsignedInteger(final UnsignedInteger uint) |
| 296 |
{ |
| 297 |
if(uint == null) |
| 298 |
{ |
| 299 |
writeNull(); |
| 300 |
} |
| 301 |
else |
| 302 |
{ |
| 303 |
_unsignedIntegerType.fastWrite(this, uint); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
@Override |
| 308 |
public void writeUnsignedLong(final UnsignedLong ulong) |
| 309 |
{ |
| 310 |
if(ulong == null) |
| 311 |
{ |
| 312 |
writeNull(); |
| 313 |
} |
| 314 |
else |
| 315 |
{ |
| 316 |
_unsignedLongType.fastWrite(this, ulong); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
@Override |
| 321 |
public void writeByte(final byte b) |
| 322 |
{ |
| 323 |
_byteType.write(b); |
| 324 |
} |
| 325 |
|
| 326 |
@Override |
| 327 |
public void writeByte(final Byte b) |
| 328 |
{ |
| 329 |
if(b == null) |
| 330 |
{ |
| 331 |
writeNull(); |
| 332 |
} |
| 333 |
else |
| 334 |
{ |
| 335 |
writeByte(b.byteValue()); |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
@Override |
| 340 |
public void writeShort(final short s) |
| 341 |
{ |
| 342 |
_shortType.write(s); |
| 343 |
} |
| 344 |
|
| 345 |
@Override |
| 346 |
public void writeShort(final Short s) |
| 347 |
{ |
| 348 |
if(s == null) |
| 349 |
{ |
| 350 |
writeNull(); |
| 351 |
} |
| 352 |
else |
| 353 |
{ |
| 354 |
writeShort(s.shortValue()); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
@Override |
| 359 |
public void writeInteger(final int i) |
| 360 |
{ |
| 361 |
_integerType.write(i); |
| 362 |
} |
| 363 |
|
| 364 |
@Override |
| 365 |
public void writeInteger(final Integer i) |
| 366 |
{ |
| 367 |
if(i == null) |
| 368 |
{ |
| 369 |
writeNull(); |
| 370 |
} |
| 371 |
else |
| 372 |
{ |
| 373 |
writeInteger(i.intValue()); |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
@Override |
| 378 |
public void writeLong(final long l) |
| 379 |
{ |
| 380 |
_longType.write(l); |
| 381 |
} |
| 382 |
|
| 383 |
@Override |
| 384 |
public void writeLong(final Long l) |
| 385 |
{ |
| 386 |
|
| 387 |
if(l == null) |
| 388 |
{ |
| 389 |
writeNull(); |
| 390 |
} |
| 391 |
else |
| 392 |
{ |
| 393 |
writeLong(l.longValue()); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
@Override |
| 398 |
public void writeFloat(final float f) |
| 399 |
{ |
| 400 |
_floatType.write(f); |
| 401 |
} |
| 402 |
|
| 403 |
@Override |
| 404 |
public void writeFloat(final Float f) |
| 405 |
{ |
| 406 |
if(f == null) |
| 407 |
{ |
| 408 |
writeNull(); |
| 409 |
} |
| 410 |
else |
| 411 |
{ |
| 412 |
writeFloat(f.floatValue()); |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
@Override |
| 417 |
public void writeDouble(final double d) |
| 418 |
{ |
| 419 |
_doubleType.write(d); |
| 420 |
} |
| 421 |
|
| 422 |
@Override |
| 423 |
public void writeDouble(final Double d) |
| 424 |
{ |
| 425 |
if(d == null) |
| 426 |
{ |
| 427 |
writeNull(); |
| 428 |
} |
| 429 |
else |
| 430 |
{ |
| 431 |
writeDouble(d.doubleValue()); |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
@Override |
| 436 |
public void writeDecimal32(final Decimal32 d) |
| 437 |
{ |
| 438 |
if(d == null) |
| 439 |
{ |
| 440 |
writeNull(); |
| 441 |
} |
| 442 |
else |
| 443 |
{ |
| 444 |
_decimal32Type.write(d); |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
@Override |
| 449 |
public void writeDecimal64(final Decimal64 d) |
| 450 |
{ |
| 451 |
if(d == null) |
| 452 |
{ |
| 453 |
writeNull(); |
| 454 |
} |
| 455 |
else |
| 456 |
{ |
| 457 |
_decimal64Type.write(d); |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
@Override |
| 462 |
public void writeDecimal128(final Decimal128 d) |
| 463 |
{ |
| 464 |
if(d == null) |
| 465 |
{ |
| 466 |
writeNull(); |
| 467 |
} |
| 468 |
else |
| 469 |
{ |
| 470 |
_decimal128Type.write(d); |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
@Override |
| 475 |
public void writeCharacter(final char c) |
| 476 |
{ |
| 477 |
// TODO - java character may be half of a pair, should probably throw exception then |
| 478 |
_characterType.write(c); |
| 479 |
} |
| 480 |
|
| 481 |
@Override |
| 482 |
public void writeCharacter(final Character c) |
| 483 |
{ |
| 484 |
if(c == null) |
| 485 |
{ |
| 486 |
writeNull(); |
| 487 |
} |
| 488 |
else |
| 489 |
{ |
| 490 |
writeCharacter(c.charValue()); |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
@Override |
| 495 |
public void writeTimestamp(final long timestamp) |
| 496 |
{ |
| 497 |
_timestampType.fastWrite(this, timestamp); |
| 498 |
} |
| 499 |
|
| 500 |
@Override |
| 501 |
public void writeTimestamp(final Date d) |
| 502 |
{ |
| 503 |
if(d == null) |
| 504 |
{ |
| 505 |
writeNull(); |
| 506 |
} |
| 507 |
else |
| 508 |
{ |
| 509 |
_timestampType.fastWrite(this, d.getTime()); |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
@Override |
| 514 |
public void writeUUID(final UUID uuid) |
| 515 |
{ |
| 516 |
if(uuid == null) |
| 517 |
{ |
| 518 |
writeNull(); |
| 519 |
} |
| 520 |
else |
| 521 |
{ |
| 522 |
_uuidType.fastWrite(this, uuid); |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
@Override |
| 527 |
public void writeBinary(final Binary b) |
| 528 |
{ |
| 529 |
if(b == null) |
| 530 |
{ |
| 531 |
writeNull(); |
| 532 |
} |
| 533 |
else |
| 534 |
{ |
| 535 |
_binaryType.fastWrite(this, b); |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
@Override |
| 540 |
public void writeString(final String s) |
| 541 |
{ |
| 542 |
if(s == null) |
| 543 |
{ |
| 544 |
writeNull(); |
| 545 |
} |
| 546 |
else |
| 547 |
{ |
| 548 |
_stringType.write(s); |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
@Override |
| 553 |
public void writeSymbol(final Symbol s) |
| 554 |
{ |
| 555 |
if(s == null) |
| 556 |
{ |
| 557 |
writeNull(); |
| 558 |
} |
| 559 |
else |
| 560 |
{ |
| 561 |
_symbolType.fastWrite(this, s); |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
@Override |
| 566 |
public void writeList(final List l) |
| 567 |
{ |
| 568 |
if(l == null) |
| 569 |
{ |
| 570 |
writeNull(); |
| 571 |
} |
| 572 |
else |
| 573 |
{ |
| 574 |
_listType.write(l); |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
@Override |
| 579 |
public void writeMap(final Map m) |
| 580 |
{ |
| 581 |
|
| 582 |
if(m == null) |
| 583 |
{ |
| 584 |
writeNull(); |
| 585 |
} |
| 586 |
else |
| 587 |
{ |
| 588 |
_mapType.write(m); |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
@Override |
| 593 |
public void writeDescribedType(final DescribedType d) |
| 594 |
{ |
| 595 |
if(d == null) |
| 596 |
{ |
| 597 |
writeNull(); |
| 598 |
} |
| 599 |
else |
| 600 |
{ |
| 601 |
_buffer.put(DESCRIBED_TYPE_OP); |
| 602 |
writeObject(d.getDescriptor()); |
| 603 |
writeObject(d.getDescribed()); |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
@Override |
| 608 |
public void writeArray(final boolean[] a) |
| 609 |
{ |
| 610 |
if(a == null) |
| 611 |
{ |
| 612 |
writeNull(); |
| 613 |
} |
| 614 |
else |
| 615 |
{ |
| 616 |
_arrayType.write(a); |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
@Override |
| 621 |
public void writeArray(final byte[] a) |
| 622 |
{ |
| 623 |
if(a == null) |
| 624 |
{ |
| 625 |
writeNull(); |
| 626 |
} |
| 627 |
else |
| 628 |
{ |
| 629 |
_arrayType.write(a); |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
@Override |
| 634 |
public void writeArray(final short[] a) |
| 635 |
{ |
| 636 |
if(a == null) |
| 637 |
{ |
| 638 |
writeNull(); |
| 639 |
} |
| 640 |
else |
| 641 |
{ |
| 642 |
_arrayType.write(a); |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
@Override |
| 647 |
public void writeArray(final int[] a) |
| 648 |
{ |
| 649 |
if(a == null) |
| 650 |
{ |
| 651 |
writeNull(); |
| 652 |
} |
| 653 |
else |
| 654 |
{ |
| 655 |
_arrayType.write(a); |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
@Override |
| 660 |
public void writeArray(final long[] a) |
| 661 |
{ |
| 662 |
if(a == null) |
| 663 |
{ |
| 664 |
writeNull(); |
| 665 |
} |
| 666 |
else |
| 667 |
{ |
| 668 |
_arrayType.write(a); |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
@Override |
| 673 |
public void writeArray(final float[] a) |
| 674 |
{ |
| 675 |
if(a == null) |
| 676 |
{ |
| 677 |
writeNull(); |
| 678 |
} |
| 679 |
else |
| 680 |
{ |
| 681 |
_arrayType.write(a); |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
@Override |
| 686 |
public void writeArray(final double[] a) |
| 687 |
{ |
| 688 |
if(a == null) |
| 689 |
{ |
| 690 |
writeNull(); |
| 691 |
} |
| 692 |
else |
| 693 |
{ |
| 694 |
_arrayType.write(a); |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
@Override |
| 699 |
public void writeArray(final char[] a) |
| 700 |
{ |
| 701 |
if(a == null) |
| 702 |
{ |
| 703 |
writeNull(); |
| 704 |
} |
| 705 |
else |
| 706 |
{ |
| 707 |
_arrayType.write(a); |
| 708 |
} |
| 709 |
} |
| 710 |
|
| 711 |
@Override |
| 712 |
public void writeArray(final Object[] a) |
| 713 |
{ |
| 714 |
if(a == null) |
| 715 |
{ |
| 716 |
writeNull(); |
| 717 |
} |
| 718 |
else |
| 719 |
{ |
| 720 |
_arrayType.write(a); |
| 721 |
} |
| 722 |
} |
| 723 |
|
| 724 |
@SuppressWarnings({ "rawtypes", "unchecked" }) |
| 725 |
@Override |
| 726 |
public void writeObject(final Object o) |
| 727 |
{ |
| 728 |
if (o != null) |
| 729 |
{ |
| 730 |
AMQPType type = _typeRegistry.get(o.getClass()); |
| 731 |
|
| 732 |
if(type != null) |
| 733 |
{ |
| 734 |
type.write(o); |
| 735 |
} |
| 736 |
else |
| 737 |
{ |
| 738 |
writeUnregisteredType(o); |
| 739 |
} |
| 740 |
} |
| 741 |
else |
| 742 |
{ |
| 743 |
_buffer.put(EncodingCodes.NULL); |
| 744 |
} |
| 745 |
} |
| 746 |
|
| 747 |
private void writeUnregisteredType(final Object o) |
| 748 |
{ |
| 749 |
if(o.getClass().isArray()) |
| 750 |
{ |
| 751 |
writeArrayType(o); |
| 752 |
} |
| 753 |
else if(o instanceof List) |
| 754 |
{ |
| 755 |
writeList((List<?>)o); |
| 756 |
} |
| 757 |
else if(o instanceof Map) |
| 758 |
{ |
| 759 |
writeMap((Map<?, ?>)o); |
| 760 |
} |
| 761 |
else if(o instanceof DescribedType) |
| 762 |
{ |
| 763 |
writeDescribedType((DescribedType)o); |
| 764 |
} |
| 765 |
else |
| 766 |
{ |
| 767 |
throw new IllegalArgumentException( |
| 768 |
"Do not know how to write Objects of class " + o.getClass().getName()); |
| 769 |
} |
| 770 |
} |
| 771 |
|
| 772 |
private void writeArrayType(Object array) { |
| 773 |
Class<?> componentType = array.getClass().getComponentType(); |
| 774 |
if(componentType.isPrimitive()) |
| 775 |
{ |
| 776 |
if(componentType == Boolean.TYPE) |
| 777 |
{ |
| 778 |
writeArray((boolean[])array); |
| 779 |
} |
| 780 |
else if(componentType == Byte.TYPE) |
| 781 |
{ |
| 782 |
writeArray((byte[])array); |
| 783 |
} |
| 784 |
else if(componentType == Short.TYPE) |
| 785 |
{ |
| 786 |
writeArray((short[])array); |
| 787 |
} |
| 788 |
else if(componentType == Integer.TYPE) |
| 789 |
{ |
| 790 |
writeArray((int[])array); |
| 791 |
} |
| 792 |
else if(componentType == Long.TYPE) |
| 793 |
{ |
| 794 |
writeArray((long[])array); |
| 795 |
} |
| 796 |
else if(componentType == Float.TYPE) |
| 797 |
{ |
| 798 |
writeArray((float[])array); |
| 799 |
} |
| 800 |
else if(componentType == Double.TYPE) |
| 801 |
{ |
| 802 |
writeArray((double[])array); |
| 803 |
} |
| 804 |
else if(componentType == Character.TYPE) |
| 805 |
{ |
| 806 |
writeArray((char[])array); |
| 807 |
} |
| 808 |
else |
| 809 |
{ |
| 810 |
throw new IllegalArgumentException("Cannot write arrays of type " + componentType.getName()); |
| 811 |
} |
| 812 |
} |
| 813 |
else |
| 814 |
{ |
| 815 |
writeArray((Object[]) array); |
| 816 |
} |
| 817 |
} |
| 818 |
|
| 819 |
public void writeRaw(final byte b) |
| 820 |
{ |
| 821 |
_buffer.put(b); |
| 822 |
} |
| 823 |
|
| 824 |
void writeRaw(final short s) |
| 825 |
{ |
| 826 |
_buffer.putShort(s); |
| 827 |
} |
| 828 |
|
| 829 |
void writeRaw(final int i) |
| 830 |
{ |
| 831 |
_buffer.putInt(i); |
| 832 |
} |
| 833 |
|
| 834 |
void writeRaw(final long l) |
| 835 |
{ |
| 836 |
_buffer.putLong(l); |
| 837 |
} |
| 838 |
|
| 839 |
void writeRaw(final float f) |
| 840 |
{ |
| 841 |
_buffer.putFloat(f); |
| 842 |
} |
| 843 |
|
| 844 |
void writeRaw(final double d) |
| 845 |
{ |
| 846 |
_buffer.putDouble(d); |
| 847 |
} |
| 848 |
|
| 849 |
void writeRaw(byte[] src, int offset, int length) |
| 850 |
{ |
| 851 |
_buffer.put(src, offset, length); |
| 852 |
} |
| 853 |
|
| 854 |
void writeRaw(final String string) |
| 855 |
{ |
| 856 |
_buffer.put(string); |
| 857 |
} |
| 858 |
|
| 859 |
AMQPType<?> getNullTypeEncoder() |
| 860 |
{ |
| 861 |
return _nullType; |
| 862 |
} |
| 863 |
} |