本文整理汇总了Java中com.google.protobuf.MessageOrBuilder类的典型用法代码示例。如果您正苦于以下问题:Java MessageOrBuilder类的具体用法?Java MessageOrBuilder怎么用?Java MessageOrBuilder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MessageOrBuilder类属于com.google.protobuf包,在下文中一共展示了MessageOrBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: encode
import com.google.protobuf.MessageOrBuilder; //导入依赖的package包/类
@Override
protected void encode(ChannelHandlerContext ctx, MessageOrBuilder in, List<Object> out)
throws Exception {
Message msg = in instanceof Message ? (Message) in : ((Message.Builder) in).build();
int typeId = register.getTypeId(msg.getClass());
if (typeId == Integer.MIN_VALUE) {
throw new IllegalArgumentException("Unrecognisable message type, maybe not registered! ");
}
byte[] messageData = msg.toByteArray();
if (messageData.length <= 0) {
out.add(ByteBufAllocator.DEFAULT.heapBuffer().writeInt(typeId)
.writeInt(0));
return;
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
GZIPOutputStream def = new GZIPOutputStream(bos);
def.write(messageData);
def.flush();
def.close();
byte[] compressedData = bos.toByteArray();
out.add(ByteBufAllocator.DEFAULT.heapBuffer().writeInt(typeId).writeInt(compressedData.length)
.writeBytes(compressedData));
}
示例2: print
import com.google.protobuf.MessageOrBuilder; //导入依赖的package包/类
void print(MessageOrBuilder message) throws IOException {
if (typeConverters.containsKey(message.getDescriptorForType().getFullName()))
{
String msg = typeConverters.get(message.getDescriptorForType().getFullName()).convert(message);
generator.print(msg);
return;
}
WellKnownTypePrinter specialPrinter =
wellKnownTypePrinters.get(message.getDescriptorForType().getFullName());
if (specialPrinter != null) {
specialPrinter.print(this, message);
return;
}
print(message, null);
}
示例3: printValue
import com.google.protobuf.MessageOrBuilder; //导入依赖的package包/类
/** Prints google.protobuf.Value */
private void printValue(MessageOrBuilder message) throws IOException {
// For a Value message, only the value of the field is formatted.
Map<FieldDescriptor, Object> fields = message.getAllFields();
if (fields.isEmpty()) {
// No value set.
generator.print("null");
return;
}
// A Value message can only have at most one field set (it only contains
// an oneof).
if (fields.size() != 1) {
throw new InvalidProtocolBufferException("Invalid Value type.");
}
for (Map.Entry<FieldDescriptor, Object> entry : fields.entrySet()) {
printSingleFieldValue(entry.getKey(), entry.getValue());
}
}
示例4: decompileOptions
import com.google.protobuf.MessageOrBuilder; //导入依赖的package包/类
protected void decompileOptions(MessageOrBuilder options) throws IOException {
for (Map.Entry<FieldDescriptor,Object> entry : options.getAllFields().entrySet()) {
FieldDescriptor field = entry.getKey();
Object value = entry.getValue();
String fieldName = field.getName();
if (field.isExtension()) {
fieldName = "(" + fieldName + ")";
}
if (field.getType() == FieldDescriptor.Type.MESSAGE) {
for (Map.Entry<FieldDescriptor,Object> subentry : ((MessageOrBuilder)value).getAllFields().entrySet()) {
FieldDescriptor subfield = subentry.getKey();
Object subvalue = subentry.getValue();
indentedFormat("option %s.%s = %s;", fieldName, subfield.getName(), literal(subvalue, subfield.getType()));
}
}
else {
indentedFormat("option %s = %s;", fieldName, literal(value, field.getType()));
}
}
}
示例5: writeAllFields
import com.google.protobuf.MessageOrBuilder; //导入依赖的package包/类
private void writeAllFields(MessageOrBuilder pb) {
//returns changed fields with values. Map is ordered by id.
Map<Descriptors.FieldDescriptor, Object> changedPbFields = pb.getAllFields();
for (Map.Entry<Descriptors.FieldDescriptor, Object> entry : changedPbFields.entrySet()) {
Descriptors.FieldDescriptor fieldDescriptor = entry.getKey();
if(fieldDescriptor.isExtension()) {
// Field index of an extension field might overlap with a base field.
throw new UnsupportedOperationException(
"Cannot convert Protobuf message with extension field(s)");
}
int fieldIndex = fieldDescriptor.getIndex();
fieldWriters[fieldIndex].writeField(entry.getValue());
}
}
示例6: readMessages
import com.google.protobuf.MessageOrBuilder; //导入依赖的package包/类
/**
* Reads messages from given file. The file could/should be created by method writeMessages
*/
public static <T extends MessageOrBuilder> List<T> readMessages(Path file) throws IOException {
ProtoParquetReader<T> reader = new ProtoParquetReader<T>(file);
List<T> result = new ArrayList<T>();
boolean hasNext = true;
while (hasNext) {
T item = reader.read();
if (item == null) {
hasNext = false;
} else {
assertNotNull(item);
// It makes sense to return message but production code wont work with messages
result.add((T) asMessage(item).toBuilder());
}
}
reader.close();
return result;
}
示例7: toJSON
import com.google.protobuf.MessageOrBuilder; //导入依赖的package包/类
/**
* Write the message to file in JSON text format.
* <p>
* If this fails then an error message is written to the ImageJ log
*
* @param message
* the message
* @param file
* the file
* @param flags
* the flags
* @return True if written
*/
public static boolean toJSON(MessageOrBuilder message, File file, int flags)
{
PrintStream fs = null;
try
{
fs = new PrintStream(file);
return toJSON(message, fs, flags);
}
catch (FileNotFoundException e)
{
if (BitFlags.anyNotSet(flags, FLAG_SILENT))
IJ.log("Unable to write message: " + e.getMessage());
}
finally
{
if (fs != null)
{
fs.close();
}
}
return false;
}
示例8: getPropertyFieldDescriptor
import com.google.protobuf.MessageOrBuilder; //导入依赖的package包/类
protected FieldDescriptor getPropertyFieldDescriptor(final MessageOrBuilder mob,
final Object property) {
final Descriptor descriptor = mob.getDescriptorForType();
FieldDescriptor field = null;
if (property instanceof String) {
field = descriptor.findFieldByName(property.toString());
} else if (property instanceof FieldDescriptor) {
field = FieldDescriptor.class.cast(property);
if (field.getContainingType() != descriptor) {
field = null;
}
}
if (field == null) {
throw new PropertyNotFoundException();
}
return field;
}
示例9: replaceMessage
import com.google.protobuf.MessageOrBuilder; //导入依赖的package包/类
@Override
@Nullable
public <T extends Message> T replaceMessage(@Nullable final FieldDescriptor field,
final T message,
final List<? extends Entry<FieldDescriptor, ? extends MessageOrBuilder>> path) {
if (replacers.isEmpty()) {
return null;
}
for (final Replacer replacer : replacers) {
final T result = replacer.replaceMessage(field, message, path);
if (result != null) {
return result;
}
}
return null;
}
示例10: replaceValue
import com.google.protobuf.MessageOrBuilder; //导入依赖的package包/类
@Override
@Nullable
public Object replaceValue(final FieldDescriptor field, final Object value,
final List<? extends Entry<FieldDescriptor, ? extends MessageOrBuilder>> path) {
if (replacers.isEmpty()) {
return null;
}
for (final Replacer replacer : replacers) {
final Object result = replacer.replaceValue(field, value, path);
if (result != null) {
return result;
}
}
return null;
}
示例11: getMessage
import com.google.protobuf.MessageOrBuilder; //导入依赖的package包/类
@SuppressWarnings("null")
private Message getMessage(@Nullable final FieldDescriptor field, final Message message,
final Replacer replacer, final LinkedList<Entry<FieldDescriptor, MessageOrBuilder>> path) {
final Message result = replacer.replaceMessage(field, message, path);
if (result != null) {
return result;
}
path.addFirst(new SimpleImmutableEntry<FieldDescriptor, MessageOrBuilder>(field, message));
final Message.Builder builder = message.newBuilderForType();
for (final Entry<FieldDescriptor, Object> entry : message.getAllFields().entrySet()) {
addField(builder, entry.getKey(), entry.getValue(), replacer, path);
}
path.removeFirst();
return builder.buildPartial();
}
示例12: addField
import com.google.protobuf.MessageOrBuilder; //导入依赖的package包/类
@SuppressWarnings({"null", "unchecked"})
private void addField(final Message.Builder builder, final FieldDescriptor field,
final Object value, final Replacer replacer,
final LinkedList<Entry<FieldDescriptor, MessageOrBuilder>> path) {
if (field.getJavaType() == JavaType.MESSAGE) {
if (field.isRepeated()) {
for (final Message message : (List<Message>) value) {
builder.addRepeatedField(field, getMessage(field, message, replacer, path));
}
} else {
builder.setField(field, getMessage(field, (Message) value, replacer, path));
}
} else {
builder.setField(field, getValue(field, value, replacer, path));
}
}
示例13: inferRecordsClass
import com.google.protobuf.MessageOrBuilder; //导入依赖的package包/类
public static Class<? extends Message> inferRecordsClass(MessageOrBuilder[] records) {
Class<? extends Message> cls = null;
for (MessageOrBuilder record : records) {
Class<? extends Message> recordClass;
if (record instanceof Message.Builder) {
recordClass = ((Message.Builder) record).build().getClass();
} else if (record instanceof Message) {
recordClass = ((Message) record).getClass();
} else {
throw new RuntimeException("Illegal class " + record);
}
if (cls == null) {
cls = recordClass;
} else if (!cls.equals(recordClass)) {
throw new RuntimeException("Class mismatch :" + cls + " and " + recordClass);
}
}
return cls;
}
示例14: addAllRepeatedField
import com.google.protobuf.MessageOrBuilder; //导入依赖的package包/类
@Override
public Builder addAllRepeatedField(final Builder builder, final FieldDescriptor field,
final Iterable<?> values) {
final RepeatedFieldBuilder<Message, Builder, MessageOrBuilder> fieldBuilder =
builder.getRepeatedFieldBuilder(field, false);
if (fieldBuilder == null) {
super.addAllRepeatedField(builder, field, values);
} else {
@SuppressWarnings("unchecked")
final Iterable<? extends Message> verifiedValues =
(Iterable<? extends Message>) getVerifiedValue(field, values, null);
fieldBuilder.addAllMessages(verifiedValues);
}
return builder;
}
示例15: clearField
import com.google.protobuf.MessageOrBuilder; //导入依赖的package包/类
@Override
public Builder clearField(final Builder builder, final FieldDescriptor field) {
final SingleFieldBuilder<Message, Builder, MessageOrBuilder> fieldBuilder =
builder.getSingleFieldBuilder(field, false);
if (fieldBuilder == null) {
super.clearField(builder, field);
} else {
final OneofDescriptor oneofDescriptor = field.getContainingOneof();
if (oneofDescriptor != null) {
// TODO update the oneof if the original ProtoBuf does so!
builder.builders.dispose(field);
builder.builder.clearField(field); // or call super.clearField(builder, field);
} else {
fieldBuilder.clear();
}
}
return builder;
}