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