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

The following examples show how to use com.google.protobuf.CodedOutputStream#writeInt32() . 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: StateTest.java    From zetasketch with Apache License 2.0 7 votes vote down vote up
@Test
public void parseUnknownField() throws IOException {
  // Create an aggregator state proto with an unknown field in the middle.
  ByteArrayOutputStream stream = new ByteArrayOutputStream();
  CodedOutputStream coded = CodedOutputStream.newInstance(stream);
  coded.writeInt32(AggregatorStateProto.NUM_VALUES_FIELD_NUMBER, 42);
  coded.writeString(999, "foobar");
  coded.writeInt32(AggregatorStateProto.ENCODING_VERSION_FIELD_NUMBER, 43);
  coded.flush();

  // Check that we can parse the proto, despite the unknown field.
  State state = new State();
  state.parse(CodedInputStream.newInstance(stream.toByteArray()));

  // Check that the fields before and after the unknown fields were correctly read.
  assertEquals(42, state.numValues);
  assertEquals(43, state.encodingVersion);
}
 
Example 2
Source File: TrackManager.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void saveData(OutputStream outputStream, FileDataSource source, @Nullable ProgressListener progressListener) throws Exception {
    if (source.tracks.size() != 1)
        throw new Exception("Only single track can be saved in mtrack format");
    Track track = source.tracks.get(0);
    if (progressListener != null)
        progressListener.onProgressStarted(track.points.size());
    CodedOutputStream output = CodedOutputStream.newInstance(outputStream);
    output.writeUInt32(FIELD_VERSION, VERSION);
    int progress = 0;
    for (Track.TrackPoint point : track.points) {
        output.writeTag(FIELD_POINT, WireFormat.WIRETYPE_LENGTH_DELIMITED);
        output.writeRawVarint32(getSerializedPointSize(point));
        output.writeInt32(FIELD_POINT_LATITUDE, point.latitudeE6);
        output.writeInt32(FIELD_POINT_LONGITUDE, point.longitudeE6);
        output.writeFloat(FIELD_POINT_ALTITUDE, point.elevation);
        output.writeFloat(FIELD_POINT_SPEED, point.speed);
        output.writeFloat(FIELD_POINT_BEARING, point.bearing);
        output.writeFloat(FIELD_POINT_ACCURACY, point.accuracy);
        output.writeUInt64(FIELD_POINT_TIMESTAMP, point.time);
        if (!point.continuous)
            //noinspection ConstantConditions
            output.writeBool(8, point.continuous);
        progress++;
        if (progressListener != null)
            progressListener.onProgressChanged(progress);
    }
    output.writeBytes(FIELD_NAME, ByteString.copyFromUtf8(track.name));
    output.writeUInt32(FIELD_COLOR, track.style.color);
    output.writeFloat(FIELD_WIDTH, track.style.width);
    output.flush();
    outputStream.close();
    if (progressListener != null)
        progressListener.onProgressFinished();
}
 
