public static int sizeOf ( int tagNumber , int contentLength ) { if ( tagNumber < 0 || contentLength < 0 ) { throw new IllegalArgumentException ( "Invalid tagNumber/contentLength: " + tagNumber + ", " + contentLength ) ; } int len = 0 ; // ID octets if ( tagNumber <= 30 ) { len ++ ; // single octet } else { len = len + 1 + ( int ) Math . ceil ( ( 1 + Integer . numberOfTrailingZeros ( Integer . highestOneBit ( tagNumber ) ) ) / 7.0d ) ; } // Length octets (TODO: indefinite form) if ( contentLength <= 0x7f ) { len ++ ; // definite form, single octet } else { len = len + 1 + ( int ) Math . ceil ( ( 1 + Integer . numberOfTrailingZeros ( Integer . highestOneBit ( contentLength ) ) ) / 8.0d ) ; } // Content octets len += contentLength ; return len ; }
Computes the DER - encoded size of content with a specified tag number .