View Javadoc

    1   /*
    2    * Copyright 2012 The Netty Project
    3    *
    4    * The Netty Project licenses this file to you under the Apache License,
    5    * version 2.0 (the "License"); you may not use this file except in compliance
    6    * with the License. You may obtain a copy of the License at:
    7    *
    8    *   http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    13   * License for the specific language governing permissions and limitations
    14   * under the License.
    15   */
    16  package org.jboss.netty.util.internal;
    17
    18  import java.util.Formatter;
    19
    20  /**
    21   * String utility class.
    22   */
    23  public final class StringUtil {
    24
    25      private StringUtil() {
    26          // Unused.
    27      }
    28
    29      public static final String NEWLINE;
    30
    31      static {
    32          String newLine = null;
    33
    34          try {
    35              newLine = new Formatter().format("%n").toString();
    36          } catch (Exception e) {
    37              newLine = "\n";
    38          }
    39
    40          NEWLINE = newLine;
    41      }
    42
    43      /**
    44       * Strip an Object of it's ISO control characters.
    45       *
    46       * @param value
    47       *          The Object that should be stripped. This objects toString method will
    48       *          called and the result passed to {@link #stripControlCharacters(String)}.
    49       * @return {@code String}
    50       *          A new String instance with its hexadecimal control characters replaced
    51       *          by a space. Or the unmodified String if it does not contain any ISO
    52       *          control characters.
    53       */
    54      public static String stripControlCharacters(Object value) {
    55          if (value == null) {
    56              return null;
    57          }
    58
    59          return stripControlCharacters(value.toString());
    60      }
    61
    62      /**
    63       * Strip a String of it's ISO control characters.
    64       *
    65       * @param value
    66       *          The String that should be stripped.
    67       * @return {@code String}
    68       *          A new String instance with its hexadecimal control characters replaced
    69       *          by a space. Or the unmodified String if it does not contain any ISO
    70       *          control characters.
    71       */
    72      public static String stripControlCharacters(String value) {
    73          if (value == null) {
    74              return null;
    75          }
    76
    77          boolean hasControlChars = false;
    78          for (int i = value.length() - 1; i >= 0; i --) {
    79              if (Character.isISOControl(value.charAt(i))) {
    80                  hasControlChars = true;
    81                  break;
    82              }
    83          }
    84
    85          if (!hasControlChars) {
    86              return value;
    87          }
    88
    89          StringBuilder buf = new StringBuilder(value.length());
    90          int i = 0;
    91
    92          // Skip initial control characters (i.e. left trim)
    93          for (; i < value.length(); i ++) {
    94              if (!Character.isISOControl(value.charAt(i))) {
    95                  break;
    96              }
    97          }
    98
    99          // Copy non control characters and substitute control characters with
    100         // a space.  The last control characters are trimmed.
    101         boolean suppressingControlChars = false;
    102         for (; i < value.length(); i ++) {
    103             if (Character.isISOControl(value.charAt(i))) {
    104                 suppressingControlChars = true;
    105                 continue;
    106             } else {
    107                 if (suppressingControlChars) {
    108                     suppressingControlChars = false;
    109                     buf.append(' ');
    110                 }
    111                 buf.append(value.charAt(i));
    112             }
    113         }
    114
    115         return buf.toString();
    116     }
    117 }