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
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