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.ShortComparator;
27
28
29 /**
30 * The Short serializer.
31 *
32 * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
33 */
34 public class ShortSerializer extends AbstractElementSerializer<Short>
35 {
36 /**
37 * Create a new instance of ShortSerializer
38 */
39 public ShortSerializer()
40 {
41 super( new ShortComparator() );
42 }
43
44
45 /**
46 * {@inheritDoc}
47 */
48 public byte[] serialize( Short element )
49 {
50 byte[] bytes = new byte[2];
51
52 return serialize( bytes, 0, element );
53 }
54
55
56 /**
57 * Serialize a short
58 *
59 * @param value the value to serialize
60 * @return The byte[] containing the serialized short
61 */
62 public static byte[] serialize( short value )
63 {
64 byte[] bytes = new byte[2];
65
66 return serialize( bytes, 0, value );
67 }
68
69
70 /**
71 * Serialize a short
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 short
75 * @param value the value to serialize
76 * @return The byte[] containing the serialized short
77 */
78 public static byte[] serialize( byte[] buffer, int start, short value )
79 {
80 buffer[start] = ( byte ) ( value >>> 8 );
81 buffer[start + 1] = ( byte ) ( value );
82
83 return buffer;
84 }
85
86
87 /**
88 * A static method used to deserialize a Short from a byte array.
89 * @param in The byte array containing the Short
90 * @return A Short
91 */
92 public static Short deserialize( byte[] in )
93 {
94 return deserialize( in, 0 );
95 }
96
97
98 /**
99 * A static method used to deserialize a Short from a byte array.
100 * @param in The byte array containing the Short
101 * @param start the position in the byte[] we will deserialize the short from
102 * @return A Short
103 */
104 public static Short deserialize( byte[] in, int start )
105 {
106 if ( ( in == null ) || ( in.length < 2 + start ) )
107 {
108 throw new RuntimeException( "Cannot extract a Short from a buffer with not enough bytes" );
109 }
110
111 return ( short ) ( ( in[start] << 8 ) + ( in[start + 1] & 0xFF ) );
112 }
113
114
115 /**
116 * {@inheritDoc}
117 */
118 public Short deserialize( ByteBuffer buffer ) throws IOException
119 {
120 return buffer.getShort();
121 }
122
123
124 /**
125 * {@inheritDoc}
126 */
127 public Short deserialize( BufferHandler bufferHandler ) throws IOException
128 {
129 byte[] in = bufferHandler.read( 2 );
130
131 return deserialize( in );
132 }
133 }