Home | History | Annotate | Download | only in nano
      1 // Protocol Buffers - Google's data interchange format
      2 // Copyright 2013 Google Inc.  All rights reserved.
      3 // https://developers.google.com/protocol-buffers/
      4 //
      5 // Redistribution and use in source and binary forms, with or without
      6 // modification, are permitted provided that the following conditions are
      7 // met:
      8 //
      9 //     * Redistributions of source code must retain the above copyright
     10 // notice, this list of conditions and the following disclaimer.
     11 //     * Redistributions in binary form must reproduce the above
     12 // copyright notice, this list of conditions and the following disclaimer
     13 // in the documentation and/or other materials provided with the
     14 // distribution.
     15 //     * Neither the name of Google Inc. nor the names of its
     16 // contributors may be used to endorse or promote products derived from
     17 // this software without specific prior written permission.
     18 //
     19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30 
     31 package com.google.protobuf.nano;
     32 
     33 import java.io.IOException;
     34 
     35 /**
     36  * This class is used internally by the Protocol Buffer library and generated
     37  * message implementations.  It is public only because those generated messages
     38  * do not reside in the {@code protobuf} package.  Others should not use this
     39  * class directly.
     40  *
     41  * This class contains constants and helper functions useful for dealing with
     42  * the Protocol Buffer wire format.
     43  *
     44  * @author kenton (at) google.com Kenton Varda
     45  */
     46 public final class WireFormatNano {
     47   // Do not allow instantiation.
     48   private WireFormatNano() {}
     49 
     50   static final int WIRETYPE_VARINT           = 0;
     51   static final int WIRETYPE_FIXED64          = 1;
     52   static final int WIRETYPE_LENGTH_DELIMITED = 2;
     53   static final int WIRETYPE_START_GROUP      = 3;
     54   static final int WIRETYPE_END_GROUP        = 4;
     55   static final int WIRETYPE_FIXED32          = 5;
     56 
     57   static final int TAG_TYPE_BITS = 3;
     58   static final int TAG_TYPE_MASK = (1 << TAG_TYPE_BITS) - 1;
     59 
     60   /** Given a tag value, determines the wire type (the lower 3 bits). */
     61   static int getTagWireType(final int tag) {
     62     return tag & TAG_TYPE_MASK;
     63   }
     64 
     65   /** Given a tag value, determines the field number (the upper 29 bits). */
     66   public static int getTagFieldNumber(final int tag) {
     67     return tag >>> TAG_TYPE_BITS;
     68   }
     69 
     70   /** Makes a tag value given a field number and wire type. */
     71   static int makeTag(final int fieldNumber, final int wireType) {
     72     return (fieldNumber << TAG_TYPE_BITS) | wireType;
     73   }
     74 
     75   public static final int EMPTY_INT_ARRAY[] = {};
     76   public static final long EMPTY_LONG_ARRAY[] = {};
     77   public static final float EMPTY_FLOAT_ARRAY[] = {};
     78   public static final double EMPTY_DOUBLE_ARRAY[] = {};
     79   public static final boolean EMPTY_BOOLEAN_ARRAY[] = {};
     80   public static final String EMPTY_STRING_ARRAY[] = {};
     81   public static final byte[] EMPTY_BYTES_ARRAY[] = {};
     82   public static final byte[] EMPTY_BYTES = {};
     83 
     84   /**
     85    * Parses an unknown field. This implementation skips the field.
     86    *
     87    * <p>Generated messages will call this for unknown fields if the store_unknown_fields
     88    * option is off.
     89    *
     90    * @return {@literal true} unless the tag is an end-group tag.
     91    */
     92   public static boolean parseUnknownField(
     93       final CodedInputByteBufferNano input,
     94       final int tag) throws IOException {
     95     return input.skipField(tag);
     96   }
     97 
     98   /**
     99    * Computes the array length of a repeated field. We assume that in the common case repeated
    100    * fields are contiguously serialized but we still correctly handle interspersed values of a
    101    * repeated field (but with extra allocations).
    102    *
    103    * Rewinds to current input position before returning.
    104    *
    105    * @param input stream input, pointing to the byte after the first tag
    106    * @param tag repeated field tag just read
    107    * @return length of array
    108    * @throws IOException
    109    */
    110   public static final int getRepeatedFieldArrayLength(
    111       final CodedInputByteBufferNano input,
    112       final int tag) throws IOException {
    113     int arrayLength = 1;
    114     int startPos = input.getPosition();
    115     input.skipField(tag);
    116     while (input.readTag() == tag) {
    117       input.skipField(tag);
    118       arrayLength++;
    119     }
    120     input.rewindToPosition(startPos);
    121     return arrayLength;
    122   }
    123 
    124 }
    125