com.google.protobuf.UnknownFieldSet Java Examples

The following examples show how to use com.google.protobuf.UnknownFieldSet. 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: ProtoDomain.java    From metastore with Apache License 2.0 7 votes vote down vote up
public Collection<Descriptors.Descriptor> findDescriptorsByOption(String optionName) {
  Descriptors.FieldDescriptor fieldDescriptor = optionsCatalog.getMessageOptionByName(optionName);
  return descriptorMap.values().stream()
      .filter(
          descriptor -> {
            DescriptorProtos.MessageOptions options = descriptor.getOptions();
            UnknownFieldSet.Field unknown =
                options.getUnknownFields().getField(fieldDescriptor.getNumber());
            if (unknown.getLengthDelimitedList().size()
                    + unknown.getFixed64List().size()
                    + unknown.getFixed32List().size()
                    + unknown.getVarintList().size()
                > 0) {
              return true;
            }
            return options.getAllFields().containsKey(fieldDescriptor);
          })
      .collect(Collectors.toList());
}
 
Example #2
Source File: ProtoDomain.java    From metastore with Apache License 2.0 6 votes vote down vote up
public Collection<Descriptors.FileDescriptor> findFileDescriptorsByOption(String optionName) {
  Descriptors.FieldDescriptor fieldDescriptor = optionsCatalog.getFileOptionByName(optionName);
  return fileDescriptorMap.values().stream()
      .filter(
          descriptor -> {
            DescriptorProtos.FileOptions options = descriptor.getOptions();
            UnknownFieldSet.Field unknown =
                options.getUnknownFields().getField(fieldDescriptor.getNumber());
            if (unknown.getLengthDelimitedList().size()
                    + unknown.getFixed64List().size()
                    + unknown.getFixed32List().size()
                    + unknown.getVarintList().size()
                > 0) {
              return true;
            }
            return options.getAllFields().containsKey(fieldDescriptor);
          })
      .collect(Collectors.toList());
}
 
Example #3
Source File: JsonJacksonFormat.java    From jigsaw-payment with Apache License 2.0 6 votes vote down vote up
private void handleMissingField(String fieldName, JsonParser parser,
                                       ExtensionRegistry extensionRegistry,
                                       UnknownFieldSet.Builder builder) throws IOException {

    JsonToken token = parser.nextToken();
    if (token.equals(JsonToken.START_OBJECT)) {
        // Message structure
    	token = parser.nextToken(); // skip name
    	while (token != null && !token.equals(JsonToken.END_OBJECT)) {
            handleMissingField(fieldName, parser, extensionRegistry, builder);
            token = parser.nextToken(); // get } or field name
        }
    } else if (token.equals(JsonToken.START_ARRAY)) {
        // Collection
        do {
            handleMissingField(fieldName, parser, extensionRegistry, builder);
            token = parser.getCurrentToken(); // got value or ]
        } while (token != null && !token.equals(JsonToken.END_ARRAY));
    } else {
        // Primitive value
    	// NULL, INT, BOOL, STRING
    	// nothing to do..
    }
}
 
Example #4
Source File: FieldNumberTree.java    From curiostack with MIT License 6 votes vote down vote up
private static FieldNumberTree fromUnknownFieldSet(UnknownFieldSet unknownFieldSet) {
  FieldNumberTree tree = new FieldNumberTree();
  for (int fieldNumber : unknownFieldSet.asMap().keySet()) {
    UnknownFieldSet.Field unknownField = unknownFieldSet.asMap().get(fieldNumber);
    for (UnknownFieldDescriptor unknownFieldDescriptor :
        UnknownFieldDescriptor.descriptors(fieldNumber, unknownField)) {
      Key key = Key.unknown(unknownFieldDescriptor);
      FieldNumberTree childTree = new FieldNumberTree();
      tree.children.put(key, childTree);

      if (unknownFieldDescriptor.type() == UnknownFieldDescriptor.Type.GROUP) {
        for (Object group : unknownFieldDescriptor.type().getValues(unknownField)) {
          childTree.merge(fromUnknownFieldSet((UnknownFieldSet) group));
        }
      }
    }
  }

  return tree;
}
 
