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.ByteElement;
13 
14 
15 import hunt.proton.codec.Data;
16 import hunt.proton.codec.Data;
17 import hunt.collection.Map;
18 import hunt.io.ByteBuffer;
19 import std.conv;
20 import hunt.proton.codec.impl.AtomicElement;
21 import hunt.proton.codec.impl.Element;
22 import hunt.proton.codec.impl.ArrayElement;
23 import hunt.proton.codec.impl.AbstractElement;
24 import hunt.Byte;
25 
26 class ByteElement : AtomicElement!Byte
27 {
28 
29     private byte _value;
30 
31     this(IElement parent, IElement prev, byte b)
32     {
33         super(parent, prev);
34         _value = b;
35     }
36 
37     public int size()
38     {
39         return isElementOfArray() ? 1 : 2;
40     }
41 
42     public Object getValue()
43     {
44         return  new Byte( _value);
45     }
46 
47     public Data.DataType getDataType()
48     {
49         return Data.DataType.BYTE;
50     }
51 
52     public int encode(ByteBuffer b)
53     {
54         if(isElementOfArray())
55         {
56             if(b.hasRemaining())
57             {
58                 b.put(_value);
59                 return 1;
60             }
61         }
62         else
63         {
64             if(b.remaining()>=2)
65             {
66                 b.put(cast(byte)0x51);
67                 b.put(_value);
68                 return 2;
69             }
70         }
71         return 0;
72     }
73 }