001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020 package org.apache.directory.server.core.partition.impl.btree.jdbm; 021 022 023 import jdbm.helper.Serializer; 024 025 import java.io.IOException; 026 027 028 /** 029 * A {@link Serializer} for Longs 030 * 031 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 032 * @version $Rev$ 033 */ 034 public class LongSerializer implements Serializer 035 { 036 private static final long serialVersionUID = 237756689544852128L; 037 public static final LongSerializer INSTANCE = new LongSerializer(); 038 039 040 public byte[] serialize( Object o ) throws IOException 041 { 042 long id = ( Long ) o; 043 byte[] bites = new byte[8]; 044 045 bites[0] = ( byte ) ( id >> 56 ); 046 bites[1] = ( byte ) ( id >> 48 ); 047 bites[2] = ( byte ) ( id >> 40 ); 048 bites[3] = ( byte ) ( id >> 32 ); 049 bites[4] = ( byte ) ( id >> 24 ); 050 bites[5] = ( byte ) ( id >> 16 ); 051 bites[6] = ( byte ) ( id >> 8 ); 052 bites[7] = ( byte ) id; 053 054 return bites; 055 } 056 057 058 public Object deserialize( byte[] bites ) throws IOException 059 { 060 long id; 061 id = bites[0] + ( ( bites[0] < 0 ) ? 256 : 0 ); 062 id <<= 8; 063 id += bites[1] + ( ( bites[1] < 0 ) ? 256 : 0 ); 064 id <<= 8; 065 id += bites[2] + ( ( bites[2] < 0 ) ? 256 : 0 ); 066 id <<= 8; 067 id += bites[3] + ( ( bites[3] < 0 ) ? 256 : 0 ); 068 id <<= 8; 069 id += bites[4] + ( ( bites[4] < 0 ) ? 256 : 0 ); 070 id <<= 8; 071 id += bites[5] + ( ( bites[5] < 0 ) ? 256 : 0 ); 072 id <<= 8; 073 id += bites[6] + ( ( bites[6] < 0 ) ? 256 : 0 ); 074 id <<= 8; 075 id += bites[7] + ( ( bites[7] < 0 ) ? 256 : 0 ); 076 return id; 077 } 078 }