Example #5
Source File: ProtobufTypeUtil.java    From datacollector with Apache License 2.0 6 votes vote down vote up
private static void handleUnknownFields(
    Record record,
    String fieldPath,
    DynamicMessage.Builder builder
) throws IOException {
  String path = fieldPath.isEmpty() ? FORWARD_SLASH : fieldPath;
  String attribute = record.getHeader().getAttribute(ProtobufTypeUtil.PROTOBUF_UNKNOWN_FIELDS_PREFIX + path);
  if (attribute != null) {
    UnknownFieldSet.Builder unknownFieldBuilder = UnknownFieldSet.newBuilder();
    unknownFieldBuilder.mergeDelimitedFrom(
        new ByteArrayInputStream(
            org.apache.commons.codec.binary.Base64.decodeBase64(attribute.getBytes(StandardCharsets.UTF_8))
        )
    );
    UnknownFieldSet unknownFieldSet = unknownFieldBuilder.build();
    builder.setUnknownFields(unknownFieldSet);
  }
}
 
Example #6
Source File: ProtobufTestUtil.java    From datacollector with Apache License 2.0 6 votes vote down vote up
public static void checkRecordForUnknownFields(Record record, int i) throws IOException {
  // unknown fields are expected in paths for person and employee
  String attribute = record.getHeader().getAttribute(ProtobufTypeUtil.PROTOBUF_UNKNOWN_FIELDS_PREFIX + "/");
  UnknownFieldSet.Builder builder = UnknownFieldSet.newBuilder();
  builder.mergeDelimitedFrom(new ByteArrayInputStream(org.apache.commons.codec.binary.Base64.decodeBase64(attribute.getBytes())));
  UnknownFieldSet unknownFieldSet = builder.build();

  UnknownFieldsUtil.checkEmployeeUnknownFields(unknownFieldSet);

  if(i%2 == 0) {
    attribute = record.getHeader().getAttribute(ProtobufTypeUtil.PROTOBUF_UNKNOWN_FIELDS_PREFIX + "/engineer/person");
  } else {
    attribute = record.getHeader().getAttribute(ProtobufTypeUtil.PROTOBUF_UNKNOWN_FIELDS_PREFIX + "/exec/person");
  }
  builder = UnknownFieldSet.newBuilder();
  builder.mergeDelimitedFrom(new ByteArrayInputStream(org.apache.commons.codec.binary.Base64.decodeBase64(attribute.getBytes())));
  unknownFieldSet = builder.build();

  UnknownFieldsUtil.checkPersonUnknownFields(unknownFieldSet);
}
 
Example #7
Source File: ShadowApply.java    From metastore with Apache License 2.0 6 votes vote down vote up
private UnknownFieldSet buildUnknownFieldSet(List<OptionChangeInfo> optionChanges) {
  UnknownFieldSet.Builder unknownFieldSetBuilder = UnknownFieldSet.newBuilder();
  for (OptionChangeInfo optionChange : optionChanges) {
    switch (optionChange.getChangeType()) {
      case ADDITION:
        unknownFieldSetBuilder =
            mergeUnknownField(unknownFieldSetBuilder, optionChange.getPayloadNew());
        break;
      case REMOVAL:
        unknownFieldSetBuilder.clearField(optionChange.getOptionNumber());
        break;
      case PAYLOAD_CHANGED:
        unknownFieldSetBuilder.clearField(optionChange.getOptionNumber());
        unknownFieldSetBuilder =
            mergeUnknownField(unknownFieldSetBuilder, optionChange.getPayloadNew());
        break;
    }
  }
  return unknownFieldSetBuilder.build();
}
 
Example #8
Source File: ProtobufTestUtil.java    From datacollector with Apache License 2.0 6 votes vote down vote up
public static UnknownFieldSet getEmployeeUnknownFields() {
  // add unknown fields
  UnknownFieldSet.Field unknownStringField = UnknownFieldSet.Field.newBuilder()
    .addLengthDelimited(ByteString.copyFromUtf8("Hello San FRancisco!"))
    .build();
  UnknownFieldSet.Field unknownVarIntField = UnknownFieldSet.Field.newBuilder()
    .addVarint(123456789)
    .build();

  UnknownFieldSet employeeUnknownFields = UnknownFieldSet.newBuilder()
    .addField(345, unknownStringField)
    .addField(456, unknownVarIntField)
    .build();

  return employeeUnknownFields;
}
 
