1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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 }