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.mavibot.btree.serializer; 21 22 23 import java.io.IOException; 24 import java.nio.ByteBuffer; 25 26 import org.apache.directory.mavibot.btree.comparator.BooleanComparator; 27 28 29 /** 30 * The Boolean serializer. 31 * 32 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 33 */ 34 public class BooleanSerializer extends AbstractElementSerializer<Boolean> 35 { 36 /** 37 * Create a new instance of BooleanSerializer 38 */ 39 public BooleanSerializer() 40 { 41 super( new BooleanComparator() ); 42 } 43 44 45 /** 46 * {@inheritDoc} 47 */ 48 public byte[] serialize( Boolean element ) 49 { 50 byte[] bytes = new byte[1]; 51 52 return serialize( bytes, 0, element ); 53 } 54 55 56 /** 57 * Serialize a boolean 58 * 59 * @param value the value to serialize 60 * @return The byte[] containing the serialized boolean 61 */ 62 public static byte[] serialize( boolean element ) 63 { 64 byte[] bytes = new byte[1]; 65 66 return serialize( bytes, 0, element ); 67 } 68 69 70 /** 71 * Serialize a boolean 72 * 73 * @param buffer the Buffer that will contain the serialized value 74 * @param start the position in the buffer we will store the serialized boolean 75 * @param value the value to serialize 76 * @return The byte[] containing the serialized boolean 77 */ 78 public static byte[] serialize( byte[] buffer, int start, boolean element ) 79 { 80 buffer[start] = element ? ( byte ) 0x01 : ( byte ) 0x00; 81 82 return buffer; 83 } 84 85 86 /** 87 * A static method used to deserialize a Boolean from a byte array. 88 * 89 * @param in The byte array containing the boolean 90 * @return A boolean 91 */ 92 public static Boolean deserialize( byte[] in ) 93 { 94 return deserialize( in, 0 ); 95 } 96 97 98 /** 99 * A static method used to deserialize a Boolean from a byte array. 100 * 101 * @param in The byte array containing the boolean 102 * @param start the position in the byte[] we will deserialize the boolean from 103 * @return A boolean 104 */ 105 public static Boolean deserialize( byte[] in, int start ) 106 { 107 if ( ( in == null ) || ( in.length < 1 + start ) ) 108 { 109 throw new RuntimeException( "Cannot extract a Boolean from a buffer with not enough bytes" ); 110 } 111 112 return in[start] == 0x01; 113 } 114 115 116 /** 117 * A method used to deserialize a Boolean from a byte array. 118 * 119 * @param in The byte array containing the boolean 120 * @return A boolean 121 */ 122 public Boolean fromBytes( byte[] in ) 123 { 124 return deserialize( in, 0 ); 125 } 126 127 128 /** 129 * A method used to deserialize a Boolean from a byte array. 130 * 131 * @param in The byte array containing the boolean 132 * @param start the position in the byte[] we will deserialize the boolean from 133 * @return A boolean 134 */ 135 public Boolean fromBytes( byte[] in, int start ) 136 { 137 if ( ( in == null ) || ( in.length < 1 + start ) ) 138 { 139 throw new RuntimeException( "Cannot extract a Boolean from a buffer with not enough bytes" ); 140 } 141 142 return in[start] == 0x01; 143 } 144 145 146 /** 147 * {@inheritDoc} 148 */ 149 public Boolean deserialize( ByteBuffer buffer ) throws IOException 150 { 151 return buffer.get() != 0x00; 152 } 153 154 155 /** 156 * {@inheritDoc} 157 */ 158 @Override 159 public Boolean deserialize( BufferHandler bufferHandler ) throws IOException 160 { 161 byte[] in = bufferHandler.read( 1 ); 162 163 return deserialize( in ); 164 } 165 }