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.ByteType;
13 
14 
15 import hunt.proton.codec.TypeEncoding;
16 import hunt.proton.codec.EncodingCodes;
17 import hunt.proton.codec.FixedSizePrimitiveTypeEncoding;
18 import hunt.proton.codec.AbstractPrimitiveType;
19 import hunt.collection.Collection;
20 import hunt.collection.Collections;
21 import hunt.proton.codec.EncoderImpl;
22 import hunt.proton.codec.DecoderImpl;
23 import hunt.Byte;
24 import hunt.proton.codec.PrimitiveTypeEncoding;
25 
26 class ByteType : AbstractPrimitiveType!(Byte)
27 {
28     private ByteEncoding _byteEncoding;
29 
30     this(EncoderImpl encoder, DecoderImpl decoder)
31     {
32         _byteEncoding = new ByteEncoding(encoder, decoder);
33         encoder.register(typeid(Byte), this);
34         decoder.register(this);
35     }
36 
37     public TypeInfo getTypeClass()
38     {
39         return typeid(Byte);
40     }
41 
42     public ITypeEncoding getEncoding(Object val)
43     {
44         return _byteEncoding;
45     }
46 
47 
48     public ByteEncoding getCanonicalEncoding()
49     {
50         return _byteEncoding;
51     }
52 
53     public Collection!(TypeEncoding!(Byte)) getAllEncodings()
54     {
55         return Collections.singleton!(TypeEncoding!(Byte))(_byteEncoding);
56     }
57 
58      //Collection!(PrimitiveTypeEncoding!(Byte)) getAllEncodings()
59      //{
60      //   return super.getAllEncodings();
61      //}
62 
63     public void writeType(byte b)
64     {
65         _byteEncoding.write(b);
66     }
67 
68 
69     class ByteEncoding : FixedSizePrimitiveTypeEncoding!(Byte)
70     {
71 
72         this(EncoderImpl encoder, DecoderImpl decoder)
73         {
74             super(encoder, decoder);
75         }
76 
77         override
78         protected int getFixedSize()
79         {
80             return 1;
81         }
82 
83         override
84         public byte getEncodingCode()
85         {
86             return EncodingCodes.BYTE;
87         }
88 
89         public ByteType getType()
90         {
91             return this.outer;
92         }
93 
94         public void writeValue(Object val)
95         {
96             getEncoder().writeRaw((cast(Byte)val).byteValue);
97         }
98 
99 
100         public void write(byte val)
101         {
102             writeConstructor();
103             getEncoder().writeRaw(val);
104         }
105 
106         public void writeValue(byte val)
107         {
108             getEncoder().writeRaw(val);
109         }
110 
111         public bool encodesSuperset(TypeEncoding!(Byte) encoding)
112         {
113             return (getType() == encoding.getType());
114         }
115 
116         public Byte readValue()
117         {
118             return readPrimitiveValue();
119         }
120 
121         public Byte readPrimitiveValue()
122         {
123             return new Byte( getDecoder().readRawByte());
124         }
125 
126 
127         override
128         public bool encodesJavaPrimitive()
129         {
130             return true;
131         }
132 
133     }
134 }