类com.google.protobuf.TextFormat源码实例Demo

下面列出了怎么用com.google.protobuf.TextFormat的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: OpenLabeler   文件: TFTrainer.java
private Pipeline.TrainEvalPipelineConfig parse(Path path) {
    try {
        String text = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
        Pipeline.TrainEvalPipelineConfig.Builder builder = Pipeline.TrainEvalPipelineConfig.newBuilder();
        TextFormat.Parser parser = TextFormat.Parser.newBuilder().build();

        // Skip unknown fields
        Field f = parser.getClass().getDeclaredField("allowUnknownFields");
        f.setAccessible(true);
        f.set(parser, true);

        parser.merge(text, builder);
        return builder.build();
    }
    catch (Exception ex) {
        LOG.log(Level.SEVERE, "Unable to parse pipeline", ex);
    }
    return null;
}
 
源代码2 项目: big-c   文件: EditLogLedgerMetadata.java
void write(ZooKeeper zkc, String path)
    throws IOException, KeeperException.NodeExistsException {
  this.zkPath = path;

  EditLogLedgerProto.Builder builder = EditLogLedgerProto.newBuilder();
  builder.setDataLayoutVersion(dataLayoutVersion)
    .setLedgerId(ledgerId).setFirstTxId(firstTxId);

  if (!inprogress) {
    builder.setLastTxId(lastTxId);
  }
  try {
    zkc.create(path, TextFormat.printToString(builder.build()).getBytes(UTF_8),
               Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
  } catch (KeeperException.NodeExistsException nee) {
    throw nee;
  } catch (KeeperException e) {
    throw new IOException("Error creating ledger znode", e);
  } catch (InterruptedException ie) {
    throw new IOException("Interrupted creating ledger znode", ie);
  }
}
 
源代码3 项目: big-c   文件: Journal.java
/**
 * @return the current state of the given segment, or null if the
 * segment does not exist.
 */
@VisibleForTesting
SegmentStateProto getSegmentInfo(long segmentTxId)
    throws IOException {
  EditLogFile elf = fjm.getLogFile(segmentTxId);
  if (elf == null) {
    return null;
  }
  if (elf.isInProgress()) {
    elf.scanLog();
  }
  if (elf.getLastTxId() == HdfsConstants.INVALID_TXID) {
    LOG.info("Edit log file " + elf + " appears to be empty. " +
        "Moving it aside...");
    elf.moveAsideEmptyFile();
    return null;
  }
  SegmentStateProto ret = SegmentStateProto.newBuilder()
      .setStartTxId(segmentTxId)
      .setEndTxId(elf.getLastTxId())
      .setIsInProgress(elf.isInProgress())
      .build();
  LOG.info("getSegmentInfo(" + segmentTxId + "): " + elf + " -> " +
      TextFormat.shortDebugString(ret));
  return ret;
}
 
源代码4 项目: bazel   文件: ProtoOutputFormatterCallback.java
private void writeData(Message message) throws IOException {
  switch (outputType) {
    case BINARY:
      message.writeTo(outputStream);
      break;
    case TEXT:
      TextFormat.print(message, printStream);
      break;
    case JSON:
      jsonPrinter.appendTo(message, printStream);
      printStream.append('\n');
      break;
    default:
      throw new IllegalStateException("Unknown outputType " + outputType.formatName());
  }
}
 
源代码5 项目: dcos-commons   文件: MultiServiceEventClient.java
/**
 * Maps the provided status to the service that owns its task, then queries that service with the status.
 *
 * <p>This is an optimization which avoids querying services about task statuses that don't relate to them.
 * <p>In addition to reducing unnecessary queries, this also improves isolation between services. They only see
 * task statuses which relate to them.
 */
@Override
public TaskStatusResponse taskStatus(Protos.TaskStatus status) {
  return multiServiceManager
      .getMatchingService(status)
      .map(x -> x.taskStatus(status))
      .orElseGet(() -> multiServiceManager
          .getServiceSanitized(frameworkName)
          .map(x -> {
            LOGGER.info("Forwarding task status to default service: {}", frameworkName);
            return x.taskStatus(status);
          })
          .orElseGet(() -> {
            // Unrecognized service. Status for old task ?
            LOGGER.info("Received status for unknown task {}: {}",
                status.getTaskId().getValue(), TextFormat.shortDebugString(status));
            return TaskStatusResponse.unknownTask();
          })
      );
}
 
源代码6 项目: bundletool   文件: ManifestEditorTest.java
/** Tests the whole process of editing manifest to catch any unintended changes. */
@Test
public void complexManifest_featureSplit() throws Exception {
  XmlNode.Builder xmlNodeBuilder = XmlNode.newBuilder();
  TextFormat.merge(TestData.openReader("testdata/manifest/manifest1.textpb"), xmlNodeBuilder);
  AndroidManifest androidManifest = AndroidManifest.create(xmlNodeBuilder.build());

  XmlNode.Builder expectedXmlNodeBuilder = XmlNode.newBuilder();
  TextFormat.merge(
      TestData.openReader("testdata/manifest/feature_split_manifest1.textpb"),
      expectedXmlNodeBuilder);

  XmlNode generatedManifest =
      androidManifest
          .toEditor()
          .setSplitIdForFeatureSplit("testModule")
          .save()
          .getManifestRoot()
          .getProto();
  assertThat(generatedManifest).isEqualTo(expectedXmlNodeBuilder.build());
}
 
源代码7 项目: tez   文件: TezUtilsInternal.java
/**
 * Convert DAGPlan to text. Skip sensitive informations like credentials.
 *
 * @param dagPlan
 * @return a string representation of the dag plan with sensitive information removed
 */
public static String convertDagPlanToString(DAGProtos.DAGPlan dagPlan) throws IOException {
  StringBuilder sb = new StringBuilder();
  for (Map.Entry<Descriptors.FieldDescriptor, Object> entry : dagPlan.getAllFields().entrySet()) {
    if (entry.getKey().getNumber() != DAGProtos.DAGPlan.CREDENTIALS_BINARY_FIELD_NUMBER) {
      TextFormat.printField(entry.getKey(), entry.getValue(), sb);
    } else {
      Credentials credentials =
          DagTypeConverters.convertByteStringToCredentials(dagPlan.getCredentialsBinary());
      TextFormat.printField(entry.getKey(),
          ByteString.copyFrom(TezCommonUtils.getCredentialsInfo(credentials,"dag").getBytes(
              Charset.forName("UTF-8"))), sb);
    }
  }
  return sb.toString();
}
 
源代码8 项目: hadoop   文件: Journal.java
/**
 * @return the current state of the given segment, or null if the
 * segment does not exist.
 */
@VisibleForTesting
SegmentStateProto getSegmentInfo(long segmentTxId)
    throws IOException {
  EditLogFile elf = fjm.getLogFile(segmentTxId);
  if (elf == null) {
    return null;
  }
  if (elf.isInProgress()) {
    elf.scanLog();
  }
  if (elf.getLastTxId() == HdfsConstants.INVALID_TXID) {
    LOG.info("Edit log file " + elf + " appears to be empty. " +
        "Moving it aside...");
    elf.moveAsideEmptyFile();
    return null;
  }
  SegmentStateProto ret = SegmentStateProto.newBuilder()
      .setStartTxId(segmentTxId)
      .setEndTxId(elf.getLastTxId())
      .setIsInProgress(elf.isInProgress())
      .build();
  LOG.info("getSegmentInfo(" + segmentTxId + "): " + elf + " -> " +
      TextFormat.shortDebugString(ret));
  return ret;
}
 
源代码9 项目: onos   文件: StreamClientImpl.java
private void handlePacketIn(P4RuntimeOuterClass.PacketIn packetInMsg) {
    if (log.isTraceEnabled()) {
        log.trace("Received packet-in from {}: {}", deviceId, packetInMsg);
    }
    if (!pipeconfService.getPipeconf(deviceId).isPresent()) {
        log.warn("Unable to handle packet-in from {}, missing pipeconf: {}",
                 deviceId, TextFormat.shortDebugString(packetInMsg));
        return;
    }
    // Decode packet message and post event.
    // TODO: consider implementing a cache to speed up
    //  encoding/deconding of packet-in/out (e.g. LLDP, ARP)
    final PiPipeconf pipeconf = pipeconfService.getPipeconf(deviceId).get();
    final PiPacketOperation pktOperation;
    try {
        pktOperation = CODECS.packetIn().decode(
                packetInMsg, null, pipeconf);
    } catch (CodecException e) {
        log.warn("Unable to process packet-int: {}", e.getMessage());
        return;
    }
    controller.postEvent(new P4RuntimeEvent(
            P4RuntimeEvent.Type.PACKET_IN,
            new PacketInEvent(deviceId, pktOperation)));
}
 
源代码10 项目: OpenLabeler   文件: TFTrainer.java
public void save() throws IOException {
    if (config == null) {
        return;
    }
    config = update(config);
    String pipelineConfig = TextFormat.printToString(config);
    Files.write(getModelConfigPath(baseModelDir), pipelineConfig.getBytes());
    LOG.info("Created "+ getModelConfigPath(baseModelDir));
}
 
@Override
protected void writeInternal(Message message, HttpOutputMessage outputMessage)
		throws IOException, HttpMessageNotWritableException {

	MediaType contentType = outputMessage.getHeaders().getContentType();
	if (contentType == null) {
		contentType = getDefaultContentType(message);
		Assert.state(contentType != null, "No content type");
	}
	Charset charset = contentType.getCharset();
	if (charset == null) {
		charset = DEFAULT_CHARSET;
	}

	if (PROTOBUF.isCompatibleWith(contentType)) {
		setProtoHeader(outputMessage, message);
		CodedOutputStream codedOutputStream = CodedOutputStream.newInstance(outputMessage.getBody());
		message.writeTo(codedOutputStream);
		codedOutputStream.flush();
	}
	else if (TEXT_PLAIN.isCompatibleWith(contentType)) {
		OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputMessage.getBody(), charset);
		TextFormat.print(message, outputStreamWriter);
		outputStreamWriter.flush();
		outputMessage.getBody().flush();
	}
	else if (this.protobufFormatSupport != null) {
		this.protobufFormatSupport.print(message, outputMessage.getBody(), contentType, charset);
		outputMessage.getBody().flush();
	}
}
 
