Class TextFormat


  • public final class TextFormat
    extends java.lang.Object
    Provide text parsing and formatting support for proto2 instances. The implementation largely follows text_format.cc.
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static TextFormat.Printer debugFormatPrinter()
      Printer instance which escapes non-ASCII characters and prints in the debug format.
      static java.lang.String escapeBytes​(byte[] input)
      Like escapeBytes(ByteString), but used for byte array.
      static java.lang.String escapeBytes​(com.google.protobuf.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.
      static java.lang.String escapeDoubleQuotesAndBackslashes​(java.lang.String input)
      Escape double quotes and backslashes in a String for emittingUnicode output of a message.
      static TextFormat.Parser getParser()
      Return a TextFormat.Parser instance which can parse text-format messages.
      static void merge​(java.lang.CharSequence input, ExtensionRegistry extensionRegistry, Message.Builder builder)
      Parse a text-format message from input and merge the contents into builder.
      static void merge​(java.lang.CharSequence input, Message.Builder builder)
      Parse a text-format message from input and merge the contents into builder.
      static void merge​(java.lang.Readable input, ExtensionRegistry extensionRegistry, Message.Builder builder)
      Parse a text-format message from input and merge the contents into builder.
      static void merge​(java.lang.Readable input, Message.Builder builder)
      Parse a text-format message from input and merge the contents into builder.
      static <T extends Message>
      T
      parse​(java.lang.CharSequence input, ExtensionRegistry extensionRegistry, java.lang.Class<T> protoClass)
      Parse a text-format message from input.
      static <T extends Message>
      T
      parse​(java.lang.CharSequence input, java.lang.Class<T> protoClass)
      Parse a text-format message from input.
      static TextFormat.Printer printer()
      Printer instance which escapes non-ASCII characters.
      static void printUnknownFieldValue​(int tag, java.lang.Object value, java.lang.Appendable output)
      Outputs a textual representation of the value of an unknown field.
      static java.lang.String shortDebugString​(MessageOrBuilder message)
      Deprecated.
      Use printer().emittingSingleLine(true).printToString(MessageOrBuilder)
      static com.google.protobuf.ByteString unescapeBytes​(java.lang.CharSequence charString)
      Un-escape a byte sequence as escaped using escapeBytes(ByteString).
      static java.lang.String unsignedToString​(int value)
      Convert an unsigned 32-bit integer to a string.
      static java.lang.String unsignedToString​(long value)
      Convert an unsigned 64-bit integer to a string.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • shortDebugString

        @Deprecated
        public static java.lang.String shortDebugString​(MessageOrBuilder message)
        Deprecated.
        Use printer().emittingSingleLine(true).printToString(MessageOrBuilder)
        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).
      • printUnknownFieldValue

        public static void printUnknownFieldValue​(int tag,
                                                  java.lang.Object value,
                                                  java.lang.Appendable output)
                                           throws java.io.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:
        java.lang.ClassCastException - if the value is not appropriate for the given field descriptor
        java.io.IOException - if there is an exception writing to the output
      • printer

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

        public static TextFormat.Printer debugFormatPrinter()
        Printer instance which escapes non-ASCII characters and prints in the debug format.
      • unsignedToString

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

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

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

        public static void merge​(java.lang.Readable input,
                                 ExtensionRegistry extensionRegistry,
                                 Message.Builder builder)
                          throws java.io.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:
        java.io.IOException
      • parse

        public static <T extends Message> T parse​(java.lang.CharSequence input,
                                                  ExtensionRegistry extensionRegistry,
                                                  java.lang.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 java.lang.String escapeBytes​(com.google.protobuf.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 java.lang.String escapeBytes​(byte[] input)
        Like escapeBytes(ByteString), but used for byte array.
      • escapeDoubleQuotesAndBackslashes

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