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;
 
 
 public class BinaryXmlStructureDecoder {
  
Continue scanning input starting from offset_. If found the end of the element which started at offset 0, then return true, else false. If this returns true, then the element end is at getOffset(). If this returns false, you should read more into input and call again. You have to pass in input each time because the array could be reallocated.

Parameters:
input The input buffer to read. This does not update its position().
Returns:
True if found the element end, false to read more input.
Throws:
EncodingException For invalid encoding.
 
   public boolean
   {
     if ()
       // Someone is calling when we already got the end.
       return true;
 
     BinaryXmlDecoder decoder = new BinaryXmlDecoder(input);
 
     while (true) {
       if ( >= input.limit())
         // All the cases assume we have some input.
         return false;
 
       if ( == ) {
         // First check for XML_CLOSE.
         if ( == 0 && input.get() == .) {
           ++;
           // Close the level.
           --;
           if ( == 0) {
             // Finished.
              = true;
             return true;
           }
           if ( < 0)
             throw new Error("BinaryXMLStructureDecoder: Unexpected close tag at offset " + ( - 1));
 
           // Get ready for the next header.
           startHeader();
           continue;
         }
 
         int startingHeaderLength = ;
         while (true) {
           if ( >= input.limit()) {
             // We can't get all of the header bytes from this input. Save in headerBuffer.
              = true;
             int nNewBytes =  - startingHeaderLength;
             .ensuredPut(input - nNewBytes);
 
             return false;
           }
           int headerByte = input.get(++);
           ++;
           if ((headerByte & .) != 0)
             // Break and read the header.
             break;
         }
 
         BinaryXmlDecoder.TypeAndValue typeAndVal;
         if () {
           // Copy the remaining bytes into headerBuffer.
           int nNewBytes =  - startingHeaderLength;
           .ensuredPut(input - nNewBytes);
 
           typeAndVal = new BinaryXmlDecoder(.flippedBuffer()).decodeTypeAndValue();
         }
         else {
           // We didn't have to use the headerBuffer.
           decoder.seek( - );
           typeAndVal = decoder.decodeTypeAndValue();
         }
 
        // Set the next state based on the type.
        int type = typeAndVal.getType();
        if (type == .)
          // We already consumed the item. READ_HEADER_OR_CLOSE again.
          // Binary XML has rules about what must follow an attribute, but we are just scanning.
          startHeader();
        else if (type == . || type == .) {
          // Start a new level and READ_HEADER_OR_CLOSE again.
          ++;
          startHeader();
        }
        else if (type == . || type == .) {
          if (type == .)
            // Start a new level and read the tag.
            ++;
          // Minimum tag or attribute length is 1.
           = typeAndVal.getValue() + 1;
           = ;
          // Binary XML has rules about what must follow an attribute, but we are just scanning.
        }
        else if (type == . || type == .) {
           = typeAndVal.getValue();
           = ;
        }
        else
          throw new EncodingException("BinaryXMLStructureDecoder: Unrecognized header type " + type);
      }
      else if ( == ) {
        int nRemainingBytes = input.limit() - ;
        if (nRemainingBytes < ) {
          // Need more.
           += nRemainingBytes;
           -= nRemainingBytes;
          return false;
        }
        // Got the bytes.  Read a new header or close.
         += ;
        startHeader();
      }
      else
        // We don't expect this to happen.
        throw new EncodingException("BinaryXMLStructureDecoder: Unrecognized state " + );
    }
  }

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

Parameters:
offset The new offset.
  public void
  seek(int offset)
  {
     = offset;
  }

  
When findElementEnd returns true, call this to get the element end.

Returns:
The position just past the element end.
  public int
  getOffset() { return ; }

  
Set the state to READ_HEADER_OR_CLOSE and set up to start reading the header.
  private void
  {
     = 0;
     = false;
  }
  private static final int READ_HEADER_OR_CLOSE = 0;
  private static final int READ_BYTES = 1;
  private boolean gotElementEnd_;
  private int offset_;
  private int level_;
  private int state_ = ;
  private int headerLength_;
  private boolean useHeaderBuffer_;
  // 10 bytes is enough to hold an encoded header with a type and a 64 bit value.
  private final DynamicByteBuffer headerBuffer_ = new DynamicByteBuffer(10);
  private int nBytesToRead_;
New to GrepCode? Check out our FAQ X