001    // Protocol Buffers - Google's data interchange format
002    // Copyright 2008 Google Inc.
003    // http://code.google.com/p/protobuf/
004    //
005    // Licensed under the Apache License, Version 2.0 (the "License");
006    // you may not use this file except in compliance with the License.
007    // You may obtain a copy of the License at
008    //
009    //      http://www.apache.org/licenses/LICENSE-2.0
010    //
011    // Unless required by applicable law or agreed to in writing, software
012    // distributed under the License is distributed on an "AS IS" BASIS,
013    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014    // See the License for the specific language governing permissions and
015    // limitations under the License.
016    
017    package org.fusesource.hawtbuf.proto;
018    
019    /**
020     * This class is used internally by the Protocol Buffer library and generated
021     * message implementations. It is public only because those generated messages
022     * do not reside in the {@code protocol2} package. Others should not use this
023     * class directly.
024     * 
025     * This class contains constants and helper functions useful for dealing with
026     * the Protocol Buffer wire format.
027     * 
028     * @author kenton@google.com Kenton Varda
029     */
030    public final class WireFormat {
031        // Do not allow instantiation.
032        private WireFormat() {
033        }
034    
035        public static final int WIRETYPE_VARINT = 0;
036        public static final int WIRETYPE_FIXED64 = 1;
037        public static final int WIRETYPE_LENGTH_DELIMITED = 2;
038        public static final int WIRETYPE_START_GROUP = 3;
039        public static final int WIRETYPE_END_GROUP = 4;
040        public static final int WIRETYPE_FIXED32 = 5;
041    
042        public static final int TAG_TYPE_BITS = 3;
043        public static final int TAG_TYPE_MASK = (1 << TAG_TYPE_BITS) - 1;
044    
045        /** Given a tag value, determines the wire type (the lower 3 bits). */
046        public static int getTagWireType(int tag) {
047            return tag & TAG_TYPE_MASK;
048        }
049    
050        /** Given a tag value, determines the field number (the upper 29 bits). */
051        public static int getTagFieldNumber(int tag) {
052            return tag >>> TAG_TYPE_BITS;
053        }
054    
055        /** Makes a tag value given a field number and wire type. */
056        public static int makeTag(int fieldNumber, int wireType) {
057            return (fieldNumber << TAG_TYPE_BITS) | wireType;
058        }
059    
060        // Field numbers for feilds in MessageSet wire format.
061        public static final int MESSAGE_SET_ITEM = 1;
062        public static final int MESSAGE_SET_TYPE_ID = 2;
063        public static final int MESSAGE_SET_MESSAGE = 3;
064    
065        // Tag numbers.
066        public static final int MESSAGE_SET_ITEM_TAG = makeTag(MESSAGE_SET_ITEM, WIRETYPE_START_GROUP);
067        public static final int MESSAGE_SET_ITEM_END_TAG = makeTag(MESSAGE_SET_ITEM, WIRETYPE_END_GROUP);
068        public static final int MESSAGE_SET_TYPE_ID_TAG = makeTag(MESSAGE_SET_TYPE_ID, WIRETYPE_VARINT);
069        public static final int MESSAGE_SET_MESSAGE_TAG = makeTag(MESSAGE_SET_MESSAGE, WIRETYPE_LENGTH_DELIMITED);
070    }