package edu.nyu.proto;

import com.google.protobuf.Message;
import com.google.protobuf.TextFormat;
import java.io.*;

/** General utilites for protos. */
public final class Protos {
  private Protos() {}

  /** Returns a protobuf parsed from the given file. */
  public static <P extends Message> P loadFromFile(File file, P example) throws IOException {
    return parseText(new BufferedReader(new FileReader(file)), example);
  }

  /** Returns a protobuf parsed from the given reader. */
  @SuppressWarnings("unchecked") /* OK - as example used to get builder. */
  public static <P extends Message> P parseText(Reader reader, P example) throws IOException {
    Message.Builder builder = example.newBuilderForType();
    TextFormat.merge(reader, builder);
    return (P) builder.build();
  }

  /**
   * Returns a protobuf parsed from the given String using the example (usually
   * P.getDefaultInstance()).
   */
  public static <P extends Message> P parseText(String input, P example) throws IOException {
    return parseText(new StringReader(input), example);
  }
}

@@ -1065,11 +1065,419 @@
 le);%0A  %7D
+%0A%0A  /**%0A   * Returns a protobuf parsed from the given String using the example (usually%0A   * P.getDefaultInstance()).%0A   */%0A  public static %3CP extends Message%3E P parseUnchecked(String input, P example) %7B%0A    try %7B%0A      return parseText(new StringReader(input), example);%0A    %7D catch (IOException e) %7B%0A      throw new IllegalArgumentException(String.format(%22unable to parse input '%25s'%22, input), e);%0A    %7D%0A  %7D
 %0A%7D%0A

add convenience method Protos.parseUnchecked