Example #9
Source File: ProtoLanguageFileWriter.java    From metastore with Apache License 2.0 6 votes vote down vote up
private Map<Descriptors.FieldDescriptor, Object> convertUnknownFieldValue(
    UnknownFieldSet unknownFieldSet, Map<Integer, Descriptors.FieldDescriptor> optionsMap) {
  Map<Descriptors.FieldDescriptor, Object> unknownFieldValues = new LinkedHashMap<>();
  unknownFieldSet
      .asMap()
      .forEach(
          (number, field) -> {
            Descriptors.FieldDescriptor fieldDescriptor = optionsMap.get(number);
            if (fieldDescriptor.isRepeated()) {
              unknownFieldValues.put(
                  fieldDescriptor, convertUnknownFieldList(fieldDescriptor, field));
            } else {
              unknownFieldValues.put(
                  fieldDescriptor, convertUnknownFieldValue(fieldDescriptor, field));
            }
          });
  return unknownFieldValues;
}
 
Example #10
Source File: ProtoDomain.java    From metastore with Apache License 2.0 6 votes vote down vote up
public Collection<Descriptors.ServiceDescriptor> findServiceDescriptorsByOption(
    String optionName) {
  Descriptors.FieldDescriptor fieldDescriptor = optionsCatalog.getServiceOptionByName(optionName);
  return serviceMap.values().stream()
      .filter(
          descriptor -> {
            DescriptorProtos.ServiceOptions options = descriptor.getOptions();
            UnknownFieldSet.Field unknown =
                options.getUnknownFields().getField(fieldDescriptor.getNumber());
            if (unknown.getLengthDelimitedList().size()
                    + unknown.getFixed64List().size()
                    + unknown.getFixed32List().size()
                    + unknown.getVarintList().size()
                > 0) {
              return true;
            }
            return options.getAllFields().containsKey(fieldDescriptor);
          })
      .collect(Collectors.toList());
}
 
Example #11
Source File: ProtoDomain.java    From metastore with Apache License 2.0 6 votes vote down vote up
public Collection<Descriptors.EnumDescriptor> findEnumDescriptorsByOption(String optionName) {
  Descriptors.FieldDescriptor fieldDescriptor = optionsCatalog.getEnumOptionByName(optionName);
  return enumMap.values().stream()
      .filter(
          descriptor -> {
            DescriptorProtos.EnumOptions options = descriptor.getOptions();
            UnknownFieldSet.Field unknown =
                options.getUnknownFields().getField(fieldDescriptor.getNumber());
            if (unknown.getLengthDelimitedList().size()
                    + unknown.getFixed64List().size()
                    + unknown.getFixed32List().size()
                    + unknown.getVarintList().size()
                > 0) {
              return true;
            }
            return options.getAllFields().containsKey(fieldDescriptor);
          })
      .collect(Collectors.toList());
}
 
Example #12
Source File: UnknownFieldDescriptor.java    From curiostack with MIT License 5 votes vote down vote up
static ImmutableList<UnknownFieldDescriptor> descriptors(
    int fieldNumber, UnknownFieldSet.Field field) {
  ImmutableList.Builder<UnknownFieldDescriptor> builder = ImmutableList.builder();
  for (Type type : Type.all()) {
    if (!type.getValues(field).isEmpty()) {
      builder.add(create(fieldNumber, type));
    }
  }
  return builder.build();
}
 
Example #13
Source File: DynamicMessageRecordSerializer.java    From fdb-record-layer with Apache License 2.0 5 votes vote down vote up
@Nonnull
protected DynamicMessage deserializeUnion(@Nonnull final Descriptors.Descriptor unionDescriptor,
                                          @Nonnull final Tuple primaryKey,
                                          @Nonnull final byte[] serialized,
                                          int metaDataVersion) {
    final DynamicMessage unionMessage = deserializeFromBytes(unionDescriptor, serialized);
    final Map<Descriptors.FieldDescriptor, Object> allFields = unionMessage.getAllFields();
    final Map<Integer, UnknownFieldSet.Field> unknownFields = unionMessage.getUnknownFields().asMap();
    if (!(allFields.size() == 1 && unknownFields.isEmpty())) {
        final String detailedMessage;
        if (!unknownFields.isEmpty()) {
            detailedMessage = " because there are unknown fields";
        } else if (allFields.size() > 1) {
            detailedMessage = " because there are extra known fields";
        } else {
            detailedMessage = " because there are no fields";
        }
        final String message = "Could not deserialize union message" + detailedMessage;
        final RecordSerializationException ex = new RecordSerializationException(message)
                .addLogInfo("unknownFields", unknownFields.keySet())
                .addLogInfo("fields", getFieldNames(allFields.keySet()))
                .addLogInfo("primaryKey", primaryKey)
                .addLogInfo("metaDataVersion", metaDataVersion);
        throw ex;
    }
    return unionMessage;
}
 
