Java Code Examples for com.google.protobuf.CodedOutputStream#writeSInt64NoTag()
The following examples show how to use
com.google.protobuf.CodedOutputStream#writeSInt64NoTag() .
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: ProtoBufSerialization.java From eagle with Apache License 2.0 | 4 votes |
@Override public byte[] serialize(Object obj) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); CodedOutputStream output = CodedOutputStream.newInstance(baos); output.writeBoolNoTag(obj == null); if (obj == null) { output.flush(); return baos.toByteArray(); } Class<?> clazz = obj.getClass(); if (clazz == int.class || clazz == Integer.class) { output.writeSInt32NoTag((Integer) obj); } else if (clazz == long.class || clazz == Long.class) { output.writeSInt64NoTag((Long) obj); } else if (clazz == boolean.class || clazz == Boolean.class) { output.writeBoolNoTag((Boolean) obj); } else if (clazz == byte.class || clazz == Byte.class) { output.writeRawByte((Byte) obj); } else if (clazz == char.class || clazz == Character.class) { output.writeSInt32NoTag((Character) obj); } else if (clazz == short.class || clazz == Short.class) { output.writeSInt32NoTag((Short) obj); } else if (clazz == double.class || clazz == Double.class) { output.writeDoubleNoTag((Double) obj); } else if (clazz == float.class || clazz == Float.class) { output.writeFloatNoTag((Float) obj); } else if (clazz == String.class) { output.writeStringNoTag(obj.toString()); } else if (MessageLite.class.isAssignableFrom(clazz)) { output.writeMessageNoTag((MessageLite) obj); } else if (Throwable.class.isAssignableFrom(clazz)) { ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(obj); oos.flush(); } else { throw new IllegalArgumentException("can't serialization " + clazz); } output.flush(); return baos.toByteArray(); }
Example 2
Source File: CodedConstant.java From jprotobuf with Apache License 2.0 | 4 votes |
/** * Write a field of arbitrary type, without its tag, to the stream. * * @param output The output stream. * @param type The field's type. * @param value Object representing the field's value. Must be of the exact type which would be returned by * {@link Message#getField(Descriptors.FieldDescriptor)} for this field. * @throws IOException Signals that an I/O exception has occurred. */ public static void writeElementNoTag(final CodedOutputStream output, final WireFormat.FieldType type, final Object value) throws IOException { switch (type) { case DOUBLE: output.writeDoubleNoTag((Double) value); break; case FLOAT: output.writeFloatNoTag((Float) value); break; case INT64: output.writeInt64NoTag((Long) value); break; case UINT64: output.writeUInt64NoTag((Long) value); break; case INT32: output.writeInt32NoTag((Integer) value); break; case FIXED64: output.writeFixed64NoTag((Long) value); break; case FIXED32: output.writeFixed32NoTag((Integer) value); break; case BOOL: output.writeBoolNoTag((Boolean) value); break; case STRING: output.writeStringNoTag((String) value); break; // group not support yet // case GROUP : output.writeGroupNoTag ((MessageLite) value); break; case MESSAGE: writeObject(output, 0, FieldType.OBJECT, value, false, false); break; case BYTES: if (value instanceof ByteString) { output.writeBytesNoTag((ByteString) value); } else { byte[] v; if (value instanceof Byte[]) { v = toByteArray((Byte[]) value); } else { v = (byte[]) value; } output.writeByteArrayNoTag(v); } break; case UINT32: output.writeUInt32NoTag((Integer) value); break; case SFIXED32: output.writeSFixed32NoTag((Integer) value); break; case SFIXED64: output.writeSFixed64NoTag((Long) value); break; case SINT32: output.writeSInt32NoTag((Integer) value); break; case SINT64: output.writeSInt64NoTag((Long) value); break; case ENUM: if (value instanceof Internal.EnumLite) { output.writeEnumNoTag(((Internal.EnumLite) value).getNumber()); } else { if (value instanceof EnumReadable) { output.writeEnumNoTag(((EnumReadable) value).value()); } else if (value instanceof Enum) { output.writeEnumNoTag(((Enum) value).ordinal()); } else { output.writeEnumNoTag(((Integer) value).intValue()); } } break; } }