源代码12 项目: startup-os   文件: FileUtils.java
/** Reads a prototxt file into a proto from zip archive. */
public Message readPrototxtFromZip(
    String zipFilePath, String pathInsideZip, Message.Builder builder) throws IOException {
  ZipFile zipFile =
      new ZipFile(expandHomeDirectory(joinPaths(getCurrentWorkingDirectory(), zipFilePath)));
  String content = streamToString(zipFile.getInputStream(zipFile.getEntry(pathInsideZip)));
  TextFormat.merge(content, builder);
  return builder.build();
}
 
源代码13 项目: bazel   文件: LinkBuildVariablesTestCase.java
/** Creates a CcToolchainFeatures from features described in the given toolchain fragment. */
public static CcToolchainFeatures buildFeatures(RuleContext ruleContext, String... toolchain)
    throws Exception {
  CToolchain.Builder toolchainBuilder = CToolchain.newBuilder();
  TextFormat.merge(Joiner.on("").join(toolchain), toolchainBuilder);
  return new CcToolchainFeatures(
      CcToolchainConfigInfo.fromToolchain(toolchainBuilder.buildPartial()),
      /* ccToolchainPath= */ PathFragment.EMPTY_FRAGMENT);
}
 
源代码14 项目: calcite-avatica   文件: ProtobufTranslationImpl.java
public Service.Request transform(ByteString serializedMessage) throws
    InvalidProtocolBufferException {
  // This should already be an aliased CodedInputStream from the WireMessage parsing.
  Message msg = parser.parseFrom(serializedMessage.newCodedInput());
  if (LOG.isTraceEnabled()) {
    LOG.trace("Deserialized request '{}'", TextFormat.shortDebugString(msg));
  }
  return impl.deserialize(msg);
}
 
