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.UnsignedLongElement; 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.UnsignedLong; 22 import hunt.proton.codec.Data; 23 24 class UnsignedLongElement : AtomicElement!UnsignedLong 25 { 26 27 private UnsignedLong _value; 28 29 this(IElement parent, IElement prev, UnsignedLong ul) 30 { 31 super(parent, prev); 32 _value = ul; 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.longValue() == 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.longValue() && _value.longValue() <= 255) 55 { 56 return 1; 57 } 58 else 59 { 60 parent.setConstructorType(ArrayElement.LARGE); 61 } 62 } 63 64 return 8; 65 66 } 67 else 68 { 69 return 0 == _value.longValue() ? 1 : (1 <= _value.longValue() && _value.longValue() <= 255) ? 2 : 9; 70 } 71 72 } 73 74 public Object getValue() 75 { 76 return _value; 77 } 78 79 public Data.DataType getDataType() 80 { 81 return Data.DataType.ULONG; 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.longValue()); 97 } 98 else 99 { 100 b.put(cast(byte)0x44); 101 } 102 break; 103 case 2: 104 b.put(cast(byte)0x53); 105 b.put(cast(byte)_value.longValue()); 106 break; 107 case 9: 108 b.put(cast(byte)0x80); 109 goto case; 110 case 8: 111 b.putLong(_value.longValue()); 112 break; 113 default: 114 break; 115 } 116 117 return size; 118 } 119 }