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 import org.apache.directory.shared.i18n.I18n; 025 026 027 /** 028 * Parse and decode an Integer value. 029 * 030 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 031 * @version $Rev: 912399 $, $Date: 2010-02-21 21:52:31 +0100 (Sun, 21 Feb 2010) $ 032 */ 033 public class IntegerDecoder 034 { 035 private static final int[] MASK = new int[] 036 { 0x000000FF, 0x0000FFFF, 0x00FFFFFF, 0xFFFFFFFF }; 037 038 039 // ~ Methods 040 // ------------------------------------------------------------------------------------ 041 042 /** 043 * Parse a byte buffer and send back an integer, controling that this number 044 * is in a specified interval. 045 * 046 * @param value 047 * The byte buffer to parse 048 * @param min 049 * Lowest value allowed, included 050 * @param max 051 * Highest value allowed, included 052 * @return An integer 053 * @throws IntegerDecoderException 054 * Thrown if the byte stream does not contains an integer 055 */ 056 public static int parse( Value value, int min, int max ) throws IntegerDecoderException 057 { 058 059 int result = 0; 060 061 byte[] bytes = value.getData(); 062 063 if ( ( bytes == null ) || ( bytes.length == 0 ) ) 064 { 065 throw new IntegerDecoderException( I18n.err( I18n.ERR_00036 ) ); 066 } 067 068 if ( bytes.length > 4 ) 069 { 070 throw new IntegerDecoderException( I18n.err( I18n.ERR_00037 ) ); 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( I18n.err( I18n.ERR_00038, 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 }