1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
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
28
29 /**
30 * The Byte serializer.
31 *
32 * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
33 */
34 public class ByteSerializer extends AbstractElementSerializer<Byte>
35 {
36 /**
37 * Create a new instance of ByteSerializer
38 */
39 public ByteSerializer()
40 {
41 super( new ByteComparator() );
42 }
43
44
45 /**
46 * {@inheritDoc}
47 */
48 public byte[] serialize( Byte element )
49 {
50 byte[] bytes = new byte[1];
51
52 return serialize( bytes, 0, element );
53 }
54
55
56 /**
57 * Serialize a byte
58 *
59 * @param value the value to serialize
60 * @return The byte[] containing the serialized byte
61 */
62 public static byte[] serialize( byte value )
63 {
64 byte[] bytes = new byte[1];
65
66 return serialize( bytes, 0, value );
67 }
68
69
70 /**
71 * Serialize a byte
72 *
73 * @param buffer the Buffer that will contain the serialized value
74 * @param start the position in the buffer we will store the serialized byte
75 * @param value the value to serialize
76 * @return The byte[] containing the serialized byte
77 */
78 public static byte[] serialize( byte[] buffer, int start, byte value )
79 {
80 buffer[start] = value;
81
82 return buffer;
83 }
84
85
86 /**
87 * A static method used to deserialize a Byte from a byte array.
88 * @param in The byte array containing the Byte
89 * @return A Byte
90 */
91 public static Byte deserialize( byte[] in )
92 {
93 return deserialize( in, 0 );
94 }
95
96
97 /**
98 * A static method used to deserialize a Byte from a byte array.
99 * @param in The byte array containing the Byte
100 * @param start the position in the byte[] we will deserialize the byte from
101 * @return A Byte
102 */
103 public static Byte deserialize( byte[] in, int start )
104 {
105 if ( ( in == null ) || ( in.length < 1 + start ) )
106 {
107 throw new RuntimeException( "Cannot extract a Byte from a buffer with not enough bytes" );
108 }
109
110 return in[start];
111 }
112
113
114 /**
115 * {@inheritDoc}
116 */
117 public Byte deserialize( ByteBuffer buffer ) throws IOException
118 {
119 return buffer.get();
120 }
121
122
123 /**
124 * {@inheritDoc}
125 */
126 public Byte deserialize( BufferHandler bufferHandler ) throws IOException
127 {
128 byte[] in = bufferHandler.read( 1 );
129
130 return deserialize( in );
131 }
132 }