TLVElement { public final Long getDecodedLong() throws TLVParserException { byte[] data = getContent(); if (data.length > 1 && data[0] == 0) { throw new TLVParserException("Integer encoding cannot contain leading zeros"); } if (data.length > 8 || data.length == 8 && data[0] < 0) { throw new TLVParserException("Integers of at most 63 unsigned bits supported by this implementation"); } int ofs = 0; long t = 0; for (int i = 0; i < data.length; ++i) { t = (t << 8) | ((long) data[ofs + i] & 0xff); } return t; } TLVElement(boolean nonCritical, boolean forwarded, int type);  TLVElement(boolean inputTlv16, boolean nonCritical, boolean forwarded, int type); static TLVElement create(byte[] bytes); static TLVElement create(int type, long value); static TLVElement create(int type, Date value); static TLVElement create(int type, DataHash value); static TLVElement create(int type, String value); static TLVElement create(int type, byte[] value); static TLVElement create(int type, HashAlgorithm value); final Long getDecodedLong(); final String getDecodedString(); final DataHash getDecodedDataHash(); final Date getDecodedDate(); HashAlgorithm getDecodedHashAlgorithm(); byte[] getContent(); void setContent(byte[] content); void setStringContent(String s); void setLongContent(long value); void setDataHashContent(DataHash dataHash); void setDateContent(Date date); void setHashAlgorithmContent(HashAlgorithm hashAlgorithm); TLVElement getFirstChildElement(int tag); TLVElement getFirstChildElement(); TLVElement getLastChildElement(); List<TLVElement> getChildElements(int tag); List<TLVElement> getChildElements(); List<TLVElement> getChildElements(int... tags); int getType(); void setType(int type); boolean isOutputTlv16(); boolean isInputTlv16(); boolean isNonCritical(); boolean isForwarded(); byte[] encodeHeader(); int getContentLength(); int getHeaderLength(); void replace(TLVElement childToBeReplaced, TLVElement newChild); void remove(TLVElement elementToRemoved); void addChildElement(TLVElement element); void addFirstChildElement(TLVElement element); void writeTo(OutputStream out); byte[] getEncoded(); @Override String toString(); @Override boolean equals(Object o); @Override int hashCode(); static final int MAX_TLV16_CONTENT_LENGTH; }
@Test(expectedExceptions = TLVParserException.class, expectedExceptionsMessageRegExp = "Integer encoding cannot contain leading zeros") public void testDecodeIntegerWithLeadingZeroes_ThrowsTLVParserException() throws Exception { TLVElement element = load(new ByteArrayInputStream(new byte[]{0x2, 0x3, 0x0, 0x0, 0x0})); element.getDecodedLong(); }
@Test(expectedExceptions = TLVParserException.class, expectedExceptionsMessageRegExp = "Integers of at most 63 unsigned bits supported by this implementation") public void testDecodeElementContainingIntegerOverflow_ThrowsTLVParserException() throws Exception { TLVElement element = load(new ByteArrayInputStream(new byte[]{0x2, 0x8, (byte) 0x80, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff})); element.getDecodedLong(); }
@Test(expectedExceptions = TLVParserException.class, expectedExceptionsMessageRegExp = "Integers of at most 63 unsigned bits supported by this implementation") public void testDecodeElementContainingIntegerOverflow2_ThrowsTLVParserException() throws Exception { TLVElement element = load(new ByteArrayInputStream(new byte[]{0x2, 0x9, (byte) 0x80, (byte) 0x80, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff})); element.getDecodedLong(); }