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 a Long value. 029 * 030 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 031 * @version $Rev: 664290 $, $Date: 2008-06-07 08:28:06 +0200 (Sat, 07 Jun 2008) $ 032 */ 033 public class LongDecoder 034 { 035 private static final long[] MASK = new long[] 036 { 037 0x00000000000000FFL, 038 0x000000000000FFFFL, 039 0x0000000000FFFFFFL, 040 0x00000000FFFFFFFFL, 041 0x000000FFFFFFFFFFL, 042 0x0000FFFFFFFFFFFFL, 043 0x00FFFFFFFFFFFFFFL, 044 0xFFFFFFFFFFFFFFFFL 045 }; 046 047 048 // ~ Methods 049 // ------------------------------------------------------------------------------------ 050 051 /** 052 * Parse a byte buffer and send back an long, controlling that this number 053 * is in a specified interval. 054 * 055 * @param value The byte buffer to parse 056 * @param min Lowest value allowed, included 057 * @param max Highest value allowed, included 058 * @return An integer 059 * @throws LongDecoderException 060 * Thrown if the byte stream does not contains an integer 061 */ 062 public static long parse( Value value, long min, long max ) throws LongDecoderException 063 { 064 065 long result = 0; 066 067 byte[] bytes = value.getData(); 068 069 if ( ( bytes == null ) || ( bytes.length == 0 ) ) 070 { 071 throw new LongDecoderException( I18n.err( I18n.ERR_00039 ) ); 072 } 073 074 if ( bytes.length > 8 ) 075 { 076 throw new LongDecoderException( I18n.err( I18n.ERR_00039 ) ); 077 } 078 079 for ( int i = 0; ( i < bytes.length ) && ( i < 9 ); i++ ) 080 { 081 result = ( result << 8 ) | ( bytes[i] & 0x00FF ); 082 } 083 084 if ( ( bytes[0] & 0x80 ) == 0x80 ) 085 { 086 result = -( ( ( ~result ) + 1 ) & MASK[bytes.length - 1] ); 087 } 088 089 if ( ( result >= min ) && ( result <= max ) ) 090 { 091 return result; 092 } 093 else 094 { 095 throw new LongDecoderException( I18n.err( I18n.ERR_00038, min, max ) ); 096 } 097 } 098 099 100 /** 101 * Parse a byte buffer and send back an integer 102 * 103 * @param value The byte buffer to parse 104 * @return An integer 105 * @throws IntegerDecoderException 106 * Thrown if the byte stream does not contains an integer 107 */ 108 public static long parse( Value value ) throws LongDecoderException 109 { 110 return parse( value, Long.MIN_VALUE, Long.MAX_VALUE ); 111 } 112 }