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 java.io.IOException; 024 025 import org.apache.directory.server.core.avltree.Marshaller; 026 import org.apache.directory.server.i18n.I18n; 027 import org.apache.directory.shared.asn1.codec.binary.Hex; 028 029 030 /** 031 * Serializes and deserializes a BTreeRedirect object to and from a byte[] 032 * representation. The serialized form is a fixed size byte array of length 033 * 9. The first byte contains the magic number of value 1 for this kind of 034 * object and the last 8 bytes encode the record identifier as a long for 035 * the BTree. 036 * 037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 038 * @version $Rev$ 039 */ 040 public class BTreeRedirectMarshaller implements Marshaller<BTreeRedirect> 041 { 042 /** fixed byte array size of 9 for serialized form */ 043 static final int SIZE = 9; 044 /** a reusable instance of this Marshaller */ 045 public static final BTreeRedirectMarshaller INSTANCE = new BTreeRedirectMarshaller(); 046 047 /** 048 * @see Marshaller#serialize(Object) 049 */ 050 public final byte[] serialize( BTreeRedirect redirect ) throws IOException 051 { 052 byte[] bites = new byte[SIZE]; 053 054 bites[0] = 1; 055 056 bites[1] = ( byte ) ( redirect.recId >> 56 ); 057 bites[2] = ( byte ) ( redirect.recId >> 48 ); 058 bites[3] = ( byte ) ( redirect.recId >> 40 ); 059 bites[4] = ( byte ) ( redirect.recId >> 32 ); 060 bites[5] = ( byte ) ( redirect.recId >> 24 ); 061 bites[6] = ( byte ) ( redirect.recId >> 16 ); 062 bites[7] = ( byte ) ( redirect.recId >> 8 ); 063 bites[8] = ( byte ) redirect.recId; 064 065 return bites; 066 } 067 068 069 /** 070 * @see Marshaller#deserialize(byte[]) 071 */ 072 public final BTreeRedirect deserialize( byte[] bites ) throws IOException 073 { 074 if ( bites == null || bites.length != SIZE || bites[0] != 1 ) 075 { 076 if ( bites != null ) 077 { 078 throw new IOException( I18n.err( I18n.ERR_568, new String( Hex.encodeHex( bites ) ) ) ); 079 } 080 else 081 { 082 throw new IOException( I18n.err( I18n.ERR_569 ) ); 083 } 084 } 085 086 long recId; 087 recId = bites[1] + ( ( bites[1] < 0 ) ? 256 : 0 ); 088 recId <<= 8; 089 recId += bites[2] + ( ( bites[2] < 0 ) ? 256 : 0 ); 090 recId <<= 8; 091 recId += bites[3] + ( ( bites[3] < 0 ) ? 256 : 0 ); 092 recId <<= 8; 093 recId += bites[4] + ( ( bites[4] < 0 ) ? 256 : 0 ); 094 recId <<= 8; 095 recId += bites[5] + ( ( bites[5] < 0 ) ? 256 : 0 ); 096 recId <<= 8; 097 recId += bites[6] + ( ( bites[6] < 0 ) ? 256 : 0 ); 098 recId <<= 8; 099 recId += bites[7] + ( ( bites[7] < 0 ) ? 256 : 0 ); 100 recId <<= 8; 101 recId += bites[8] + ( ( bites[8] < 0 ) ? 256 : 0 ); 102 103 return new BTreeRedirect( recId ); 104 } 105 106 107 /** 108 * Checks to see if a byte[] contains a redirect. 109 * 110 * @param bites the bites to check for a redirect 111 * @return true if bites contain BTreeRedirect, false otherwise 112 */ 113 public static boolean isRedirect( byte[] bites ) 114 { 115 return bites != null && bites.length == SIZE && bites[0] == 1; 116 } 117 }