Class TextFormat

java.lang.Object
com.google.protobuf.TextFormat

public final class TextFormat extends Object
Provide text parsing and formatting support for proto2 instances. The implementation largely follows google/protobuf/text_format.cc.
  • Field Details

  • Constructor Details

    • TextFormat

      private TextFormat()
  • Method Details

    • print

      @Deprecated public static void print(MessageOrBuilder message, Appendable output) throws IOException
      Deprecated.
      Use printer().print(MessageOrBuilder, Appendable)
      Outputs a textual representation of the Protocol Message supplied into the parameter output. (This representation is the new version of the classic "ProtocolPrinter" output from the original Protocol Buffer system)
      Throws:
      IOException
    • print

      @Deprecated public static void print(UnknownFieldSet fields, Appendable output) throws IOException
      Deprecated.
      Use printer().print(UnknownFieldSet, Appendable)
      Outputs a textual representation of fields to output.
      Throws:
      IOException
    • printUnicode

      @Deprecated public static void printUnicode(MessageOrBuilder message, Appendable output) throws IOException
      Deprecated.
      Use printer().escapingNonAscii(false).print(MessageOrBuilder, Appendable)
      Same as print(), except that non-ASCII characters are not escaped.
      Throws:
      IOException
    • printUnicode

      @Deprecated public static void printUnicode(UnknownFieldSet fields, Appendable output) throws IOException
      Deprecated.
      Use printer().escapingNonAscii(false).print(UnknownFieldSet, Appendable)
      Same as print(), except that non-ASCII characters are not escaped.
      Throws:
      IOException
    • shortDebugString

      public static String shortDebugString(MessageOrBuilder message)
      Generates a human readable form of this message, useful for debugging and other purposes, with no newline characters. This is just a trivial wrapper around TextFormat.Printer.shortDebugString(MessageOrBuilder).
    • shortDebugString

      @Deprecated public static String shortDebugString(Descriptors.FieldDescriptor field, Object value)
      Deprecated.
      Use printer().shortDebugString(FieldDescriptor, Object)
      Generates a human readable form of the field, useful for debugging and other purposes, with no newline characters.
    • shortDebugString

      @Deprecated public static String shortDebugString(UnknownFieldSet fields)
      Deprecated.
      Use printer().shortDebugString(UnknownFieldSet)
      Generates a human readable form of the unknown fields, useful for debugging and other purposes, with no newline characters.
    • printToString

      @Deprecated public static String printToString(MessageOrBuilder message)
      Deprecated.
      Use message.toString()
      Like print(), but writes directly to a String and returns it.
    • printToString

      @Deprecated public static String printToString(UnknownFieldSet fields)
      Like print(), but writes directly to a String and returns it.
    • printToUnicodeString

      @Deprecated public static String printToUnicodeString(MessageOrBuilder message)
      Deprecated.
      Use printer().escapingNonAscii(false).printToString(MessageOrBuilder)
      Same as printToString(), except that non-ASCII characters in string type fields are not escaped in backslash+octals.
    • printToUnicodeString

      @Deprecated public static String printToUnicodeString(UnknownFieldSet fields)
      Deprecated.
      Use printer().escapingNonAscii(false).printToString(UnknownFieldSet)
      Same as printToString(), except that non-ASCII characters in string type fields are not escaped in backslash+octals.
    • printField

      @Deprecated public static void printField(Descriptors.FieldDescriptor field, Object value, Appendable output) throws IOException
      Deprecated.
      Use printer().printField(FieldDescriptor, Object, Appendable)
      Throws:
      IOException
    • printFieldToString

      @Deprecated public static String printFieldToString(Descriptors.FieldDescriptor field, Object value)
      Deprecated.
      Use printer().printFieldToString(FieldDescriptor, Object)
    • printUnicodeFieldValue

      @Deprecated public static void printUnicodeFieldValue(Descriptors.FieldDescriptor field, Object value, Appendable output) throws IOException
      Deprecated.
      Use printer().escapingNonAscii(false).printFieldValue(FieldDescriptor, Object, Appendable)
      Outputs a unicode textual representation of the value of given field value.

      Same as printFieldValue(), except that non-ASCII characters in string type fields are not escaped in backslash+octals.

      Parameters:
      field - the descriptor of the field
      value - the value of the field
      output - the output to which to append the formatted value
      Throws:
      ClassCastException - if the value is not appropriate for the given field descriptor
      IOException - if there is an exception writing to the output
    • printFieldValue

      @Deprecated public static void printFieldValue(Descriptors.FieldDescriptor field, Object value, Appendable output) throws IOException
      Deprecated.
      Use printer().printFieldValue(FieldDescriptor, Object, Appendable)
      Outputs a textual representation of the value of given field value.
      Parameters:
      field - the descriptor of the field
      value - the value of the field
      output - the output to which to append the formatted value
      Throws:
      ClassCastException - if the value is not appropriate for the given field descriptor
      IOException - if there is an exception writing to the output
    • printUnknownFieldValue

      public static void printUnknownFieldValue(int tag, Object value, Appendable output) throws IOException
      Outputs a textual representation of the value of an unknown field.
      Parameters:
      tag - the field's tag number
      value - the value of the field
      output - the output to which to append the formatted value
      Throws:
      ClassCastException - if the value is not appropriate for the given field descriptor
      IOException - if there is an exception writing to the output
    • printUnknownFieldValue

      private static void printUnknownFieldValue(int tag, Object value, TextFormat.TextGenerator generator) throws IOException
      Throws:
      IOException
    • printer

      public static TextFormat.Printer printer()
      Printer instance which escapes non-ASCII characters.
    • unsignedToString

      public static String unsignedToString(int value)
      Convert an unsigned 32-bit integer to a string.
    • unsignedToString

      public static String unsignedToString(long value)
      Convert an unsigned 64-bit integer to a string.
    • multiLineOutput

      private static TextFormat.TextGenerator multiLineOutput(Appendable output)
    • singleLineOutput

      private static TextFormat.TextGenerator singleLineOutput(Appendable output)
    • getParser

      public static TextFormat.Parser getParser()
      Return a TextFormat.Parser instance which can parse text-format messages. The returned instance is thread-safe.
    • merge

      public static void merge(Readable input, Message.Builder builder) throws IOException
      Parse a text-format message from input and merge the contents into builder.
      Throws:
      IOException
    • merge

      public static void merge(CharSequence input, Message.Builder builder) throws TextFormat.ParseException
      Parse a text-format message from input and merge the contents into builder.
      Throws:
      TextFormat.ParseException
    • parse

      public static <T extends Message> T parse(CharSequence input, Class<T> protoClass) throws TextFormat.ParseException
      Parse a text-format message from input.
      Returns:
      the parsed message, guaranteed initialized
      Throws:
      TextFormat.ParseException
    • merge

      public static void merge(Readable input, ExtensionRegistry extensionRegistry, Message.Builder builder) throws IOException
      Parse a text-format message from input and merge the contents into builder. Extensions will be recognized if they are registered in extensionRegistry.
      Throws:
      IOException
    • merge

      public static void merge(CharSequence input, ExtensionRegistry extensionRegistry, Message.Builder builder) throws TextFormat.ParseException
      Parse a text-format message from input and merge the contents into builder. Extensions will be recognized if they are registered in extensionRegistry.
      Throws:
      TextFormat.ParseException
    • parse

      public static <T extends Message> T parse(CharSequence input, ExtensionRegistry extensionRegistry, Class<T> protoClass) throws TextFormat.ParseException
      Parse a text-format message from input. Extensions will be recognized if they are registered in extensionRegistry.
      Returns:
      the parsed message, guaranteed initialized
      Throws:
      TextFormat.ParseException
    • escapeBytes

      public static String escapeBytes(ByteString input)
      Escapes bytes in the format used in protocol buffer text format, which is the same as the format used for C string literals. All bytes that are not printable 7-bit ASCII characters are escaped, as well as backslash, single-quote, and double-quote characters. Characters for which no defined short-hand escape sequence is defined will be escaped using 3-digit octal sequences.
    • escapeBytes

      public static String escapeBytes(byte[] input)
      Like escapeBytes(ByteString), but used for byte array.
    • unescapeBytes

      public static ByteString unescapeBytes(CharSequence charString) throws TextFormat.InvalidEscapeSequenceException
      Un-escape a byte sequence as escaped using escapeBytes(ByteString). Two-digit hex escapes (starting with "\x") are also recognized.
      Throws:
      TextFormat.InvalidEscapeSequenceException
    • escapeText

      static String escapeText(String input)
      Like escapeBytes(ByteString), but escapes a text string. Non-ASCII characters are first encoded as UTF-8, then each byte is escaped individually as a 3-digit octal escape. Yes, it's weird.
    • escapeDoubleQuotesAndBackslashes

      public static String escapeDoubleQuotesAndBackslashes(String input)
      Escape double quotes and backslashes in a String for emittingUnicode output of a message.
    • unescapeText

      static String unescapeText(String input) throws TextFormat.InvalidEscapeSequenceException
      Un-escape a text string as escaped using escapeText(String). Two-digit hex escapes (starting with "\x") are also recognized.
      Throws:
      TextFormat.InvalidEscapeSequenceException
    • isOctal

      private static boolean isOctal(byte c)
      Is this an octal digit?
    • isHex

      private static boolean isHex(byte c)
      Is this a hex digit?
    • digitValue

      private static int digitValue(byte c)
      Interpret a character as a digit (in any base up to 36) and return the numeric value. This is like Character.digit() but we don't accept non-ASCII digits.
    • parseInt32

      static int parseInt32(String text) throws NumberFormatException
      Parse a 32-bit signed integer from the text. Unlike the Java standard Integer.parseInt(), this function recognizes the prefixes "0x" and "0" to signify hexadecimal and octal numbers, respectively.
      Throws:
      NumberFormatException
    • parseUInt32

      static int parseUInt32(String text) throws NumberFormatException
      Parse a 32-bit unsigned integer from the text. Unlike the Java standard Integer.parseInt(), this function recognizes the prefixes "0x" and "0" to signify hexadecimal and octal numbers, respectively. The result is coerced to a (signed) int when returned since Java has no unsigned integer type.
      Throws:
      NumberFormatException
    • parseInt64

      static long parseInt64(String text) throws NumberFormatException
      Parse a 64-bit signed integer from the text. Unlike the Java standard Integer.parseInt(), this function recognizes the prefixes "0x" and "0" to signify hexadecimal and octal numbers, respectively.
      Throws:
      NumberFormatException
    • parseUInt64

      static long parseUInt64(String text) throws NumberFormatException
      Parse a 64-bit unsigned integer from the text. Unlike the Java standard Integer.parseInt(), this function recognizes the prefixes "0x" and "0" to signify hexadecimal and octal numbers, respectively. The result is coerced to a (signed) long when returned since Java has no unsigned long type.
      Throws:
      NumberFormatException
    • parseInteger

      private static long parseInteger(String text, boolean isSigned, boolean isLong) throws NumberFormatException
      Throws:
      NumberFormatException