当前位置: 首页>>代码示例>>Java>>正文


Java Type.BYTES属性代码示例

本文整理汇总了Java中com.google.protobuf.Descriptors.FieldDescriptor.Type.BYTES属性的典型用法代码示例。如果您正苦于以下问题:Java Type.BYTES属性的具体用法?Java Type.BYTES怎么用?Java Type.BYTES使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在com.google.protobuf.Descriptors.FieldDescriptor.Type的用法示例。


在下文中一共展示了Type.BYTES属性的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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");
}
 
开发者ID:named-data,项目名称:ndn-dot-net,代码行数:33,代码来源:ProtobufTlv.java

示例2: buildAttributeValue

public AttributeValue buildAttributeValue(Object value) {
  Type type = field.getType();
  if (type == Type.BYTES) {
    ByteString b = (ByteString) value;
    AttributeValue attributeValue = new AttributeValue().withB(b.asReadOnlyByteBuffer());
    return attributeValue;
    // AttributeValue attributeValue = tableInfo.mapToAttributeValue(fieldDescriptor, fieldValue);
    // expected.put(attributeName, new ExpectedAttributeValue(toAttributeValue(where.getVersion())));
  } else {
    throw new UnsupportedOperationException("Unsupport field type: " + type);
  }
}
 
开发者ID:justinsb,项目名称:cloudata,代码行数:12,代码来源:DynamodbDataStore.java

示例3: 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");
    }
  }
}
 
开发者ID:named-data,项目名称:ndn-dot-net,代码行数:59,代码来源:ProtobufTlv.java


注:本文中的com.google.protobuf.Descriptors.FieldDescriptor.Type.BYTES属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。