/// <summary>
        /// Encode interest using NDN-TLV and return the encoding.
        /// </summary>
        ///
        /// <param name="interest">The Interest object to encode.</param>
        /// <param name="signedPortionBeginOffset">name component and ends just before the final name component (which is assumed to be a signature for a signed interest).</param>
        /// <param name="signedPortionEndOffset">name component and ends just before the final name component (which is assumed to be a signature for a signed interest).</param>
        /// <returns>A Blob containing the encoding.</returns>
        public override Blob encodeInterest(Interest interest,
				int[] signedPortionBeginOffset, int[] signedPortionEndOffset)
        {
            TlvEncoder encoder = new TlvEncoder();
            int saveLength = encoder.getLength();

            // Encode backwards.
            encoder.writeOptionalNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.SelectedDelegation,
                    interest.getSelectedDelegationIndex());
            try {
                Blob linkWireEncoding = interest.getLinkWireEncoding(this);
                if (!linkWireEncoding.isNull())
                    // Encode the entire link as is.
                    encoder.writeBuffer(linkWireEncoding.buf());
            } catch (EncodingException ex) {
                throw new Exception(ex.Message);
            }

            encoder.writeOptionalNonNegativeIntegerTlvFromDouble(
                    net.named_data.jndn.encoding.tlv.Tlv.InterestLifetime,
                    interest.getInterestLifetimeMilliseconds());

            // Encode the Nonce as 4 bytes.
            if (interest.getNonce().size() == 0) {
                // This is the most common case. Generate a nonce.
                ByteBuffer nonce = ILOG.J2CsMapping.NIO.ByteBuffer.allocate(4);
                random_.nextBytes(nonce.array());
                encoder.writeBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.Nonce, nonce);
            } else if (interest.getNonce().size() < 4) {
                ByteBuffer nonce_0 = ILOG.J2CsMapping.NIO.ByteBuffer.allocate(4);
                // Copy existing nonce bytes.
                nonce_0.put(interest.getNonce().buf());

                // Generate random bytes for remaining bytes in the nonce.
                for (int i = 0; i < 4 - interest.getNonce().size(); ++i)
                    nonce_0.put((byte) random_.Next());

                nonce_0.flip();
                encoder.writeBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.Nonce, nonce_0);
            } else if (interest.getNonce().size() == 4)
                // Use the nonce as-is.
                encoder.writeBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.Nonce, interest.getNonce().buf());
            else {
                // Truncate.
                ByteBuffer nonce_1 = interest.getNonce().buf();
                // buf() returns a new ByteBuffer, so we can change its limit.
                nonce_1.limit(nonce_1.position() + 4);
                encoder.writeBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.Nonce, nonce_1);
            }

            encodeSelectors(interest, encoder);
            int[] tempSignedPortionBeginOffset = new int[1];
            int[] tempSignedPortionEndOffset = new int[1];
            encodeName(interest.getName(), tempSignedPortionBeginOffset,
                    tempSignedPortionEndOffset, encoder);
            int signedPortionBeginOffsetFromBack = encoder.getLength()
                    - tempSignedPortionBeginOffset[0];
            int signedPortionEndOffsetFromBack = encoder.getLength()
                    - tempSignedPortionEndOffset[0];

            encoder.writeTypeAndLength(net.named_data.jndn.encoding.tlv.Tlv.Interest, encoder.getLength()
                    - saveLength);
            signedPortionBeginOffset[0] = encoder.getLength()
                    - signedPortionBeginOffsetFromBack;
            signedPortionEndOffset[0] = encoder.getLength()
                    - signedPortionEndOffsetFromBack;

            return new Blob(encoder.getOutput(), false);
        }
        private static void encodeControlParameters(
				ControlParameters controlParameters, TlvEncoder encoder)
        {
            int saveLength = encoder.getLength();

            // Encode backwards.
            encoder.writeOptionalNonNegativeIntegerTlvFromDouble(
                    net.named_data.jndn.encoding.tlv.Tlv.ControlParameters_ExpirationPeriod,
                    controlParameters.getExpirationPeriod());

            // Encode strategy
            if (controlParameters.getStrategy().size() != 0) {
                int strategySaveLength = encoder.getLength();
                encodeName(controlParameters.getStrategy(), new int[1], new int[1],
                        encoder);
                encoder.writeTypeAndLength(net.named_data.jndn.encoding.tlv.Tlv.ControlParameters_Strategy,
                        encoder.getLength() - strategySaveLength);
            }

            // Encode ForwardingFlags
            int flags = controlParameters.getForwardingFlags()
                    .getNfdForwardingFlags();
            if (flags != new ForwardingFlags().getNfdForwardingFlags())
                // The flags are not the default value.
                encoder.writeNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.ControlParameters_Flags,
                        flags);

            encoder.writeOptionalNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.ControlParameters_Cost,
                    controlParameters.getCost());
            encoder.writeOptionalNonNegativeIntegerTlv(
                    net.named_data.jndn.encoding.tlv.Tlv.ControlParameters_Origin, controlParameters.getOrigin());
            encoder.writeOptionalNonNegativeIntegerTlv(
                    net.named_data.jndn.encoding.tlv.Tlv.ControlParameters_LocalControlFeature,
                    controlParameters.getLocalControlFeature());

            // Encode URI
            if (controlParameters.getUri().Length != 0) {
                encoder.writeBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.ControlParameters_Uri, new Blob(
                        controlParameters.getUri()).buf());
            }

            encoder.writeOptionalNonNegativeIntegerTlv(
                    net.named_data.jndn.encoding.tlv.Tlv.ControlParameters_FaceId, controlParameters.getFaceId());

            // Encode name
            if (controlParameters.getName() != null) {
                encodeName(controlParameters.getName(), new int[1], new int[1],
                        encoder);
            }

            encoder.writeTypeAndLength(net.named_data.jndn.encoding.tlv.Tlv.ControlParameters_ControlParameters,
                    encoder.getLength() - saveLength);
        }
        private static void encodeMetaInfo(MetaInfo metaInfo, TlvEncoder encoder)
        {
            int saveLength = encoder.getLength();

            // Encode backwards.
            ByteBuffer finalBlockIdBuf = metaInfo.getFinalBlockId().getValue()
                    .buf();
            if (finalBlockIdBuf != null && finalBlockIdBuf.remaining() > 0) {
                // FinalBlockId has an inner NameComponent.
                int finalBlockIdSaveLength = encoder.getLength();
                encodeNameComponent(metaInfo.getFinalBlockId(), encoder);
                encoder.writeTypeAndLength(net.named_data.jndn.encoding.tlv.Tlv.FinalBlockId, encoder.getLength()
                        - finalBlockIdSaveLength);
            }

            encoder.writeOptionalNonNegativeIntegerTlvFromDouble(
                    net.named_data.jndn.encoding.tlv.Tlv.FreshnessPeriod, metaInfo.getFreshnessPeriod());
            if (!(metaInfo.getType() == net.named_data.jndn.ContentType.BLOB)) {
                // Not the default, so we need to encode the type.
                if (metaInfo.getType() == net.named_data.jndn.ContentType.LINK
                        || metaInfo.getType() == net.named_data.jndn.ContentType.KEY
                        || metaInfo.getType() == net.named_data.jndn.ContentType.NACK)
                    // The ContentType enum is set up with the correct integer for
                    // each NDN-TLV ContentType.
                    encoder.writeNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.ContentType, metaInfo
                            .getType().getNumericType());
                else if (metaInfo.getType() == net.named_data.jndn.ContentType.OTHER_CODE)
                    encoder.writeNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.ContentType,
                            metaInfo.getOtherTypeCode());
                else
                    // We don't expect this to happen.
                    throw new Exception("unrecognized TLV ContentType");
            }

            encoder.writeTypeAndLength(net.named_data.jndn.encoding.tlv.Tlv.MetaInfo, encoder.getLength()
                    - saveLength);
        }