class TLV {

  public boolean parse(Tag tag, int offset) {
    if (tag == null) {
      throw new IllegalArgumentException("TLV parsing: tag can't be null.");
    }
    try {
      this.tag = new Tag(binary, offset);
    } catch (IndexOutOfBoundsException e) {
      throw new IllegalArgumentException("TLV parsing: index is too large.");
    }
    length = 0;
    if (tag.equals(this.tag)) {
      offset += this.tag.getTagSize();
      position += this.tag.getTagSize();
      if ((binary[offset] & (byte) 0x80) == (byte) 0x00) {
        /* short form: single octet length */
        length += binary[offset];
        position++;
      } else {
        /* long form: first octet (b6-b0)) gives the number of following length octets */
        int following = (binary[offset] & (byte) 0x7F);
        position++;
        while (following > 0) {
          offset++;
          position++;
          length += (binary[offset] & 0xFF);
          if (following > 1) {
            length <<= 8;
          }
          following--;
        }
      }
      return true;
    } else {
      return false;
    }
  }

  public  TLV(byte[] binary);

}