本文整理汇总了Java中com.google.protobuf.Descriptors.FieldDescriptor.Type.STRING属性的典型用法代码示例。如果您正苦于以下问题:Java Type.STRING属性的具体用法?Java Type.STRING怎么用?Java Type.STRING使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类com.google.protobuf.Descriptors.FieldDescriptor.Type
的用法示例。
在下文中一共展示了Type.STRING属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: if
/**
* This is a helper for decodeMessageValue. Decode a single field and return
* the value. Assume the field type is not Type.MESSAGE.
*/
private static Object
decodeFieldValue
(FieldDescriptor field, int tlvType, TlvDecoder decoder, int endOffset)
throws EncodingException
{
if (field.getType() == Type.UINT32)
return (int)decoder.readNonNegativeIntegerTlv(tlvType);
else if (field.getType() == Type.UINT64)
return decoder.readNonNegativeIntegerTlv(tlvType);
else if (field.getType() == Type.ENUM)
return field.getEnumType().findValueByNumber
((int)decoder.readNonNegativeIntegerTlv(tlvType));
else if (field.getType() == Type.BYTES)
return ByteString.copyFrom(decoder.readBlobTlv(tlvType));
else if (field.getType() == Type.STRING) {
try {
ByteBuffer byteBuffer = decoder.readBlobTlv(tlvType);
// Use Blob to get the byte array.
return new String(new Blob(byteBuffer, false).getImmutableArray(), "UTF-8");
} catch (UnsupportedEncodingException ex) {
// We don't expect this to happen.
throw new Error("UTF-8 decoder not supported: " + ex.getMessage());
}
}
else if (field.getType() == Type.BOOL)
return decoder.readBooleanTlv(tlvType, endOffset);
else
throw new Error("ProtobufTlv.decode: Unknown field type");
}
示例2: encodeMessageValue
private static void
encodeMessageValue(Message message, TlvEncoder encoder)
{
// Note: We can't use ListFields because it sorts by field number.
Descriptor descriptor = message.getDescriptorForType();
// Go in reverse so that we encode backwards.
List fields = descriptor.getFields();
for (int i = fields.size() - 1; i >= 0; --i) {
FieldDescriptor field = (FieldDescriptor)fields.get(i);
int tlvType = field.getNumber();
int valueCount = 0;
if (field.isRepeated())
valueCount = message.getRepeatedFieldCount(field);
else {
if (message.hasField(field))
valueCount = 1;
}
// Reverse so that we encode backwards.
for (int iValue = valueCount - 1; iValue >= 0; --iValue) {
Object value;
if (field.isRepeated())
value = message.getRepeatedField(field, iValue);
else
value = message.getField(field);
if (field.getType() == Type.MESSAGE) {
int saveLength = encoder.getLength();
// Encode backwards.
encodeMessageValue((Message)value, encoder);
encoder.writeTypeAndLength(tlvType, encoder.getLength() - saveLength);
}
else if (field.getType() == Type.UINT32)
encoder.writeNonNegativeIntegerTlv(tlvType, (Integer)value);
else if (field.getType() == Type.UINT64)
encoder.writeNonNegativeIntegerTlv(tlvType, (Long)value);
else if (field.getType() == Type.ENUM) {
int intValue = ((EnumValueDescriptor)value).getNumber();
if (intValue < 0)
throw new Error("ProtobufTlv.encode: ENUM value may not be negative");
encoder.writeNonNegativeIntegerTlv(tlvType, intValue);
}
else if (field.getType() == Type.BYTES)
encoder.writeBlobTlv(tlvType, ((ByteString)value).asReadOnlyByteBuffer());
else if (field.getType() == Type.STRING)
// Use Blob to UTF-8 encode and get a ByteBuffer.
encoder.writeBlobTlv(tlvType, new Blob((String)value).buf());
else if (field.getType() == Type.BOOL) {
if ((boolean)(Boolean)value)
encoder.writeTypeAndLength(tlvType, 0);
}
else
throw new Error("ProtobufTlv.encode: Unknown field type");
}
}
}