源代码15 项目: lams   文件: ProtobufHttpMessageConverter.java
@Override
protected void writeInternal(Message message, HttpOutputMessage outputMessage)
		throws IOException, HttpMessageNotWritableException {

	MediaType contentType = outputMessage.getHeaders().getContentType();
	if (contentType == null) {
		contentType = getDefaultContentType(message);
	}
	Charset charset = contentType.getCharset();
	if (charset == null) {
		charset = DEFAULT_CHARSET;
	}

	if (MediaType.TEXT_PLAIN.isCompatibleWith(contentType)) {
		OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputMessage.getBody(), charset);
		TextFormat.print(message, outputStreamWriter);
		outputStreamWriter.flush();
	}
	else if (MediaType.APPLICATION_JSON.isCompatibleWith(contentType)) {
		JSON_FORMAT.print(message, outputMessage.getBody(), charset);
	}
	else if (MediaType.APPLICATION_XML.isCompatibleWith(contentType)) {
		XML_FORMAT.print(message, outputMessage.getBody(), charset);
	}
	else if (MediaType.TEXT_HTML.isCompatibleWith(contentType)) {
		HTML_FORMAT.print(message, outputMessage.getBody(), charset);
	}
	else if (PROTOBUF.isCompatibleWith(contentType)) {
		setProtoHeader(outputMessage, message);
		FileCopyUtils.copy(message.toByteArray(), outputMessage.getBody());
	}
}
 
