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 */ 019package org.apache.directory.api.util; 020 021 022/** 023 * A class containing static methods used to serialize and deserialize base types 024 * 025 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 026 */ 027public class Serialize 028{ 029 public final static byte TRUE = 0x01; 030 public final static byte FALSE = 0x00; 031 032 033 /** 034 * Write an integer into a buffer at a given position 035 * 036 * @param value The value to serialize 037 * @param buffer The buffer to store the value into 038 * @param pos The position where we serialize the integer 039 */ 040 public static int serialize( int value, byte[] buffer, int pos ) 041 { 042 if ( buffer.length - pos < 4 ) 043 { 044 throw new ArrayIndexOutOfBoundsException(); 045 } 046 047 buffer[pos++] = ( byte ) ( ( value >>> 24 ) & 0xFF ); 048 buffer[pos++] = ( byte ) ( ( value >>> 16 ) & 0xFF ); 049 buffer[pos++] = ( byte ) ( ( value >>> 8 ) & 0xFF ); 050 buffer[pos++] = ( byte ) ( ( value >>> 0 ) & 0xFF ); 051 052 return pos; 053 } 054 055 056 /** 057 * Write a byte[] into a buffer at a given position 058 * 059 * @param value The value to serialize 060 * @param buffer The buffer to store the value into 061 * @param pos The position where we serialize the byte[] 062 */ 063 public static int serialize( byte[] value, byte[] buffer, int pos ) 064 { 065 if ( buffer.length - pos < 4 + value.length ) 066 { 067 throw new ArrayIndexOutOfBoundsException(); 068 } 069 070 buffer[pos++] = ( byte ) ( ( value.length >>> 24 ) & 0xFF ); 071 buffer[pos++] = ( byte ) ( ( value.length >>> 16 ) & 0xFF ); 072 buffer[pos++] = ( byte ) ( ( value.length >>> 8 ) & 0xFF ); 073 buffer[pos++] = ( byte ) ( ( value.length >>> 0 ) & 0xFF ); 074 075 System.arraycopy( value, 0, buffer, pos, value.length ); 076 077 return pos + value.length; 078 } 079 080 081 /** 082 * Read an integer from a buffer at a given position 083 * 084 * @param buffer The buffer containing the serialized integer 085 * @param pos The position from which we will read an integer 086 * @return the deserialized integer 087 */ 088 public static int deserializeInt( byte[] buffer, int pos ) 089 { 090 if ( buffer.length - pos < 4 ) 091 { 092 throw new ArrayIndexOutOfBoundsException(); 093 } 094 095 return ( buffer[pos] << 24 ) + ( buffer[pos + 1] << 16 ) + ( buffer[pos + 2] << 8 ) + ( buffer[pos + 3] << 0 ); 096 } 097 098 099 /** 100 * Read a byte[] from a buffer at a given position 101 * 102 * @param buffer The buffer containing the serialized byte[] 103 * @param pos The position from which we will read a byte[] 104 * @return the deserialized byte[] 105 */ 106 public static byte[] deserializeBytes( byte[] buffer, int pos ) 107 { 108 if ( buffer.length - pos < 4 ) 109 { 110 throw new ArrayIndexOutOfBoundsException(); 111 } 112 113 int len = deserializeInt( buffer, pos ); 114 pos += 4; 115 116 if ( len > 0 ) 117 { 118 if ( buffer.length - pos < len ) 119 { 120 throw new ArrayIndexOutOfBoundsException(); 121 } 122 123 byte[] result = new byte[len]; 124 125 System.arraycopy( buffer, pos, result, 0, len ); 126 127 return result; 128 } 129 else 130 { 131 return Strings.EMPTY_BYTES; 132 } 133 } 134 135 136 /** 137 * Read a boolean from a buffer at a given position 138 * 139 * @param buffer The buffer containing the serialized boolean 140 * @param pos The position from which we will read a boolean 141 * @return the deserialized boolean 142 */ 143 public static boolean deserializeBoolean( byte[] buffer, int pos ) 144 { 145 if ( buffer.length - pos < 1 ) 146 { 147 throw new ArrayIndexOutOfBoundsException(); 148 } 149 150 byte value = buffer[pos]; 151 152 return ( value != 0x00 ); 153 } 154}