Java Code Examples for com.google.protobuf.CodedOutputStream#computeUInt32Size()

The following examples show how to use com.google.protobuf.CodedOutputStream#computeUInt32Size() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TrackManager.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
public int getSerializedPropertiesSize(Track track) {
    int size = 0;
    size += CodedOutputStream.computeStringSize(FIELD_NAME, track.name);
    size += CodedOutputStream.computeUInt32Size(FIELD_COLOR, track.style.color);
    size += CodedOutputStream.computeFloatSize(FIELD_WIDTH, track.style.width);
    return size;
}
 
Example 2
Source File: ReflectiveCodec.java    From jprotobuf with Apache License 2.0 4 votes vote down vote up
private int computeSize(FieldInfo fieldInfo, Object value) throws IOException {
	FieldType fieldType = fieldInfo.getFieldType();

	int size = 0;
	if (value instanceof List) {
		// if list
		size = CodedConstant.computeListSize(fieldInfo.getOrder(), (List) value, fieldInfo.getFieldType(), true, null);
		return size;
	}

	int order = fieldInfo.getOrder();
	switch (fieldType) {
	case DOUBLE:
		size = CodedOutputStream.computeDoubleSize(order, (Double) value);
		break;
	case BYTES:
		ByteString bytes = ByteString.copyFrom((byte[]) value);
		size = CodedOutputStream.computeBytesSize(order, bytes);
		break;
	case STRING:
		ByteString string = ByteString.copyFromUtf8(value.toString());
		size = CodedOutputStream.computeBytesSize(order, string);
		break;
	case BOOL:
		size = CodedOutputStream.computeBoolSize(order, (Boolean) value);
		break;
	case FIXED32:
		size = CodedOutputStream.computeFixed32Size(order, (Integer) value);
		break;
	case SFIXED32:
		size = CodedOutputStream.computeSFixed32Size(order, (Integer) value);
		break;
	case SINT32:
		size = CodedOutputStream.computeSInt32Size(order, (Integer) value);
		break;
	case INT32:
		size = CodedOutputStream.computeInt32Size(order, (Integer) value);
		break;
	case UINT32:
		size = CodedOutputStream.computeUInt32Size(order, (Integer) value);
		break;
	case FIXED64:
		size = CodedOutputStream.computeFixed64Size(order, (Long) value);
		break;
	case SFIXED64:
		size = CodedOutputStream.computeSFixed64Size(order, (Long) value);
		break;
	case SINT64:
		size = CodedOutputStream.computeSInt64Size(order, (Long) value);
		break;
	case INT64:
		size = CodedOutputStream.computeInt64Size(order, (Long) value);
		break;
	case UINT64:
		size = CodedOutputStream.computeUInt64Size(order, (Long) value);
		break;
	case ENUM:
		int i;
		i = getEnumValue(value);
		size = CodedOutputStream.computeEnumSize(order, i);
		break;
	case FLOAT:
		size = CodedOutputStream.computeFloatSize(order, (Float) value);
		break;
	case OBJECT:
		Class c = value.getClass();
		ReflectiveCodec codec = new ReflectiveCodec(c);
		
		int objectSize = codec.size(value);
		
		size = size + CodedOutputStream.computeRawVarint32Size(objectSize);
		size = size + CodedOutputStream.computeTagSize(order);
		
		size += objectSize;
		break;
	default:
		throw new IOException("Unknown field type on field '" + fieldInfo.getField().getName() + "'");
	}

	return size;
}