1// Protocol Buffers - Google's data interchange format 2// Copyright 2013 Google Inc. All rights reserved. 3// http://code.google.com/p/protobuf/ 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 31package com.google.protobuf.nano; 32 33import 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@google.com Kenton Varda 45 */ 46public 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 // Field numbers for feilds in MessageSet wire format. 76 static final int MESSAGE_SET_ITEM = 1; 77 static final int MESSAGE_SET_TYPE_ID = 2; 78 static final int MESSAGE_SET_MESSAGE = 3; 79 80 // Tag numbers. 81 static final int MESSAGE_SET_ITEM_TAG = 82 makeTag(MESSAGE_SET_ITEM, WIRETYPE_START_GROUP); 83 static final int MESSAGE_SET_ITEM_END_TAG = 84 makeTag(MESSAGE_SET_ITEM, WIRETYPE_END_GROUP); 85 static final int MESSAGE_SET_TYPE_ID_TAG = 86 makeTag(MESSAGE_SET_TYPE_ID, WIRETYPE_VARINT); 87 static final int MESSAGE_SET_MESSAGE_TAG = 88 makeTag(MESSAGE_SET_MESSAGE, WIRETYPE_LENGTH_DELIMITED); 89 90 public static final int EMPTY_INT_ARRAY[] = {}; 91 public static final long EMPTY_LONG_ARRAY[] = {}; 92 public static final float EMPTY_FLOAT_ARRAY[] = {}; 93 public static final double EMPTY_DOUBLE_ARRAY[] = {}; 94 public static final boolean EMPTY_BOOLEAN_ARRAY[] = {}; 95 public static final String EMPTY_STRING_ARRAY[] = {}; 96 public static final byte[] EMPTY_BYTES_ARRAY[] = {}; 97 public static final byte[] EMPTY_BYTES = {}; 98 99 /** 100 * Called by subclasses to parse an unknown field. 101 * @return {@code true} unless the tag is an end-group tag. 102 */ 103 public static boolean parseUnknownField( 104 final CodedInputByteBufferNano input, 105 final int tag) throws IOException { 106 return input.skipField(tag); 107 } 108 109 /** 110 * Computes the array length of a repeated field. We assume that in the common case repeated 111 * fields are contiguously serialized but we still correctly handle interspersed values of a 112 * repeated field (but with extra allocations). 113 * 114 * Rewinds to current input position before returning. 115 * 116 * @param input stream input, pointing to the byte after the first tag 117 * @param tag repeated field tag just read 118 * @return length of array 119 * @throws IOException 120 */ 121 public static final int getRepeatedFieldArrayLength( 122 final CodedInputByteBufferNano input, 123 final int tag) throws IOException { 124 int arrayLength = 1; 125 int startPos = input.getPosition(); 126 input.skipField(tag); 127 while (input.getBytesUntilLimit() > 0) { 128 int thisTag = input.readTag(); 129 if (thisTag != tag) { 130 break; 131 } 132 input.skipField(tag); 133 arrayLength++; 134 } 135 input.rewindToPosition(startPos); 136 return arrayLength; 137 } 138} 139