源代码16 项目: dcos-commons   文件: FrameworkScheduler.java
@Override
public void statusUpdate(SchedulerDriver driver, Protos.TaskStatus status) {
  try {
    LOGGER.info("Received status update for taskId={} state={} message={} protobuf={}",
        status.getTaskId().getValue(),
        status.getState().toString(),
        status.getMessage(),
        TextFormat.shortDebugString(status));
    Metrics.record(status);
    TaskStatusResponse response = mesosEventClient.taskStatus(status);
    boolean eligibleToKill = TaskKiller.update(status);
    switch (response.result) { // SUPPRESS CHECKSTYLE MissingSwitchDefaultCheck
      case UNKNOWN_TASK:
        if (eligibleToKill) {
          LOGGER.info("Received status update for unknown task, marking task to be killed: {}",
              status.getTaskId().getValue());
          TaskKiller.killTask(status.getTaskId());
        } else {
          // Special case: Mesos can send TASK_LOST+REASON_RECONCILIATION as a response to a
          // prior kill request against a task that is unknown to Mesos. When this happens, we
          // don't want to repeat the kill, because that would create a Kill -> Status -> Kill
          // -> ... loop
          LOGGER.warn(
              "Received status update for unknown task, but task should not be killed again: {}",
              status.getTaskId().getValue());
        }
        break;
      case PROCESSED:
        // No-op
        break;
    }
  } catch (Throwable e) {
    logExceptionAndExit(e);
  }
}
 
源代码17 项目: onos   文件: P4InfoParser.java
private static P4Info getP4InfoMessage(URL p4InfoUrl) throws IOException {
    InputStream p4InfoStream = p4InfoUrl.openStream();
    P4Info.Builder p4InfoBuilder = P4Info.newBuilder();
    TextFormat.getParser().merge(new InputStreamReader(p4InfoStream),
                                 ExtensionRegistry.getEmptyRegistry(),
                                 p4InfoBuilder);
    return p4InfoBuilder.build();
}
 
源代码18 项目: bazel   文件: ClassMemberRetargetConfig.java
@Memoized
MethodInvocations invocationReplacementConfigProto() {
  try {
    String protoText = Resources.toString(invocationReplacementConfigUrl(), UTF_8);
    return TextFormat.parse(
        protoText, ExtensionRegistry.getEmptyRegistry(), MethodInvocations.class);
  } catch (IOException e) {
    throw new IOError(e);
  }
}
 
源代码19 项目: dcos-cassandra-service   文件: CassandraState.java
private Optional<Protos.TaskInfo> getTemplate(CassandraDaemonTask daemon) {
    String templateTaskName = CassandraTemplateTask.toTemplateTaskName(daemon.getName());
    try {
        Optional<Protos.TaskInfo> info = getStateStore().fetchTask(templateTaskName);
        LOGGER.info("Fetched template task for daemon '{}': {}",
                daemon.getName(), TextFormat.shortDebugString(info.get()));
        return info;
    } catch (Exception e) {
        LOGGER.warn(String.format(
                "Failed to retrieve template task '%s'", templateTaskName), e);
        return Optional.empty();
    }
}
 
源代码20 项目: big-c   文件: QuorumCall.java
public static <K> String mapToString(
    Map<K, ? extends Message> map) {
  StringBuilder sb = new StringBuilder();
  boolean first = true;
  for (Map.Entry<K, ? extends Message> e : map.entrySet()) {
    if (!first) {
      sb.append("\n");
    }
    first = false;
    sb.append(e.getKey()).append(": ")
      .append(TextFormat.shortDebugString(e.getValue()));
  }
  return sb.toString();
}
 
