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