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 jdbm.helper.Serializer;
24  
25  import java.io.IOException;
26  
27  
28  /**
29   * A {@link Serializer} for Longs
30   *
31   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
32   */
33  public class LongSerializer implements Serializer
34  {
35      private static final long serialVersionUID = 237756689544852128L;
36      public static final LongSerializerrver/core/partition/impl/btree/jdbm/LongSerializer.html#LongSerializer">LongSerializer INSTANCE = new LongSerializer();
37  
38  
39      public byte[] serialize( Object o ) throws IOException
40      {
41          long id = ( Long ) o;
42          byte[] bites = new byte[8];
43  
44          bites[0] = ( byte ) ( id >> 56 );
45          bites[1] = ( byte ) ( id >> 48 );
46          bites[2] = ( byte ) ( id >> 40 );
47          bites[3] = ( byte ) ( id >> 32 );
48          bites[4] = ( byte ) ( id >> 24 );
49          bites[5] = ( byte ) ( id >> 16 );
50          bites[6] = ( byte ) ( id >> 8 );
51          bites[7] = ( byte ) id;
52  
53          return bites;
54      }
55  
56  
57      public Object deserialize( byte[] bytes ) throws IOException
58      {
59          return  ( ( long ) ( bytes[0] & 0xFF ) << 56 )
60                  | ( ( long ) ( bytes[1] & 0xFF ) << 48 )
61                  | ( ( long ) ( bytes[2] & 0xFF ) << 40 )
62                  | ( ( long ) ( bytes[3] & 0xFF ) << 32 )
63                  | ( ( long ) ( bytes[4] & 0xFF ) << 24 )
64                  | ( ( long ) ( bytes[5] & 0xFF ) << 16 )
65                  | ( ( long ) ( bytes[6] & 0xFF ) << 8 )
66                  | ( ( long ) ( bytes[7] & 0xFF ) );
67      }
68  }