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;
21  
22  
23  import java.io.IOException;
24  import java.nio.ByteBuffer;
25  
26  import org.apache.directory.mavibot.btree.exception.SerializerCreationException;
27  import org.apache.directory.mavibot.btree.serializer.AbstractElementSerializer;
28  import org.apache.directory.mavibot.btree.serializer.BufferHandler;
29  import org.apache.directory.mavibot.btree.serializer.ByteArraySerializer;
30  import org.apache.directory.mavibot.btree.serializer.IntSerializer;
31  import org.apache.directory.mavibot.btree.serializer.LongSerializer;
32  import org.apache.directory.mavibot.btree.serializer.StringSerializer;
33  import org.apache.directory.mavibot.btree.util.Strings;
34  
35  
36  /**
37   * A serializer for the RevisionName object. The RevisionName will be serialized
38   * as a long (the revision), followed by the String.
39   *
40   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
41   */
42  /* no qualifier*/class RevisionNameSerializer extends AbstractElementSerializer<RevisionName>
43  {
44      /**
45       * Create a new instance of a RevisionNameSerializer
46       */
47      /* no qualifier*/RevisionNameSerializer()
48      {
49          super( new RevisionNameComparator() );
50      }
51  
52  
53      /**
54       * A static method used to deserialize a RevisionName from a byte array.
55       *
56       * @param in The byte array containing the RevisionName
57       * @return A RevisionName instance
58       */
59      /* no qualifier*/static RevisionName deserialize( byte[] in )
60      {
61          return deserialize( in, 0 );
62      }
63  
64  
65      /**
66       * A static method used to deserialize a RevisionName from a byte array.
67       *
68       * @param in The byte array containing the RevisionName
69       * @param start the position in the byte[] we will deserialize the RevisionName from
70       * @return A RevisionName instance
71       */
72      /* no qualifier*/static RevisionName deserialize( byte[] in, int start )
73      {
74          // The buffer must be 8 bytes plus 4 bytes long (the revision is a long, and the name is a String
75          if ( ( in == null ) || ( in.length < 12 + start ) )
76          {
77              throw new SerializerCreationException( "Cannot extract a RevisionName from a buffer with not enough bytes" );
78          }
79  
80          long revision = LongSerializer.deserialize( in, start );
81          String name = StringSerializer.deserialize( in, 8 + start );
82  
83          RevisionName revisionName = new RevisionName( revision, name );
84  
85          return revisionName;
86      }
87  
88  
89      /**
90       * A static method used to deserialize a RevisionName from a byte array.
91       *
92       * @param in The byte array containing the RevisionName
93       * @return A RevisionName instance
94       */
95      public RevisionName fromBytes( byte[] in )
96      {
97          return deserialize( in, 0 );
98      }
99  
100 
101     /**
102      * A static method used to deserialize a RevisionName from a byte array.
103      *
104      * @param in The byte array containing the RevisionName
105      * @param start the position in the byte[] we will deserialize the RevisionName from
106      * @return A RevisionName instance
107      */
108     public RevisionName fromBytes( byte[] in, int start )
109     {
110         // The buffer must be 8 bytes plus 4 bytes long (the revision is a long, and the name is a String
111         if ( ( in == null ) || ( in.length < 12 + start ) )
112         {
113             throw new SerializerCreationException( "Cannot extract a RevisionName from a buffer with not enough bytes" );
114         }
115 
116         long revision = LongSerializer.deserialize( in, start );
117         String name = StringSerializer.deserialize( in, 8 + start );
118 
119         RevisionName revisionName = new RevisionName( revision, name );
120 
121         return revisionName;
122     }
123 
124 
125     /**
126      * {@inheritDoc}
127      */
128     @Override
129     public byte[] serialize( RevisionName revisionName )
130     {
131         if ( revisionName == null )
132         {
133             throw new SerializerCreationException( "The revisionName instance should not be null " );
134         }
135 
136         byte[] result = null;
137 
138         if ( revisionName.getName() != null )
139         {
140             byte[] stringBytes = Strings.getBytesUtf8( revisionName.getName() );
141             int stringLen = stringBytes.length;
142             result = new byte[8 + 4 + stringBytes.length];
143             LongSerializer.serialize( result, 0, revisionName.getRevision() );
144 
145             if ( stringLen > 0 )
146             {
147                 ByteArraySerializer.serialize( result, 8, stringBytes );
148             }
149         }
150         else
151         {
152             result = new byte[8 + 4];
153             LongSerializer.serialize( result, 0, revisionName.getRevision() );
154             StringSerializer.serialize( result, 8, null );
155         }
156 
157         return result;
158     }
159 
160 
161     /**
162      * Serialize a RevisionName
163      *
164      * @param buffer the Buffer that will contain the serialized value
165      * @param start the position in the buffer we will store the serialized RevisionName
166      * @param value the value to serialize
167      * @return The byte[] containing the serialized RevisionName
168      */
169     /* no qualifier*/static byte[] serialize( byte[] buffer, int start, RevisionName revisionName )
170     {
171         if ( revisionName.getName() != null )
172         {
173             byte[] stringBytes = Strings.getBytesUtf8( revisionName.getName() );
174             int stringLen = stringBytes.length;
175             LongSerializer.serialize( buffer, start, revisionName.getRevision() );
176             IntSerializer.serialize( buffer, 8 + start, stringLen );
177             ByteArraySerializer.serialize( buffer, 12 + start, stringBytes );
178         }
179         else
180         {
181             LongSerializer.serialize( buffer, start, revisionName.getRevision() );
182             StringSerializer.serialize( buffer, 8, null );
183         }
184 
185         return buffer;
186     }
187 
188 
189     /**
190      * {@inheritDoc}
191      */
192     @Override
193     public RevisionName deserialize( BufferHandler bufferHandler ) throws IOException
194     {
195         byte[] revisionBytes = bufferHandler.read( 8 );
196         long revision = LongSerializer.deserialize( revisionBytes );
197 
198         byte[] lengthBytes = bufferHandler.read( 4 );
199 
200         int len = IntSerializer.deserialize( lengthBytes );
201 
202         switch ( len )
203         {
204             case 0:
205                 return new RevisionName( revision, "" );
206 
207             case -1:
208                 return new RevisionName( revision, null );
209 
210             default:
211                 byte[] nameBytes = bufferHandler.read( len );
212 
213                 return new RevisionName( revision, Strings.utf8ToString( nameBytes ) );
214         }
215     }
216 
217 
218     /**
219      * {@inheritDoc}
220      */
221     @Override
222     public RevisionName deserialize( ByteBuffer buffer ) throws IOException
223     {
224         // The revision
225         long revision = buffer.getLong();
226 
227         // The name's length
228         int len = buffer.getInt();
229 
230         switch ( len )
231         {
232             case 0:
233                 return new RevisionName( revision, "" );
234 
235             case -1:
236                 return new RevisionName( revision, null );
237 
238             default:
239                 byte[] nameBytes = new byte[len];
240                 buffer.get( nameBytes );
241 
242                 return new RevisionName( revision, Strings.utf8ToString( nameBytes ) );
243         }
244     }
245 }