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.
    • Field Detail

      • logger

        private static final java.util.logging.Logger logger
      • DEBUG_STRING_SILENT_MARKER

        private static final java.lang.String DEBUG_STRING_SILENT_MARKER
        See Also:
        Constant Field Values
    • Constructor Detail

      • TextFormat

        private TextFormat()
    • Method Detail

      • shortDebugString

        @Deprecated
        @InlineMe(replacement="TextFormat.printer().emittingSingleLine(true).printToString(message)",
                  imports="com.google.protobuf.TextFormat")
        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
      • printUnknownFieldValue

        private static void printUnknownFieldValue​(int tag,
                                                   java.lang.Object value,
                                                   TextFormat.TextGenerator generator,
                                                   boolean redact)
                                            throws java.io.IOException
        Throws:
        java.io.IOException
      • printer

        public static TextFormat.Printer printer()
        Printer instance which escapes non-ASCII characters.
      • 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.
      • setSingleLineOutput

        private static TextFormat.TextGenerator setSingleLineOutput​(java.lang.Appendable output,
                                                                    boolean singleLine)
      • 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​(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.
      • escapeText

        static java.lang.String escapeText​(java.lang.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 java.lang.String escapeDoubleQuotesAndBackslashes​(java.lang.String input)
        Escape double quotes and backslashes in a String for emittingUnicode output of a message.
      • 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​(java.lang.String text)
                       throws java.lang.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:
        java.lang.NumberFormatException
      • parseUInt32

        static int parseUInt32​(java.lang.String text)
                        throws java.lang.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:
        java.lang.NumberFormatException
      • parseInt64

        static long parseInt64​(java.lang.String text)
                        throws java.lang.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:
        java.lang.NumberFormatException
      • parseUInt64

        static long parseUInt64​(java.lang.String text)
                         throws java.lang.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:
        java.lang.NumberFormatException
      • parseInteger

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