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