001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.directory.mavibot.btree.serializer;
021
022
023import java.io.IOException;
024import java.nio.ByteBuffer;
025
026import org.apache.directory.mavibot.btree.comparator.ShortComparator;
027import org.apache.directory.mavibot.btree.exception.SerializerCreationException;
028
029
030/**
031 * The Short serializer.
032 *
033 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
034 */
035public class ShortSerializer extends AbstractElementSerializer<Short>
036{
037    /** A static instance of a ShortSerializer */
038    public final static ShortSerializer INSTANCE = new ShortSerializer();
039
040    /**
041     * Create a new instance of ShortSerializer
042     */
043    private ShortSerializer()
044    {
045        super( ShortComparator.INSTANCE );
046    }
047
048
049    /**
050     * {@inheritDoc}
051     */
052    public byte[] serialize( Short element )
053    {
054        byte[] bytes = new byte[2];
055
056        return serialize( bytes, 0, element );
057    }
058
059
060    /**
061     * Serialize a short
062     *
063     * @param value the value to serialize
064     * @return The byte[] containing the serialized short
065     */
066    public static byte[] serialize( short value )
067    {
068        byte[] bytes = new byte[2];
069
070        return serialize( bytes, 0, value );
071    }
072
073
074    /**
075     * Serialize a short
076     *
077     * @param buffer the Buffer that will contain the serialized value
078     * @param start the position in the buffer we will store the serialized short
079     * @param value the value to serialize
080     * @return The byte[] containing the serialized short
081     */
082    public static byte[] serialize( byte[] buffer, int start, short value )
083    {
084        buffer[start] = ( byte ) ( value >>> 8 );
085        buffer[start + 1] = ( byte ) ( value );
086
087        return buffer;
088    }
089
090
091    /**
092     * A static method used to deserialize a Short from a byte array.
093     * @param in The byte array containing the Short
094     * @return A Short
095     */
096    public static Short deserialize( byte[] in )
097    {
098        return deserialize( in, 0 );
099    }
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}