源代码21 项目: hadoop   文件: ApplicationAttemptStartDataPBImpl.java
@Override
public String toString() {
  return TextFormat.shortDebugString(getProto());
}
 
源代码22 项目: hadoop   文件: RefreshAdminAclsResponsePBImpl.java
@Override
public String toString() {
  return TextFormat.shortDebugString(getProto());
}
 
源代码23 项目: sql-layer   文件: ProtobufRowConverter.java
public void format(DynamicMessage msg, Appendable output) throws IOException {
    TextFormat.print(msg, output);
}
 
源代码24 项目: hadoop   文件: PipelineAck.java
@Override //Object
public String toString() {
  return TextFormat.shortDebugString(proto);
}
 
源代码25 项目: Bats   文件: EndpointAffinity.java
@Override
public String toString() {
  return "EndpointAffinity [endpoint=" + TextFormat.shortDebugString(endpoint) + ", affinity=" + affinity +
      ", mandatory=" + mandatory + ", maxWidth=" + maxWidth + "]";
}
 
@Override
public String toString() {
  return TextFormat.shortDebugString(getProto());
}
 
源代码27 项目: hadoop   文件: RefreshNodesResponsePBImpl.java
@Override
public String toString() {
  return TextFormat.shortDebugString(getProto());
}
 
源代码28 项目: big-c   文件: GetNewApplicationRequestPBImpl.java
@Override
public String toString() {
  return TextFormat.shortDebugString(getProto());
}
 
源代码29 项目: hedera-mirror-node   文件: HCSMAPITopicSampler.java
/**
 * Runs single load test thread by calling gRPC Consensus service subscribeTopic endpoint. Success is dependant on
 * StreamObserver observing the expected count for historic and incoming messages in the allotted time Returns
 * SamplerResult to client
 *
 * @param messageListener listener properties
 * @return Sampler result representing success and observed message counts
 */
@Override
public HCSSamplerResult subscribeTopic(MessageListener messageListener) {
    log.info("Subscribing to topic using MAPI with {}, {}", () -> TextFormat
            .shortDebugString(request), () -> messageListener);

    CountDownLatch historicMessagesLatch = new CountDownLatch(messageListener.getHistoricMessagesCount());
    CountDownLatch incomingMessagesLatch = new CountDownLatch(messageListener.getFutureMessagesCount());
    TopicID topicId = request.getTopicID();
    HCSMAPISamplerResult result = HCSMAPISamplerResult
            .builder()
            .realmNum(topicId.getRealmNum())
            .topicNum(topicId.getTopicNum())
            .success(true)
            .build();
    MirrorSubscriptionHandle subscription = null;
    ScheduledExecutorService scheduler = null;

    try {
        subscription = mirrorConsensusTopicQuery
                .subscribe(mirrorClient,
                        resp -> {
                            result.onNext(resp);
                            if (result.isHistorical()) {
                                historicMessagesLatch.countDown();
                            } else {
                                incomingMessagesLatch.countDown();
                            }
                        },
                        result::onError);

        scheduler = Executors.newSingleThreadScheduledExecutor();
        scheduler.scheduleAtFixedRate(() -> {
            result.printProgress();
        }, 0, 1, TimeUnit.MINUTES);

        // await some new messages
        if (!historicMessagesLatch.await(messageListener.getMessagesLatchWaitSeconds(), TimeUnit.SECONDS)) {
            log.error("Historic messages latch count is {}, did not reach zero", historicMessagesLatch.getCount());
            result.setSuccess(false);
        }

        if (historicMessagesLatch.getCount() == 0 && !incomingMessagesLatch
                .await(messageListener.getMessagesLatchWaitSeconds(), TimeUnit.SECONDS)) {
            log.error("incomingMessagesLatch count is {}, did not reach zero", incomingMessagesLatch.getCount());
            result.setSuccess(false);
        }

        result.onComplete();
    } catch (Exception ex) {
        log.error("Error subscribing to topic", ex);
        throw ex;
    } finally {
        if (subscription != null) {
            subscription.unsubscribe();
            log.info("Unsubscribed from {}", subscription);
        }

        scheduler.shutdownNow();

        return result;
    }
}
 
源代码30 项目: big-c   文件: NMTokenIdentifier.java
@Override
public String toString() {
  return TextFormat.shortDebugString(getProto());
}
 
 类所在包
 同包方法