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.CharComparator;
27
28
29
30
31
32
33
34 public class CharSerializer extends AbstractElementSerializer<Character>
35 {
36
37
38
39 public CharSerializer()
40 {
41 super( new CharComparator() );
42 }
43
44
45
46
47
48 public byte[] serialize( Character element )
49 {
50 byte[] bytes = new byte[2];
51
52 return serialize( bytes, 0, element );
53 }
54
55
56
57
58
59
60
61
62 public static byte[] serialize( char value )
63 {
64 byte[] bytes = new byte[2];
65
66 return serialize( bytes, 0, value );
67 }
68
69
70
71
72
73
74
75
76
77
78 public static byte[] serialize( byte[] buffer, int start, char value )
79 {
80 buffer[start] = ( byte ) ( value >>> 8 );
81 buffer[start + 1] = ( byte ) ( value );
82
83 return buffer;
84 }
85
86
87
88
89
90
91
92 public static Character deserialize( byte[] in )
93 {
94 return deserialize( in, 0 );
95 }
96
97
98
99
100
101
102
103
104 public static Character deserialize( byte[] in, int start )
105 {
106 if ( ( in == null ) || ( in.length < 2 + start ) )
107 {
108 throw new RuntimeException( "Cannot extract a Character from a buffer with not enough bytes" );
109 }
110
111 return Character.valueOf( ( char ) ( ( in[start] << 8 ) +
112 ( in[start + 1] & 0xFF ) ) );
113 }
114
115
116
117
118
119
120
121 public Character fromBytes( byte[] in )
122 {
123 return deserialize( in, 0 );
124 }
125
126
127
128
129
130
131
132
133 public Character fromBytes( byte[] in, int start )
134 {
135 if ( ( in == null ) || ( in.length < 2 + start ) )
136 {
137 throw new RuntimeException( "Cannot extract a Character from a buffer with not enough bytes" );
138 }
139
140 return Character.valueOf( ( char ) ( ( in[start] << 8 ) +
141 ( in[start + 1] & 0xFF ) ) );
142 }
143
144
145
146
147
148 public Character deserialize( ByteBuffer buffer ) throws IOException
149 {
150 return buffer.getChar();
151 }
152
153
154
155
156
157 public Character deserialize( BufferHandler bufferHandler ) throws IOException
158 {
159 byte[] in = bufferHandler.read( 2 );
160
161 return deserialize( in );
162 }
163 }