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 */ 020package org.apache.directory.mavibot.btree.serializer; 021 022 023import java.io.IOException; 024import java.nio.ByteBuffer; 025 026import org.apache.directory.mavibot.btree.comparator.IntComparator; 027import org.apache.directory.mavibot.btree.exception.SerializerCreationException; 028 029 030/** 031 * The Integer serializer. 032 * 033 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 034 */ 035public class IntSerializer extends AbstractElementSerializer<Integer> 036{ 037 /** A static instance of a IntSerializer */ 038 public static final IntSerializer INSTANCE = new IntSerializer(); 039 040 /** 041 * Create a new instance of IntSerializer 042 */ 043 private IntSerializer() 044 { 045 super( IntComparator.INSTANCE ); 046 } 047 048 049 /** 050 * A static method used to deserialize an Integer from a byte array. 051 * @param in The byte array containing the Integer 052 * @return An Integer 053 */ 054 public static Integer deserialize( byte[] in ) 055 { 056 return deserialize( in, 0 ); 057 } 058 059 060 /** 061 * A static method used to deserialize an Integer from a byte array. 062 * @param in The byte array containing the Integer 063 * @param start the position in the byte[] we will deserialize the int from 064 * @return An Integer 065 */ 066 public static Integer deserialize( byte[] in, int start ) 067 { 068 if ( ( in == null ) || ( in.length < 4 + start ) ) 069 { 070 throw new SerializerCreationException( "Cannot extract a Integer from a buffer with not enough bytes" ); 071 } 072 073 return ( in[start] << 24 ) + 074 ( ( in[start + 1] & 0xFF ) << 16 ) + 075 ( ( in[start + 2] & 0xFF ) << 8 ) + 076 ( in[start + 3] & 0xFF ); 077 } 078 079 080 /** 081 * A method used to deserialize an Integer from a byte array. 082 * @param in The byte array containing the Integer 083 * @return An Integer 084 */ 085 public Integer fromBytes( byte[] in ) 086 { 087 return deserialize( in, 0 ); 088 } 089 090 091 /** 092 * A method used to deserialize an Integer from a byte array. 093 * @param in The byte array containing the Integer 094 * @param start the position in the byte[] we will deserialize the int from 095 * @return An Integer 096 */ 097 public Integer fromBytes( byte[] in, int start ) 098 { 099 if ( ( in == null ) || ( in.length < 4 + start ) ) 100 { 101 throw new SerializerCreationException( "Cannot extract a Integer from a buffer with not enough bytes" ); 102 } 103 104 return ( in[start] << 24 ) + 105 ( ( in[start + 1] & 0xFF ) << 16 ) + 106 ( ( in[start + 2] & 0xFF ) << 8 ) + 107 ( in[start + 3] & 0xFF ); 108 } 109 110 111 /** 112 * {@inheritDoc} 113 */ 114 public Integer deserialize( ByteBuffer buffer ) throws IOException 115 { 116 return buffer.getInt(); 117 } 118 119 120 /** 121 * {@inheritDoc} 122 */ 123 public Integer deserialize( BufferHandler bufferHandler ) throws IOException 124 { 125 byte[] in = bufferHandler.read( 4 ); 126 127 return deserialize( in ); 128 } 129 130 131 /** 132 * {@inheritDoc} 133 */ 134 public byte[] serialize( Integer element ) 135 { 136 return serialize( element.intValue() ); 137 } 138 139 140 /** 141 * Serialize an int 142 * 143 * @param value the value to serialize 144 * @return The byte[] containing the serialized int 145 */ 146 public static byte[] serialize( int value ) 147 { 148 byte[] bytes = new byte[4]; 149 150 return serialize( bytes, 0, value ); 151 } 152 153 154 /** 155 * Serialize an int 156 * 157 * @param buffer the Buffer that will contain the serialized value 158 * @param start the position in the buffer we will store the serialized int 159 * @param value the value to serialize 160 * @return The byte[] containing the serialized int 161 */ 162 public static byte[] serialize( byte[] buffer, int start, int value ) 163 { 164 buffer[start] = ( byte ) ( value >>> 24 ); 165 buffer[start + 1] = ( byte ) ( value >>> 16 ); 166 buffer[start + 2] = ( byte ) ( value >>> 8 ); 167 buffer[start + 3] = ( byte ) ( value ); 168 169 return buffer; 170 } 171}