Example #14
Source File: CouchDBFormat.java    From jigsaw-payment with Apache License 2.0 5 votes vote down vote up
/**
 * Outputs a textual representation of {@code fields} to {@code output}.
 */
public void print(final UnknownFieldSet fields, Appendable output) throws IOException {
    CouchDBGenerator generator = new CouchDBGenerator(output);
    generator.print("{");
    printUnknownFields(fields, generator);
    generator.print("}");
}
 
Example #15
Source File: AbstractCharBasedFormatter.java    From incubator-tajo with Apache License 2.0 5 votes vote down vote up
@Override
public void print(UnknownFieldSet fields, OutputStream output, Charset cs)
		throws IOException {
	OutputStreamWriter writer = new OutputStreamWriter(output, cs);
	print(fields, writer);
	writer.flush();
}
 
Example #16
Source File: ProtobufFormatter.java    From jigsaw-payment with Apache License 2.0 5 votes vote down vote up
/**
    * Like {@code print()}, but writes directly to a {@code String} and returns it.
    */
public String printToString(final UnknownFieldSet fields) {
	try {
           ByteArrayOutputStream out = new ByteArrayOutputStream();
           print(fields, out, defaultCharset);
           out.flush();
           return out.toString();
       } catch (IOException e) {
           throw new RuntimeException("Writing to a StringBuilder threw an IOException (should never happen).",
                                      e);
       }
}
 
Example #17
Source File: XmlFormat.java    From jigsaw-payment with Apache License 2.0 5 votes vote down vote up
/**
 * Outputs a textual representation of {@code fields} to {@code output}.
 */
public void print(final UnknownFieldSet fields, Appendable output) throws IOException {
    XmlGenerator generator = new XmlGenerator(output);
    generator.print("<message>");
    printUnknownFields(fields, generator);
    generator.print("</message>");
}
 
Example #18
Source File: JsonJacksonFormat.java    From jigsaw-payment with Apache License 2.0 5 votes vote down vote up
/**
 * Outputs a Smile representation of {@code fields} to {@code output}.
 */
public void print(final UnknownFieldSet fields, OutputStream output, Charset cs) throws IOException {
	JsonGenerator generator = createGenerator(output);
	generator.writeStartObject();
	printUnknownFields(fields, generator);
    generator.writeEndObject();
    generator.close();
}
 
Example #19
Source File: ProtobufFormatter.java    From tajo with Apache License 2.0 5 votes vote down vote up
/**
    * Like {@code print()}, but writes directly to a {@code String} and returns it.
    */
public String printToString(final UnknownFieldSet fields) {
	try {
           ByteArrayOutputStream out = new ByteArrayOutputStream();
           print(fields, out, defaultCharset);
           out.flush();
           return out.toString();
       } catch (IOException e) {
           throw new RuntimeException("Writing to a StringBuilder threw an IOException (should never happen).",
                                      e);
       }
}
 
Example #20
Source File: HtmlFormat.java    From jigsaw-payment with Apache License 2.0 5 votes vote down vote up
public void print(final UnknownFieldSet fields, Appendable output) throws IOException {
	HtmlGenerator generator = new HtmlGenerator(output);
       generator.print("<html>");
       generator.print(META_CONTENT);
       generator.print("</head><body>");
       printUnknownFields(fields, generator);
       generator.print("</body></html>");
}
 
Example #21
Source File: JsonFormat.java    From jigsaw-payment with Apache License 2.0 5 votes vote down vote up
/**
 * Outputs a textual representation of {@code fields} to {@code output}.
 */
public void print(final UnknownFieldSet fields, Appendable output) throws IOException {
    JsonGenerator generator = new JsonGenerator(output);
    generator.print("{");
    printUnknownFields(fields, generator);
    generator.print("}");
}
 
