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.SymbolElement; 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 import hunt.text.Charset; 21 import hunt.proton.amqp.Binary; 22 import hunt.proton.amqp.Symbol; 23 import hunt.proton.codec.Data; 24 25 class SymbolElement : AtomicElement!Symbol 26 { 27 private static Charset ASCII = StandardCharsets.US_ASCII; 28 private Symbol _value; 29 30 this(IElement parent, IElement prev, Symbol s) 31 { 32 super(parent, prev); 33 _value = s; 34 } 35 36 public int size() 37 { 38 int length = _value.length(); 39 40 if(isElementOfArray()) 41 { 42 ArrayElement parent = cast(ArrayElement) parent(); 43 44 if(parent.constructorType() == ArrayElement.SMALL) 45 { 46 if(length > 255) 47 { 48 parent.setConstructorType(ArrayElement.LARGE); 49 return 4+length; 50 } 51 else 52 { 53 return 1+length; 54 } 55 } 56 else 57 { 58 return 4+length; 59 } 60 } 61 else 62 { 63 if(length >255) 64 { 65 return 5 + length; 66 } 67 else 68 { 69 return 2 + length; 70 } 71 } 72 } 73 74 public Object getValue() 75 { 76 return _value; 77 } 78 79 public Data.DataType getDataType() 80 { 81 return Data.DataType.SYMBOL; 82 } 83 84 public int encode(ByteBuffer b) 85 { 86 int size = size(); 87 if(b.remaining()<size) 88 { 89 return 0; 90 } 91 if(isElementOfArray()) 92 { 93 ArrayElement parent = cast(ArrayElement) parent(); 94 95 if(parent.constructorType() == ArrayElement.SMALL) 96 { 97 b.put(cast(byte)_value.length()); 98 } 99 else 100 { 101 b.putInt(_value.length()); 102 } 103 } 104 else if(_value.length()<=255) 105 { 106 b.put(cast(byte)0xa3); 107 b.put(cast(byte)_value.length()); 108 } 109 else 110 { 111 b.put(cast(byte)0xb3); 112 b.put(cast(byte)_value.length()); 113 } 114 b.put(_value.toString().dup); 115 return size; 116 } 117 }