001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.shared.asn1.util;
021
022
023 import org.apache.directory.shared.asn1.ber.tlv.Value;
024
025
026 /**
027 * Parse and decode an Integer value.
028 *
029 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
030 * @version $Rev: 664290 $, $Date: 2008-06-07 08:28:06 +0200 (Sam, 07 jui 2008) $
031 */
032 public class IntegerDecoder
033 {
034 private static final int[] MASK = new int[]
035 { 0x000000FF, 0x0000FFFF, 0x00FFFFFF, 0xFFFFFFFF };
036
037
038 // ~ Methods
039 // ------------------------------------------------------------------------------------
040
041 /**
042 * Parse a byte buffer and send back an integer, controling that this number
043 * is in a specified interval.
044 *
045 * @param value
046 * The byte buffer to parse
047 * @param min
048 * Lowest value allowed, included
049 * @param max
050 * Highest value allowed, included
051 * @return An integer
052 * @throws IntegerDecoderException
053 * Thrown if the byte stream does not contains an integer
054 */
055 public static int parse( Value value, int min, int max ) throws IntegerDecoderException
056 {
057
058 int result = 0;
059
060 byte[] bytes = value.getData();
061
062 if ( ( bytes == null ) || ( bytes.length == 0 ) )
063 {
064 throw new IntegerDecoderException( "The value is 0 byte long. This is not allowed for an integer" );
065 }
066
067 if ( bytes.length > 4 )
068 {
069 throw new IntegerDecoderException(
070 "The value is more than 4 bytes long. This is not allowed for an integer" );
071 }
072
073 for ( int i = 0; ( i < bytes.length ) && ( i < 5 ); i++ )
074 {
075 result = ( result << 8 ) | ( bytes[i] & 0x00FF );
076 }
077
078 if ( ( bytes[0] & 0x80 ) == 0x80 )
079 {
080 result = -( ( ( ~result ) + 1 ) & MASK[bytes.length - 1] );
081 }
082
083 if ( ( result >= min ) && ( result <= max ) )
084 {
085 return result;
086 }
087 else
088 {
089 throw new IntegerDecoderException( "The value is not in the range [" + min + ", " + max + "]" );
090 }
091 }
092
093
094 /**
095 * Parse a byte buffer and send back an integer
096 *
097 * @param value
098 * The byte buffer to parse
099 * @return An integer
100 * @throws IntegerDecoderException
101 * Thrown if the byte stream does not contains an integer
102 */
103 public static int parse( Value value ) throws IntegerDecoderException
104 {
105 return parse( value, Integer.MIN_VALUE, Integer.MAX_VALUE );
106 }
107 }