1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.asn1.der;
19
20
21 /***
22 * DER Enumerated object.
23 */
24 public class DEREnumerated extends DERObject
25 {
26 /***
27 * Basic DERObject constructor.
28 */
29 public DEREnumerated( byte[] value )
30 {
31 super( ENUMERATED, value );
32 }
33
34 /***
35 * Static factory method, type-conversion operator.
36 */
37 public static DEREnumerated valueOf( int integer )
38 {
39 return new DEREnumerated( intToOctet( integer ) );
40 }
41
42 /***
43 * Lazy accessor
44 * @return integer value
45 */
46 public int intValue()
47 {
48 return octetToInt( value );
49 }
50
51 private static int octetToInt( byte[] bytes )
52 {
53 int result = 0;
54
55 for ( int ii = 0; ii < Math.min( 4, bytes.length ); ii++ )
56 {
57 result += bytes[ ii ] * ( 16 ^ ii );
58 }
59 return result;
60 }
61
62 private static byte[] intToOctet( int integer )
63 {
64 byte[] result = new byte[ 4 ];
65
66 for ( int ii = 0, shift = 24; ii < 4; ii++, shift -= 8 )
67 {
68 result[ ii ] = (byte) ( 0xFF & ( integer >> shift ) );
69 }
70 return result;
71 }
72 }
73