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