Example 3
Source File: ReflectiveCodec.java    From jprotobuf with Apache License 2.0 4 votes vote down vote up
private void writeTo(FieldInfo fieldInfo, Object value, CodedOutputStream out) throws IOException {
	FieldType fieldType = fieldInfo.getFieldType();
	int order = fieldInfo.getOrder();

	if (value instanceof List) {
		// if check list
		CodedConstant.writeToList(out, order, fieldType, (List) value);
		return;
	}

	switch (fieldType) {
	case DOUBLE:
		out.writeDouble(order, (Double) value);
		break;
	case BYTES:
		ByteString bytes = ByteString.copyFrom((byte[]) value);
		out.writeBytes(order, bytes);
		break;
	case STRING:
		ByteString string = ByteString.copyFromUtf8(value.toString());
		out.writeBytes(order, string);
		break;
	case BOOL:
		out.writeBool(order, (Boolean) value);
		break;
	case FIXED32:
		out.writeFixed32(order, (Integer) value);
		break;
	case SFIXED32:
		out.writeSFixed32(order, (Integer) value);
		break;
	case SINT32:
		out.writeSInt32(order, (Integer) value);
		break;
	case INT32:
		out.writeInt32(order, (Integer) value);
		break;
	case UINT32:
		out.writeUInt32(order, (Integer) value);
		break;
	case FIXED64:
		out.writeFixed64(order, (Long) value);
		break;
	case SFIXED64:
		out.writeSFixed64(order, (Long) value);
		break;
	case SINT64:
		out.writeSInt64(order, (Long) value);
		break;
	case INT64:
		out.writeInt64(order, (Long) value);
		break;
	case UINT64:
		out.writeUInt64(order, (Long) value);
		break;
	case ENUM:
		int i;
		i = getEnumValue(value);
		out.writeEnum(order, i);
		break;
	case FLOAT:
		out.writeFloat(order, (Float) value);
		break;
	case OBJECT:
		Class c = value.getClass();
		ReflectiveCodec codec = new ReflectiveCodec(c);
		out.writeRawVarint32(CodedConstant.makeTag(order, WireFormat.WIRETYPE_LENGTH_DELIMITED));
		out.writeRawVarint32(codec.size(value));
		codec.writeTo(value, out);
		break;
	default:
		throw new IOException("Unknown field type on field '" + fieldInfo.getField().getName() + "'");
	}

}
 
Example 4
Source File: CodedConstant.java    From jprotobuf with Apache License 2.0 4 votes vote down vote up
/**
 * Write object to byte array by {@link FieldType}
 * 
 * @param out
 * @param order
 * @param type
 * @param o
 * @throws IOException
 */
public static void writeObject(CodedOutputStream out, int order, FieldType type, Object o, boolean list)
        throws IOException {
    if (o == null) {
        return;
    }

    if (type == FieldType.OBJECT) {

        Class cls = o.getClass();
        Codec target = ProtobufProxy.create(cls);

        out.writeRawVarint32(makeTag(order, WireFormat.WIRETYPE_LENGTH_DELIMITED));
        out.writeRawVarint32(target.size(o));

        target.writeTo(o, out);
        return;
    }

    if (type == FieldType.BOOL) {
        out.writeBool(order, (Boolean) o);
    } else if (type == FieldType.BYTES) {
        byte[] bb = (byte[]) o;
        out.writeBytes(order, ByteString.copyFrom(bb));
    } else if (type == FieldType.DOUBLE) {
        out.writeDouble(order, (Double) o);
    } else if (type == FieldType.FIXED32) {
        out.writeFixed32(order, (Integer) o);
    } else if (type == FieldType.FIXED64) {
        out.writeFixed64(order, (Long) o);
    } else if (type == FieldType.FLOAT) {
        out.writeFloat(order, (Float) o);
    } else if (type == FieldType.INT32) {
        out.writeInt32(order, (Integer) o);
    } else if (type == FieldType.INT64) {
        out.writeInt64(order, (Long) o);
    } else if (type == FieldType.SFIXED32) {
        out.writeSFixed32(order, (Integer) o);
    } else if (type == FieldType.SFIXED64) {
        out.writeSFixed64(order, (Long) o);
    } else if (type == FieldType.SINT32) {
        out.writeSInt32(order, (Integer) o);
    } else if (type == FieldType.SINT64) {
        out.writeSInt64(order, (Long) o);
    } else if (type == FieldType.STRING) {
        out.writeBytes(order, ByteString.copyFromUtf8(String.valueOf(o)));
    } else if (type == FieldType.UINT32) {
        out.writeUInt32(order, (Integer) o);
    } else if (type == FieldType.UINT64) {
        out.writeUInt64(order, (Long) o);
    } else if (type == FieldType.ENUM) {
        int value = 0;
        if (o instanceof EnumReadable) {
            value = ((EnumReadable) o).value();
        } else if (o instanceof Enum) {
            value = ((Enum) o).ordinal();
        }
        out.writeEnum(order, value);
    }
}