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