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.BooleanComparator;
027import org.apache.directory.mavibot.btree.exception.SerializerCreationException;
028
029
030/**
031 * The Boolean serializer.
032 *
033 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
034 */
035public class BooleanSerializer extends AbstractElementSerializer<Boolean>
036{
037    /** A static instance of a BooleanSerializer */
038    public static final BooleanSerializer INSTANCE = new BooleanSerializer();
039
040    /**
041     * Create a new instance of BooleanSerializer
042     */
043    private BooleanSerializer()
044    {
045        super( BooleanComparator.INSTANCE );
046    }
047
048
049    /**
050     * {@inheritDoc}
051     */
052    public byte[] serialize( Boolean element )
053    {
054        byte[] bytes = new byte[1];
055
056        return serialize( bytes, 0, element );
057    }
058
059
060    /**
061     * Serialize a boolean
062     *
063     * @param value the value to serialize
064     * @return The byte[] containing the serialized boolean
065     */
066    public static byte[] serialize( boolean element )
067    {
068        byte[] bytes = new byte[1];
069
070        return serialize( bytes, 0, element );
071    }
072
073
074    /**
075     * Serialize a boolean
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 boolean
079     * @param value the value to serialize
080     * @return The byte[] containing the serialized boolean
081     */
082    public static byte[] serialize( byte[] buffer, int start, boolean element )
083    {
084        buffer[start] = element ? ( byte ) 0x01 : ( byte ) 0x00;
085
086        return buffer;
087    }
088
089
090    /**
091     * A static method used to deserialize a Boolean from a byte array.
092     *
093     * @param in The byte array containing the boolean
094     * @return A boolean
095     */
096    public static Boolean deserialize( byte[] in )
097    {
098        return deserialize( in, 0 );
099    }
100
101
102    /**
103     * A static method used to deserialize a Boolean from a byte array.
104     *
105     * @param in The byte array containing the boolean
106     * @param start the position in the byte[] we will deserialize the boolean from
107     * @return A boolean
108     */
109    public static Boolean deserialize( byte[] in, int start )
110    {
111        if ( ( in == null ) || ( in.length < 1 + start ) )
112        {
113            throw new SerializerCreationException( "Cannot extract a Boolean from a buffer with not enough bytes" );
114        }
115
116        return in[start] == 0x01;
117    }
118
119
120    /**
121     * A method used to deserialize a Boolean from a byte array.
122     *
123     * @param in The byte array containing the boolean
124     * @return A boolean
125     */
126    public Boolean fromBytes( byte[] in )
127    {
128        return deserialize( in, 0 );
129    }
130
131
132    /**
133     * A method used to deserialize a Boolean from a byte array.
134     *
135     * @param in The byte array containing the boolean
136     * @param start the position in the byte[] we will deserialize the boolean from
137     * @return A boolean
138     */
139    public Boolean fromBytes( byte[] in, int start )
140    {
141        if ( ( in == null ) || ( in.length < 1 + start ) )
142        {
143            throw new SerializerCreationException( "Cannot extract a Boolean from a buffer with not enough bytes" );
144        }
145
146        return in[start] == 0x01;
147    }
148
149
150    /**
151     * {@inheritDoc}
152     */
153    public Boolean deserialize( ByteBuffer buffer ) throws IOException
154    {
155        return buffer.get() != 0x00;
156    }
157
158
159    /**
160     * {@inheritDoc}
161     */
162    @Override
163    public Boolean deserialize( BufferHandler bufferHandler ) throws IOException
164    {
165        byte[] in = bufferHandler.read( 1 );
166
167        return deserialize( in );
168    }
169}