aboutsummaryrefslogtreecommitdiffhomepage
path: root/java/src/main/java/com/google/protobuf/TextFormat.java
diff options
context:
space:
mode:
Diffstat (limited to 'java/src/main/java/com/google/protobuf/TextFormat.java')
-rw-r--r--java/src/main/java/com/google/protobuf/TextFormat.java296
1 files changed, 164 insertions, 132 deletions
diff --git a/java/src/main/java/com/google/protobuf/TextFormat.java b/java/src/main/java/com/google/protobuf/TextFormat.java
index 3dcf68c8..a855720b 100644
--- a/java/src/main/java/com/google/protobuf/TextFormat.java
+++ b/java/src/main/java/com/google/protobuf/TextFormat.java
@@ -52,22 +52,25 @@ import java.util.regex.Pattern;
* @author kenton@google.com Kenton Varda
*/
public final class TextFormat {
+ private TextFormat() {
+ }
/**
* 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)
*/
- public static void print(Message message, Appendable output)
+ public static void print(final Message message, final Appendable output)
throws IOException {
- TextGenerator generator = new TextGenerator(output);
+ final TextGenerator generator = new TextGenerator(output);
print(message, generator);
}
/** Outputs a textual representation of {@code fields} to {@code output}. */
- public static void print(UnknownFieldSet fields, Appendable output)
+ public static void print(final UnknownFieldSet fields,
+ final Appendable output)
throws IOException {
- TextGenerator generator = new TextGenerator(output);
+ final TextGenerator generator = new TextGenerator(output);
printUnknownFields(fields, generator);
}
@@ -75,9 +78,9 @@ public final class TextFormat {
* Like {@code print()}, but writes directly to a {@code String} and
* returns it.
*/
- public static String printToString(Message message) {
+ public static String printToString(final Message message) {
try {
- StringBuilder text = new StringBuilder();
+ final StringBuilder text = new StringBuilder();
print(message, text);
return text.toString();
} catch (IOException e) {
@@ -91,9 +94,9 @@ public final class TextFormat {
* Like {@code print()}, but writes directly to a {@code String} and
* returns it.
*/
- public static String printToString(UnknownFieldSet fields) {
+ public static String printToString(final UnknownFieldSet fields) {
try {
- StringBuilder text = new StringBuilder();
+ final StringBuilder text = new StringBuilder();
print(fields, text);
return text.toString();
} catch (IOException e) {
@@ -103,22 +106,44 @@ public final class TextFormat {
}
}
- private static void print(Message message, TextGenerator generator)
+ private static void print(final Message message,
+ final TextGenerator generator)
throws IOException {
- for (Map.Entry<FieldDescriptor, Object> field :
+ for (final Map.Entry<FieldDescriptor, Object> field :
message.getAllFields().entrySet()) {
printField(field.getKey(), field.getValue(), generator);
}
printUnknownFields(message.getUnknownFields(), generator);
}
+
+ public static void printField(final FieldDescriptor field,
+ final Object value,
+ final Appendable output)
+ throws IOException {
+ final TextGenerator generator = new TextGenerator(output);
+ printField(field, value, generator);
+ }
- public static void printField(FieldDescriptor field,
- Object value,
- TextGenerator generator)
+ public static String printFieldToString(final FieldDescriptor field,
+ final Object value) {
+ try {
+ final StringBuilder text = new StringBuilder();
+ printField(field, value, text);
+ return text.toString();
+ } catch (IOException e) {
+ throw new RuntimeException(
+ "Writing to a StringBuilder threw an IOException (should never " +
+ "happen).", e);
+ }
+ }
+
+ private static void printField(final FieldDescriptor field,
+ final Object value,
+ final TextGenerator generator)
throws IOException {
if (field.isRepeated()) {
// Repeated field. Print each element.
- for (Object element : (List) value) {
+ for (final Object element : (List) value) {
printSingleField(field, element, generator);
}
} else {
@@ -126,9 +151,9 @@ public final class TextFormat {
}
}
- private static void printSingleField(FieldDescriptor field,
- Object value,
- TextGenerator generator)
+ private static void printSingleField(final FieldDescriptor field,
+ final Object value,
+ final TextGenerator generator)
throws IOException {
if (field.isExtension()) {
generator.print("[");
@@ -168,9 +193,9 @@ public final class TextFormat {
generator.print("\n");
}
- private static void printFieldValue(FieldDescriptor field,
- Object value,
- TextGenerator generator)
+ private static void printFieldValue(final FieldDescriptor field,
+ final Object value,
+ final TextGenerator generator)
throws IOException {
switch (field.getType()) {
case INT32:
@@ -202,17 +227,15 @@ public final class TextFormat {
generator.print("\"");
break;
- case BYTES: {
+ case BYTES:
generator.print("\"");
generator.print(escapeBytes((ByteString) value));
generator.print("\"");
break;
- }
- case ENUM: {
+ case ENUM:
generator.print(((EnumValueDescriptor) value).getName());
break;
- }
case MESSAGE:
case GROUP:
@@ -221,39 +244,39 @@ public final class TextFormat {
}
}
- private static void printUnknownFields(UnknownFieldSet unknownFields,
- TextGenerator generator)
+ private static void printUnknownFields(final UnknownFieldSet unknownFields,
+ final TextGenerator generator)
throws IOException {
- for (Map.Entry<Integer, UnknownFieldSet.Field> entry :
+ for (final Map.Entry<Integer, UnknownFieldSet.Field> entry :
unknownFields.asMap().entrySet()) {
- String prefix = entry.getKey().toString() + ": ";
- UnknownFieldSet.Field field = entry.getValue();
+ final String prefix = entry.getKey().toString() + ": ";
+ final UnknownFieldSet.Field field = entry.getValue();
- for (long value : field.getVarintList()) {
+ for (final long value : field.getVarintList()) {
generator.print(entry.getKey().toString());
generator.print(": ");
generator.print(unsignedToString(value));
generator.print("\n");
}
- for (int value : field.getFixed32List()) {
+ for (final int value : field.getFixed32List()) {
generator.print(entry.getKey().toString());
generator.print(": ");
generator.print(String.format((Locale) null, "0x%08x", value));
generator.print("\n");
}
- for (long value : field.getFixed64List()) {
+ for (final long value : field.getFixed64List()) {
generator.print(entry.getKey().toString());
generator.print(": ");
generator.print(String.format((Locale) null, "0x%016x", value));
generator.print("\n");
}
- for (ByteString value : field.getLengthDelimitedList()) {
+ for (final ByteString value : field.getLengthDelimitedList()) {
generator.print(entry.getKey().toString());
generator.print(": \"");
generator.print(escapeBytes(value));
generator.print("\"\n");
}
- for (UnknownFieldSet value : field.getGroupList()) {
+ for (final UnknownFieldSet value : field.getGroupList()) {
generator.print(entry.getKey().toString());
generator.print(" {\n");
generator.indent();
@@ -265,7 +288,7 @@ public final class TextFormat {
}
/** Convert an unsigned 32-bit integer to a string. */
- private static String unsignedToString(int value) {
+ private static String unsignedToString(final int value) {
if (value >= 0) {
return Integer.toString(value);
} else {
@@ -274,7 +297,7 @@ public final class TextFormat {
}
/** Convert an unsigned 64-bit integer to a string. */
- private static String unsignedToString(long value) {
+ private static String unsignedToString(final long value) {
if (value >= 0) {
return Long.toString(value);
} else {
@@ -288,13 +311,12 @@ public final class TextFormat {
/**
* An inner class for writing text to the output stream.
*/
- static private final class TextGenerator {
+ private static final class TextGenerator {
+ private Appendable output;
+ private boolean atStartOfLine = true;
+ private final StringBuilder indent = new StringBuilder();
- Appendable output;
- boolean atStartOfLine = true;
- StringBuilder indent = new StringBuilder();
-
- public TextGenerator(Appendable output) {
+ private TextGenerator(final Appendable output) {
this.output = output;
}
@@ -312,7 +334,7 @@ public final class TextFormat {
* level is zero.
*/
public void outdent() {
- int length = indent.length();
+ final int length = indent.length();
if (length == 0) {
throw new IllegalArgumentException(
" Outdent() without matching Indent().");
@@ -323,8 +345,8 @@ public final class TextFormat {
/**
* Print text to the output stream.
*/
- public void print(CharSequence text) throws IOException {
- int size = text.length();
+ public void print(final CharSequence text) throws IOException {
+ final int size = text.length();
int pos = 0;
for (int i = 0; i < size; i++) {
@@ -337,7 +359,8 @@ public final class TextFormat {
write(text.subSequence(pos, size), size - pos);
}
- private void write(CharSequence data, int size) throws IOException {
+ private void write(final CharSequence data, final int size)
+ throws IOException {
if (size == 0) {
return;
}
@@ -399,27 +422,27 @@ public final class TextFormat {
// We use possesive quantifiers (*+ and ++) because otherwise the Java
// regex matcher has stack overflows on large inputs.
- private static Pattern WHITESPACE =
+ private static final Pattern WHITESPACE =
Pattern.compile("(\\s|(#.*$))++", Pattern.MULTILINE);
- private static Pattern TOKEN = Pattern.compile(
+ private static final Pattern TOKEN = Pattern.compile(
"[a-zA-Z_][0-9a-zA-Z_+-]*+|" + // an identifier
"[0-9+-][0-9a-zA-Z_.+-]*+|" + // a number
"\"([^\"\n\\\\]|\\\\.)*+(\"|\\\\?$)|" + // a double-quoted string
"\'([^\"\n\\\\]|\\\\.)*+(\'|\\\\?$)", // a single-quoted string
Pattern.MULTILINE);
- private static Pattern DOUBLE_INFINITY = Pattern.compile(
+ private static final Pattern DOUBLE_INFINITY = Pattern.compile(
"-?inf(inity)?",
Pattern.CASE_INSENSITIVE);
- private static Pattern FLOAT_INFINITY = Pattern.compile(
+ private static final Pattern FLOAT_INFINITY = Pattern.compile(
"-?inf(inity)?f?",
Pattern.CASE_INSENSITIVE);
- private static Pattern FLOAT_NAN = Pattern.compile(
+ private static final Pattern FLOAT_NAN = Pattern.compile(
"nanf?",
Pattern.CASE_INSENSITIVE);
/** Construct a tokenizer that parses tokens from the given text. */
- public Tokenizer(CharSequence text) {
+ private Tokenizer(final CharSequence text) {
this.text = text;
this.matcher = WHITESPACE.matcher(text);
skipWhitespace();
@@ -481,7 +504,7 @@ public final class TextFormat {
* If the next token exactly matches {@code token}, consume it and return
* {@code true}. Otherwise, return {@code false} without doing anything.
*/
- public boolean tryConsume(String token) {
+ public boolean tryConsume(final String token) {
if (currentToken.equals(token)) {
nextToken();
return true;
@@ -494,7 +517,7 @@ public final class TextFormat {
* If the next token exactly matches {@code token}, consume it. Otherwise,
* throw a {@link ParseException}.
*/
- public void consume(String token) throws ParseException {
+ public void consume(final String token) throws ParseException {
if (!tryConsume(token)) {
throw parseException("Expected \"" + token + "\".");
}
@@ -509,7 +532,7 @@ public final class TextFormat {
return false;
}
- char c = currentToken.charAt(0);
+ final char c = currentToken.charAt(0);
return ('0' <= c && c <= '9') ||
c == '-' || c == '+';
}
@@ -520,7 +543,7 @@ public final class TextFormat {
*/
public String consumeIdentifier() throws ParseException {
for (int i = 0; i < currentToken.length(); i++) {
- char c = currentToken.charAt(i);
+ final char c = currentToken.charAt(i);
if (('a' <= c && c <= 'z') ||
('A' <= c && c <= 'Z') ||
('0' <= c && c <= '9') ||
@@ -531,7 +554,7 @@ public final class TextFormat {
}
}
- String result = currentToken;
+ final String result = currentToken;
nextToken();
return result;
}
@@ -542,7 +565,7 @@ public final class TextFormat {
*/
public int consumeInt32() throws ParseException {
try {
- int result = parseInt32(currentToken);
+ final int result = parseInt32(currentToken);
nextToken();
return result;
} catch (NumberFormatException e) {
@@ -556,7 +579,7 @@ public final class TextFormat {
*/
public int consumeUInt32() throws ParseException {
try {
- int result = parseUInt32(currentToken);
+ final int result = parseUInt32(currentToken);
nextToken();
return result;
} catch (NumberFormatException e) {
@@ -570,7 +593,7 @@ public final class TextFormat {
*/
public long consumeInt64() throws ParseException {
try {
- long result = parseInt64(currentToken);
+ final long result = parseInt64(currentToken);
nextToken();
return result;
} catch (NumberFormatException e) {
@@ -584,7 +607,7 @@ public final class TextFormat {
*/
public long consumeUInt64() throws ParseException {
try {
- long result = parseUInt64(currentToken);
+ final long result = parseUInt64(currentToken);
nextToken();
return result;
} catch (NumberFormatException e) {
@@ -600,7 +623,7 @@ public final class TextFormat {
// We need to parse infinity and nan separately because
// Double.parseDouble() does not accept "inf", "infinity", or "nan".
if (DOUBLE_INFINITY.matcher(currentToken).matches()) {
- boolean negative = currentToken.startsWith("-");
+ final boolean negative = currentToken.startsWith("-");
nextToken();
return negative ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
}
@@ -609,7 +632,7 @@ public final class TextFormat {
return Double.NaN;
}
try {
- double result = Double.parseDouble(currentToken);
+ final double result = Double.parseDouble(currentToken);
nextToken();
return result;
} catch (NumberFormatException e) {
@@ -625,7 +648,7 @@ public final class TextFormat {
// We need to parse infinity and nan separately because
// Float.parseFloat() does not accept "inf", "infinity", or "nan".
if (FLOAT_INFINITY.matcher(currentToken).matches()) {
- boolean negative = currentToken.startsWith("-");
+ final boolean negative = currentToken.startsWith("-");
nextToken();
return negative ? Float.NEGATIVE_INFINITY : Float.POSITIVE_INFINITY;
}
@@ -634,7 +657,7 @@ public final class TextFormat {
return Float.NaN;
}
try {
- float result = Float.parseFloat(currentToken);
+ final float result = Float.parseFloat(currentToken);
nextToken();
return result;
} catch (NumberFormatException e) {
@@ -672,7 +695,8 @@ public final class TextFormat {
* {@link ParseException}.
*/
public ByteString consumeByteString() throws ParseException {
- char quote = currentToken.length() > 0 ? currentToken.charAt(0) : '\0';
+ final char quote = currentToken.length() > 0 ? currentToken.charAt(0)
+ : '\0';
if (quote != '\"' && quote != '\'') {
throw parseException("Expected string.");
}
@@ -683,11 +707,12 @@ public final class TextFormat {
}
try {
- String escaped = currentToken.substring(1, currentToken.length() - 1);
- ByteString result = unescapeBytes(escaped);
+ final String escaped =
+ currentToken.substring(1, currentToken.length() - 1);
+ final ByteString result = unescapeBytes(escaped);
nextToken();
return result;
- } catch (InvalidEscapeSequence e) {
+ } catch (InvalidEscapeSequenceException e) {
throw parseException(e.getMessage());
}
}
@@ -696,7 +721,7 @@ public final class TextFormat {
* Returns a {@link ParseException} with the current line and column
* numbers in the description, suitable for throwing.
*/
- public ParseException parseException(String description) {
+ public ParseException parseException(final String description) {
// Note: People generally prefer one-based line and column numbers.
return new ParseException(
(line + 1) + ":" + (column + 1) + ": " + description);
@@ -706,7 +731,8 @@ public final class TextFormat {
* Returns a {@link ParseException} with the line and column numbers of
* the previous token in the description, suitable for throwing.
*/
- public ParseException parseExceptionPreviousToken(String description) {
+ public ParseException parseExceptionPreviousToken(
+ final String description) {
// Note: People generally prefer one-based line and column numbers.
return new ParseException(
(previousLine + 1) + ":" + (previousColumn + 1) + ": " + description);
@@ -716,7 +742,8 @@ public final class TextFormat {
* Constructs an appropriate {@link ParseException} for the given
* {@code NumberFormatException} when trying to parse an integer.
*/
- private ParseException integerParseException(NumberFormatException e) {
+ private ParseException integerParseException(
+ final NumberFormatException e) {
return parseException("Couldn't parse integer: " + e.getMessage());
}
@@ -724,14 +751,16 @@ public final class TextFormat {
* Constructs an appropriate {@link ParseException} for the given
* {@code NumberFormatException} when trying to parse a float or double.
*/
- private ParseException floatParseException(NumberFormatException e) {
+ private ParseException floatParseException(final NumberFormatException e) {
return parseException("Couldn't parse number: " + e.getMessage());
}
}
/** Thrown when parsing an invalid text format message. */
public static class ParseException extends IOException {
- public ParseException(String message) {
+ private static final long serialVersionUID = 3196188060225107702L;
+
+ public ParseException(final String message) {
super(message);
}
}
@@ -740,9 +769,9 @@ public final class TextFormat {
* Parse a text-format message from {@code input} and merge the contents
* into {@code builder}.
*/
- public static void merge(Readable input,
- Message.Builder builder)
- throws ParseException, IOException {
+ public static void merge(final Readable input,
+ final Message.Builder builder)
+ throws IOException {
merge(input, ExtensionRegistry.getEmptyRegistry(), builder);
}
@@ -750,8 +779,8 @@ public final class TextFormat {
* Parse a text-format message from {@code input} and merge the contents
* into {@code builder}.
*/
- public static void merge(CharSequence input,
- Message.Builder builder)
+ public static void merge(final CharSequence input,
+ final Message.Builder builder)
throws ParseException {
merge(input, ExtensionRegistry.getEmptyRegistry(), builder);
}
@@ -761,10 +790,10 @@ public final class TextFormat {
* into {@code builder}. Extensions will be recognized if they are
* registered in {@code extensionRegistry}.
*/
- public static void merge(Readable input,
- ExtensionRegistry extensionRegistry,
- Message.Builder builder)
- throws ParseException, IOException {
+ public static void merge(final Readable input,
+ final ExtensionRegistry extensionRegistry,
+ final Message.Builder builder)
+ throws IOException {
// Read the entire input to a String then parse that.
// If StreamTokenizer were not quite so crippled, or if there were a kind
@@ -780,12 +809,12 @@ public final class TextFormat {
// TODO(chrisn): See if working around java.io.Reader#read(CharBuffer)
// overhead is worthwhile
- private static StringBuilder toStringBuilder(Readable input)
+ private static StringBuilder toStringBuilder(final Readable input)
throws IOException {
- StringBuilder text = new StringBuilder();
- CharBuffer buffer = CharBuffer.allocate(BUFFER_SIZE);
+ final StringBuilder text = new StringBuilder();
+ final CharBuffer buffer = CharBuffer.allocate(BUFFER_SIZE);
while (true) {
- int n = input.read(buffer);
+ final int n = input.read(buffer);
if (n == -1) {
break;
}
@@ -800,11 +829,11 @@ public final class TextFormat {
* into {@code builder}. Extensions will be recognized if they are
* registered in {@code extensionRegistry}.
*/
- public static void merge(CharSequence input,
- ExtensionRegistry extensionRegistry,
- Message.Builder builder)
+ public static void merge(final CharSequence input,
+ final ExtensionRegistry extensionRegistry,
+ final Message.Builder builder)
throws ParseException {
- Tokenizer tokenizer = new Tokenizer(input);
+ final Tokenizer tokenizer = new Tokenizer(input);
while (!tokenizer.atEnd()) {
mergeField(tokenizer, extensionRegistry, builder);
@@ -815,19 +844,20 @@ public final class TextFormat {
* Parse a single field from {@code tokenizer} and merge it into
* {@code builder}.
*/
- private static void mergeField(Tokenizer tokenizer,
- ExtensionRegistry extensionRegistry,
- Message.Builder builder)
+ private static void mergeField(final Tokenizer tokenizer,
+ final ExtensionRegistry extensionRegistry,
+ final Message.Builder builder)
throws ParseException {
FieldDescriptor field;
- Descriptor type = builder.getDescriptorForType();
+ final Descriptor type = builder.getDescriptorForType();
ExtensionRegistry.ExtensionInfo extension = null;
if (tokenizer.tryConsume("[")) {
// An extension.
- StringBuilder name = new StringBuilder(tokenizer.consumeIdentifier());
+ final StringBuilder name =
+ new StringBuilder(tokenizer.consumeIdentifier());
while (tokenizer.tryConsume(".")) {
- name.append(".");
+ name.append('.');
name.append(tokenizer.consumeIdentifier());
}
@@ -846,7 +876,7 @@ public final class TextFormat {
field = extension.descriptor;
} else {
- String name = tokenizer.consumeIdentifier();
+ final String name = tokenizer.consumeIdentifier();
field = type.findFieldByName(name);
// Group names are expected to be capitalized as they appear in the
@@ -855,7 +885,7 @@ public final class TextFormat {
if (field == null) {
// Explicitly specify US locale so that this code does not break when
// executing in Turkey.
- String lowerName = name.toLowerCase(Locale.US);
+ final String lowerName = name.toLowerCase(Locale.US);
field = type.findFieldByName(lowerName);
// If the case-insensitive match worked but the field is NOT a group,
if (field != null && field.getType() != FieldDescriptor.Type.GROUP) {
@@ -880,7 +910,7 @@ public final class TextFormat {
if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
tokenizer.tryConsume(":"); // optional
- String endToken;
+ final String endToken;
if (tokenizer.tryConsume("<")) {
endToken = ">";
} else {
@@ -888,7 +918,7 @@ public final class TextFormat {
endToken = "}";
}
- Message.Builder subBuilder;
+ final Message.Builder subBuilder;
if (extension == null) {
subBuilder = builder.newBuilderForField(field);
} else {
@@ -951,19 +981,19 @@ public final class TextFormat {
value = tokenizer.consumeByteString();
break;
- case ENUM: {
- EnumDescriptor enumType = field.getEnumType();
+ case ENUM:
+ final EnumDescriptor enumType = field.getEnumType();
if (tokenizer.lookingAtInteger()) {
- int number = tokenizer.consumeInt32();
+ final int number = tokenizer.consumeInt32();
value = enumType.findValueByNumber(number);
if (value == null) {
throw tokenizer.parseExceptionPreviousToken(
"Enum type \"" + enumType.getFullName() +
- "\" has no value with number " + number + ".");
+ "\" has no value with number " + number + '.');
}
} else {
- String id = tokenizer.consumeIdentifier();
+ final String id = tokenizer.consumeIdentifier();
value = enumType.findValueByName(id);
if (value == null) {
throw tokenizer.parseExceptionPreviousToken(
@@ -973,7 +1003,6 @@ public final class TextFormat {
}
break;
- }
case MESSAGE:
case GROUP:
@@ -1002,10 +1031,10 @@ public final class TextFormat {
* which no defined short-hand escape sequence is defined will be escaped
* using 3-digit octal sequences.
*/
- static String escapeBytes(ByteString input) {
- StringBuilder builder = new StringBuilder(input.size());
+ static String escapeBytes(final ByteString input) {
+ final StringBuilder builder = new StringBuilder(input.size());
for (int i = 0; i < input.size(); i++) {
- byte b = input.byteAt(i);
+ final byte b = input.byteAt(i);
switch (b) {
// Java does not recognize \a or \v, apparently.
case 0x07: builder.append("\\a" ); break;
@@ -1038,9 +1067,9 @@ public final class TextFormat {
* {@link #escapeBytes(ByteString)}. Two-digit hex escapes (starting with
* "\x") are also recognized.
*/
- static ByteString unescapeBytes(CharSequence input)
- throws InvalidEscapeSequence {
- byte[] result = new byte[input.length()];
+ static ByteString unescapeBytes(final CharSequence input)
+ throws InvalidEscapeSequenceException {
+ final byte[] result = new byte[input.length()];
int pos = 0;
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
@@ -1080,7 +1109,7 @@ public final class TextFormat {
++i;
code = digitValue(input.charAt(i));
} else {
- throw new InvalidEscapeSequence(
+ throw new InvalidEscapeSequenceException(
"Invalid escape sequence: '\\x' with no digits");
}
if (i + 1 < input.length() && isHex(input.charAt(i + 1))) {
@@ -1091,12 +1120,12 @@ public final class TextFormat {
break;
default:
- throw new InvalidEscapeSequence(
- "Invalid escape sequence: '\\" + c + "'");
+ throw new InvalidEscapeSequenceException(
+ "Invalid escape sequence: '\\" + c + '\'');
}
}
} else {
- throw new InvalidEscapeSequence(
+ throw new InvalidEscapeSequenceException(
"Invalid escape sequence: '\\' at end of string.");
}
} else {
@@ -1111,8 +1140,10 @@ public final class TextFormat {
* Thrown by {@link TextFormat#unescapeBytes} and
* {@link TextFormat#unescapeText} when an invalid escape sequence is seen.
*/
- static class InvalidEscapeSequence extends IOException {
- public InvalidEscapeSequence(String description) {
+ static class InvalidEscapeSequenceException extends IOException {
+ private static final long serialVersionUID = -8164033650142593304L;
+
+ InvalidEscapeSequenceException(final String description) {
super(description);
}
}
@@ -1122,7 +1153,7 @@ public final class TextFormat {
* 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.
*/
- static String escapeText(String input) {
+ static String escapeText(final String input) {
return escapeBytes(ByteString.copyFromUtf8(input));
}
@@ -1130,17 +1161,18 @@ public final class TextFormat {
* Un-escape a text string as escaped using {@link #escapeText(String)}.
* Two-digit hex escapes (starting with "\x") are also recognized.
*/
- static String unescapeText(String input) throws InvalidEscapeSequence {
+ static String unescapeText(final String input)
+ throws InvalidEscapeSequenceException {
return unescapeBytes(input).toStringUtf8();
}
/** Is this an octal digit? */
- private static boolean isOctal(char c) {
+ private static boolean isOctal(final char c) {
return '0' <= c && c <= '7';
}
/** Is this a hex digit? */
- private static boolean isHex(char c) {
+ private static boolean isHex(final char c) {
return ('0' <= c && c <= '9') ||
('a' <= c && c <= 'f') ||
('A' <= c && c <= 'F');
@@ -1151,7 +1183,7 @@ public final class TextFormat {
* numeric value. This is like {@code Character.digit()} but we don't accept
* non-ASCII digits.
*/
- private static int digitValue(char c) {
+ private static int digitValue(final char c) {
if ('0' <= c && c <= '9') {
return c - '0';
} else if ('a' <= c && c <= 'z') {
@@ -1166,7 +1198,7 @@ public final class TextFormat {
* {@code Integer.parseInt()}, this function recognizes the prefixes "0x"
* and "0" to signify hexidecimal and octal numbers, respectively.
*/
- static int parseInt32(String text) throws NumberFormatException {
+ static int parseInt32(final String text) throws NumberFormatException {
return (int) parseInteger(text, true, false);
}
@@ -1177,7 +1209,7 @@ public final class TextFormat {
* result is coerced to a (signed) {@code int} when returned since Java has
* no unsigned integer type.
*/
- static int parseUInt32(String text) throws NumberFormatException {
+ static int parseUInt32(final String text) throws NumberFormatException {
return (int) parseInteger(text, false, false);
}
@@ -1186,7 +1218,7 @@ public final class TextFormat {
* {@code Integer.parseInt()}, this function recognizes the prefixes "0x"
* and "0" to signify hexidecimal and octal numbers, respectively.
*/
- static long parseInt64(String text) throws NumberFormatException {
+ static long parseInt64(final String text) throws NumberFormatException {
return parseInteger(text, true, true);
}
@@ -1197,13 +1229,13 @@ public final class TextFormat {
* result is coerced to a (signed) {@code long} when returned since Java has
* no unsigned long type.
*/
- static long parseUInt64(String text) throws NumberFormatException {
+ static long parseUInt64(final String text) throws NumberFormatException {
return parseInteger(text, false, true);
}
- private static long parseInteger(String text,
- boolean isSigned,
- boolean isLong)
+ private static long parseInteger(final String text,
+ final boolean isSigned,
+ final boolean isLong)
throws NumberFormatException {
int pos = 0;
@@ -1224,7 +1256,7 @@ public final class TextFormat {
radix = 8;
}
- String numberText = text.substring(pos);
+ final String numberText = text.substring(pos);
long result = 0;
if (numberText.length() < 16) {