Example #22
Source File: JsonFormat.java    From compiler with Apache License 2.0 5 votes vote down vote up
/**
   * Outputs a textual representation of {@code fields} to {@code output}.
   */
  public static void print(UnknownFieldSet fields, Appendable output) throws IOException {
      JsonGenerator generator = new JsonGenerator(output);
      generator.print("{\n");
generator.indent();
      printUnknownFields(fields, generator);
generator.outdent();
      generator.print("}");
  }
 
Example #23
Source File: ProtoAssertTest.java    From curiostack with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testUnknownFields() throws InvalidProtocolBufferException {
  Message message =
      fromUnknownFields(
          UnknownFieldSet.newBuilder()
              .addField(99, UnknownFieldSet.Field.newBuilder().addVarint(42).build())
              .build());
  Message diffMessage =
      fromUnknownFields(
          UnknownFieldSet.newBuilder()
              .addField(93, UnknownFieldSet.Field.newBuilder().addVarint(42).build())
              .build());

  assertThat(diffMessage).isNotEqualTo(message);
  assertThat(diffMessage).ignoringFieldAbsence().isEqualTo(message);

  assertThatThrownBy(() -> assertThat(diffMessage).isEqualTo(message))
      .isInstanceOf(AssertionError.class)
      .satisfies(isEqualToFailureMessage())
      .hasMessageContaining("added: 93[0]: 42")
      .hasMessageContaining("deleted: 99[0]: 42");

  assertThatThrownBy(() -> assertThat(diffMessage).ignoringFieldAbsence().isNotEqualTo(message))
      .isInstanceOf(AssertionError.class)
      .satisfies(isNotEqualToFailureMessage());
}
 
Example #24
Source File: ProtobufTest.java    From feeyo-redisproxy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void main(String[] args) {
	
	Entry entry = Entry.newBuilder().setContext(ByteString.copyFromUtf8("Entry0"))
			.setData(ByteString.copyFromUtf8("Entry0")).setEntryType(EntryType.EntryNormal).setEntryTypeValue(0)
			.setIndex(0).setSyncLog(false).setTerm(20)
			.setUnknownFields(UnknownFieldSet.getDefaultInstance()).build();

	ConfState state = ConfState.newBuilder().addLearners(100).addLearners(101)
			.addNodes(102).addNodes(103)
			.setUnknownFields(UnknownFieldSet.getDefaultInstance()).build();

	SnapshotMetadata metadata = SnapshotMetadata.newBuilder().setConfState(state)
			.setIndex(0) .setTerm(20)
			.setUnknownFields(UnknownFieldSet.getDefaultInstance()).build();

	Snapshot snapshot = Snapshot.newBuilder().setData(ByteString.copyFromUtf8("snapshot"))
			.setMetadata(metadata)
			.setUnknownFields(UnknownFieldSet.getDefaultInstance()).build();

	Message fromMsg = Message.newBuilder().setMsgType(MessageType.MsgAppend).setTo(9000).setFrom(1000)
			.setTerm(20).setLogTerm(30).setIndex(0).addEntries(entry).setCommit(123).setSnapshot(snapshot)
			.setReject(true).setRejectHint(1).setContext(ByteString.copyFromUtf8("message0")).build();

	ProtobufEncoder encoder = new ProtobufEncoder( true );

	ByteBuffer protobuf = encoder.encode(fromMsg);
	
	ProtobufDecoder<Message> decoder = new ProtobufDecoder<Message>(Message.getDefaultInstance(), true);
	Message toMsg = (Message) decoder.decode(protobuf.array()).get(0);
	System.out.println(toMsg.getIndex());
	
}
 
