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.tlv;
 
 
 public class TlvStructureDecoder {
  
Continue scanning input starting from offset_ to find the element end. If the end of the element which started at offset 0 is found, this returns true and getOffset() is the length of the element. Otherwise, this returns false which means you should read more into input and call again.

Parameters:
input The input buffer. This reads starting from index 0 (not index.position()) to input.limit(). This does not update its position(). You have to pass in input each time because the buffer could be reallocated.
Returns:
true if found the element end, false if not.
 
   public final boolean
   {
     if ()
       // Someone is calling when we already got the end.
       return true;
 
     TlvDecoder decoder = new TlvDecoder(input);
 
     while (true) {
       if ( >= input.limit())
         // All the cases assume we have some input. Return and wait for more.
         return false;
 
       if ( == .) {
         int firstOctet = (int)input.get() & 0xff;
          += 1;
         if (firstOctet < 253)
           // The value is simple, so we can skip straight to reading the length.
            = .;
         else {
           // Set up to skip the type bytes.
           if (firstOctet == 253)
              = 2;
           else if (firstOctet == 254)
              = 4;
           else
             // value == 255.
              = 8;
 
            = .;
         }
       }
       else if ( == .) {
         int nRemainingBytes = input.limit() - ;
         if (nRemainingBytes < ) {
           // Need more.
            += nRemainingBytes;
            -= nRemainingBytes;
           return false;
         }
 
         // Got the type bytes. Move on to read the length.
          += ;
       }
       else if ( == .) {
         int firstOctet = (int)input.get() & 0xff;
          += 1;
         if (firstOctet < 253) {
           // The value is simple, so we can skip straight to reading
           //  the value bytes.
            = firstOctet;
           if ( == 0) {
             // No value bytes to read. We're finished.
              = true;
             return true;
           }
 
            = .;
         }
         else {
           // We need to read the bytes in the extended encoding of
          //  the length.
          if (firstOctet == 253)
             = 2;
          else if (firstOctet == 254)
             = 4;
          else
            // value == 255.
             = 8;
          // We need to use firstOctet in the next state.
           = firstOctet;
        }
      }
      else if ( == .) {
        int nRemainingBytes = input.limit() - ;
        if (! && nRemainingBytes >= ) {
          // We don't have to use the headerBuffer. Set nBytesToRead.
          decoder.seek();
           = decoder.readExtendedVarNumber();
          // Update offset_ to the decoder's offset after reading.
           = decoder.getOffset();
        }
        else {
           = true;
          int nNeededBytes =  - .position();
          if (nNeededBytes > nRemainingBytes) {
            // We can't get all of the header bytes from this input.
            // Save in headerBuffer.
            if (.position() + nRemainingBytes >
                .limit())
              // We don't expect this to happen.
              throw new Error
                ("Cannot store more header bytes than the size of headerBuffer");
            ByteBuffer remainingInput = input.duplicate();
            remainingInput.position();
            .put(remainingInput);
             += nRemainingBytes;
            return false;
          }
          // Copy the remaining bytes into headerBuffer, read the
          //   length and set nBytesToRead.
          if (.position() + nNeededBytes > .limit())
            // We don't expect this to happen.
            throw new Error
              ("Cannot store more header bytes than the size of headerBuffer");
          ByteBuffer remainingLengthBytes = input.duplicate();
          remainingLengthBytes.position();
          remainingLengthBytes.limit( + nNeededBytes);
          .put(remainingLengthBytes);
           += nNeededBytes;
          // Use a local decoder just for the headerBuffer.
          .flip();
          TlvDecoder bufferDecoder = new TlvDecoder();
          // Replace nBytesToRead with the length of the value.
           = bufferDecoder.readExtendedVarNumber();
        }
        if ( == 0) {
          // No value bytes to read. We're finished.
           = true;
          return true;
        }
        // Get ready to read the value bytes.
      }
      else if ( == .) {
        int nRemainingBytes = input.limit() - ;
        if (nRemainingBytes < ) {
          // Need more.
           += nRemainingBytes;
           -= nRemainingBytes;
          return false;
        }
        // Got the bytes. We're finished.
         += ;
         = true;
        return true;
      }
      else
        // We don't expect this to happen.
        throw new Error("findElementEnd: unrecognized state");
    }
  }

  
Get the current offset into the input buffer.

Returns:
The offset.
  public final int
  getOffset() { return ; }

  
Set the offset into the input, used for the next read.

Parameters:
offset The new offset.
  public final void
  seek(int offset) {  = offset; }
  private static int READ_TYPE =         0;
  private static int READ_TYPE_BYTES =   1;
  private static int READ_LENGTH =       2;
  private static int READ_LENGTH_BYTES = 3;
  private static int READ_VALUE_BYTES =  4;
  private boolean gotElementEnd_ = false;
  private int offset_ = 0;
  private int state_ = ;
  private boolean useHeaderBuffer_ = false;
  // 8 bytes is enough to hold the extended bytes in the length encoding
  // where it is an 8-byte number.
  private ByteBuffer headerBuffer_ = ByteBuffer.allocate(8);
  private int nBytesToRead_ = 0;
  private int firstOctet_;
New to GrepCode? Check out our FAQ X