View Javadoc
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  import org.apache.directory.mavibot.btree.exception.SerializerCreationException;
28  
29  
30  /**
31   * The Short serializer.
32   *
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   */
35  public class ShortSerializer extends AbstractElementSerializer<Short>
36  {
37      /** A static instance of a ShortSerializer */
38      public final static ShortSerializer INSTANCE = new ShortSerializer();
39  
40      /**
41       * Create a new instance of ShortSerializer
42       */
43      private ShortSerializer()
44      {
45          super( ShortComparator.INSTANCE );
46      }
47  
48  
49      /**
50       * {@inheritDoc}
51       */
52      public byte[] serialize( Short element )
53      {
54          byte[] bytes = new byte[2];
55  
56          return serialize( bytes, 0, element );
57      }
58  
59  
60      /**
61       * Serialize a short
62       *
63       * @param value the value to serialize
64       * @return The byte[] containing the serialized short
65       */
66      public static byte[] serialize( short value )
67      {
68          byte[] bytes = new byte[2];
69  
70          return serialize( bytes, 0, value );
71      }
72  
73  
74      /**
75       * Serialize a short
76       *
77       * @param buffer the Buffer that will contain the serialized value
78       * @param start the position in the buffer we will store the serialized short
79       * @param value the value to serialize
80       * @return The byte[] containing the serialized short
81       */
82      public static byte[] serialize( byte[] buffer, int start, short value )
83      {
84          buffer[start] = ( byte ) ( value >>> 8 );
85          buffer[start + 1] = ( byte ) ( value );
86  
87          return buffer;
88      }
89  
90  
91      /**
92       * A static method used to deserialize a Short from a byte array.
93       * @param in The byte array containing the Short
94       * @return A Short
95       */
96      public static Short deserialize( byte[] in )
97      {
98          return deserialize( in, 0 );
99      }
100 
101 
102     /**
103      * A static method used to deserialize a Short from a byte array.
104      * @param in The byte array containing the Short
105     * @param start the position in the byte[] we will deserialize the short from
106      * @return A Short
107      */
108     public static Short deserialize( byte[] in, int start )
109     {
110         if ( ( in == null ) || ( in.length < 2 + start ) )
111         {
112             throw new SerializerCreationException( "Cannot extract a Short from a buffer with not enough bytes" );
113         }
114 
115         return ( short ) ( ( in[start] << 8 ) + ( in[start + 1] & 0xFF ) );
116     }
117 
118 
119     /**
120      * A method used to deserialize a Short from a byte array.
121      * @param in The byte array containing the Short
122      * @return A Short
123      */
124     public Short fromBytes( byte[] in )
125     {
126         return deserialize( in, 0 );
127     }
128 
129 
130     /**
131      * A method used to deserialize a Short from a byte array.
132      * @param in The byte array containing the Short
133      * @param start the position in the byte[] we will deserialize the short from
134      * @return A Short
135      */
136     public Short fromBytes( byte[] in, int start )
137     {
138         if ( ( in == null ) || ( in.length < 2 + start ) )
139         {
140             throw new SerializerCreationException( "Cannot extract a Short from a buffer with not enough bytes" );
141         }
142 
143         return ( short ) ( ( in[start] << 8 ) + ( in[start + 1] & 0xFF ) );
144     }
145 
146 
147     /**
148      * {@inheritDoc}
149      */
150     public Short deserialize( ByteBuffer buffer ) throws IOException
151     {
152         return buffer.getShort();
153     }
154 
155 
156     /**
157      * {@inheritDoc}
158      */
159     public Short deserialize( BufferHandler bufferHandler ) throws IOException
160     {
161         byte[] in = bufferHandler.read( 2 );
162 
163         return deserialize( in );
164     }
165 }