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   * @version $Rev$
33   */
34  public class LongSerializer implements Serializer
35  {
36      private static final long serialVersionUID = 237756689544852128L;
37      public static final LongSerializer INSTANCE = new LongSerializer();
38  
39  
40      public byte[] serialize( Object o ) throws IOException
41      {
42          long id = ( Long ) o;
43          byte[] bites = new byte[8];
44  
45          bites[0] = ( byte ) ( id >> 56 );
46          bites[1] = ( byte ) ( id >> 48 );
47          bites[2] = ( byte ) ( id >> 40 );
48          bites[3] = ( byte ) ( id >> 32 );
49          bites[4] = ( byte ) ( id >> 24 );
50          bites[5] = ( byte ) ( id >> 16 );
51          bites[6] = ( byte ) ( id >> 8 );
52          bites[7] = ( byte ) id;
53  
54          return bites;
55      }
56  
57  
58      public Object deserialize( byte[] bites ) throws IOException
59      {
60          long id;
61          id = bites[0]  + ( ( bites[0] < 0 ) ? 256 : 0 );
62          id <<= 8;
63          id += bites[1] + ( ( bites[1] < 0 ) ? 256 : 0 );
64          id <<= 8;
65          id += bites[2] + ( ( bites[2] < 0 ) ? 256 : 0 );
66          id <<= 8;
67          id += bites[3] + ( ( bites[3] < 0 ) ? 256 : 0 );
68          id <<= 8;
69          id += bites[4] + ( ( bites[4] < 0 ) ? 256 : 0 );
70          id <<= 8;
71          id += bites[5] + ( ( bites[5] < 0 ) ? 256 : 0 );
72          id <<= 8;
73          id += bites[6] + ( ( bites[6] < 0 ) ? 256 : 0 );
74          id <<= 8;
75          id += bites[7] + ( ( bites[7] < 0 ) ? 256 : 0 );
76          return id;
77      }
78  }