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 * https://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 private BooleanDecoder() 041 { 042 } 043 044 /** 045 * Parse a Value containing a byte[] and send back a boolean. 046 * 047 * @param value The Value to parse 048 * @return A boolean. 049 * @throws BooleanDecoderException Thrown if the Value does not contains a boolean 050 */ 051 public static boolean parse( BerValue value ) throws BooleanDecoderException 052 { 053 byte[] bytes = value.getData(); 054 055 if ( Strings.isEmpty( bytes ) ) 056 { 057 throw new BooleanDecoderException( I18n.err( I18n.ERR_01302_0_BYTES_LONG_BOOLEAN ) ); 058 } 059 060 if ( bytes.length != 1 ) 061 { 062 throw new BooleanDecoderException( I18n.err( I18n.ERR_01303_N_BYTES_LONG_BOOLEAN ) ); 063 } 064 065 if ( ( bytes[0] != 0 ) && ( bytes[0] != ( byte ) 0xFF ) ) 066 { 067 if ( LOG.isWarnEnabled() ) 068 { 069 LOG.warn( I18n.msg( I18n.MSG_01300_BOOLEAN_0X00_0XFF ) ); 070 } 071 } 072 073 return bytes[0] != 0; 074 } 075}