1 /* 2 * hunt-proton: AMQP Protocol library for D programming language. 3 * 4 * Copyright (C) 2018-2019 HuntLabs 5 * 6 * Website: https://www.huntlabs.net/ 7 * 8 * Licensed under the Apache-2.0 License. 9 * 10 */ 11 12 module hunt.proton.codec.impl.UnsignedIntegerElement; 13 14 import hunt.io.ByteBuffer; 15 import std.conv; 16 import hunt.proton.codec.impl.AtomicElement; 17 import hunt.proton.codec.impl.Element; 18 import hunt.proton.codec.impl.ArrayElement; 19 import hunt.proton.codec.impl.AbstractElement; 20 21 import hunt.proton.amqp.UnsignedInteger; 22 import hunt.proton.codec.Data; 23 24 class UnsignedIntegerElement : AtomicElement!(UnsignedInteger) 25 { 26 27 private UnsignedInteger _value; 28 29 this(IElement parent, IElement prev, UnsignedInteger i) 30 { 31 super(parent, prev); 32 _value = i; 33 } 34 35 public int size() 36 { 37 if(isElementOfArray()) 38 { 39 ArrayElement parent = cast(ArrayElement) parent(); 40 if(parent.constructorType() == ArrayElement.TINY) 41 { 42 if(_value.intValue() == 0) 43 { 44 return 0; 45 } 46 else 47 { 48 parent.setConstructorType(ArrayElement.SMALL); 49 } 50 } 51 52 if(parent.constructorType() == ArrayElement.SMALL) 53 { 54 if(0 <= _value.intValue() && _value.intValue() <= 255) 55 { 56 return 1; 57 } 58 else 59 { 60 parent.setConstructorType(ArrayElement.LARGE); 61 } 62 } 63 64 return 4; 65 66 } 67 else 68 { 69 return 0 == _value.intValue() ? 1 : (1 <= _value.intValue() && _value.intValue() <= 255) ? 2 : 5; 70 } 71 72 } 73 74 public Object getValue() 75 { 76 return _value; 77 } 78 79 public Data.DataType getDataType() 80 { 81 return Data.DataType.UINT; 82 } 83 84 public int encode(ByteBuffer b) 85 { 86 int size = size(); 87 if(size > b.remaining()) 88 { 89 return 0; 90 } 91 switch(size) 92 { 93 case 1: 94 if(isElementOfArray()) 95 { 96 b.put(cast(byte)_value.intValue()); 97 } 98 else 99 { 100 b.put(cast(byte)0x43); 101 } 102 break; 103 case 2: 104 b.put(cast(byte)0x52); 105 b.put(cast(byte)_value.intValue()); 106 break; 107 case 5: 108 b.put(cast(byte)0x70); 109 goto case; 110 case 4: 111 b.putInt(_value.intValue()); 112 break; 113 default: 114 break; 115 116 } 117 118 return size; 119 } 120 }