001 /*
002 * Copyright (c) 2000 - 2006 The Legion Of The Bouncy Castle (http://www.bouncycastle.org)
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a copy of this
005 * software and associated documentation files (the "Software"), to deal in the Software
006 * without restriction, including without limitation the rights to use, copy, modify,
007 * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
008 * permit persons to whom the Software is furnished to do so, subject to the following
009 * conditions:
010 *
011 * The above copyright notice and this permission notice shall be included in all copies
012 * or substantial portions of the Software.
013 *
014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
015 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
016 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
017 * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
018 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
019 * DEALINGS IN THE SOFTWARE.
020 *
021 */
022
023 package org.apache.directory.shared.asn1.der;
024
025
026 /**
027 * DER Enumerated object.
028 */
029 public class DEREnumerated extends DERObject
030 {
031 /**
032 * Basic DERObject constructor.
033 */
034 public DEREnumerated(byte[] value)
035 {
036 super( ENUMERATED, value );
037 }
038
039
040 /**
041 * Static factory method, type-conversion operator.
042 */
043 public static DEREnumerated valueOf( int integer )
044 {
045 return new DEREnumerated( intToOctet( integer ) );
046 }
047
048
049 /**
050 * Lazy accessor
051 *
052 * @return integer value
053 */
054 public int intValue()
055 {
056 return octetToInt( value );
057 }
058
059
060 private static int octetToInt( byte[] bytes )
061 {
062 int result = 0;
063
064 for ( int ii = 0; ii < Math.min( 4, bytes.length ); ii++ )
065 {
066 result += bytes[ii] * ( 16 ^ ii );
067 }
068 return result;
069 }
070
071
072 private static byte[] intToOctet( int integer )
073 {
074 byte[] result = new byte[4];
075
076 for ( int ii = 0, shift = 24; ii < 4; ii++, shift -= 8 )
077 {
078 result[ii] = ( byte ) ( 0xFF & ( integer >> shift ) );
079 }
080 return result;
081 }
082 }