1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.mavibot.btree.serializer;
21
22
23 import java.io.IOException;
24 import java.nio.ByteBuffer;
25
26 import org.apache.directory.mavibot.btree.comparator.ByteComparator;
27 import org.apache.directory.mavibot.btree.exception.SerializerCreationException;
28
29
30
31
32
33
34
35 public class ByteSerializer extends AbstractElementSerializer<Byte>
36 {
37
38 public static final ByteSerializer INSTANCE = new ByteSerializer();
39
40
41
42
43 private ByteSerializer()
44 {
45 super( ByteComparator.INSTANCE );
46 }
47
48
49
50
51
52 public byte[] serialize( Byte element )
53 {
54 byte[] bytes = new byte[1];
55
56 return serialize( bytes, 0, element );
57 }
58
59
60
61
62
63
64
65
66 public static byte[] serialize( byte value )
67 {
68 byte[] bytes = new byte[1];
69
70 return serialize( bytes, 0, value );
71 }
72
73
74
75
76
77
78
79
80
81
82 public static byte[] serialize( byte[] buffer, int start, byte value )
83 {
84 buffer[start] = value;
85
86 return buffer;
87 }
88
89
90
91
92
93
94
95 public static Byte deserialize( byte[] in )
96 {
97 return deserialize( in, 0 );
98 }
99
100
101
102
103
104
105
106
107 public static Byte deserialize( byte[] in, int start )
108 {
109 if ( ( in == null ) || ( in.length < 1 + start ) )
110 {
111 throw new SerializerCreationException( "Cannot extract a Byte from a buffer with not enough bytes" );
112 }
113
114 return in[start];
115 }
116
117
118
119
120
121
122
123 public Byte fromBytes( byte[] in )
124 {
125 return deserialize( in, 0 );
126 }
127
128
129
130
131
132
133
134
135 public Byte fromBytes( byte[] in, int start )
136 {
137 if ( ( in == null ) || ( in.length < 1 + start ) )
138 {
139 throw new SerializerCreationException( "Cannot extract a Byte from a buffer with not enough bytes" );
140 }
141
142 return in[start];
143 }
144
145
146
147
148
149 public Byte deserialize( ByteBuffer buffer ) throws IOException
150 {
151 return buffer.get();
152 }
153
154
155
156
157
158 public Byte deserialize( BufferHandler bufferHandler ) throws IOException
159 {
160 byte[] in = bufferHandler.read( 1 );
161
162 return deserialize( in );
163 }
164 }