Debian Codegraph @

proton-j/src/main/java/org/apache/qpid/proton/codec/DecoderImpl.java

raw
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.lang.reflect.Array;
24 import java.nio.ByteBuffer;
25 import java.nio.charset.CharsetDecoder;
26 import java.nio.charset.StandardCharsets;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.Date;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.UUID;
34
35 import org.apache.qpid.proton.amqp.Binary;
36 import org.apache.qpid.proton.amqp.Decimal128;
37 import org.apache.qpid.proton.amqp.Decimal32;
38 import org.apache.qpid.proton.amqp.Decimal64;
39 import org.apache.qpid.proton.amqp.DescribedType;
40 import org.apache.qpid.proton.amqp.Symbol;
41 import org.apache.qpid.proton.amqp.UnsignedByte;
42 import org.apache.qpid.proton.amqp.UnsignedInteger;
43 import org.apache.qpid.proton.amqp.UnsignedLong;
44 import org.apache.qpid.proton.amqp.UnsignedShort;
45
46 public class DecoderImpl implements ByteBufferDecoder
47 {
48 private ReadableBuffer _buffer;
49
50 private final CharsetDecoder _charsetDecoder = StandardCharsets.UTF_8.newDecoder();
51
52 private final PrimitiveTypeEncoding[] _constructors = new PrimitiveTypeEncoding[256];
53 private final Map<Object, DescribedTypeConstructor> _dynamicTypeConstructors =
54 new HashMap<Object, DescribedTypeConstructor>();
55
56 private final Map<Object, FastPathDescribedTypeConstructor<?>> _fastPathTypeConstructors =
57 new HashMap<Object, FastPathDescribedTypeConstructor<?>>();
58
59 public DecoderImpl()
60 {
61 }
62
63 DecoderImpl(final ByteBuffer buffer)
64 {
65 _buffer = new ReadableBuffer.ByteBufferReader(buffer);
66 }
67
68 public TypeConstructor<?> peekConstructor()
69 {
70 _buffer.mark();
71 try
72 {
73 return readConstructor();
74 }
75 finally
76 {
77 _buffer.reset();
78 }
79 }
80
81 @SuppressWarnings("rawtypes")
82 public TypeConstructor readConstructor()
83 {
84 return readConstructor(false);
85 }
86
87 @SuppressWarnings("rawtypes")
88 public TypeConstructor readConstructor(boolean excludeFastPathConstructors)
89 {
90 int code = ((int)readRawByte()) & 0xff;
91 if(code == EncodingCodes.DESCRIBED_TYPE_INDICATOR)
92 {
93 final byte encoding = _buffer.get(_buffer.position());
94 final Object descriptor;
95
96 if (EncodingCodes.SMALLULONG == encoding || EncodingCodes.ULONG == encoding)
97 {
98 descriptor = readUnsignedLong(null);
99 }
100 else if (EncodingCodes.SYM8 == encoding || EncodingCodes.SYM32 == encoding)
101 {
102 descriptor = readSymbol(null);
103 }
104 else
105 {
106 descriptor = readObject();
107 }
108
109 if (!excludeFastPathConstructors)
110 {
111 TypeConstructor<?> fastPathTypeConstructor = _fastPathTypeConstructors.get(descriptor);
112 if (fastPathTypeConstructor != null)
113 {
114 return fastPathTypeConstructor;
115 }
116 }
117
118 TypeConstructor<?> nestedEncoding = readConstructor(false);
119 DescribedTypeConstructor<?> dtc = _dynamicTypeConstructors.get(descriptor);
120 if(dtc == null)
121 {
122 dtc = new DescribedTypeConstructor()
123 {
124 @Override
125 public DescribedType newInstance(final Object described)
126 {
127 return new UnknownDescribedType(descriptor, described);
128 }
129
130 @Override
131 public Class<?> getTypeClass()
132 {
133 return UnknownDescribedType.class;
134 }
135 };
136 register(descriptor, dtc);
137 }
138 return new DynamicTypeConstructor(dtc, nestedEncoding);
139 }
140 else
141 {
142 return _constructors[code];
143 }
144 }
145
146 @Override
147 public void register(final Object descriptor, final FastPathDescribedTypeConstructor<?> btc)
148 {
149 _fastPathTypeConstructors.put(descriptor, btc);
150 }
151
152 @Override
153 public void register(final Object descriptor, final DescribedTypeConstructor dtc)
154 {
155 // Allow external type constructors to replace the built-in instances.
156 _fastPathTypeConstructors.remove(descriptor);
157 _dynamicTypeConstructors.put(descriptor, dtc);
158 }
159
160 private ClassCastException unexpectedType(final Object val, Class clazz)
161 {
162 return new ClassCastException("Unexpected type "
163 + val.getClass().getName()
164 + ". Expected "
165 + clazz.getName() +".");
166 }
167
168
169 @Override
170 public Boolean readBoolean()
171 {
172 return readBoolean(null);
173 }
174
175 @Override
176 public Boolean readBoolean(final Boolean defaultVal)
177 {
178 byte encodingCode = _buffer.get();
179
180 switch (encodingCode)
181 {
182 case EncodingCodes.BOOLEAN_TRUE:
183 return Boolean.TRUE;
184 case EncodingCodes.BOOLEAN_FALSE:
185 return Boolean.FALSE;
186 case EncodingCodes.BOOLEAN:
187 return readRawByte() == 0 ? Boolean.FALSE : Boolean.TRUE;
188 case EncodingCodes.NULL:
189 return defaultVal;
190 default:
191 throw new DecodeException("Expected Boolean type but found encoding: " + EncodingCodes.toString(encodingCode));
192 }
193 }
194
195 @Override
196 public boolean readBoolean(final boolean defaultVal)
197 {
198 byte encodingCode = _buffer.get();
199
200 switch (encodingCode)
201 {
202 case EncodingCodes.BOOLEAN_TRUE:
203 return true;
204 case EncodingCodes.BOOLEAN_FALSE:
205 return false;
206 case EncodingCodes.BOOLEAN:
207 return readRawByte() != 0;
208 case EncodingCodes.NULL:
209 return defaultVal;
210 default:
211 throw new DecodeException("Expected Boolean type but found encoding: " + EncodingCodes.toString(encodingCode));
212 }
213 }
214
215 @Override
216 public Byte readByte()
217 {
218 return readByte(null);
219 }
220
221 @Override
222 public Byte readByte(final Byte defaultVal)
223 {
224 byte encodingCode = _buffer.get();
225
226 switch (encodingCode) {
227 case EncodingCodes.BYTE:
228 return (Byte) readRawByte();
229 case EncodingCodes.NULL:
230 return defaultVal;
231 default:
232 throw new DecodeException("Expected Byte type but found encoding: " + EncodingCodes.toString(encodingCode));
233 }
234 }
235
236 @Override
237 public byte readByte(final byte defaultVal)
238 {
239 TypeConstructor<?> constructor = readConstructor();
240 if(constructor instanceof ByteType.ByteEncoding)
241 {
242 return ((ByteType.ByteEncoding)constructor).readPrimitiveValue();
243 }
244 else
245 {
246 Object val = constructor.readValue();
247 if(val == null)
248 {
249 return defaultVal;
250 }
251 else
252 {
253 throw unexpectedType(val, Byte.class);
254 }
255 }
256 }
257
258 @Override
259 public Short readShort()
260 {
261 return readShort(null);
262 }
263
264 @Override
265 public Short readShort(final Short defaultVal)
266 {
267 byte encodingCode = _buffer.get();
268
269 switch (encodingCode)
270 {
271 case EncodingCodes.SHORT:
272 return Short.valueOf(readRawShort());
273 case EncodingCodes.NULL:
274 return defaultVal;
275 default:
276 throw new DecodeException("Expected Short type but found encoding: " + EncodingCodes.toString(encodingCode));
277 }
278 }
279
280 @Override
281 public short readShort(final short defaultVal)
282 {
283 TypeConstructor<?> constructor = readConstructor();
284 if(constructor instanceof ShortType.ShortEncoding)
285 {
286 return ((ShortType.ShortEncoding)constructor).readPrimitiveValue();
287 }
288 else
289 {
290 Object val = constructor.readValue();
291 if(val == null)
292 {
293 return defaultVal;
294 }
295 else
296 {
297 throw unexpectedType(val, Short.class);
298 }
299 }
300 }
301
302 @Override
303 public Integer readInteger()
304 {
305 return readInteger(null);
306 }
307
308 @Override
309 public Integer readInteger(final Integer defaultVal)
310 {
311 byte encodingCode = _buffer.get();
312
313 switch (encodingCode)
314 {
315 case EncodingCodes.SMALLINT:
316 return Integer.valueOf(readRawByte());
317 case EncodingCodes.INT:
318 return Integer.valueOf(readRawInt());
319 case EncodingCodes.NULL:
320 return defaultVal;
321 default:
322 throw new DecodeException("Expected Integer type but found encoding: " + EncodingCodes.toString(encodingCode));
323 }
324 }
325
326 @Override
327 public int readInteger(final int defaultVal)
328 {
329 TypeConstructor<?> constructor = readConstructor();
330 if(constructor instanceof IntegerType.IntegerEncoding)
331 {
332 return ((IntegerType.IntegerEncoding)constructor).readPrimitiveValue();
333 }
334 else
335 {
336 Object val = constructor.readValue();
337 if(val == null)
338 {
339 return defaultVal;
340 }
341 else
342 {
343 throw unexpectedType(val, Integer.class);
344 }
345 }
346 }
347
348 @Override
349 public Long readLong()
350 {
351 return readLong(null);
352 }
353
354 @Override
355 public Long readLong(final Long defaultVal)
356 {
357 byte encodingCode = _buffer.get();
358
359 switch (encodingCode)
360 {
361 case EncodingCodes.SMALLLONG:
362 return Long.valueOf(readRawByte());
363 case EncodingCodes.LONG:
364 return Long.valueOf(readRawLong());
365 case EncodingCodes.NULL:
366 return defaultVal;
367 default:
368 throw new DecodeException("Expected Long type but found encoding: " + EncodingCodes.toString(encodingCode));
369 }
370 }
371
372 @Override
373 public long readLong(final long defaultVal)
374 {
375 TypeConstructor<?> constructor = readConstructor();
376 if(constructor instanceof LongType.LongEncoding)
377 {
378 return ((LongType.LongEncoding)constructor).readPrimitiveValue();
379 }
380 else
381 {
382 Object val = constructor.readValue();
383 if(val == null)
384 {
385 return defaultVal;
386 }
387 else
388 {
389 throw unexpectedType(val, Long.class);
390 }
391 }
392 }
393
394 @Override
395 public UnsignedByte readUnsignedByte()
396 {
397 return readUnsignedByte(null);
398 }
399
400 @Override
401 public UnsignedByte readUnsignedByte(final UnsignedByte defaultVal)
402 {
403 byte encodingCode = _buffer.get();
404
405 switch (encodingCode)
406 {
407 case EncodingCodes.UBYTE:
408 return UnsignedByte.valueOf(readRawByte());
409 case EncodingCodes.NULL:
410 return defaultVal;
411 default:
412 throw new DecodeException("Expected UnsignedByte type but found encoding: " + EncodingCodes.toString(encodingCode));
413 }
414 }
415
416 @Override
417 public UnsignedShort readUnsignedShort()
418 {
419 return readUnsignedShort(null);
420 }
421
422 @Override
423 public UnsignedShort readUnsignedShort(final UnsignedShort defaultVal)
424 {
425 byte encodingCode = _buffer.get();
426
427 switch (encodingCode)
428 {
429 case EncodingCodes.USHORT:
430 return UnsignedShort.valueOf(readRawShort());
431 case EncodingCodes.NULL:
432 return defaultVal;
433 default:
434 throw new DecodeException("Expected UnsignedShort type but found encoding: " + EncodingCodes.toString(encodingCode));
435 }
436 }
437
438 @Override
439 public UnsignedInteger readUnsignedInteger()
440 {
441 return readUnsignedInteger(null);
442 }
443
444 @Override
445 public UnsignedInteger readUnsignedInteger(final UnsignedInteger defaultVal)
446 {
447 byte encodingCode = _buffer.get();
448
449 switch (encodingCode)
450 {
451 case EncodingCodes.UINT0:
452 return UnsignedInteger.ZERO;
453 case EncodingCodes.SMALLUINT:
454 return UnsignedInteger.valueOf(((int) readRawByte()) & 0xff);
455 case EncodingCodes.UINT:
456 return UnsignedInteger.valueOf(readRawInt());
457 case EncodingCodes.NULL:
458 return defaultVal;
459 default:
460 throw new DecodeException("Expected UnsignedInteger type but found encoding: " + EncodingCodes.toString(encodingCode));
461 }
462 }
463
464 @Override
465 public UnsignedLong readUnsignedLong()
466 {
467 return readUnsignedLong(null);
468 }
469
470 @Override
471 public UnsignedLong readUnsignedLong(final UnsignedLong defaultVal)
472 {
473 byte encodingCode = _buffer.get();
474
475 switch (encodingCode)
476 {
477 case EncodingCodes.ULONG0:
478 return UnsignedLong.ZERO;
479 case EncodingCodes.SMALLULONG:
480 return UnsignedLong.valueOf(((long) readRawByte())&0xffl);
481 case EncodingCodes.ULONG:
482 return UnsignedLong.valueOf(readRawLong());
483 case EncodingCodes.NULL:
484 return defaultVal;
485 default:
486 throw new DecodeException("Expected UnsignedLong type but found encoding: " + EncodingCodes.toString(encodingCode));
487 }
488 }
489
490 @Override
491 public Character readCharacter()
492 {
493 return readCharacter(null);
494 }
495
496 @Override
497 public Character readCharacter(final Character defaultVal)
498 {
499 byte encodingCode = _buffer.get();
500
501 switch (encodingCode)
502 {
503 case EncodingCodes.CHAR:
504 return Character.valueOf((char) (readRawInt() & 0xffff));
505 case EncodingCodes.NULL:
506 return defaultVal;
507 default:
508 throw new DecodeException("Expected Character type but found encoding: " + EncodingCodes.toString(encodingCode));
509 }
510 }
511
512 @Override
513 public char readCharacter(final char defaultVal)
514 {
515 byte encodingCode = _buffer.get();
516
517 switch (encodingCode)
518 {
519 case EncodingCodes.CHAR:
520 return (char) (readRawInt() & 0xffff);
521 case EncodingCodes.NULL:
522 return defaultVal;
523 default:
524 throw new DecodeException("Expected Character type but found encoding: " + EncodingCodes.toString(encodingCode));
525 }
526 }
527
528 @Override
529 public Float readFloat()
530 {
531 return readFloat(null);
532 }
533
534 @Override
535 public Float readFloat(final Float defaultVal)
536 {
537 byte encodingCode = _buffer.get();
538
539 switch (encodingCode)
540 {
541 case EncodingCodes.FLOAT:
542 return Float.valueOf(readRawFloat());
543 case EncodingCodes.NULL:
544 return defaultVal;
545 default:
546 throw new DecodeException("Expected Float type but found encoding: " + EncodingCodes.toString(encodingCode));
547 }
548 }
549
550 @Override
551 public float readFloat(final float defaultVal)
552 {
553 TypeConstructor<?> constructor = readConstructor();
554 if(constructor instanceof FloatType.FloatEncoding)
555 {
556 return ((FloatType.FloatEncoding)constructor).readPrimitiveValue();
557 }
558 else
559 {
560 Object val = constructor.readValue();
561 if(val == null)
562 {
563 return defaultVal;
564 }
565 else
566 {
567 throw unexpectedType(val, Float.class);
568 }
569 }
570 }
571
572 @Override
573 public Double readDouble()
574 {
575 return readDouble(null);
576 }
577
578 @Override
579 public Double readDouble(final Double defaultVal)
580 {
581 byte encodingCode = _buffer.get();
582
583 switch (encodingCode)
584 {
585 case EncodingCodes.DOUBLE:
586 return Double.valueOf(readRawDouble());
587 case EncodingCodes.NULL:
588 return defaultVal;
589 default:
590 throw new DecodeException("Expected Double type but found encoding: " + EncodingCodes.toString(encodingCode));
591 }
592 }
593
594 @Override
595 public double readDouble(final double defaultVal)
596 {
597 TypeConstructor<?> constructor = readConstructor();
598 if(constructor instanceof DoubleType.DoubleEncoding)
599 {
600 return ((DoubleType.DoubleEncoding)constructor).readPrimitiveValue();
601 }
602 else
603 {
604 Object val = constructor.readValue();
605 if(val == null)
606 {
607 return defaultVal;
608 }
609 else
610 {
611 throw unexpectedType(val, Double.class);
612 }
613 }
614 }
615
616 @Override
617 public UUID readUUID()
618 {
619 return readUUID(null);
620 }
621
622 @Override
623 public UUID readUUID(final UUID defaultVal)
624 {
625 byte encodingCode = _buffer.get();
626
627 switch (encodingCode)
628 {
629 case EncodingCodes.UUID:
630 return new UUID(readRawLong(), readRawLong());
631 case EncodingCodes.NULL:
632 return defaultVal;
633 default:
634 throw new DecodeException("Expected UUID type but found encoding: " + EncodingCodes.toString(encodingCode));
635 }
636 }
637
638 @Override
639 public Decimal32 readDecimal32()
640 {
641 return readDecimal32(null);
642 }
643
644 @Override
645 public Decimal32 readDecimal32(final Decimal32 defaultValue)
646 {
647 byte encodingCode = _buffer.get();
648
649 switch (encodingCode)
650 {
651 case EncodingCodes.DECIMAL32:
652 return (Decimal32) _constructors[EncodingCodes.DECIMAL32 & 0xff].readValue();
653 case EncodingCodes.NULL:
654 return defaultValue;
655 default:
656 throw new DecodeException("Expected Decimal32 type but found encoding: " + EncodingCodes.toString(encodingCode));
657 }
658 }
659
660 @Override
661 public Decimal64 readDecimal64()
662 {
663 return readDecimal64(null);
664 }
665
666 @Override
667 public Decimal64 readDecimal64(final Decimal64 defaultValue)
668 {
669 byte encodingCode = _buffer.get();
670
671 switch (encodingCode)
672 {
673 case EncodingCodes.DECIMAL64:
674 return (Decimal64) _constructors[EncodingCodes.DECIMAL64 & 0xff].readValue();
675 case EncodingCodes.NULL:
676 return defaultValue;
677 default:
678 throw new DecodeException("Expected Decimal64 type but found encoding: " + EncodingCodes.toString(encodingCode));
679 }
680 }
681
682 @Override
683 public Decimal128 readDecimal128()
684 {
685 return readDecimal128(null);
686 }
687
688 @Override
689 public Decimal128 readDecimal128(final Decimal128 defaultValue)
690 {
691 byte encodingCode = _buffer.get();
692
693 switch (encodingCode)
694 {
695 case EncodingCodes.DECIMAL128:
696 return (Decimal128) _constructors[EncodingCodes.DECIMAL128 & 0xff].readValue();
697 case EncodingCodes.NULL:
698 return defaultValue;
699 default:
700 throw new DecodeException("Expected Decimal128 type but found encoding: " + EncodingCodes.toString(encodingCode));
701 }
702 }
703
704 @Override
705 public Date readTimestamp()
706 {
707 return readTimestamp(null);
708 }
709
710 @Override
711 public Date readTimestamp(final Date defaultValue)
712 {
713 byte encodingCode = _buffer.get();
714
715 switch (encodingCode)
716 {
717 case EncodingCodes.TIMESTAMP:
718 return new Date(readRawLong());
719 case EncodingCodes.NULL:
720 return defaultValue;
721 default:
722 throw new DecodeException("Expected Timestamp type but found encoding: " + EncodingCodes.toString(encodingCode));
723 }
724 }
725
726 @Override
727 public Binary readBinary()
728 {
729 return readBinary(null);
730 }
731
732 @Override
733 public Binary readBinary(final Binary defaultValue)
734 {
735 byte encodingCode = _buffer.get();
736
737 switch (encodingCode)
738 {
739 case EncodingCodes.VBIN8:
740 return (Binary) _constructors[EncodingCodes.VBIN8 & 0xff].readValue();
741 case EncodingCodes.VBIN32:
742 return (Binary) _constructors[EncodingCodes.VBIN32 & 0xff].readValue();
743 case EncodingCodes.NULL:
744 return defaultValue;
745 default:
746 throw new DecodeException("Expected Binary type but found encoding: " + EncodingCodes.toString(encodingCode));
747 }
748 }
749
750 @Override
751 public Symbol readSymbol()
752 {
753 return readSymbol(null);
754 }
755
756 @Override
757 public Symbol readSymbol(final Symbol defaultValue)
758 {
759 byte encodingCode = _buffer.get();
760
761 switch (encodingCode)
762 {
763 case EncodingCodes.SYM8:
764 return (Symbol) _constructors[EncodingCodes.SYM8 & 0xff].readValue();
765 case EncodingCodes.SYM32:
766 return (Symbol) _constructors[EncodingCodes.SYM32 & 0xff].readValue();
767 case EncodingCodes.NULL:
768 return defaultValue;
769 default:
770 throw new DecodeException("Expected Symbol type but found encoding: " + EncodingCodes.toString(encodingCode));
771 }
772 }
773
774 @Override
775 public String readString()
776 {
777 return readString(null);
778 }
779
780 @Override
781 public String readString(final String defaultValue)
782 {
783 byte encodingCode = _buffer.get();
784
785 switch (encodingCode)
786 {
787 case EncodingCodes.STR8:
788 return (String) _constructors[EncodingCodes.STR8 & 0xff].readValue();
789 case EncodingCodes.STR32:
790 return (String) _constructors[EncodingCodes.STR32 & 0xff].readValue();
791 case EncodingCodes.NULL:
792 return defaultValue;
793 default:
794 throw new DecodeException("Expected String type but found encoding: " + EncodingCodes.toString(encodingCode));
795 }
796 }
797
798 @Override
799 @SuppressWarnings("rawtypes")
800 public List readList()
801 {
802 byte encodingCode = _buffer.get();
803
804 switch (encodingCode)
805 {
806 case EncodingCodes.LIST0:
807 return Collections.EMPTY_LIST;
808 case EncodingCodes.LIST8:
809 return (List) _constructors[EncodingCodes.LIST8 & 0xff].readValue();
810 case EncodingCodes.LIST32:
811 return (List) _constructors[EncodingCodes.LIST32 & 0xff].readValue();
812 case EncodingCodes.NULL:
813 return null;
814 default:
815 throw new DecodeException("Expected List type but found encoding: " + EncodingCodes.toString(encodingCode));
816 }
817 }
818
819 @Override
820 public <T> void readList(final ListProcessor<T> processor)
821 {
822 //TODO.
823 }
824
825 @Override
826 @SuppressWarnings("rawtypes")
827 public Map readMap()
828 {
829 byte encodingCode = _buffer.get();
830
831 switch (encodingCode)
832 {
833 case EncodingCodes.MAP8:
834 return (Map) _constructors[EncodingCodes.MAP8 & 0xff].readValue();
835 case EncodingCodes.MAP32:
836 return (Map) _constructors[EncodingCodes.MAP32 & 0xff].readValue();
837 case EncodingCodes.NULL:
838 return null;
839 default:
840 throw new DecodeException("Expected Map type but found encoding: " + EncodingCodes.toString(encodingCode));
841 }
842 }
843
844 @Override
845 public <T> T[] readArray(final Class<T> clazz)
846 {
847 return null; //TODO.
848 }
849
850 @Override
851 public Object[] readArray()
852 {
853 return (Object[]) readConstructor().readValue();
854
855 }
856
857 @Override
858 public boolean[] readBooleanArray()
859 {
860 return (boolean[]) ((ArrayType.ArrayEncoding)readConstructor()).readValueArray();
861 }
862
863 @Override
864 public byte[] readByteArray()
865 {
866 return (byte[]) ((ArrayType.ArrayEncoding)readConstructor()).readValueArray();
867 }
868
869 @Override
870 public short[] readShortArray()
871 {
872 return (short[]) ((ArrayType.ArrayEncoding)readConstructor()).readValueArray();
873 }
874
875 @Override
876 public int[] readIntegerArray()
877 {
878 return (int[]) ((ArrayType.ArrayEncoding)readConstructor()).readValueArray();
879 }
880
881 @Override
882 public long[] readLongArray()
883 {
884 return (long[]) ((ArrayType.ArrayEncoding)readConstructor()).readValueArray();
885 }
886
887 @Override
888 public float[] readFloatArray()
889 {
890 return (float[]) ((ArrayType.ArrayEncoding)readConstructor()).readValueArray();
891 }
892
893 @Override
894 public double[] readDoubleArray()
895 {
896 return (double[]) ((ArrayType.ArrayEncoding)readConstructor()).readValueArray();
897 }
898
899 @Override
900 public char[] readCharacterArray()
901 {
902 return (char[]) ((ArrayType.ArrayEncoding)readConstructor()).readValueArray();
903 }
904
905 @Override
906 public <T> T[] readMultiple(final Class<T> clazz)
907 {
908 Object val = readObject();
909 if(val == null)
910 {
911 return null;
912 }
913 else if(val.getClass().isArray())
914 {
915 if(clazz.isAssignableFrom(val.getClass().getComponentType()))
916 {
917 return (T[]) val;
918 }
919 else
920 {
921 throw unexpectedType(val, Array.newInstance(clazz, 0).getClass());
922 }
923 }
924 else if(clazz.isAssignableFrom(val.getClass()))
925 {
926 T[] array = (T[]) Array.newInstance(clazz, 1);
927 array[0] = (T) val;
928 return array;
929 }
930 else
931 {
932 throw unexpectedType(val, Array.newInstance(clazz, 0).getClass());
933 }
934 }
935
936 @Override
937 public Object[] readMultiple()
938 {
939 Object val = readObject();
940 if(val == null)
941 {
942 return null;
943 }
944 else if(val.getClass().isArray())
945 {
946 return (Object[]) val;
947 }
948 else
949 {
950 Object[] array = (Object[]) Array.newInstance(val.getClass(), 1);
951 array[0] = val;
952 return array;
953 }
954 }
955
956 @Override
957 public byte[] readByteMultiple()
958 {
959 return new byte[0]; //TODO.
960 }
961
962 @Override
963 public short[] readShortMultiple()
964 {
965 return new short[0]; //TODO.
966 }
967
968 @Override
969 public int[] readIntegerMultiple()
970 {
971 return new int[0]; //TODO.
972 }
973
974 @Override
975 public long[] readLongMultiple()
976 {
977 return new long[0]; //TODO.
978 }
979
980 @Override
981 public float[] readFloatMultiple()
982 {
983 return new float[0]; //TODO.
984 }
985
986 @Override
987 public double[] readDoubleMultiple()
988 {
989 return new double[0]; //TODO.
990 }
991
992 @Override
993 public char[] readCharacterMultiple()
994 {
995 return new char[0]; //TODO.
996 }
997
998 @Override
999 public Object readObject()
1000 {
1001 boolean arrayType = false;
1002 byte code = _buffer.get(_buffer.position());
1003 switch (code)
1004 {
1005 case EncodingCodes.ARRAY8:
1006 case EncodingCodes.ARRAY32:
1007 arrayType = true;
1008 }
1009
1010 TypeConstructor<?> constructor = readConstructor();
1011 if(constructor== null)
1012 {
1013 throw new DecodeException("Unknown constructor");
1014 }
1015
1016 if (arrayType) {
1017 return ((ArrayType.ArrayEncoding)constructor).readValueArray();
1018 } else {
1019 return constructor.readValue();
1020 }
1021 }
1022
1023 @Override
1024 public Object readObject(final Object defaultValue)
1025 {
1026 Object val = readObject();
1027 return val == null ? defaultValue : val;
1028 }
1029
1030 <V> void register(PrimitiveType<V> type)
1031 {
1032 Collection<? extends PrimitiveTypeEncoding<V>> encodings = type.getAllEncodings();
1033
1034 for(PrimitiveTypeEncoding<V> encoding : encodings)
1035 {
1036 _constructors[((int) encoding.getEncodingCode()) & 0xFF ] = encoding;
1037 }
1038
1039 }
1040
1041 byte readRawByte()
1042 {
1043 return _buffer.get();
1044 }
1045
1046 int readRawInt()
1047 {
1048 return _buffer.getInt();
1049 }
1050
1051 long readRawLong()
1052 {
1053 return _buffer.getLong();
1054 }
1055
1056 short readRawShort()
1057 {
1058 return _buffer.getShort();
1059 }
1060
1061 float readRawFloat()
1062 {
1063 return _buffer.getFloat();
1064 }
1065
1066 double readRawDouble()
1067 {
1068 return _buffer.getDouble();
1069 }
1070
1071 void readRaw(final byte[] data, final int offset, final int length)
1072 {
1073 _buffer.get(data, offset, length);
1074 }
1075
1076 <V> V readRaw(TypeDecoder<V> decoder, int size)
1077 {
1078 V decode = decoder.decode(this, _buffer.slice().limit(size));
1079 _buffer.position(_buffer.position()+size);
1080 return decode;
1081 }
1082
1083 @Override
1084 public void setByteBuffer(final ByteBuffer buffer)
1085 {
1086 _buffer = new ReadableBuffer.ByteBufferReader(buffer);
1087 }
1088
1089 public ByteBuffer getByteBuffer()
1090 {
1091 return _buffer.byteBuffer();
1092 }
1093
1094 public void setBuffer(final ReadableBuffer buffer)
1095 {
1096 _buffer = buffer;
1097 }
1098
1099 public ReadableBuffer getBuffer()
1100 {
1101 return _buffer;
1102 }
1103
1104 CharsetDecoder getCharsetDecoder()
1105 {
1106 return _charsetDecoder;
1107 }
1108
1109 interface TypeDecoder<V>
1110 {
1111 V decode(DecoderImpl decoder, ReadableBuffer buf);
1112 }
1113
1114 private static class UnknownDescribedType implements DescribedType
1115 {
1116 private final Object _descriptor;
1117 private final Object _described;
1118
1119 public UnknownDescribedType(final Object descriptor, final Object described)
1120 {
1121 _descriptor = descriptor;
1122 _described = described;
1123 }
1124
1125 @Override
1126 public Object getDescriptor()
1127 {
1128 return _descriptor;
1129 }
1130
1131 @Override
1132 public Object getDescribed()
1133 {
1134 return _described;
1135 }
1136
1137
1138 @Override
1139 public boolean equals(Object obj)
1140 {
1141
1142 return obj instanceof DescribedType
1143 && _descriptor == null ? ((DescribedType) obj).getDescriptor() == null
1144 : _descriptor.equals(((DescribedType) obj).getDescriptor())
1145 && _described == null ? ((DescribedType) obj).getDescribed() == null
1146 : _described.equals(((DescribedType) obj).getDescribed());
1147
1148 }
1149
1150 }
1151
1152 @Override
1153 public int getByteBufferRemaining() {
1154 return _buffer.remaining();
1155 }
1156 }