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.LongComparator;
027import org.apache.directory.mavibot.btree.exception.SerializerCreationException;
028
029
030/**
031 * The Long serializer.
032 *
033 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
034 */
035public class LongSerializer extends AbstractElementSerializer<Long>
036{
037    /** A static instance of a LongSerializer */
038    public final static LongSerializer INSTANCE = new LongSerializer();
039
040    /**
041     * Create a new instance of LongSerializer
042     */
043    private LongSerializer()
044    {
045        super( LongComparator.INSTANCE );
046    }
047
048
049    /**
050     * {@inheritDoc}
051     */
052    public byte[] serialize( Long element )
053    {
054        return serialize( element.longValue() );
055    }
056
057
058    /**
059     * Serialize an long
060     *
061     * @param value the value to serialize
062     * @return The byte[] containing the serialized long
063     */
064    public static byte[] serialize( long value )
065    {
066        byte[] bytes = new byte[8];
067
068        return serialize( bytes, 0, value );
069    }
070
071
072    /**
073     * Serialize an long
074     *
075     * @param buffer the Buffer that will contain the serialized value
076     * @param start the position in the buffer we will store the serialized long
077     * @param value the value to serialize
078     * @return The byte[] containing the serialized long
079     */
080    public static byte[] serialize( byte[] buffer, int start, long value )
081    {
082        buffer[start] = ( byte ) ( value >>> 56 );
083        buffer[start + 1] = ( byte ) ( value >>> 48 );
084        buffer[start + 2] = ( byte ) ( value >>> 40 );
085        buffer[start + 3] = ( byte ) ( value >>> 32 );
086        buffer[start + 4] = ( byte ) ( value >>> 24 );
087        buffer[start + 5] = ( byte ) ( value >>> 16 );
088        buffer[start + 6] = ( byte ) ( value >>> 8 );
089        buffer[start + 7] = ( byte ) ( value );
090
091        return buffer;
092    }
093
094
095    /**
096     * A static method used to deserialize a Long from a byte array.
097     * @param in The byte array containing the Long
098     * @param start the position in the byte[] we will deserialize the long from
099     * @return A Long
100     */
101    public static Long deserialize( byte[] in )
102    {
103        return deserialize( in, 0 );
104    }
105
106
107    /**
108     * A static method used to deserialize an Integer from a byte array.
109     * @param in The byte array containing the Integer
110     * @param start the position in the byte[] we will deserialize the long from
111     * @return An Integer
112     */
113    public static Long deserialize( byte[] in, int start )
114    {
115        if ( ( in == null ) || ( in.length < 8 + start ) )
116        {
117            throw new SerializerCreationException( "Cannot extract a Long from a buffer with not enough bytes" );
118        }
119
120        long result = ( ( long ) in[start] << 56 ) +
121            ( ( in[start + 1] & 0x00FFL ) << 48 ) +
122            ( ( in[start + 2] & 0x00FFL ) << 40 ) +
123            ( ( in[start + 3] & 0x00FFL ) << 32 ) +
124            ( ( in[start + 4] & 0x00FFL ) << 24 ) +
125            ( ( in[start + 5] & 0x00FFL ) << 16 ) +
126            ( ( in[start + 6] & 0x00FFL ) << 8 ) +
127            ( in[start + 7] & 0x00FFL );
128
129        return result;
130    }
131
132
133    /**
134     * A method used to deserialize a Long from a byte array.
135     * @param in The byte array containing the Long
136     * @param start the position in the byte[] we will deserialize the long from
137     * @return A Long
138     */
139    public Long fromBytes( byte[] in )
140    {
141        return deserialize( in, 0 );
142    }
143
144
145    /**
146     * A method used to deserialize an Integer from a byte array.
147     * @param in The byte array containing the Integer
148     * @param start the position in the byte[] we will deserialize the long from
149     * @return An Integer
150     */
151    public Long fromBytes( byte[] in, int start )
152    {
153        if ( ( in == null ) || ( in.length < 8 + start ) )
154        {
155            throw new SerializerCreationException( "Cannot extract a Long from a buffer with not enough bytes" );
156        }
157
158        long result = ( ( long ) in[start] << 56 ) +
159            ( ( in[start + 1] & 0xFFL ) << 48 ) +
160            ( ( in[start + 2] & 0xFFL ) << 40 ) +
161            ( ( in[start + 3] & 0xFFL ) << 32 ) +
162            ( ( in[start + 4] & 0xFFL ) << 24 ) +
163            ( ( in[start + 5] & 0xFFL ) << 16 ) +
164            ( ( in[start + 6] & 0xFFL ) << 8 ) +
165            ( in[start + 7] & 0xFFL );
166
167        return result;
168    }
169
170
171    /**
172     * {@inheritDoc}
173     */
174    public Long deserialize( BufferHandler bufferHandler ) throws IOException
175    {
176        byte[] in = bufferHandler.read( 8 );
177
178        return deserialize( in );
179    }
180
181
182    /**
183     * {@inheritDoc}
184     */
185    public Long deserialize( ByteBuffer buffer ) throws IOException
186    {
187        return buffer.getLong();
188    }
189}