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