code: public static int decodeLength(ByteBuffer buf) {
          if (buf == null || buf.remaining() == 0) {
              throw new IllegalArgumentException("Null or empty buffer");
          }
  
          int len = 0;
  
          byte first = buf.get();
          if (first >> 7 == 0) {
              // Bit 8 is zero: Short form
              len = first & 0x7f;
          } else {
              // Bit 8 is one: Long form
              // Bit 7-1 represents number of subsequent octets
              int numOctets = first & 0x7f;
              if (buf.remaining() < numOctets) {
                  throw new IllegalArgumentException("Insufficient data to decode long-form DER length");
              }
  
              // Combine subsequent octets to compute length
              for (int i = 0; i < numOctets; i++) {
                  len = (len << 8) + (0xff & buf.get());
              }
          }
  
          return len;
      }
docs: Decode octets and extract the length information from a DER-encoded message.
  
  @param buf
  the DER-encoded data buffer with position immediately after the DER-identifier octets
  @return the length of the DER-encoded contents