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.server.core.partition.impl.btree.jdbm;
21  
22  
23  import java.io.IOException;
24  
25  import org.apache.directory.server.core.avltree.Marshaller;
26  import org.apache.directory.shared.asn1.codec.binary.Hex;
27  
28  
29  /**
30   * Serializes and deserializes a BTreeRedirect object to and from a byte[]
31   * representation.  The serialized form is a fixed size byte array of length
32   * 9.  The first byte contains the magic number of value 1 for this kind of
33   * object and the last 8 bytes encode the record identifier as a long for
34   * the BTree.
35   *
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   * @version $Rev$
38   */public class BTreeRedirectMarshaller implements Marshaller<BTreeRedirect>
39  {
40      /** fixed byte array size of 9 for serialized form */
41      static final int SIZE = 9;
42      /** a reusable instance of this Marshaller */
43      public static final BTreeRedirectMarshaller INSTANCE = new BTreeRedirectMarshaller();
44  
45      /**
46       * @see Marshaller#serialize(Object)
47       */
48      public final byte[] serialize( BTreeRedirect redirect ) throws IOException
49      {
50          byte[] bites = new byte[SIZE];
51  
52          bites[0] = 1;
53  
54          bites[1] = ( byte ) ( redirect.recId >> 56 );
55          bites[2] = ( byte ) ( redirect.recId >> 48 );
56          bites[3] = ( byte ) ( redirect.recId >> 40 );
57          bites[4] = ( byte ) ( redirect.recId >> 32 );
58          bites[5] = ( byte ) ( redirect.recId >> 24 );
59          bites[6] = ( byte ) ( redirect.recId >> 16 );
60          bites[7] = ( byte ) ( redirect.recId >> 8 );
61          bites[8] = ( byte ) redirect.recId;
62  
63          return bites;
64      }
65  
66  
67      /**
68       * @see Marshaller#deserialize(byte[]) 
69       */
70      public final BTreeRedirect deserialize( byte[] bites ) throws IOException
71      {
72          if ( bites == null || bites.length != SIZE || bites[0] != 1 )
73          {
74              if ( bites != null )
75              {
76                  throw new IOException( "Not a serialized BTreeRedirect object: "
77                          + new String( Hex.encodeHex( bites ) ) );
78              }
79              else
80              {
81                  throw new IOException( "Not a serialized BTreeRedirect object: byte array is null." );
82              }
83          }
84  
85          long recId;
86          recId = bites[1] + ( ( bites[1] < 0 ) ? 256 : 0 );
87          recId <<= 8;
88          recId += bites[2] + ( ( bites[2] < 0 ) ? 256 : 0 );
89          recId <<= 8;
90          recId += bites[3] + ( ( bites[3] < 0 ) ? 256 : 0 );
91          recId <<= 8;
92          recId += bites[4] + ( ( bites[4] < 0 ) ? 256 : 0 );
93          recId <<= 8;
94          recId += bites[5] + ( ( bites[5] < 0 ) ? 256 : 0 );
95          recId <<= 8;
96          recId += bites[6] + ( ( bites[6] < 0 ) ? 256 : 0 );
97          recId <<= 8;
98          recId += bites[7] + ( ( bites[7] < 0 ) ? 256 : 0 );
99          recId <<= 8;
100         recId += bites[8] + ( ( bites[8] < 0 ) ? 256 : 0 );
101 
102         return new BTreeRedirect( recId );
103     }
104 
105 
106     /**
107      * Checks to see if a byte[] contains a redirect.
108      *
109      * @param bites the bites to check for a redirect
110      * @return true if bites contain BTreeRedirect, false otherwise
111      */
112     public static boolean isRedirect( byte[] bites )
113     {
114         return bites != null && bites.length == SIZE && bites[0] == 1;
115     }
116 }