Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2014-2015 Regents of the University of California.

Author(s):
Jeff Thompson <jefft0@remap.ucla.edu> This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. A copy of the GNU Lesser General Public License is in the file COPYING.
 
 
 package net.named_data.jndn.encoding;
 
A Tlv0_1_1WireFormat implements the WireFormat interface for encoding and decoding with the NDN-TLV wire format, version 0.1.1.
 
 public class Tlv0_1_1WireFormat extends WireFormat {
  
Encode name in NDN-TLV and return the encoding.

Parameters:
name The Name object to encode.
Returns:
A Blob containing the encoding.
 
   public Blob
   encodeName(Name name)
   {
     TlvEncoder encoder = new TlvEncoder();
     encodeName(namenew int[1], new int[1], encoder);
     return new Blob(encoder.getOutput(), false);
   }

  
Decode input as a name in NDN-TLV and set the fields of the interest object.

Parameters:
name The Name object whose fields are updated.
input The input buffer to decode. This reads from position() to limit(), but does not change the position.
Throws:
EncodingException For invalid encoding.
 
   public void
   decodeName(Name nameByteBuffer inputthrows EncodingException
   {
     TlvDecoder decoder = new TlvDecoder(input);
     decodeName(namenew int[1], new int[1], decoder);
   }

  
Encode interest using NDN-TLV and return the encoding.

Parameters:
interest The Interest object to encode.
signedPortionBeginOffset Return the offset in the encoding of the beginning of the signed portion. The signed portion starts from the first name component and ends just before the final name component (which is assumed to be a signature for a signed interest).
signedPortionEndOffset Return the offset in the encoding of the end of the signed portion. The signed portion starts from the first name component and ends just before the final name component (which is assumed to be a signature for a signed interest).
Returns:
A Blob containing the encoding.
 
   public Blob
     (Interest interestint[] signedPortionBeginOffsetint[] signedPortionEndOffset)
   {
     TlvEncoder encoder = new TlvEncoder();
     int saveLength = encoder.getLength();
 
     // Encode backwards.
     encoder.writeOptionalNonNegativeIntegerTlv(.interest.getScope());
    // Encode the Nonce as 4 bytes.
    if (interest.getNonce().size() == 0)
    {
      // This is the most common case. Generate a nonce.
      ByteBuffer nonce = ByteBuffer.allocate(4);
      .nextBytes(nonce.array());
      encoder.writeBlobTlv(.nonce);
    }
    else if (interest.getNonce().size() < 4) {
      ByteBuffer nonce = ByteBuffer.allocate(4);
      // Copy existing nonce bytes.
      nonce.put(interest.getNonce().buf());
      // Generate random bytes for remaining bytes in the nonce.
      for (int i = 0; i < 4 - interest.getNonce().size(); ++i)
        nonce.put((byte).nextInt());
      nonce.flip();
      encoder.writeBlobTlv(.nonce);
    }
    else if (interest.getNonce().size() == 4)
      // Use the nonce as-is.
      encoder.writeBlobTlv(.interest.getNonce().buf());
    else
    {
      // Truncate.
      ByteBuffer nonce = interest.getNonce().buf();
      // buf() returns a new ByteBuffer, so we can change its limit.
      nonce.limit(nonce.position() + 4);
      encoder.writeBlobTlv(.nonce);
    }
    encodeSelectors(interestencoder);
    int[] tempSignedPortionBeginOffset = new int[1];
    int[] tempSignedPortionEndOffset = new int[1];
      (interest.getName(), tempSignedPortionBeginOffset,
       tempSignedPortionEndOffsetencoder);
    int signedPortionBeginOffsetFromBack =
      encoder.getLength() - tempSignedPortionBeginOffset[0];
    int signedPortionEndOffsetFromBack =
      encoder.getLength() - tempSignedPortionEndOffset[0];
    encoder.writeTypeAndLength(.encoder.getLength() - saveLength);
    signedPortionBeginOffset[0] =
      encoder.getLength() - signedPortionBeginOffsetFromBack;
    signedPortionEndOffset[0] =
      encoder.getLength() - signedPortionEndOffsetFromBack;
    return new Blob(encoder.getOutput(), false);
  }

  
Decode input as an interest in NDN-TLV and set the fields of the interest object.

Parameters:
interest The Interest object whose fields are updated.
input The input buffer to decode. This reads from position() to limit(), but does not change the position.
signedPortionBeginOffset Return the offset in the encoding of the beginning of the signed portion. The signed portion starts from the first name component and ends just before the final name component (which is assumed to be a signature for a signed interest).
signedPortionEndOffset Return the offset in the encoding of the end of the signed portion. The signed portion starts from the first name component and ends just before the final name component (which is assumed to be a signature for a signed interest).
Throws:
EncodingException For invalid encoding.
  public void
    (Interest interestByteBuffer inputint[] signedPortionBeginOffset,
     int[] signedPortionEndOffsetthrows EncodingException
  {
    TlvDecoder decoder = new TlvDecoder(input);
    int endOffset = decoder.readNestedTlvsStart(.);
      (interest.getName(), signedPortionBeginOffset,signedPortionEndOffset,
       decoder);
    if (decoder.peekType(.endOffset))
      decodeSelectors(interestdecoder);
    // Require a Nonce, but don't force it to be 4 bytes.
    ByteBuffer nonce = decoder.readBlobTlv(.);
    interest.setScope((int)decoder.readOptionalNonNegativeIntegerTlv
      (.endOffset));
      (decoder.readOptionalNonNegativeIntegerTlv(.endOffset));
    // Set the nonce last because setting other interest fields clears it.
    interest.setNonce(new Blob(noncetrue));
    decoder.finishNestedTlvs(endOffset);
  }

  
Encode data in NDN-TLV and return the encoding.

Parameters:
data The Data object to encode.
signedPortionBeginOffset Return the offset in the encoding of the beginning of the signed portion by setting signedPortionBeginOffset[0]. If you are not encoding in order to sign, you can call encodeData(data) to ignore this returned value.
signedPortionEndOffset Return the offset in the encoding of the end of the signed portion by setting signedPortionEndOffset[0]. If you are not encoding in order to sign, you can call encodeData(data) to ignore this returned value.
Returns:
A Blob containing the encoding.
  public Blob
    (Data dataint[] signedPortionBeginOffsetint[] signedPortionEndOffset)
  {
    TlvEncoder encoder = new TlvEncoder(1500);
    int saveLength = encoder.getLength();
    // Encode backwards.
    encoder.writeBlobTlv
      (., (data.getSignature()).getSignature().buf());
    int signedPortionEndOffsetFromBack = encoder.getLength();
    encodeSignatureInfo(data.getSignature(), encoder);
    encoder.writeBlobTlv(.data.getContent().buf());
    encodeMetaInfo(data.getMetaInfo(), encoder);
    encodeName(data.getName(), new int[1], new int[1], encoder);
    int signedPortionBeginOffsetFromBack = encoder.getLength();
    encoder.writeTypeAndLength(.encoder.getLength() - saveLength);
    signedPortionBeginOffset[0] =
      encoder.getLength() - signedPortionBeginOffsetFromBack;
    signedPortionEndOffset[0] =
      encoder.getLength() - signedPortionEndOffsetFromBack;
    return new Blob(encoder.getOutput(), false);
  }

  
Decode input as a data packet in NDN-TLV and set the fields in the data object.

Parameters:
data The Data object whose fields are updated.
input The input buffer to decode. This reads from position() to limit(), but does not change the position.
signedPortionBeginOffset Return the offset in the input buffer of the beginning of the signed portion by setting signedPortionBeginOffset[0]. If you are not decoding in order to verify, you can call decodeData(data, input) to ignore this returned value.
signedPortionEndOffset Return the offset in the input buffer of the end of the signed portion by setting signedPortionEndOffset[0]. If you are not decoding in order to verify, you can call decodeData(data, input) to ignore this returned value.
Throws:
EncodingException For invalid encoding.
  public void
    (Data dataByteBuffer inputint[] signedPortionBeginOffset,
     int[] signedPortionEndOffsetthrows EncodingException
  {
    TlvDecoder decoder = new TlvDecoder(input);
    int endOffset = decoder.readNestedTlvsStart(.);
    signedPortionBeginOffset[0] = decoder.getOffset();
    decodeName(data.getName(), new int[1], new int[1], decoder);
    decodeMetaInfo(data.getMetaInfo(), decoder);
    data.setContent(new Blob(decoder.readBlobTlv(.), true));
    decodeSignatureInfo(datadecoder);
    signedPortionEndOffset[0] = decoder.getOffset();
      (new Blob(decoder.readBlobTlv(.), true));
    decoder.finishNestedTlvs(endOffset);
  }

  
Encode controlParameters in NDN-TLV and return the encoding.

Parameters:
controlParameters The ControlParameters object to encode.
Returns:
A Blob containing the encoding.
  public Blob
  {
    TlvEncoder encoder = new TlvEncoder(256);
    int saveLength = encoder.getLength();
    // Encode backwards.
       controlParameters.getExpirationPeriod());
    // Encode strategy
    if(controlParameters.getStrategy().size() != 0){
      int strategySaveLength = encoder.getLength();
      encodeName(controlParameters.getStrategy(), new int[1], new int[1],
        encoder);
        encoder.getLength() - strategySaveLength);
    }
    // Encode ForwardingFlags
    int flags = controlParameters.getForwardingFlags().getNfdForwardingFlags();
    if (flags != new ForwardingFlags().getNfdForwardingFlags())
        // The flags are not the default value.
      (.controlParameters.getCost());
      (.controlParameters.getOrigin());
       controlParameters.getLocalControlFeature());
    // Encode URI
    if(!controlParameters.getUri().isEmpty()){
        new Blob(controlParameters.getUri()).buf());
    }
      (.controlParameters.getFaceId());
    // Encode name
    if (controlParameters.getName() != null) {
      encodeName(controlParameters.getName(), new int[1], new int[1], encoder);
    }
    encoder.writeTypeAndLength
      (.encoder.getLength() - saveLength);
    return new Blob(encoder.getOutput(), false);
  }

  
