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.UnsignedByteElement;
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.UnsignedByte;
22 import hunt.proton.codec.Data;
23 
24 class UnsignedByteElement : AtomicElement!UnsignedByte
25 {
26 
27     private UnsignedByte _value;
28 
29     this(IElement parent, IElement prev, UnsignedByte ub)
30     {
31         super(parent, prev);
32         _value = ub;
33     }
34 
35     public int size()
36     {
37         return isElementOfArray() ? 1 : 2;
38     }
39 
40     public Object getValue()
41     {
42         return _value;
43     }
44 
45     public Data.DataType getDataType()
46     {
47         return Data.DataType.UBYTE;
48     }
49 
50     public int encode(ByteBuffer b)
51     {
52         if(isElementOfArray())
53         {
54             if(b.hasRemaining())
55             {
56                 b.put(_value.byteValue());
57                 return 1;
58             }
59         }
60         else
61         {
62             if(b.remaining()>=2)
63             {
64                 b.put(cast(byte)0x50);
65                 b.put(_value.byteValue());
66                 return 2;
67             }
68         }
69         return 0;
70     }
71 }