Java Type Size sizeOf(int tagNumber, int contentLength)

Here you can find the source of sizeOf(int tagNumber, int contentLength)

Description

Computes the DER-encoded size of content with a specified tag number.

License

Open Source License

Parameter

Parameter Description
tagNumber the DER tag number in the identifier
contentLength the length of the content in bytes

Declaration

public static int sizeOf(int tagNumber, int contentLength) 

Method Source Code

//package com.java2s;
/**//from w  w w  . j av  a 2 s. c o m
 * Copyright 2007-2016, Kaazing Corporation. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Computes the DER-encoded size of content with a specified tag number.
     *
     * @param tagNumber
     *            the DER tag number in the identifier
     * @param contentLength
     *            the length of the content in bytes
     * @return
     */
    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;
    }
}

Related

  1. sizeOf(Class cls)
  2. sizeOf(Class clz)
  3. sizeOf(double v)
  4. sizeOf(int fieldNumber)
  5. sizeOf(int value)
  6. sizeOf(int[] arr)
  7. sizeOf(String str)
  8. sizeOf(String type)