Example #25
Source File: HttpClientTest.java    From feeyo-redisproxy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void main(String[] args) {

		if (System.getProperty("FEEYO_HOME") == null) {
			System.setProperty("FEEYO_HOME", System.getProperty("user.dir"));
		}

		// 设置 LOG4J
		Log4jInitializer.configureAndWatch(System.getProperty("FEEYO_HOME"), "log4j.xml", 30000L);

		
		Entry entry0 = Entry.newBuilder().setContext(ByteString.copyFromUtf8("Entry" + 1))
				.setData(ByteString.copyFromUtf8("Entry " + 1)).setEntryType(EntryType.EntryNormal).setEntryTypeValue(0)
				.setIndex(1).setSyncLog(false).setTerm(20).setUnknownFields(UnknownFieldSet.getDefaultInstance())
				.build();

		ConfState state = ConfState.newBuilder().addLearners(100).addLearners(101).addNodes(102).addNodes(103)
				.setUnknownFields(UnknownFieldSet.getDefaultInstance()).build();

		SnapshotMetadata metadata = SnapshotMetadata.newBuilder().setConfState(state).setIndex(1).setTerm(20)
				.setUnknownFields(UnknownFieldSet.getDefaultInstance()).build();

		Snapshot snapshot = Snapshot.newBuilder().setData(ByteString.copyFromUtf8("snapshot" + 1)).setMetadata(metadata)
				.setUnknownFields(UnknownFieldSet.getDefaultInstance()).build();

		Message msg = Message.newBuilder().setMsgType(MessageType.MsgAppend).setTo(9000).setFrom(1000).setTerm(20)
				.setLogTerm(30).setIndex(1).addEntries(entry0).setCommit(123).setSnapshot(snapshot).setReject(true)
				.setRejectHint(1).setContext(ByteString.copyFromUtf8("message" + 1)).build();

		HttpClientTest test = new HttpClientTest();
		test.post("http://192.168.14.158:8066/proto/eraftpb/message", msg);
	}
 
Example #26
Source File: ProtobufTestUtil.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public static void checkPersonUnknownFields(UnknownFieldSet unknownFieldSet) {
  Map<Integer, UnknownFieldSet.Field> integerFieldMap = unknownFieldSet.asMap();
  Assert.assertEquals(2, integerFieldMap.size());
  Assert.assertTrue(integerFieldMap.containsKey(123));
  Assert.assertEquals(1, integerFieldMap.get(123).getFixed32List().size());
  Assert.assertEquals(1234, (int)integerFieldMap.get(123).getFixed32List().get(0));
  Assert.assertTrue(integerFieldMap.containsKey(234));
  Assert.assertEquals(1, integerFieldMap.get(234).getFixed64List().size());
  Assert.assertEquals(12345678, (long)integerFieldMap.get(234).getFixed64List().get(0));
}
 
Example #27
Source File: ProtobufTestUtil.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public static void checkEmployeeUnknownFields(UnknownFieldSet unknownFieldSet) {
  Map<Integer, UnknownFieldSet.Field> integerFieldMap = unknownFieldSet.asMap();
  Assert.assertEquals(2, integerFieldMap.size());
  Assert.assertTrue(integerFieldMap.containsKey(345));
  Assert.assertEquals(1, integerFieldMap.get(345).getLengthDelimitedList().size());
  Assert.assertEquals(
    integerFieldMap.get(345).getLengthDelimitedList().get(0),
    ByteString.copyFromUtf8("Hello San FRancisco!")
  );
  Assert.assertTrue(integerFieldMap.containsKey(456));
  Assert.assertEquals(1, integerFieldMap.get(456).getVarintList().size());
  Assert.assertEquals(123456789, (long)integerFieldMap.get(456).getVarintList().get(0));
}
 
Example #28
Source File: AbstractCharBasedFormatter.java    From tajo with Apache License 2.0 5 votes vote down vote up
@Override
public void print(UnknownFieldSet fields, OutputStream output, Charset cs)
		throws IOException {
	OutputStreamWriter writer = new OutputStreamWriter(output, cs);
	print(fields, writer);
	writer.flush();
}
 
Example #29
Source File: JsonFormat.java    From compiler with Apache License 2.0 5 votes vote down vote up
/**
 * Like {@code print()}, but writes directly to a {@code String} and returns it.
 */
public static String printToString(UnknownFieldSet fields) {
    try {
        StringBuilder text = new StringBuilder();
        print(fields, text);
        return text.toString();
    } catch (IOException e) {
        throw new RuntimeException("Writing to a StringBuilder threw an IOException (should never happen).",
                                   e);
    }
}
 
Example #30
Source File: ProtobufTestUtil.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public static byte[] getBytes(UnknownFieldSet unknownFieldSet) throws IOException {
  ByteArrayOutputStream bOut = new ByteArrayOutputStream();
  unknownFieldSet.writeDelimitedTo(bOut);
  bOut.flush();
  bOut.close();
  return org.apache.commons.codec.binary.Base64.encodeBase64(bOut.toByteArray());
}