public byte[] toBytes(T object) { return object.toByteArray(); }
@Override public com.google.protobuf.Message.Builder newBuilder() { return mapEntryMessageDefaultInstance.newBuilderForType(); }
/** * Converts a list of MapEntry messages into a Map used for equals() and * hashCode(). */ @SuppressWarnings({"rawtypes", "unchecked"}) private static Map convertMapEntryListToMap(List list) { if (list.isEmpty()) { return Collections.emptyMap(); } Map result = new HashMap(); Iterator iterator = list.iterator(); Message entry = (Message) iterator.next(); Descriptors.Descriptor descriptor = entry.getDescriptorForType(); Descriptors.FieldDescriptor key = descriptor.findFieldByName("key"); Descriptors.FieldDescriptor value = descriptor.findFieldByName("value"); Object fieldValue = entry.getField(value); if (fieldValue instanceof EnumValueDescriptor) { fieldValue = ((EnumValueDescriptor) fieldValue).getNumber(); } result.put(entry.getField(key), fieldValue); while (iterator.hasNext()) { entry = (Message) iterator.next(); fieldValue = entry.getField(value); if (fieldValue instanceof EnumValueDescriptor) { fieldValue = ((EnumValueDescriptor) fieldValue).getNumber(); } result.put(entry.getField(key), fieldValue); } return result; }
private DataBuffer encodeMessage(Message message, DataBufferFactory bufferFactory, boolean streaming) { DataBuffer buffer = bufferFactory.allocateBuffer(); OutputStream outputStream = buffer.asOutputStream(); try { if (streaming) { message.writeDelimitedTo(outputStream); } else { message.writeTo(outputStream); } return buffer; } catch (IOException ex) { throw new IllegalStateException("Unexpected I/O error while writing to data buffer", ex); } }
public static void writeMessage(DataOutputStream output, Message message) throws IOException { /* * We don't use varints here because the c++ version of the protocol * buffer classes seem to be buggy requesting more data than necessary * from the underlying stream causing it to block forever */ output.writeInt(message.getSerializedSize()); CodedOutputStream codedOut = CodedOutputStream.newInstance(output); message.writeTo(codedOut); codedOut.flush(); }
private static Map<Object, Object> toProtoMap(@NullableDecl Object container) { if (container == null) { return Collections.emptyMap(); } List<?> entryMessages = (List<?>) container; Map<Object, Object> retVal = Maps.newHashMap(); for (Object entry : entryMessages) { Message message = (Message) entry; Object key = message.getAllFields().get(message.getDescriptorForType().findFieldByNumber(1)); Object value = message.getAllFields().get(message.getDescriptorForType().findFieldByNumber(2)); retVal.put(key, value); } return retVal; }
public BuilderType mergeFrom(final Message other) { if (other.getDescriptorForType() != getDescriptorForType()) { throw new IllegalArgumentException( "mergeFrom(Message) can only merge messages of the same type."); other.getAllFields().entrySet()) { final FieldDescriptor field = entry.getKey(); if (field.isRepeated()) { for (final Object element : (List)entry.getValue()) { addRepeatedField(field, element); } else if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { final Message existingValue = (Message)getField(field); if (existingValue == existingValue.getDefaultInstanceForType()) { setField(field, entry.getValue()); } else { setField(field, existingValue.newBuilderForType() .mergeFrom(existingValue) .mergeFrom((Message)entry.getValue()) .build()); mergeUnknownFields(other.getUnknownFields());
/** * Set the "X-Protobuf-*" HTTP headers when responding with a message of * content type "application/x-protobuf" * <p><b>Note:</b> <code>outputMessage.getBody()</code> should not have been called * before because it writes HTTP headers (making them read only).</p> */ private void setProtoHeader(HttpOutputMessage response, Message message) { response.getHeaders().set(X_PROTOBUF_SCHEMA_HEADER, message.getDescriptorForType().getFile().getName()); response.getHeaders().set(X_PROTOBUF_MESSAGE_HEADER, message.getDescriptorForType().getFullName()); }
public static <T extends com.google.protobuf.Message> Any pack( T message) { return Any.newBuilder() .setTypeUrl(getTypeUrl("type.googleapis.com", message.getDescriptorForType())) .setValue(message.toByteString()) .build(); }
public static Message getRequest(Service service, Descriptors.MethodDescriptor methodDesc, org.apache.hbase.thirdparty.com.google.protobuf.ByteString shadedRequest) throws IOException { Message.Builder builderForType = service.getRequestPrototype(methodDesc).newBuilderForType(); org.apache.hadoop.hbase.protobuf.ProtobufUtil.mergeFrom(builderForType, // TODO: COPY FROM SHADED TO NON_SHADED. DO I HAVE TOO? shadedRequest.toByteArray()); return builderForType.build(); }
static <R, S, P extends Message, Q extends Message, T extends Message> AggregateRequest validateArgAndGetPB(Scan scan, ColumnInterpreter<R, S, P, Q, T> ci, boolean canFamilyBeAbsent) throws IOException { validateParameters(scan, canFamilyBeAbsent); final AggregateRequest.Builder requestBuilder = AggregateRequest.newBuilder(); requestBuilder.setInterpreterClassName(ci.getClass().getCanonicalName()); P columnInterpreterSpecificData = ci.getRequestData(); if (columnInterpreterSpecificData != null) { requestBuilder.setInterpreterSpecificBytes(columnInterpreterSpecificData.toByteString()); } requestBuilder.setScan(ProtobufUtil.toScan(scan)); return requestBuilder.build(); }
throws IOException { final boolean isMessageSet = message.getDescriptorForType().getOptions().getMessageSetWireFormat(); if (alwaysWriteRequiredFields) { fields = new TreeMap<FieldDescriptor, Object>(fields); for (final FieldDescriptor field : message.getDescriptorForType().getFields()) { if (field.isRequired() && !fields.containsKey(field)) { fields.put(field, message.getField(field)); final Descriptors.FieldDescriptor field = entry.getKey(); final Object value = entry.getValue(); if (isMessageSet && field.isExtension() && field.getType() == Descriptors.FieldDescriptor.Type.MESSAGE && !field.isRepeated()) { output.writeMessageSetExtension(field.getNumber(), (Message) value); final UnknownFieldSet unknownFields = message.getUnknownFields(); if (isMessageSet) { unknownFields.writeAsMessageSetTo(output);
@Override public boolean equals(final Object other) { if (other == this) { return true; } if (!(other instanceof Message)) { return false; } final Message otherMessage = (Message) other; if (getDescriptorForType() != otherMessage.getDescriptorForType()) { return false; } return getAllFields().equals(otherMessage.getAllFields()) && getUnknownFields().equals(otherMessage.getUnknownFields()); }
@SuppressWarnings({ "rawtypes", "unchecked" }) public SBase convertProtocolBuffersMessageToSObject(Message message, SBase newInstance, SClass sClass) throws ConvertException { try { Descriptor descriptor = message.getDescriptorForType(); if (newInstance == null) { newInstance = sClass.newInstance(); for (FieldDescriptor fieldDescriptor : descriptor.getFields()) { if (fieldDescriptor.getName().equals("__actual_type")) { sClass = sClass.getServicesMap().getSType((String)message.getField(fieldDescriptor)); newInstance = sClass.newInstance(); } else if (fieldDescriptor.getName().startsWith("__")) { if (fieldDescriptor.getName().substring(2).equals(sClass.getSimpleName())) { subTypeMessage = (Message) message.getField(fieldDescriptor); Object val = message.getField(fieldDescriptor); SField field = newInstance.getSClass().getField(fieldDescriptor.getName()); if (field == null) { int size = message.getRepeatedFieldCount(fieldDescriptor); for (int index=0; index<size; index++) { Object repeatedField = message.getRepeatedField(fieldDescriptor, index); list.add(convertFieldValue(field, repeatedField));
Map<FieldDescriptor, Object> actualFields = actual.getAllFields(); Map<FieldDescriptor, Object> expectedFields = expected.getAllFields(); for (FieldDescriptor fieldDescriptor : Sets.union(actualFields.keySet(), expectedFields.keySet())) { if (shouldCompare == FieldScopeResult.EXCLUDED_RECURSIVELY) { builder.addSingularField( fieldDescriptor.getNumber(), SingularField.ignored(name(fieldDescriptor))); continue; if (fieldDescriptor.isRepeated()) { if (fieldDescriptor.isMapField()) { Map<Object, Object> actualMap = toProtoMap(actualFields.get(fieldDescriptor)); Map<Object, Object> expectedMap = toProtoMap(expectedFields.get(fieldDescriptor)); actualFields.get(fieldDescriptor), expectedFields.get(fieldDescriptor), actual.getDefaultInstanceForType().getField(fieldDescriptor), shouldCompare == FieldScopeResult.EXCLUDED_NONRECURSIVELY, fieldDescriptor, diffUnknowns(actual.getUnknownFields(), expected.getUnknownFields(), config); builder.setUnknownFields(diff);
public static Message getResponse( org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.CoprocessorServiceResponse result, com.google.protobuf.Message responsePrototype) throws IOException { Message response; if (result.getValue().hasValue()) { Message.Builder builder = responsePrototype.newBuilderForType(); builder.mergeFrom(result.getValue().getValue().newInput()); response = builder.build(); } else { response = responsePrototype.getDefaultInstanceForType(); } if (LOG.isTraceEnabled()) { LOG.trace("Master Result is value=" + response); } return response; }
@Override protected Object getField(Object record, String name, int pos, Object state) { Message m = (Message)record; FieldDescriptor f = ((FieldDescriptor[])state)[pos]; switch (f.getType()) { case MESSAGE: if (!f.isRepeated() && !m.hasField(f)) return null; default: return m.getField(f); } }
static int getSerializedSize( Message message, Map<FieldDescriptor, Object> fields) { int size = 0; final boolean isMessageSet = message.getDescriptorForType().getOptions().getMessageSetWireFormat(); for (final Map.Entry<Descriptors.FieldDescriptor, Object> entry : fields.entrySet()) { final Descriptors.FieldDescriptor field = entry.getKey(); final Object value = entry.getValue(); if (isMessageSet && field.isExtension() && field.getType() == Descriptors.FieldDescriptor.Type.MESSAGE && !field.isRepeated()) { size += CodedOutputStream.computeMessageSetExtensionSize( field.getNumber(), (Message) value); } else { size += FieldSet.computeFieldSize(field, value); } } final UnknownFieldSet unknownFields = message.getUnknownFields(); if (isMessageSet) { size += unknownFields.getSerializedSizeAsMessageSet(); } else { size += unknownFields.getSerializedSize(); } return size; }
/** * Writes a single message to {@code file}. Existing content is replaced, the message is not * appended. */ public static void write(Message message, File toFile) { OutputStream out = null; try { out = new BufferedOutputStream(new FileOutputStream(toFile, false)); message.writeTo(out); } catch (Exception e) { throw ContextException.of("Unable to write message", e).addContext("file", toFile); } finally { IOUtils.closeQuietly(out); } }
@Override void writeTo(ResponseBuffer out) throws IOException { int length = message.getSerializedSize(); length += CodedOutputStream.computeRawVarint32Size(length); out.ensureCapacity(length); message.writeDelimitedTo(out); }