code: public byte[] encodeHeader() throws TLVParserException {
          DataOutputStream out = null;
          try {
              ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
              out = new DataOutputStream(byteArrayOutputStream);
  
              int dataLength = getContentLength();
  
              boolean tlv16 = isOutputTlv16();
              int firstByte = (tlv16 ? TLVInputStream.TLV16_FLAG : 0) + (isNonCritical() ? TLVInputStream.NON_CRITICAL_FLAG : 0)
                      + (isForwarded() ? TLVInputStream.FORWARD_FLAG : 0);
  
              if (tlv16) {
                  firstByte = firstByte | (getType() >>> TLVInputStream.BYTE_BITS) & TLVInputStream.TYPE_MASK;
                  out.writeByte(firstByte);
                  out.writeByte(getType());
                  if (dataLength < 1) {
                      out.writeShort(0);
                  } else {
                      out.writeShort(dataLength);
                  }
              } else {
                  firstByte = firstByte | getType() & TLVInputStream.TYPE_MASK;
                  out.writeByte(firstByte);
                  if (dataLength < 1) {
                      out.writeByte(0);
                  } else {
                      out.writeByte(dataLength);
                  }
              }
              return byteArrayOutputStream.toByteArray();
          } catch (IOException e) {
              throw new TLVParserException("TLV header encoding failed", e);
          } finally {
              Util.closeQuietly(out);
          }
      }
docs: Encodes TLV header.
  
  @return Byte array containing encoded TLV header.
  
  @throws TLVParserException
  when TLV header encoding fails or I/O error occurs.