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.api.asn1.ber.tlv; 021 022 023import org.apache.directory.api.i18n.I18n; 024import org.apache.directory.api.util.Strings; 025import org.slf4j.Logger; 026import org.slf4j.LoggerFactory; 027 028 029/** 030 * Parse and decode a Boolean value. 031 * 032 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 033 */ 034public final class BooleanDecoder 035{ 036 /** The logger */ 037 private static final Logger LOG = LoggerFactory.getLogger( BooleanDecoder.class ); 038 039 040 /** 041 * Parse a Value containing a byte[] and send back a boolean. 042 * 043 * @param value The Value to parse 044 * @return A boolean. 045 * @throws BooleanDecoderException Thrown if the Value does not contains a boolean 046 */ 047 public static boolean parse( BerValue value ) throws BooleanDecoderException 048 { 049 byte[] bytes = value.getData(); 050 051 if ( Strings.isEmpty( bytes ) ) 052 { 053 throw new BooleanDecoderException( I18n.err( I18n.ERR_00034_0_BYTES_LONG_BOOLEAN ) ); 054 } 055 056 if ( bytes.length != 1 ) 057 { 058 throw new BooleanDecoderException( I18n.err( I18n.ERR_00035_N_BYTES_LONG_BOOLEAN ) ); 059 } 060 061 if ( ( bytes[0] != 0 ) && ( bytes[0] != ( byte ) 0xFF ) ) 062 { 063 LOG.warn( "A boolean must be encoded with a 0x00 or a 0xFF value" ); 064 } 065 066 return bytes[0] != 0; 067 } 068}