diff --git a/common/src/main/java/io/netty/util/internal/StringUtil.java b/common/src/main/java/io/netty/util/internal/StringUtil.java index b73d0046ac..f5ca73de7a 100644 --- a/common/src/main/java/io/netty/util/internal/StringUtil.java +++ b/common/src/main/java/io/netty/util/internal/StringUtil.java @@ -51,11 +51,14 @@ public final class StringUtil { // Determine the newline character of the current platform. String newLine; + Formatter formatter = new Formatter(); try { - newLine = new Formatter().format("%n").toString(); + newLine = formatter.format("%n").toString(); } catch (Exception e) { // Should not reach here, but just in case. newLine = "\n"; + } finally { + formatter.close(); } NEWLINE = newLine; @@ -374,6 +377,20 @@ public final class StringUtil { escaped.append(DOUBLE_QUOTE) : value; } + /** + * Get the length of a string, {@code null} input is considered {@code 0} length. + */ + public static int length(String s) { + return s == null ? 0 : s.length(); + } + + /** + * Determine if a string is {@code null} or {@link String#isEmpty()} returns {@code true}. + */ + public static boolean isNullOrEmpty(String s) { + return s == null || s.isEmpty(); + } + private static boolean isDoubleQuote(char c) { return c == DOUBLE_QUOTE; }