Decode input as a control parameters in NDN-TLV and set the fields of the controlParameters object.

Parameters:
controlParameters The ControlParameters object whose fields are updated.
input The input buffer to decode. This reads from position() to limit(), but does not change the position.
Throws:
EncodingException For invalid encoding
  public void
    (ControlParameters controlParametersByteBuffer input)
    throws EncodingException
  {
    controlParameters.clear();
    TlvDecoder decoder = new TlvDecoder(input);
    int endOffset = decoder.
    // decode name
    if (decoder.peekType(.endOffset)) {
      Name name = new Name();
      decodeName(namenew int[1], new int[1], decoder);
      controlParameters.setName(name);
    }
    // decode face ID
    controlParameters.setFaceId((intdecoder.readOptionalNonNegativeIntegerTlv(.endOffset));
    // decode URI
    if (decoder.peekType(.endOffset)) {
      Blob uri = new Blob(decoder.readOptionalBlobTlv(.endOffset), true);
      controlParameters.setUri(uri.toString());
    }
    // decode integers
    controlParameters.setLocalControlFeature((intdecoder.
        .endOffset));
    controlParameters.setOrigin((intdecoder.
        endOffset));
    controlParameters.setCost((intdecoder.readOptionalNonNegativeIntegerTlv(
      .endOffset));
    // set forwarding flags
    if (decoder.peekType(.endOffset)) {
      ForwardingFlags flags = new ForwardingFlags();
      flags.setNfdForwardingFlags((intdecoder.
      controlParameters.setForwardingFlags(flags);
    }
    // decode strategy
    if (decoder.peekType(.endOffset)) {
      int strategyEndOffset = decoder.readNestedTlvsStart(.);
      decodeName(controlParameters.getStrategy(), new int[1], new int[1], decoder);
      decoder.finishNestedTlvs(strategyEndOffset);
    }
    // decode expiration period
    decoder.finishNestedTlvs(endOffset);
  }

  
Encode signature as a SignatureInfo in NDN-TLV and return the encoding.

Parameters:
signature An object of a subclass of Signature to encode.
Returns:
A Blob containing the encoding.
  public Blob
  {
    TlvEncoder encoder = new TlvEncoder(256);
    encodeSignatureInfo(signatureencoder);
    return new Blob(encoder.getOutput(), false);
  }
  private static class SimpleSignatureHolder implements SignatureHolder {
    public SignatureHolder setSignature(Signature signature)
    {
       = signature;
      return this;
    }
    public Signature getSignature()
    {
      return ;
    }
    private Signature signature_;
  }

  
Decode signatureInfo as an NDN-TLV signature info and signatureValue as the related NDN-TLV SignatureValue, and return a new object which is a subclass of Signature.

Parameters:
signatureInfo The signature info input buffer to decode. This reads from position() to limit(), but does not change the position.
signatureValue The signature value input buffer to decode. This reads from position() to limit(), but does not change the position.
Returns:
A new object which is a subclass of Signature.
Throws:
EncodingException For invalid encoding.
  public Signature
    (ByteBuffer signatureInfoByteBuffer signatureValuethrows EncodingException
  {
    // Use a SignatureHolder to imitate a Data object for _decodeSignatureInfo.
    SimpleSignatureHolder signatureHolder = new SimpleSignatureHolder();
    TlvDecoder decoder = new TlvDecoder(signatureInfo);
    decodeSignatureInfo(signatureHolderdecoder);
    decoder = new TlvDecoder(signatureValue);
    signatureHolder.getSignature().setSignature
      (new Blob(decoder.readBlobTlv(.), true));
    return signatureHolder.getSignature();
  }

  
Encode the signatureValue in the Signature object as a SignatureValue (the signature bits) in NDN-TLV and return the encoding.

Parameters:
signature An object of a subclass of Signature with the signature value to encode.
Returns:
A Blob containing the encoding.
  public Blob
  {
    TlvEncoder encoder = new TlvEncoder(256);
    encoder.writeBlobTlv(.signature.getSignature().buf());
    return new Blob(encoder.getOutput(), false);
  }

  
Encode the LocalControlHeader in NDN-TLV and return the encoding.

Parameters:
localControlHeader The LocalControlHeader object to encode.
Returns:
A Blob containing the encoding.
  public Blob
  {
    TlvEncoder encoder = new TlvEncoder(256);
    int saveLength = encoder.getLength();
    // Encode backwards.
    // Encode the entire payload as is.
    encoder.writeBuffer(localControlHeader.getPayloadWireEncoding().buf());
    // TODO: Encode CachingPolicy when we want to include it for an outgoing Data.
      .localControlHeader.getNextHopFaceId());
      .localControlHeader.getIncomingFaceId());
    encoder.writeTypeAndLength
      (.encoder.getLength() - saveLength);
    return new Blob(encoder.getOutput(), false);
  }

  
Decode input as a LocalControlHeader in NDN-TLV and set the fields of the localControlHeader object.

Parameters:
localControlHeader The LocalControlHeader object whose fields are updated.
input The input buffer to decode. This reads from position() to limit(), but does not change the position.
Throws:
EncodingException For invalid encoding
  public void
    (LocalControlHeader localControlHeaderByteBuffer input)
    throws EncodingException
  {
    TlvDecoder decoder = new TlvDecoder(input);
    int endOffset = decoder.
    localControlHeader.setIncomingFaceId(decoder.readOptionalNonNegativeIntegerTlv(
      .endOffset));
    localControlHeader.setNextHopFaceId(decoder.readOptionalNonNegativeIntegerTlv(
      .endOffset));
    // Ignore CachingPolicy. // TODO: Process CachingPolicy when we want the
    // client library to receive it for an incoming Data.
    if (decoder.peekType(.endOffset)) {
      int cachingPolicyEndOffset = decoder.readNestedTlvsStart
      decoder.finishNestedTlvs(cachingPolicyEndOffset);
    }
    // Set the payload to a slice of the remaining input.
    ByteBuffer payload = input.duplicate();
    payload.limit(endOffset);
    payload.position(decoder.getOffset());
    localControlHeader.setPayloadWireEncoding(new Blob(payloadfalse));
    // Don't call finishNestedTlvs since we got the payload and don't want to
    // decode any of it now.
  }

  
Get a singleton instance of a Tlv1_0a2WireFormat. To always use the preferred version NDN-TLV, you should use TlvWireFormat.get().

Returns:
The singleton instance.
  public static Tlv0_1_1WireFormat
  get()
  {
    return ;
  }

  
Encode the name to the encoder.

Parameters:
name The name to encode.
signedPortionBeginOffset Return the offset in the encoding of the beginning of the signed portion. The signed portion starts from the first name component and ends just before the final name component (which is assumed to be a signature for a signed interest).
signedPortionEndOffset Return the offset in the encoding of the end of the signed portion. The signed portion starts from the first name component and ends just before the final name component (which is assumed to be a signature for a signed interest).
encoder The TlvEncoder to receive the encoding.
  private static void
    (Name nameint[] signedPortionBeginOffsetint[] signedPortionEndOffset,
     TlvEncoder encoder)
  {
    int saveLength = encoder.getLength();
    // Encode the components backwards.
    int signedPortionEndOffsetFromBack = 0;
    for (int i = name.size() - 1; i >= 0; --i) {
      encoder.writeBlobTlv(.name.get(i).getValue().buf());
      if (i == name.size() - 1)
          signedPortionEndOffsetFromBack = encoder.getLength();
    }
    int signedPortionBeginOffsetFromBack = encoder.getLength();
    encoder.writeTypeAndLength(.encoder.getLength() - saveLength);
    signedPortionBeginOffset[0] =
      encoder.getLength() - signedPortionBeginOffsetFromBack;
    if (name.size() == 0)
        // There is no "final component", so set signedPortionEndOffset
        //   arbitrarily.
        signedPortionEndOffset[0] = signedPortionBeginOffset[0];
    else
        signedPortionEndOffset[0] =
          encoder.getLength() - signedPortionEndOffsetFromBack;
  }

  
Decode the name as NDN-TLV and set the fields in name.

Parameters:
name The name object whose fields are set.
signedPortionBeginOffset Return the offset in the encoding of the beginning of the signed portion. The signed portion starts from the first name component and ends just before the final name component (which is assumed to be a signature for a signed interest). If you are not decoding in order to verify, you can ignore this returned value.
signedPortionEndOffset Return the offset in the encoding of the end of the signed portion. The signed portion starts from the first name component and ends just before the final name component (which is assumed to be a signature for a signed interest). If you are not decoding in order to verify, you can ignore this returned value.
decoder The decoder with the input to decode.
Throws:
EncodingException
  private static void
    (Name nameint[] signedPortionBeginOffsetint[] signedPortionEndOffset,
     TlvDecoder decoderthrows EncodingException
  {
    name.clear();
    int endOffset = decoder.readNestedTlvsStart(.);
    signedPortionBeginOffset[0] = decoder.getOffset();
    // In case there are no components, set signedPortionEndOffset arbitrarily.
    signedPortionEndOffset[0] = signedPortionBeginOffset[0];
    while (decoder.getOffset() < endOffset) {
      signedPortionEndOffset[0] = decoder.getOffset();
      name.append(new Blob(decoder.readBlobTlv(.), true));
    }
    decoder.finishNestedTlvs(endOffset);
  }

  
Encode the interest selectors. If no selectors are written, do not output a Selectors TLV.
  private static void
  encodeSelectors(Interest interestTlvEncoder encoder)
  {
    int saveLength = encoder.getLength();
    // Encode backwards.
    if (interest.getMustBeFresh())
      encoder.writeTypeAndLength(., 0);
      .interest.getChildSelector());
    if (interest.getExclude().size() > 0)
      encodeExclude(interest.getExclude(), encoder);
    if (interest.getKeyLocator().getType() != .)
        (.interest.getKeyLocator(), encoder);
    else {
      // There is no keyLocator. If there is a publisherPublicKeyDigest, then
      //   encode as KEY_LOCATOR_DIGEST. (When we remove the deprecated
      //   publisherPublicKeyDigest, we don't need this.)
      if (interest.getPublisherPublicKeyDigest().getPublisherPublicKeyDigest().size() > 0) {
        int savePublisherPublicKeyDigestLength = encoder.getLength();
        encoder.writeBlobTlv
          (.,
           interest.getPublisherPublicKeyDigest().getPublisherPublicKeyDigest().buf());
        encoder.writeTypeAndLength
          (.encoder.getLength() - savePublisherPublicKeyDigestLength);
      }
    }
    // Only output the type and length if values were written.
    if (encoder.getLength() != saveLength)
      encoder.writeTypeAndLength(.encoder.getLength() - saveLength);
  }
  private static void
  decodeSelectors(Interest interestTlvDecoder decoderthrows EncodingException
  {
    int endOffset = decoder.readNestedTlvsStart(.);
      (.endOffset));
      (.endOffset));
    // Initially set publisherPublicKeyDigest to none.
    if (decoder.peekType(.endOffset)) {
        (.interest.getKeyLocator(), decoder);
      if (interest.getKeyLocator().getType() == .) {
        // For backwards compatibility, also set the publisherPublicKeyDigest.
          (interest.getKeyLocator().getKeyData());
      }
    }
    else
      interest.getKeyLocator().clear();
    if (decoder.peekType(.endOffset))
      decodeExclude(interest.getExclude(), decoder);
    else
      interest.getExclude().clear();
      (.endOffset));
    interest.setMustBeFresh(decoder.readBooleanTlv(.endOffset));
    decoder.finishNestedTlvs(endOffset);
  }
  private static void
  encodeExclude(Exclude excludeTlvEncoder encoder)
  {
    int saveLength = encoder.getLength();
    // TODO: Do we want to order the components (except for ANY)?
    // Encode the entries backwards.
    for (int i = exclude.size() - 1; i >= 0; --i) {
      Exclude.Entry entry = exclude.get(i);
      if (entry.getType() == ..)
        encoder.writeTypeAndLength(., 0);
      else
        encoder.writeBlobTlv
          (.entry.getComponent().getValue().buf());
    }
    encoder.writeTypeAndLength(.encoder.getLength() - saveLength);
  }
  private static void
  decodeExclude(Exclude excludeTlvDecoder decoderthrows EncodingException
  {
    int endOffset = decoder.readNestedTlvsStart(.);
    exclude.clear();
    while (true) {
      if (decoder.peekType(.endOffset))
        exclude.appendComponent(new Name.Component
          (new Blob(decoder.readBlobTlv(.), true)));
      else if (decoder.readBooleanTlv(.endOffset))
        exclude.appendAny();
      else
        // Else no more entries.
        break;
    }
    decoder.finishNestedTlvs(endOffset);
  }
  private static void
  encodeKeyLocator(int typeKeyLocator keyLocatorTlvEncoder encoder)
  {
    int saveLength = encoder.getLength();
    // Encode backwards.
    if (keyLocator.getType() != .) {
      if (keyLocator.getType() == .)
        encodeName(keyLocator.getKeyName(), new int[1], new int[1], encoder);
      else if (keyLocator.getType() == . &&
               keyLocator.getKeyData().size() > 0)
        encoder.writeBlobTlv(.keyLocator.getKeyData().buf());
      else
        throw new Error("Unrecognized KeyLocatorType " + keyLocator.getType());
    }
    encoder.writeTypeAndLength(typeencoder.getLength() - saveLength);
  }
  private static void
    (int expectedTypeKeyLocator keyLocatorTlvDecoder decoderthrows EncodingException
  {
    int endOffset = decoder.readNestedTlvsStart(expectedType);
    keyLocator.clear();
    if (decoder.getOffset() == endOffset)
      // The KeyLocator is omitted, so leave the fields as none.
      return;
    if (decoder.peekType(.endOffset)) {
      // KeyLocator is a Name.
      keyLocator.setType(.);
      decodeName(keyLocator.getKeyName(), new int[1], new int[1], decoder);
    }
    else if (decoder.peekType(.endOffset)) {
      // KeyLocator is a KeyLocatorDigest.
      keyLocator.setKeyData
        (new Blob(decoder.readBlobTlv(.), true));
    }
    else
      throw new EncodingException
        ("decodeKeyLocator: Unrecognized key locator type");
    decoder.finishNestedTlvs(endOffset);
  }

  
An internal method to encode signature as the appropriate form of SignatureInfo in NDN-TLV.

Parameters:
signature An object of a subclass of Signature to encode.
encoder The TlvEncoder to receive the encoding.
  private void
  encodeSignatureInfo(Signature signatureTlvEncoder encoder)
  {
    int saveLength = encoder.getLength();
    // Encode backwards.
    if (signature instanceof Sha256WithRsaSignature) {
        (., ((Sha256WithRsaSignature)signature).getKeyLocator(),
         encoder);
      encoder.writeNonNegativeIntegerTlv
    }
    else if (signature instanceof Sha256WithEcdsaSignature) {
        (., ((Sha256WithEcdsaSignature)signature).getKeyLocator(),
         encoder);
      encoder.writeNonNegativeIntegerTlv
    }
    else if (signature instanceof DigestSha256Signature)
      encoder.writeNonNegativeIntegerTlv
    else
      throw new Error("encodeSignatureInfo: Unrecognized Signature object type");
    encoder.writeTypeAndLength
      (.encoder.getLength() - saveLength);
  }
  private static void
    (SignatureHolder signatureHolderTlvDecoder decoderthrows EncodingException
  {
    int endOffset = decoder.readNestedTlvsStart(.);
    int signatureType = (int)decoder.readNonNegativeIntegerTlv(.);
    if (signatureType == .) {
        signatureHolder.setSignature(new Sha256WithRsaSignature());
        // Modify data's signature object because if we create an object
        //   and set it, then data will have to copy all the fields.
        Sha256WithRsaSignature signatureInfo =
          (Sha256WithRsaSignature)signatureHolder.getSignature();
        decodeKeyLocator(.signatureInfo.getKeyLocator(), decoder);
    }
    else if (signatureType == .) {
        signatureHolder.setSignature(new Sha256WithEcdsaSignature());
        Sha256WithEcdsaSignature signatureInfo =
          (Sha256WithEcdsaSignature)signatureHolder.getSignature();
        decodeKeyLocator(.signatureInfo.getKeyLocator(), decoder);
    }
    else if (signatureType == .)
        signatureHolder.setSignature(new DigestSha256Signature());
    else
        throw new EncodingException
         ("decodeSignatureInfo: unrecognized SignatureInfo type" + signatureType);
    decoder.finishNestedTlvs(endOffset);
  }
  private static void
  encodeMetaInfo(MetaInfo metaInfoTlvEncoder 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();
      encoder.writeBlobTlv(.finalBlockIdBuf);
      encoder.writeTypeAndLength
        (.encoder.getLength() - finalBlockIdSaveLength);
    }
      (.metaInfo.getFreshnessPeriod());
    if (!(metaInfo.getType() == . ||
          metaInfo.getType() == .)) {
      // Not the default, so we need to encode the type.
      if (metaInfo.getType() == . ||
          metaInfo.getType() == .)
        // The ContentType enum is set up with the correct integer for
        // each NDN-TLV ContentType.
        encoder.writeNonNegativeIntegerTlv
          (.metaInfo.getType().getNumericType());
      else
        throw new Error("unrecognized TLV ContentType");
    }
    encoder.writeTypeAndLength(.encoder.getLength() - saveLength);
  }
  private static void
  decodeMetaInfo(MetaInfo metaInfoTlvDecoder decoderthrows EncodingException
  {
    int endOffset = decoder.readNestedTlvsStart(.);
    // The ContentType enum is set up with the correct integer for each
    // NDN-TLV ContentType.  If readOptionalNonNegativeIntegerTlv returns
    // None, then setType will convert it to BLOB.
    int type = (int)decoder.readOptionalNonNegativeIntegerTlv
      (.endOffset);
    if (type == ..getNumericType())
      metaInfo.setType(.);
    if (type == ..getNumericType())
      metaInfo.setType(.);
    else
      // Default to BLOB.
      metaInfo.setType(.);
    metaInfo.setFreshnessPeriod
      (decoder.readOptionalNonNegativeIntegerTlv(.endOffset));
    if (decoder.peekType(.endOffset)) {
      int finalBlockIdEndOffset = decoder.readNestedTlvsStart(.);
      metaInfo.setFinalBlockId
        (new Name.Component
         (new Blob(decoder.readBlobTlv(.), true)));
      decoder.finishNestedTlvs(finalBlockIdEndOffset);
    }
    else
      metaInfo.setFinalBlockId(null);
    decoder.finishNestedTlvs(endOffset);
  }
  private static final SecureRandom random_ = new SecureRandom();
  private static Tlv0_1_1WireFormat instance_ = new Tlv0_1_1WireFormat();
New to GrepCode? Check out our FAQ X