View Javadoc

1   /*
2    *   Copyright 2005 The Apache Software Foundation
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   *
16   */
17  package org.apache.asn1new.util;
18  
19  import org.apache.asn1.codec.DecoderException;
20  import org.apache.asn1new.ber.tlv.Value;
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  
24  
25  /***
26   * Parse and decode a Boolean value.
27   * 
28   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
29   */
30  public class BooleanDecoder
31  {
32      /*** The logger */
33      private static final Logger log = LoggerFactory.getLogger( BooleanDecoder.class );
34  
35      //~ Methods ------------------------------------------------------------------------------------
36  
37      /***
38       * Parse a byte buffer and send back a booelan.
39       * 
40       * @param value The byte buffer to parse
41       *
42       * @return A boolean.
43       *
44       * @throws DecoderException Thrown if the byte stream does not contains a boolean
45       */
46      public static boolean parse( Value value ) throws BooleanDecoderException
47      {
48          byte[] bytes  = value.getData();
49  
50          if ( bytes.length != 1 )
51          {
52              throw new BooleanDecoderException(
53                  "The value is not 1 byte long. This is not allowed for a boolean" );
54          }
55  
56          if ( ( bytes[0] != 0 ) && ( bytes[0] != 255 ) )
57          {
58              log.warn( "A boolean must be encoded with a 0x00 or a 0xFF value" );
59          }
60  
61          return bytes[0] != 0;
62      }
63  }