Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2013-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 ElementReader lets you call onReceivedData multiple times which uses a BinaryXmlStructureDecoder or TlvStructureDecoder to detect the end of a binary XML or NDN-TLV element and calls elementListener.onReceivedElement(element) with the element. This handles the case where a single call to onReceivedData may contain multiple elements.
 
 public class ElementReader {
  
Create a new ElementReader with the elementListener.

Parameters:
elementListener The ElementListener used by onReceivedData.
 
   public
   ElementReader(ElementListener elementListener)
   {
      = elementListener;
   }

  
Continue to read data until the end of an element, then call elementListener.onReceivedElement(element ). The buffer passed to onReceivedElement is only valid during this call. If you need the data later, you must copy.

Parameters:
data The input data containing bytes of the element to read. This reads from position() to limit(), but does not change the position.
Throws:
EncodingException For invalid encoding.
 
   public void
   {
     // We may repeatedly set data to a slice as we read elements.
     data = data.slice();
 
     // Process multiple objects in the data.
     while(true) {
       boolean gotElementEnd;
       int offset;
 
       try {
         if (!) {
           // This is the beginning of an element. Check whether it is binaryXML or
           //   TLV.
           if (data.remaining() <= 0)
             // Wait for more data.
             return;
 
           // The type codes for TLV Interest and Data packets are chosen to not
           //   conflict with the first byte of a binary XML packet, so we can
           //   just look at the first byte.
           int firstByte = (int)data.get(0) & 0xff;
           if (firstByte == . || firstByte == . ||
               firstByte == . ||
               firstByte == 100)
              = true;
           else
             // Binary XML.
              = false;
         }
 
         if () {
           // Scan the input to check if a whole TLV object has been read.
           .seek(0);
           gotElementEnd = .findElementEnd(data);
           offset = .getOffset();
         }
         else {
           // Scan the input to check if a whole binary XML object has been read.
           .seek(0);
           gotElementEnd = .findElementEnd(data);
           offset = .getOffset();
         }
       } catch (EncodingException ex) {
        // Reset to read a new element on the next call.
         = false;
         = new TlvStructureDecoder();
        throw ex;
      }
      if (gotElementEnd) {
        // Got the remainder of an element.  Report to the caller.
        ByteBuffer element;
        if () {
          // We have partial data from a previous call, so append this data and point to partialData.
          .ensuredPut(data, 0, offset);
          element = .flippedBuffer();
          // Assume we don't need to use partialData anymore until needed.
           = false;
        }
        else {
          // We are not using partialData, so just point to the input data buffer.
          element = data.duplicate();
          element.limit(offset);
        }
        // Reset to read a new object. Do this before calling onReceivedElement
        // in case it throws an exception.
        data.position(offset);
        data = data.slice();
         = new TlvStructureDecoder();
        .onReceivedElement(element);
        if (data.remaining() <= 0)
          // No more data in the packet.
          return;
        // else loop back to decode.
      }
      else {
        // Save remaining data for a later call.
        if (!) {
           = true;
          .position(0);
        }
        if (.buffer().position() + data.remaining() >
            .) {
          // Reset to read a new element on the next call.
           = false;
           = new TlvStructureDecoder();
          throw new EncodingException
            ("The incoming packet exceeds the maximum limit Face.getMaxNdnPacketSize()");
        }
        .ensuredPut(data);
        return;
      }
    }
  }
  private boolean usePartialData_;
  private boolean useTlv_;
New to GrepCode? Check out our FAQ X