|
|
|
@ -46,12 +46,12 @@ public class ProtoBuf { |
|
|
|
|
private static final String MSG_UNSUPPORTED = "Unsupp.Type"; |
|
|
|
|
|
|
|
|
|
// names copied from //net/proto2/internal/wire_format.cc
|
|
|
|
|
private static final int WIRETYPE_END_GROUP = 4; |
|
|
|
|
private static final int WIRETYPE_FIXED32 = 5; |
|
|
|
|
private static final int WIRETYPE_FIXED64 = 1; |
|
|
|
|
private static final int WIRETYPE_LENGTH_DELIMITED = 2; |
|
|
|
|
private static final int WIRETYPE_START_GROUP = 3; |
|
|
|
|
private static final int WIRETYPE_VARINT = 0; |
|
|
|
|
static final int WIRETYPE_END_GROUP = 4; |
|
|
|
|
static final int WIRETYPE_FIXED32 = 5; |
|
|
|
|
static final int WIRETYPE_FIXED64 = 1; |
|
|
|
|
static final int WIRETYPE_LENGTH_DELIMITED = 2; |
|
|
|
|
static final int WIRETYPE_START_GROUP = 3; |
|
|
|
|
static final int WIRETYPE_VARINT = 0; |
|
|
|
|
|
|
|
|
|
/** Maximum number of bytes for VARINT wire format (64 bit, 7 bit/byte) */ |
|
|
|
|
private static final int VARINT_MAX_BYTES = 10; |
|
|
|
@ -62,7 +62,7 @@ public class ProtoBuf { |
|
|
|
|
new Long(10), new Long(11), new Long(12), new Long(13), new Long(14), |
|
|
|
|
new Long(15)}; |
|
|
|
|
|
|
|
|
|
private final ProtoBufType msgType; |
|
|
|
|
private ProtoBufType msgType; |
|
|
|
|
private final Vector values = new Vector(); |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -123,6 +123,20 @@ public class ProtoBuf { |
|
|
|
|
insertLong(tag, getCount(tag), value); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Appends the given (repeated) tag with the given float value. |
|
|
|
|
*/ |
|
|
|
|
public void addFloat(int tag, float value) { |
|
|
|
|
insertFloat(tag, getCount(tag), value); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Appends the given (repeated) tag with the given double value. |
|
|
|
|
*/ |
|
|
|
|
public void addDouble(int tag, double value) { |
|
|
|
|
insertDouble(tag, getCount(tag), value); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Appends the given (repeated) tag with the given group or message value. |
|
|
|
|
*/ |
|
|
|
@ -130,6 +144,28 @@ public class ProtoBuf { |
|
|
|
|
insertProtoBuf(tag, getCount(tag), value); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Adds a new protobuf for the specified tag, setting the child protobuf's |
|
|
|
|
* type correctly for the tag. |
|
|
|
|
* @param tag the tag for which to create a new protobuf |
|
|
|
|
* @return the newly created protobuf |
|
|
|
|
*/ |
|
|
|
|
public ProtoBuf addNewProtoBuf(int tag) { |
|
|
|
|
ProtoBuf child = newProtoBufForTag(tag); |
|
|
|
|
addProtoBuf(tag, child); |
|
|
|
|
return child; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Creates and returns a new protobuf for the specified tag, setting the new |
|
|
|
|
* protobuf's type correctly for the tag. |
|
|
|
|
* @param tag the tag for which to create a new protobuf |
|
|
|
|
* @return the newly created protobuf |
|
|
|
|
*/ |
|
|
|
|
public ProtoBuf newProtoBufForTag(int tag) { |
|
|
|
|
return new ProtoBuf((ProtoBufType) msgType.getData(tag)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Appends the given (repeated) tag with the given String value. |
|
|
|
|
*/ |
|
|
|
@ -196,6 +232,34 @@ public class ProtoBuf { |
|
|
|
|
return ((Long) getObject(tag, index, ProtoBufType.TYPE_INT64)).longValue(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns the float value for the given tag. |
|
|
|
|
*/ |
|
|
|
|
public float getFloat(int tag) { |
|
|
|
|
return Float.intBitsToFloat(getInt(tag)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns the float value for the given repeated tag at the given index. |
|
|
|
|
*/ |
|
|
|
|
public float getFloat(int tag, int index) { |
|
|
|
|
return Float.intBitsToFloat(getInt(tag, index)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns the double value for the given tag. |
|
|
|
|
*/ |
|
|
|
|
public double getDouble(int tag) { |
|
|
|
|
return Double.longBitsToDouble(getLong(tag)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns the double value for the given repeated tag at the given index. |
|
|
|
|
*/ |
|
|
|
|
public double getDouble(int tag, int index) { |
|
|
|
|
return Double.longBitsToDouble(getLong(tag, index)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns the group or nested message for the given tag. |
|
|
|
|
*/ |
|
|
|
@ -234,6 +298,20 @@ public class ProtoBuf { |
|
|
|
|
return msgType; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Sets the type definition of this protocol buffer. Used internally in |
|
|
|
|
* ProtoBufUtil for incremental reading. |
|
|
|
|
* |
|
|
|
|
* @param type the new type |
|
|
|
|
*/ |
|
|
|
|
void setType(ProtoBufType type) { |
|
|
|
|
if (values.size() != 0 || |
|
|
|
|
(msgType != null && type != null && type != msgType)) { |
|
|
|
|
throw new IllegalArgumentException(); |
|
|
|
|
} |
|
|
|
|
this.msgType = type; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Convenience method for determining whether a tag has a value. Note: in |
|
|
|
|
* contrast to getCount(tag) > 0, this method takes the default value |
|
|
|
@ -652,6 +730,20 @@ public class ProtoBuf { |
|
|
|
|
? SMALL_NUMBERS[(int) value] : new Long(value)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Sets the given tag to the given double value. |
|
|
|
|
*/ |
|
|
|
|
public void setDouble(int tag, double value) { |
|
|
|
|
setLong(tag, Double.doubleToLongBits(value)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Sets the given tag to the given float value. |
|
|
|
|
*/ |
|
|
|
|
public void setFloat(int tag, float value) { |
|
|
|
|
setInt(tag, Float.floatToIntBits(value)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Sets the given tag to the given Group or nested Message. |
|
|
|
|
*/ |
|
|
|
@ -659,6 +751,18 @@ public class ProtoBuf { |
|
|
|
|
setObject(tag, pb); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Sets a new protobuf for the specified tag, setting the child protobuf's |
|
|
|
|
* type correctly for the tag. |
|
|
|
|
* @param tag the tag for which to create a new protobuf |
|
|
|
|
* @return the newly created protobuf |
|
|
|
|
*/ |
|
|
|
|
public ProtoBuf setNewProtoBuf(int tag) { |
|
|
|
|
ProtoBuf child = newProtoBufForTag(tag); |
|
|
|
|
setProtoBuf(tag, child); |
|
|
|
|
return child; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Sets the given tag to the given String value. |
|
|
|
|
*/ |
|
|
|
@ -695,6 +799,20 @@ public class ProtoBuf { |
|
|
|
|
? SMALL_NUMBERS[(int) value] : new Long(value)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Inserts the given float value for the given tag at the given index. |
|
|
|
|
*/ |
|
|
|
|
public void insertFloat(int tag, int index, float value) { |
|
|
|
|
insertInt(tag, index, Float.floatToIntBits(value)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Inserts the given double value for the given tag at the given index. |
|
|
|
|
*/ |
|
|
|
|
public void insertDouble(int tag, int index, double value) { |
|
|
|
|
insertLong(tag, index, Double.doubleToLongBits(value)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Inserts the given group or message for the given tag at the given index. |
|
|
|
|
*/ |
|
|
|
@ -739,6 +857,8 @@ public class ProtoBuf { |
|
|
|
|
case ProtoBufType.TYPE_UINT64: |
|
|
|
|
case ProtoBufType.TYPE_SINT32: |
|
|
|
|
case ProtoBufType.TYPE_SINT64: |
|
|
|
|
case ProtoBufType.TYPE_FLOAT: |
|
|
|
|
case ProtoBufType.TYPE_DOUBLE: |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
} else if (object instanceof byte[]){ |
|
|
|
|