Class TextFormat


  • public final class TextFormat
    extends java.lang.Object
    Provide ascii text parsing and formatting support for proto2 instances. The implementation largely follows google/protobuf/text_format.cc. HRC: I wish the original class was not package protected so we did not need to copy this file over. We need to request that the protobuf folks open this class up amoung a few others.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static int BUFFER_SIZE  
    • Constructor Summary

      Constructors 
      Constructor Description
      TextFormat()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static int digitValue​(char c)
      Interpret a character as a digit (in any base up to 36) and return the numeric value.
      (package private) static java.lang.String escapeBytes​(Buffer input)
      Escapes bytes in the format used in protocol buffer text format, which is the same as the format used for C string literals.
      (package private) static java.lang.String escapeText​(java.lang.String input)
      Like escapeBytes(Buffer), but escapes a text string.
      private static boolean isHex​(char c)
      Is this a hex digit?
      private static boolean isOctal​(char c)
      Is this an octal digit?
      (package private) static int parseInt32​(java.lang.String text)
      Parse a 32-bit signed integer from the text.
      (package private) static long parseInt64​(java.lang.String text)
      Parse a 64-bit signed integer from the text.
      private static long parseInteger​(java.lang.String text, boolean isSigned, boolean isLong)  
      (package private) static int parseUInt32​(java.lang.String text)
      Parse a 32-bit unsigned integer from the text.
      (package private) static long parseUInt64​(java.lang.String text)
      Parse a 64-bit unsigned integer from the text.
      private static java.lang.StringBuilder toStringBuilder​(java.lang.Readable input)  
      (package private) static Buffer unescapeBytes​(java.lang.CharSequence input)
      Un-escape a byte sequence as escaped using escapeBytes(Buffer).
      (package private) static java.lang.String unescapeText​(java.lang.String input)
      Un-escape a text string as escaped using escapeText(String).
      private static java.lang.String unsignedToString​(int value)
      Convert an unsigned 32-bit integer to a string.
      private 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
    • Constructor Detail

      • TextFormat

        public TextFormat()
    • Method Detail

      • unsignedToString

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

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

        private static java.lang.StringBuilder toStringBuilder​(java.lang.Readable input)
                                                        throws java.io.IOException
        Throws:
        java.io.IOException
      • escapeBytes

        static java.lang.String escapeBytes​(Buffer 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.
      • escapeText

        static java.lang.String escapeText​(java.lang.String input)
        Like escapeBytes(Buffer), 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.
      • isOctal

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

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

        private static int digitValue​(char 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 hexidecimal 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 hexidecimal 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 hexidecimal 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 hexidecimal 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