Java/Data Type/String ASCII

Материал из Java эксперт
Перейти к: навигация, поиск

Содержание

Allow text containing mostly ASCII characters to be decipherable on an ASCII terminal without decoding.

  
/*
 * Copyright 2001-2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.BitSet;

/**
 * <p>
 * Similar to the Quoted-Printable content-transfer-encoding defined in 
     */
    protected String encodeText(final String text, final String charset)
     throws Exception, UnsupportedEncodingException  
    {
        if (text == null) {
            return null;
        }
        StringBuffer buffer = new StringBuffer();
        buffer.append("=?"); 
        buffer.append(charset); 
        buffer.append("?"); 
        buffer.append(getEncoding()); 
        buffer.append("?");
        byte [] rawdata = doEncoding(text.getBytes(charset)); 
        buffer.append(new String(rawdata, "US-ASCII"));
        buffer.append("?="); 
        return buffer.toString();
    }
    
    /**
     * Applies an RFC 1522 compliant decoding scheme to the given string of text. This method 
     * processes the "encoded-word" header common to all the RFC 1522 codecs and then invokes 
     * {@link #doEncoding(byte [])} method of a concrete class to perform the specific deconding.
     * 
     * @param text a string to decode
     * 
     * @throws DecoderException thrown if there is an error conidition during the Decoding 
     *  process.
     * @throws UnsupportedEncodingException thrown if charset specified in the "encoded-word" 
     *  header is not supported 
     */
    protected String decodeText(final String text)
     throws Exception, UnsupportedEncodingException  
    {
        if (text == null) {
            return null;
        }
        if ((!text.startsWith("=?")) || (!text.endsWith("?="))) {
            throw new RuntimeException("RFC 1522 violation: malformed encoded content");
        }
        int termnator = text.length() - 2;
        int from = 2;
        int to = text.indexOf("?", from);
        if ((to == -1) || (to == termnator)) {
            throw new RuntimeException("RFC 1522 violation: charset token not found");
        }
        String charset = text.substring(from, to);
        if (charset.equals("")) {
            throw new RuntimeException("RFC 1522 violation: charset not specified");
        }
        from = to + 1;
        to = text.indexOf("?", from);
        if ((to == -1) || (to == termnator)) {
            throw new RuntimeException("RFC 1522 violation: encoding token not found");
        }
        String encoding = text.substring(from, to);
        if (!getEncoding().equalsIgnoreCase(encoding)) {
            throw new RuntimeException("This codec cannot decode " + 
                encoding + " encoded content");
        }
        from = to + 1;
        to = text.indexOf("?", from);
        byte[] data = text.substring(from, to).getBytes("US-ASCII");
        data = doDecoding(data); 
        return new String(data, charset);
    }
    /**
     * Returns the codec name (referred to as encoding in the RFC 1522)
     * 
     * @return name of the codec
     */    
    protected abstract String getEncoding();
    /**
     * Encodes an array of bytes using the defined encoding scheme
     * 
     * @param bytes Data to be encoded
     *
     * @return A byte array containing the encoded data
     * 
     * @throws EncoderException thrown if the Encoder encounters a failure condition 
     *  during the encoding process.
     */    
    protected abstract byte[] doEncoding(byte[] bytes) throws Exception;
    /**
     * Decodes an array of bytes using the defined encoding scheme
     * 
     * @param bytes Data to be decoded
     *
     * @return a byte array that contains decoded data
     * 
     * @throws DecoderException A decoder exception is thrown if a Decoder encounters a 
     *  failure condition during the decode process.
     */    
    protected abstract byte[] doDecoding(byte[] bytes) throws Exception;
}
class QuotedPrintableCodec {
  /**
   * The default charset used for string decoding and encoding.
   */
  private String charset = "UTF8";
  /**
   * BitSet of printable characters as defined in RFC 1521.
   */
  private static final BitSet PRINTABLE_CHARS = new BitSet(256);
  private static byte ESCAPE_CHAR = "=";
  private static byte TAB = 9;
  private static byte SPACE = 32;
  // Static initializer for printable chars collection
  static {
      // alpha characters
      for (int i = 33; i <= 60; i++) {
          PRINTABLE_CHARS.set(i);
      }
      for (int i = 62; i <= 126; i++) {
          PRINTABLE_CHARS.set(i);
      }
      PRINTABLE_CHARS.set(TAB);
      PRINTABLE_CHARS.set(SPACE);
  }
  /**
   * Default constructor.
   */
  public QuotedPrintableCodec() {
      super();
  }
  /**
   * Constructor which allows for the selection of a default charset
   * 
   * @param charset
   *                  the default string charset to use.
   */
  public QuotedPrintableCodec(String charset) {
      super();
      this.charset = charset;
  }
  /**
   * Encodes byte into its quoted-printable representation.
   * 
   * @param b
   *                  byte to encode
   * @param buffer
   *                  the buffer to write to
   */
  private static final void encodeQuotedPrintable(int b, ByteArrayOutputStream buffer) {
      buffer.write(ESCAPE_CHAR);
      char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, 16));
      char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, 16));
      buffer.write(hex1);
      buffer.write(hex2);
  }
  /**
   * Encodes an array of bytes into an array of quoted-printable 7-bit characters. Unsafe characters are escaped.
   * 
   * <p>
   * This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in
   * RFC 1521 and is suitable for encoding binary data and unformatted text.
   * </p>
   * 
   * @param printable
   *                  bitset of characters deemed quoted-printable
   * @param bytes
   *                  array of bytes to be encoded
   * @return array of bytes containing quoted-printable data
   */
  public static final byte[] encodeQuotedPrintable(BitSet printable, byte[] bytes) {
      if (bytes == null) {
          return null;
      }
      if (printable == null) {
          printable = PRINTABLE_CHARS;
      }
      ByteArrayOutputStream buffer = new ByteArrayOutputStream();
      for (int i = 0; i < bytes.length; i++) {
          int b = bytes[i];
          if (b < 0) {
              b = 256 + b;
          }
          if (printable.get(b)) {
              buffer.write(b);
          } else {
              encodeQuotedPrintable(b, buffer);
          }
      }
      return buffer.toByteArray();
  }
  /**
   * Decodes an array quoted-printable characters into an array of original bytes. Escaped characters are converted
   * back to their original representation.
   * 
   * <p>
   * This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in
   * RFC 1521.
   * </p>
   * 
   * @param bytes
   *                  array of quoted-printable characters
   * @return array of original bytes
   * @throws DecoderException
   *                  Thrown if quoted-printable decoding is unsuccessful
   */
  public static final byte[] decodeQuotedPrintable(byte[] bytes) throws Exception {
      if (bytes == null) {
          return null;
      }
      ByteArrayOutputStream buffer = new ByteArrayOutputStream();
      for (int i = 0; i < bytes.length; i++) {
          int b = bytes[i];
          if (b == ESCAPE_CHAR) {
              try {
                  int u = Character.digit((char) bytes[++i], 16);
                  int l = Character.digit((char) bytes[++i], 16);
                  if (u == -1 || l == -1) {
                      throw new RuntimeException("Invalid quoted-printable encoding");
                  }
                  buffer.write((char) ((u << 4) + l));
              } catch (ArrayIndexOutOfBoundsException e) {
                  throw new RuntimeException("Invalid quoted-printable encoding");
              }
          } else {
              buffer.write(b);
          }
      }
      return buffer.toByteArray();
  }
  /**
   * Encodes an array of bytes into an array of quoted-printable 7-bit characters. Unsafe characters are escaped.
   * 
   * <p>
   * This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in
   * RFC 1521 and is suitable for encoding binary data and unformatted text.
   * </p>
   * 
   * @param bytes
   *                  array of bytes to be encoded
   * @return array of bytes containing quoted-printable data
   */
  public byte[] encode(byte[] bytes) {
      return encodeQuotedPrintable(PRINTABLE_CHARS, bytes);
  }
  /**
   * Decodes an array of quoted-printable characters into an array of original bytes. Escaped characters are converted
   * back to their original representation.
   * 
   * <p>
   * This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in
   * RFC 1521.
   * </p>
   * 
   * @param bytes
   *                  array of quoted-printable characters
   * @return array of original bytes
   * @throws DecoderException
   *                  Thrown if quoted-printable decoding is unsuccessful
   */
  public byte[] decode(byte[] bytes) throws Exception {
      return decodeQuotedPrintable(bytes);
  }
  /**
   * Encodes a string into its quoted-printable form using the default string charset. Unsafe characters are escaped.
   * 
   * <p>
   * This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in
   * RFC 1521 and is suitable for encoding binary data.
   * </p>
   * 
   * @param pString
   *                  string to convert to quoted-printable form
   * @return quoted-printable string
   * 
   * @throws EncoderException
   *                  Thrown if quoted-printable encoding is unsuccessful
   * 
   * @see #getDefaultCharset()
   */
  public String encode(String pString) throws Exception {
      if (pString == null) {
          return null;
      }
      try {
          return encode(pString, getDefaultCharset());
      } catch (UnsupportedEncodingException e) {
          throw new RuntimeException(e.getMessage());
      }
  }
  /**
   * Decodes a quoted-printable string into its original form using the specified string charset. Escaped characters
   * are converted back to their original representation.
   * 
   * @param pString
   *                  quoted-printable string to convert into its original form
   * @param charset
   *                  the original string charset
   * @return original string
   * @throws DecoderException
   *                  Thrown if quoted-printable decoding is unsuccessful
   * @throws UnsupportedEncodingException
   *                  Thrown if charset is not supported
   */
  public String decode(String pString, String charset) throws Exception, UnsupportedEncodingException {
      if (pString == null) {
          return null;
      }
      return new String(decode(pString.getBytes("US-ASCII")), charset);
  }
  /**
   * Decodes a quoted-printable string into its original form using the default string charset. Escaped characters are
   * converted back to their original representation.
   * 
   * @param pString
   *                  quoted-printable string to convert into its original form
   * @return original string
   * @throws DecoderException
   *                  Thrown if quoted-printable decoding is unsuccessful
   * @throws UnsupportedEncodingException
   *                  Thrown if charset is not supported
   * @see #getDefaultCharset()
   */
  public String decode(String pString) throws Exception {
      if (pString == null) {
          return null;
      }
      try {
          return decode(pString, getDefaultCharset());
      } catch (UnsupportedEncodingException e) {
          throw new RuntimeException(e.getMessage());
      }
  }
  /**
   * Encodes an object into its quoted-printable safe form. Unsafe characters are escaped.
   * 
   * @param pObject
   *                  string to convert to a quoted-printable form
   * @return quoted-printable object
   * @throws EncoderException
   *                  Thrown if quoted-printable encoding is not applicable to objects of this type or if encoding is
   *                  unsuccessful
   */
  public Object encode(Object pObject) throws Exception {
      if (pObject == null) {
          return null;
      } else if (pObject instanceof byte[]) {
          return encode((byte[]) pObject);
      } else if (pObject instanceof String) {
          return encode((String) pObject);
      } else {
          throw new RuntimeException("Objects of type "
              + pObject.getClass().getName()
              + " cannot be quoted-printable encoded");
      }
  }
  /**
   * Decodes a quoted-printable object into its original form. Escaped characters are converted back to their original
   * representation.
   * 
   * @param pObject
   *                  quoted-printable object to convert into its original form
   * @return original object
   * @throws DecoderException
   *                  Thrown if quoted-printable decoding is not applicable to objects of this type if decoding is
   *                  unsuccessful
   */
  public Object decode(Object pObject) throws Exception {
      if (pObject == null) {
          return null;
      } else if (pObject instanceof byte[]) {
          return decode((byte[]) pObject);
      } else if (pObject instanceof String) {
          return decode((String) pObject);
      } else {
          throw new RuntimeException("Objects of type "
              + pObject.getClass().getName()
              + " cannot be quoted-printable decoded");
      }
  }
  /**
   * Returns the default charset used for string decoding and encoding.
   * 
   * @return the default string charset.
   */
  public String getDefaultCharset() {
      return this.charset;
  }
  /**
   * Encodes a string into its quoted-printable form using the specified charset. Unsafe characters are escaped.
   * 
   * <p>
   * This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in
   * RFC 1521 and is suitable for encoding binary data and unformatted text.
   * </p>
   * 
   * @param pString
   *                  string to convert to quoted-printable form
   * @param charset
   *                  the charset for pString
   * @return quoted-printable string
   * 
   * @throws UnsupportedEncodingException
   *                  Thrown if the charset is not supported
   */
  public String encode(String pString, String charset) throws UnsupportedEncodingException {
      if (pString == null) {
          return null;
      }
      return new String(encode(pString.getBytes(charset)), "US-ASCII");
  }
}





Checks whether the character is ASCII 7 bit.

  
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * <p>Operations on char primitives and Character objects.</p>
 *
 * <p>This class tries to handle <code>null</code> input gracefully.
 * An exception will not be thrown for a <code>null</code> input.
 * Each method documents its behaviour in more detail.</p>
 * 
 * @author Stephen Colebourne
 * @since 2.1
 * @version $Id: CharUtils.java 437554 2006-08-28 06:21:41Z bayard $
 */
public class Main {
  
  //--------------------------------------------------------------------------
  /**
   * <p>Checks whether the character is ASCII 7 bit.</p>
   *
   * <pre>
   *   CharUtils.isAscii("a")  = true
   *   CharUtils.isAscii("A")  = true
   *   CharUtils.isAscii("3")  = true
   *   CharUtils.isAscii("-")  = true
   *   CharUtils.isAscii("\n") = true
   *   CharUtils.isAscii("&copy;") = false
   * </pre>
   * 
   * @param ch  the character to check
   * @return true if less than 128
   */
  public static boolean isAscii(char ch) {
      return ch < 128;
  }
}





Checks whether the character is ASCII 7 bit alphabetic.

  

/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * <p>Operations on char primitives and Character objects.</p>
 *
 * <p>This class tries to handle <code>null</code> input gracefully.
 * An exception will not be thrown for a <code>null</code> input.
 * Each method documents its behaviour in more detail.</p>
 * 
 * @author Stephen Colebourne
 * @since 2.1
 * @version $Id: CharUtils.java 437554 2006-08-28 06:21:41Z bayard $
 */
public class Main {
  /**
   * <p>Checks whether the character is ASCII 7 bit alphabetic.</p>
   *
   * <pre>
   *   CharUtils.isAsciiAlpha("a")  = true
   *   CharUtils.isAsciiAlpha("A")  = true
   *   CharUtils.isAsciiAlpha("3")  = false
   *   CharUtils.isAsciiAlpha("-")  = false
   *   CharUtils.isAsciiAlpha("\n") = false
   *   CharUtils.isAsciiAlpha("&copy;") = false
   * </pre>
   * 
   * @param ch  the character to check
   * @return true if between 65 and 90 or 97 and 122 inclusive
   */
  public static boolean isAsciiAlpha(char ch) {
      return (ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z");
  }
}





Checks whether the character is ASCII 7 bit alphabetic lower case.

  
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * <p>Operations on char primitives and Character objects.</p>
 *
 * <p>This class tries to handle <code>null</code> input gracefully.
 * An exception will not be thrown for a <code>null</code> input.
 * Each method documents its behaviour in more detail.</p>
 * 
 * @author Stephen Colebourne
 * @since 2.1
 * @version $Id: CharUtils.java 437554 2006-08-28 06:21:41Z bayard $
 */
public class Main {
  
  /**
   * <p>Checks whether the character is ASCII 7 bit alphabetic lower case.</p>
   *
   * <pre>
   *   CharUtils.isAsciiAlphaLower("a")  = true
   *   CharUtils.isAsciiAlphaLower("A")  = false
   *   CharUtils.isAsciiAlphaLower("3")  = false
   *   CharUtils.isAsciiAlphaLower("-")  = false
   *   CharUtils.isAsciiAlphaLower("\n") = false
   *   CharUtils.isAsciiAlphaLower("&copy;") = false
   * </pre>
   * 
   * @param ch  the character to check
   * @return true if between 97 and 122 inclusive
   */
  public static boolean isAsciiAlphaLower(char ch) {
      return ch >= "a" && ch <= "z";
  }
}





Checks whether the character is ASCII 7 bit alphabetic upper case.

  
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * <p>Operations on char primitives and Character objects.</p>
 *
 * <p>This class tries to handle <code>null</code> input gracefully.
 * An exception will not be thrown for a <code>null</code> input.
 * Each method documents its behaviour in more detail.</p>
 * 
 * @author Stephen Colebourne
 * @since 2.1
 * @version $Id: CharUtils.java 437554 2006-08-28 06:21:41Z bayard $
 */
public class Main {
  /**
   * <p>Checks whether the character is ASCII 7 bit alphabetic upper case.</p>
   *
   * <pre>
   *   CharUtils.isAsciiAlphaUpper("a")  = false
   *   CharUtils.isAsciiAlphaUpper("A")  = true
   *   CharUtils.isAsciiAlphaUpper("3")  = false
   *   CharUtils.isAsciiAlphaUpper("-")  = false
   *   CharUtils.isAsciiAlphaUpper("\n") = false
   *   CharUtils.isAsciiAlphaUpper("&copy;") = false
   * </pre>
   * 
   * @param ch  the character to check
   * @return true if between 65 and 90 inclusive
   */
  public static boolean isAsciiAlphaUpper(char ch) {
      return ch >= "A" && ch <= "Z";
  }
}





Checks whether the character is ASCII 7 bit control.

  
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * <p>Operations on char primitives and Character objects.</p>
 *
 * <p>This class tries to handle <code>null</code> input gracefully.
 * An exception will not be thrown for a <code>null</code> input.
 * Each method documents its behaviour in more detail.</p>
 * 
 * @author Stephen Colebourne
 * @since 2.1
 * @version $Id: CharUtils.java 437554 2006-08-28 06:21:41Z bayard $
 */
public class Main {
  /**
   * <p>Checks whether the character is ASCII 7 bit control.</p>
   *
   * <pre>
   *   CharUtils.isAsciiControl("a")  = false
   *   CharUtils.isAsciiControl("A")  = false
   *   CharUtils.isAsciiControl("3")  = false
   *   CharUtils.isAsciiControl("-")  = false
   *   CharUtils.isAsciiControl("\n") = true
   *   CharUtils.isAsciiControl("&copy;") = false
   * </pre>
   * 
   * @param ch  the character to check
   * @return true if less than 32 or equals 127
   */
  public static boolean isAsciiControl(char ch) {
      return ch < 32 || ch == 127;
  }
}





Checks whether the character is ASCII 7 bit numeric.

  
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * <p>Operations on char primitives and Character objects.</p>
 *
 * <p>This class tries to handle <code>null</code> input gracefully.
 * An exception will not be thrown for a <code>null</code> input.
 * Each method documents its behaviour in more detail.</p>
 * 
 * @author Stephen Colebourne
 * @since 2.1
 * @version $Id: CharUtils.java 437554 2006-08-28 06:21:41Z bayard $
 */
public class Main {
  
  
  /**
   * <p>Checks whether the character is ASCII 7 bit numeric.</p>
   *
   * <pre>
   *   CharUtils.isAsciiNumeric("a")  = false
   *   CharUtils.isAsciiNumeric("A")  = false
   *   CharUtils.isAsciiNumeric("3")  = true
   *   CharUtils.isAsciiNumeric("-")  = false
   *   CharUtils.isAsciiNumeric("\n") = false
   *   CharUtils.isAsciiNumeric("&copy;") = false
   * </pre>
   * 
   * @param ch  the character to check
   * @return true if between 48 and 57 inclusive
   */
  public static boolean isAsciiNumeric(char ch) {
      return ch >= "0" && ch <= "9";
  }
}





Checks whether the character is ASCII 7 bit numeric and character.

  
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * <p>Operations on char primitives and Character objects.</p>
 *
 * <p>This class tries to handle <code>null</code> input gracefully.
 * An exception will not be thrown for a <code>null</code> input.
 * Each method documents its behaviour in more detail.</p>
 * 
 * @author Stephen Colebourne
 * @since 2.1
 * @version $Id: CharUtils.java 437554 2006-08-28 06:21:41Z bayard $
 */
public class Main {
  
  
  /**
   * <p>Checks whether the character is ASCII 7 bit numeric and character.</p>
   *
   * <pre>
   *   CharUtils.isAsciiAlphanumeric("a")  = true
   *   CharUtils.isAsciiAlphanumeric("A")  = true
   *   CharUtils.isAsciiAlphanumeric("3")  = true
   *   CharUtils.isAsciiAlphanumeric("-")  = false
   *   CharUtils.isAsciiAlphanumeric("\n") = false
   *   CharUtils.isAsciiAlphanumeric("&copy;") = false
   * </pre>
   * 
   * @param ch  the character to check
   * @return true if between 48 and 57 or 65 and 90 or 97 and 122 inclusive
   */
  public static boolean isAsciiAlphanumeric(char ch) {
      return (ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z") || (ch >= "0" && ch <= "9");
  }
}





Checks whether the character is ASCII 7 bit printable.

  
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * <p>Operations on char primitives and Character objects.</p>
 *
 * <p>This class tries to handle <code>null</code> input gracefully.
 * An exception will not be thrown for a <code>null</code> input.
 * Each method documents its behaviour in more detail.</p>
 * 
 * @author Stephen Colebourne
 * @since 2.1
 * @version $Id: CharUtils.java 437554 2006-08-28 06:21:41Z bayard $
 */
public class Main {
  
  /**
   * <p>Checks whether the character is ASCII 7 bit printable.</p>
   *
   * <pre>
   *   CharUtils.isAsciiPrintable("a")  = true
   *   CharUtils.isAsciiPrintable("A")  = true
   *   CharUtils.isAsciiPrintable("3")  = true
   *   CharUtils.isAsciiPrintable("-")  = true
   *   CharUtils.isAsciiPrintable("\n") = false
   *   CharUtils.isAsciiPrintable("&copy;") = false
   * </pre>
   * 
   * @param ch  the character to check
   * @return true if between 32 and 126 inclusive
   */
  public static boolean isAsciiPrintable(char ch) {
      return ch >= 32 && ch < 127;
  }
}





Get 7-bit ASCII character array from input String.

  
/*
 Derby - Class org.apache.derby.iapi.util.PropertyUtil
 Licensed to the Apache Software Foundation (ASF) under one or more
 contributor license agreements.  See the NOTICE file distributed with
 this work for additional information regarding copyright ownership.
 The ASF licenses this file to you under the Apache License, Version 2.0
 (the "License"); you may not use this file except in compliance with
 the License.  You may obtain a copy of the License at
 http://www.apache.org/licenses/LICENSE-2.0
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */
public class Main {

  /**
    Get 7-bit ASCII character array from input String.
    The lower 7 bits of each character in the input string is assumed to be
    the ASCII character value.
     Hexadecimal - Character
     | 00 NUL| 01 SOH| 02 STX| 03 ETX| 04 EOT| 05 ENQ| 06 ACK| 07 BEL|
     | 08 BS | 09 HT | 0A NL | 0B VT | 0C NP | 0D CR | 0E SO | 0F SI |
     | 10 DLE| 11 DC1| 12 DC2| 13 DC3| 14 DC4| 15 NAK| 16 SYN| 17 ETB|
     | 18 CAN| 19 EM | 1A SUB| 1B ESC| 1C FS | 1D GS | 1E RS | 1F US |
     | 20 SP | 21  ! | 22  " | 23  # | 24  $ | 25  % | 26  & | 27  " |
     | 28  ( | 29  ) | 2A  * | 2B  + | 2C  , | 2D  - | 2E  . | 2F  / |
     | 30  0 | 31  1 | 32  2 | 33  3 | 34  4 | 35  5 | 36  6 | 37  7 |
     | 38  8 | 39  9 | 3A  : | 3B  ; | 3C  < | 3D  = | 3E  > | 3F  ? |
     | 40  @ | 41  A | 42  B | 43  C | 44  D | 45  E | 46  F | 47  G |
     | 48  H | 49  I | 4A  J | 4B  K | 4C  L | 4D  M | 4E  N | 4F  O |
     | 50  P | 51  Q | 52  R | 53  S | 54  T | 55  U | 56  V | 57  W |
     | 58  X | 59  Y | 5A  Z | 5B  [ | 5C  \ | 5D  ] | 5E  ^ | 5F  _ |
     | 60  ` | 61  a | 62  b | 63  c | 64  d | 65  e | 66  f | 67  g |
     | 68  h | 69  i | 6A  j | 6B  k | 6C  l | 6D  m | 6E  n | 6F  o |
     | 70  p | 71  q | 72  r | 73  s | 74  t | 75  u | 76  v | 77  w |
     | 78  x | 79  y | 7A  z | 7B  { | 7C  | | 7D  } | 7E  ~ | 7F DEL|
   */
  public static byte[] getAsciiBytes(String input)
  {
    char[] c = input.toCharArray();
    byte[] b = new byte[c.length];
    for (int i = 0; i < c.length; i++)
      b[i] = (byte)(c[i] & 0x007F);
    return b;
  }

}





Removes control characters (char <= 32) from both ends returning an empty String ("") if the String is empty ("") after the trim or if it is null.

  
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.TimeZone;

/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


/**
 * <p>Operations on {@link java.lang.String} that are
 * <code>null</code> safe.</p>
 *
 * @see java.lang.String
 * @author 
 * @author Arun Mammen Thomas
 * @author Gary Gregory
 * @author Phil Steitz
 * @author Al Chou
 * @author Michael Davey
 * @author Reuben Sivan
 * @author Chris Hyzer
 * @author Scott Johnson
 * @since 1.0
 * @version $Id: StringUtils.java 635447 2008-03-10 06:27:09Z bayard $
 */
public class Main {
  /**
   * <p>Removes control characters (char &lt;= 32) from both
   * ends of this String returning an empty String ("") if the String
   * is empty ("") after the trim or if it is <code>null</code>.
   *
   * <p>The String is trimmed using {@link String#trim()}.
   * Trim removes start and end characters &lt;= 32.
   * To strip whitespace use {@link #stripToEmpty(String)}.</p>
   *
   * <pre>
   * StringUtils.trimToEmpty(null)          = ""
   * StringUtils.trimToEmpty("")            = ""
   * StringUtils.trimToEmpty("     ")       = ""
   * StringUtils.trimToEmpty("abc")         = "abc"
   * StringUtils.trimToEmpty("    abc    ") = "abc"
   * </pre>
   *
   * @param str  the String to be trimmed, may be null
   * @return the trimmed String, or an empty String if <code>null</code> input
   * @since 2.0
   */
  public static String trimToEmpty(String str) {
      return str == null ? "" : str.trim();
  }
}





Removes control characters (char <= 32) from both ends returning null if the String is empty ("") after the trim or if it is null

  
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.TimeZone;

/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


/**
 * <p>Operations on {@link java.lang.String} that are
 * <code>null</code> safe.</p>
 *
 * @see java.lang.String
 * @author 
 * @author Arun Mammen Thomas
 * @author Gary Gregory
 * @author Phil Steitz
 * @author Al Chou
 * @author Michael Davey
 * @author Reuben Sivan
 * @author Chris Hyzer
 * @author Scott Johnson
 * @since 1.0
 * @version $Id: StringUtils.java 635447 2008-03-10 06:27:09Z bayard $
 */
public class Main {

  /**
   * <p>Removes control characters (char &lt;= 32) from both
   * ends of this String, handling <code>null</code> by returning
   * <code>null</code>.</p>
   *
   * <p>The String is trimmed using {@link String#trim()}.
   * Trim removes start and end characters &lt;= 32.
   * To strip whitespace use {@link #strip(String)}.</p>
   *
   * <p>To trim your choice of characters, use the
   * {@link #strip(String, String)} methods.</p>
   *
   * <pre>
   * StringUtils.trim(null)          = null
   * StringUtils.trim("")            = ""
   * StringUtils.trim("     ")       = ""
   * StringUtils.trim("abc")         = "abc"
   * StringUtils.trim("    abc    ") = "abc"
   * </pre>
   *
   * @param str  the String to be trimmed, may be null
   * @return the trimmed string, <code>null</code> if null String input
   */
  public static String trim(String str) {
      return str == null ? null : str.trim();
  }
  /**
   * <p>Removes control characters (char &lt;= 32) from both
   * ends of this String returning <code>null</code> if the String is
   * empty ("") after the trim or if it is <code>null</code>.
   *
   * <p>The String is trimmed using {@link String#trim()}.
   * Trim removes start and end characters &lt;= 32.
   * To strip whitespace use {@link #stripToNull(String)}.</p>
   *
   * <pre>
   * StringUtils.trimToNull(null)          = null
   * StringUtils.trimToNull("")            = null
   * StringUtils.trimToNull("     ")       = null
   * StringUtils.trimToNull("abc")         = "abc"
   * StringUtils.trimToNull("    abc    ") = "abc"
   * </pre>
   *
   * @param str  the String to be trimmed, may be null
   * @return the trimmed String,
   *  <code>null</code> if only chars &lt;= 32, empty or null String input
   * @since 2.0
   */
  public static String trimToNull(String str) {
      String ts = trim(str);
      return isEmpty(ts) ? null : ts;
  }
  // Empty checks
  //-----------------------------------------------------------------------
  /**
   * <p>Checks if a String is empty ("") or null.</p>
   *
   * <pre>
   * StringUtils.isEmpty(null)      = true
   * StringUtils.isEmpty("")        = true
   * StringUtils.isEmpty(" ")       = false
   * StringUtils.isEmpty("bob")     = false
   * StringUtils.isEmpty("  bob  ") = false
   * </pre>
   *
   * <p>NOTE: This method changed in Lang version 2.0.
   * It no longer trims the String.
   * That functionality is available in isBlank().</p>
   *
   * @param str  the String to check, may be null
   * @return <code>true</code> if the String is empty or null
   */
  public static boolean isEmpty(String str) {
      return str == null || str.length() == 0;
  }
}





Removes spaces (char <= 32) from both start and ends of this bytes array

  
import java.io.File;
import java.io.FileFilter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *  
 *    http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License. 
 *  
 */

/**
 * Various string manipulation methods that are more efficient then chaining
 * string operations: all is done in the same buffer without creating a bunch of
 * string objects.
 * 
 * @author 
 */
public class Main {
  /**
   * <p>
   * Removes spaces (char &lt;= 32) from both start and ends of this bytes
   * array, handling <code>null</code> by returning <code>null</code>.
   * </p>
   * Trim removes start and end characters &lt;= 32.
   * 
   * <pre>
   *  StringUtils.trim(null)          = null
   *  StringUtils.trim(&quot;&quot;)            = &quot;&quot;
   *  StringUtils.trim(&quot;     &quot;)       = &quot;&quot;
   *  StringUtils.trim(&quot;abc&quot;)         = &quot;abc&quot;
   *  StringUtils.trim(&quot;    abc    &quot;) = &quot;abc&quot;
   * </pre>
   * 
   * @param bytes the byte array to be trimmed, may be null
   * 
   * @return the trimmed byte array
   */
  public static final byte[] trim( byte[] bytes )
  {
      if ( isEmpty( bytes ) )
      {
          return new byte[0];
      }
      int start = trimLeft( bytes, 0 );
      int end = trimRight( bytes, bytes.length - 1 );
      int length = end - start + 1;
      if ( length != 0 )
      {
          byte[] newBytes = new byte[end - start + 1];
          System.arraycopy( bytes, start, newBytes, 0, length );
          return newBytes;
      }
      else
      {
          return new byte[0];
      }
  }
  /**
   * <p>
   * Removes spaces (char &lt;= 32) from start of this array, handling
   * <code>null</code> by returning <code>null</code>.
   * </p>
   * Trim removes start characters &lt;= 32.
   * 
   * <pre>
   *  StringUtils.trimLeft(null)          = null
   *  StringUtils.trimLeft(&quot;&quot;)            = &quot;&quot;
   *  StringUtils.trimLeft(&quot;     &quot;)       = &quot;&quot;
   *  StringUtils.trimLeft(&quot;abc&quot;)         = &quot;abc&quot;
   *  StringUtils.trimLeft(&quot;    abc    &quot;) = &quot;abc    &quot;
   * </pre>
   * 
   * @param bytes
   *            the byte array to be trimmed, may be null
   * @return the position of the first byte which is not a space, or the last
   *         position of the array.
   */
  public static final int trimLeft( byte[] bytes, int pos )
  {
      if ( bytes == null )
      {
          return pos;
      }
      while ( ( pos < bytes.length ) && ( bytes[pos] == " " ) )
      {
          pos++;
      }
      return pos;
  }
  /**
   * <p>
   * Removes spaces (char &lt;= 32) from end of this array, handling
   * <code>null</code> by returning <code>null</code>.
   * </p>
   * Trim removes start characters &lt;= 32.
   * 
   * <pre>
   *  StringUtils.trimRight(null)          = null
   *  StringUtils.trimRight(&quot;&quot;)            = &quot;&quot;
   *  StringUtils.trimRight(&quot;     &quot;)       = &quot;&quot;
   *  StringUtils.trimRight(&quot;abc&quot;)         = &quot;abc&quot;
   *  StringUtils.trimRight(&quot;    abc    &quot;) = &quot;    abc&quot;
   * </pre>
   * 
   * @param bytes
   *            the chars array to be trimmed, may be null
   * @return the position of the first char which is not a space, or the last
   *         position of the array.
   */
  public static final int trimRight( byte[] bytes, int pos )
  {
      if ( bytes == null )
      {
          return pos;
      }
      while ( ( pos >= 0 ) && ( bytes[pos] == " " ) )
      {
          pos--;
      }
      return pos;
  }
  /**
   * Checks if a bytes array is empty or null.
   * 
   * @param bytes
   *            The bytes array to check, may be null
   * @return <code>true</code> if the bytes array is empty or null
   */
  public static final boolean isEmpty( byte[] bytes )
  {
      return bytes == null || bytes.length == 0;
  }
}





Removes spaces (char <= 32) from end of this array by index, handling null by returning null

  
/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *  
 *    http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License. 
 *  
 */

/**
 * Various string manipulation methods that are more efficient then chaining
 * string operations: all is done in the same buffer without creating a bunch of
 * string objects.
 * 
 * @author 
 */
public class Main {
  /**
   * <p>
   * Removes spaces (char &lt;= 32) from end of this array, handling
   * <code>null</code> by returning <code>null</code>.
   * </p>
   * Trim removes start characters &lt;= 32.
   * 
   * <pre>
   *  StringUtils.trimRight(null)          = null
   *  StringUtils.trimRight(&quot;&quot;)            = &quot;&quot;
   *  StringUtils.trimRight(&quot;     &quot;)       = &quot;&quot;
   *  StringUtils.trimRight(&quot;abc&quot;)         = &quot;abc&quot;
   *  StringUtils.trimRight(&quot;    abc    &quot;) = &quot;    abc&quot;
   * </pre>
   * 
   * @param bytes
   *            the chars array to be trimmed, may be null
   * @return the position of the first char which is not a space, or the last
   *         position of the array.
   */
  public static final int trimRight( byte[] bytes, int pos )
  {
      if ( bytes == null )
      {
          return pos;
      }
      while ( ( pos >= 0 ) && ( bytes[pos] == " " ) )
      {
          pos--;
      }
      return pos;
  }
}





Removes spaces (char <= 32) from end of this array, handling null by returning null

  
/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *  
 *    http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License. 
 *  
 */

/**
 * Various string manipulation methods that are more efficient then chaining
 * string operations: all is done in the same buffer without creating a bunch of
 * string objects.
 * 
 * @author 
 */
public class Main {
  /**
   * <p>
   * Removes spaces (char &lt;= 32) from end of this array, handling
   * <code>null</code> by returning <code>null</code>.
   * </p>
   * Trim removes start characters &lt;= 32.
   * 
   * <pre>
   *  StringUtils.trimRight(null)          = null
   *  StringUtils.trimRight(&quot;&quot;)            = &quot;&quot;
   *  StringUtils.trimRight(&quot;     &quot;)       = &quot;&quot;
   *  StringUtils.trimRight(&quot;abc&quot;)         = &quot;abc&quot;
   *  StringUtils.trimRight(&quot;    abc    &quot;) = &quot;    abc&quot;
   * </pre>
   * 
   * @param bytes
   *            the chars array to be trimmed, may be null
   * @return the position of the first char which is not a space, or the last
   *         position of the array.
   */
  public static final int trimRight( byte[] bytes, int pos )
  {
      if ( bytes == null )
      {
          return pos;
      }
      while ( ( pos >= 0 ) && ( bytes[pos] == " " ) )
      {
          pos--;
      }
      return pos;
  }
  /**
   * Checks if a bytes array is empty or null.
   * 
   * @param bytes
   *            The bytes array to check, may be null
   * @return <code>true</code> if the bytes array is empty or null
   */
  public static final boolean isEmpty( byte[] bytes )
  {
      return bytes == null || bytes.length == 0;
  }
}





Removes spaces (char <= 32) from end of this array ny position, handling null by returning null

  

/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *  
 *    http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License. 
 *  
 */

/**
 * Various string manipulation methods that are more efficient then chaining
 * string operations: all is done in the same buffer without creating a bunch of
 * string objects.
 * 
 * @author 
 */
public class Main {
  /**
   * <p>
   * Removes spaces (char &lt;= 32) from end of this array, handling
   * <code>null</code> by returning <code>null</code>.
   * </p>
   * Trim removes start characters &lt;= 32.
   * 
   * <pre>
   *  StringUtils.trimRight(null)          = null
   *  StringUtils.trimRight(&quot;&quot;)            = &quot;&quot;
   *  StringUtils.trimRight(&quot;     &quot;)       = &quot;&quot;
   *  StringUtils.trimRight(&quot;abc&quot;)         = &quot;abc&quot;
   *  StringUtils.trimRight(&quot;    abc    &quot;) = &quot;    abc&quot;
   * </pre>
   * 
   * @param chars
   *            the chars array to be trimmed, may be null
   * @return the position of the first char which is not a space, or the last
   *         position of the array.
   */
  public static final int trimRight( char[] chars, int pos )
  {
      if ( chars == null )
      {
          return pos;
      }
      while ( ( pos >= 0 ) && ( chars[pos - 1] == " " ) )
      {
          pos--;
      }
      return pos;
  }
}





Removes spaces (char <= 32) from end of this String, handling null by returning null

  
/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *  
 *    http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License. 
 *  
 */

/**
 * Various string manipulation methods that are more efficient then chaining
 * string operations: all is done in the same buffer without creating a bunch of
 * string objects.
 * 
 * @author 
 */
public class Main {
  /**
   * <p>
   * Removes spaces (char &lt;= 32) from end of this String, handling
   * <code>null</code> by returning <code>null</code>.
   * </p>
   * Trim removes start characters &lt;= 32.
   * 
   * <pre>
   *  StringUtils.trimRight(null)          = null
   *  StringUtils.trimRight(&quot;&quot;)            = &quot;&quot;
   *  StringUtils.trimRight(&quot;     &quot;)       = &quot;&quot;
   *  StringUtils.trimRight(&quot;abc&quot;)         = &quot;abc&quot;
   *  StringUtils.trimRight(&quot;    abc    &quot;) = &quot;    abc&quot;
   * </pre>
   * 
   * @param str
   *            the String to be trimmed, may be null
   * @return the trimmed string, <code>null</code> if null String input
   */
  public static final String trimRight( String str )
  {
      if ( isEmpty( str ) )
      {
          return "";
      }
      int length = str.length();
      int end = length;
      
      while ( ( end > 0 ) && ( str.charAt( end - 1 ) == " " ) )
      {
          if ( ( end > 1 ) && ( str.charAt(  end - 2 ) == "\\" ) )
          {
              break;
          }
          
          end--;
      }
      return ( end == length ? str : str.substring( 0, end ) );
  }
  /**
   * <p>
   * Removes spaces (char &lt;= 32) from start of this String, handling
   * <code>null</code> by returning <code>null</code>.
   * </p>
   * Trim removes start characters &lt;= 32.
   * 
   * <pre>
   *  StringUtils.trimLeft(null)          = null
   *  StringUtils.trimLeft(&quot;&quot;)            = &quot;&quot;
   *  StringUtils.trimLeft(&quot;     &quot;)       = &quot;&quot;
   *  StringUtils.trimLeft(&quot;abc&quot;)         = &quot;abc&quot;
   *  StringUtils.trimLeft(&quot;    abc    &quot;) = &quot;abc    &quot;
   * </pre>
   * 
   * @param str
   *            the String to be trimmed, may be null
   * @return the trimmed string, <code>null</code> if null String input
   */
  public static final String trimLeft( String str )
  {
      if ( isEmpty( str ) )
      {
          return "";
      }
      int start = 0;
      int end = str.length();
      
      while ( ( start < end ) && ( str.charAt( start ) == " " ) )
      {
          start++;
      }
      return ( start == 0 ? str : str.substring( start ) );
  }
  /**
   * <p>
   * Checks if a String is empty ("") or null.
   * </p>
   * 
   * <pre>
   *  StringUtils.isEmpty(null)      = true
   *  StringUtils.isEmpty(&quot;&quot;)        = true
   *  StringUtils.isEmpty(&quot; &quot;)       = false
   *  StringUtils.isEmpty(&quot;bob&quot;)     = false
   *  StringUtils.isEmpty(&quot;  bob  &quot;) = false
   * </pre>
   * 
   * <p>
   * NOTE: This method changed in Lang version 2.0. It no longer trims the
   * String. That functionality is available in isBlank().
   * </p>
   * 
   * @param str
   *            the String to check, may be null
   * @return <code>true</code> if the String is empty or null
   */
  public static final boolean isEmpty( String str )
  {
      return str == null || str.length() == 0;
  }
}





Removes spaces (char <= 32) from end of this String with escape, handling null by returning null

  

/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *  
 *    http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License. 
 *  
 */

/**
 * Various string manipulation methods that are more efficient then chaining
 * string operations: all is done in the same buffer without creating a bunch of
 * string objects.
 * 
 * @author 
 */
public class Main {
  /**
   * <p>
   * Removes spaces (char &lt;= 32) from end of this String, handling
   * <code>null</code> by returning <code>null</code>.
   * </p>
   * Trim removes start characters &lt;= 32.
   * 
   * <pre>
   *  StringUtils.trimRight(null)          = null
   *  StringUtils.trimRight(&quot;&quot;)            = &quot;&quot;
   *  StringUtils.trimRight(&quot;     &quot;)       = &quot;&quot;
   *  StringUtils.trimRight(&quot;abc&quot;)         = &quot;abc&quot;
   *  StringUtils.trimRight(&quot;    abc    &quot;) = &quot;    abc&quot;
   * </pre>
   * 
   * @param str the String to be trimmed, may be null
   * @param escapedSpace The last escaped space, if any
   * @return the trimmed string, <code>null</code> if null String input
   */
  public static final String trimRight( String str, int escapedSpace )
  {
      if ( isEmpty( str ) )
      {
          return "";
      }
      int length = str.length();
      int end = length;
      
      while ( ( end > 0 ) && ( str.charAt( end - 1 ) == " " ) && ( end > escapedSpace ) )
      {
          if ( ( end > 1 ) && ( str.charAt(  end - 2 ) == "\\" ) )
          {
              break;
          }
          
          end--;
      }
      return ( end == length ? str : str.substring( 0, end ) );
  }
  /**
   * <p>
   * Checks if a String is empty ("") or null.
   * </p>
   * 
   * <pre>
   *  StringUtils.isEmpty(null)      = true
   *  StringUtils.isEmpty(&quot;&quot;)        = true
   *  StringUtils.isEmpty(&quot; &quot;)       = false
   *  StringUtils.isEmpty(&quot;bob&quot;)     = false
   *  StringUtils.isEmpty(&quot;  bob  &quot;) = false
   * </pre>
   * 
   * <p>
   * NOTE: This method changed in Lang version 2.0. It no longer trims the
   * String. That functionality is available in isBlank().
   * </p>
   * 
   * @param str
   *            the String to check, may be null
   * @return <code>true</code> if the String is empty or null
   */
  public static final boolean isEmpty( String str )
  {
      return str == null || str.length() == 0;
  }
}





Removes spaces (char <= 32) from start of this array, handling null by returning null

  
/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *  
 *    http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License. 
 *  
 */

/**
 * Various string manipulation methods that are more efficient then chaining
 * string operations: all is done in the same buffer without creating a bunch of
 * string objects.
 * 
 * @author 
 */
public class Main {
  /**
   * <p>
   * Removes spaces (char &lt;= 32) from start of this array, handling
   * <code>null</code> by returning <code>null</code>.
   * </p>
   * Trim removes start characters &lt;= 32.
   * 
   * <pre>
   *  StringUtils.trimLeft(null)          = null
   *  StringUtils.trimLeft(&quot;&quot;)            = &quot;&quot;
   *  StringUtils.trimLeft(&quot;     &quot;)       = &quot;&quot;
   *  StringUtils.trimLeft(&quot;abc&quot;)         = &quot;abc&quot;
   *  StringUtils.trimLeft(&quot;    abc    &quot;) = &quot;abc    &quot;
   * </pre>
   * 
   * @param bytes
   *            the byte array to be trimmed, may be null
   * @return the position of the first byte which is not a space, or the last
   *         position of the array.
   */
  public static final int trimLeft( byte[] bytes, int pos )
  {
      if ( bytes == null )
      {
          return pos;
      }
      while ( ( pos < bytes.length ) && ( bytes[pos] == " " ) )
      {
          pos++;
      }
      return pos;
  }
  /**
   * <p>
   * Removes spaces (char &lt;= 32) from end of this array, handling
   * <code>null</code> by returning <code>null</code>.
   * </p>
   * Trim removes start characters &lt;= 32.
   * 
   * <pre>
   *  StringUtils.trimRight(null)          = null
   *  StringUtils.trimRight(&quot;&quot;)            = &quot;&quot;
   *  StringUtils.trimRight(&quot;     &quot;)       = &quot;&quot;
   *  StringUtils.trimRight(&quot;abc&quot;)         = &quot;abc&quot;
   *  StringUtils.trimRight(&quot;    abc    &quot;) = &quot;    abc&quot;
   * </pre>
   * 
   * @param bytes
   *            the chars array to be trimmed, may be null
   * @return the position of the first char which is not a space, or the last
   *         position of the array.
   */
  public static final int trimRight( byte[] bytes, int pos )
  {
      if ( bytes == null )
      {
          return pos;
      }
      while ( ( pos >= 0 ) && ( bytes[pos] == " " ) )
      {
          pos--;
      }
      return pos;
  }
  /**
   * Checks if a bytes array is empty or null.
   * 
   * @param bytes
   *            The bytes array to check, may be null
   * @return <code>true</code> if the bytes array is empty or null
   */
  public static final boolean isEmpty( byte[] bytes )
  {
      return bytes == null || bytes.length == 0;
  }
}





Removes spaces (char <= 32) from start of this String, handling null by returning null

  
/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *  
 *    http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License. 
 *  
 */

/**
 * Various string manipulation methods that are more efficient then chaining
 * string operations: all is done in the same buffer without creating a bunch of
 * string objects.
 * 
 * @author 
 */
public class Main {
  /**
   * <p>
   * Removes spaces (char &lt;= 32) from start of this String, handling
   * <code>null</code> by returning <code>null</code>.
   * </p>
   * Trim removes start characters &lt;= 32.
   * 
   * <pre>
   *  StringUtils.trimLeft(null)          = null
   *  StringUtils.trimLeft(&quot;&quot;)            = &quot;&quot;
   *  StringUtils.trimLeft(&quot;     &quot;)       = &quot;&quot;
   *  StringUtils.trimLeft(&quot;abc&quot;)         = &quot;abc&quot;
   *  StringUtils.trimLeft(&quot;    abc    &quot;) = &quot;abc    &quot;
   * </pre>
   * 
   * @param str
   *            the String to be trimmed, may be null
   * @return the trimmed string, <code>null</code> if null String input
   */
  public static final String trimLeft( String str )
  {
      if ( isEmpty( str ) )
      {
          return "";
      }
      int start = 0;
      int end = str.length();
      
      while ( ( start < end ) && ( str.charAt( start ) == " " ) )
      {
          start++;
      }
      return ( start == 0 ? str : str.substring( start ) );
  }
  /**
   * <p>
   * Checks if a String is empty ("") or null.
   * </p>
   * 
   * <pre>
   *  StringUtils.isEmpty(null)      = true
   *  StringUtils.isEmpty(&quot;&quot;)        = true
   *  StringUtils.isEmpty(&quot; &quot;)       = false
   *  StringUtils.isEmpty(&quot;bob&quot;)     = false
   *  StringUtils.isEmpty(&quot;  bob  &quot;) = false
   * </pre>
   * 
   * <p>
   * NOTE: This method changed in Lang version 2.0. It no longer trims the
   * String. That functionality is available in isBlank().
   * </p>
   * 
   * @param str
   *            the String to check, may be null
   * @return <code>true</code> if the String is empty or null
   */
  public static final boolean isEmpty( String str )
  {
      return str == null || str.length() == 0;
  }
}





Test if the character is a digit <digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"

  
import java.io.File;
import java.io.FileFilter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *  
 *    http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License. 
 *  
 */

/**
 * Various string manipulation methods that are more efficient then chaining
 * string operations: all is done in the same buffer without creating a bunch of
 * string objects.
 * 
 * @author 
 */
public class Main {
  /** "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" */
  private static final boolean[] DIGIT =
      { 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          true,  true,  true,  true,  true,  true,  true,  true, 
          true,  true,  false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false
      };

  /**
   * Test if the character is a digit &lt;digit> ::= "0" | "1" | "2" |
   * "3" | "4" | "5" | "6" | "7" | "8" | "9"
   * 
   * @param chars
   *            The buffer which contains the data
   * @return <code>true</code> if the current character is a Digit
   */
  public static final boolean isDigit( char[] chars )
  {
      if ( ( chars == null ) || ( chars.length == 0 ) )
      {
          return false;
      }
      else
      {
          return ( ( ( ( chars[0] | 0x7F ) != 0x7F ) || !DIGIT[chars[0]] ) ? false : true );
      }
  }
}





Test if the current character is a digit <digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"

  
import java.io.File;
import java.io.FileFilter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *  
 *    http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License. 
 *  
 */

/**
 * Various string manipulation methods that are more efficient then chaining
 * string operations: all is done in the same buffer without creating a bunch of
 * string objects.
 * 
 * @author 
 */
public class Main {
  /** "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" */
  private static final boolean[] DIGIT =
      { 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          true,  true,  true,  true,  true,  true,  true,  true, 
          true,  true,  false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false
      };
  /**
   * Test if the current character is a digit &lt;digit> ::= "0" | "1" | "2" |
   * "3" | "4" | "5" | "6" | "7" | "8" | "9"
   * 
   * @param byteArray
   *            The buffer which contains the data
   * @return <code>true</code> if the current character is a Digit
   */
  public static final boolean isDigit( byte[] byteArray )
  {
      if ( ( byteArray == null ) || ( byteArray.length == 0 ) )
      {
          return false;
      }
      else
      {
          return ( ( ( ( byteArray[0] | 0x7F ) != 0x7F ) || !DIGIT[byteArray[0]] ) ? false : true );
      }
  }
}





Test if the current character is an Alpha character : <alpha> ::= [0x41-0x5A] | [0x61-0x7A]

  
import java.io.File;
import java.io.FileFilter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *  
 *    http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License. 
 *  
 */

/**
 * Various string manipulation methods that are more efficient then chaining
 * string operations: all is done in the same buffer without creating a bunch of
 * string objects.
 * 
 * @author 
 */
public class Main {
  /** &lt;alpha> ::= [0x41-0x5A] | [0x61-0x7A] */
  private static final boolean[] ALPHA =
      { 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, true,  true,  true,  true,  true,  true,  true, 
          true,  true,  true,  true,  true,  true,  true,  true, 
          true,  true,  true,  true,  true,  true,  true,  true, 
          true,  true,  true,  false, false, false, false, false, 
          false, true,  true,  true,  true,  true,  true,  true, 
          true,  true,  true,  true,  true,  true,  true,  true, 
          true,  true,  true,  true,  true,  true,  true,  true, 
          true,  true,  true,  false, false, false, false, false 
      };
  /**
   * Test if the current character is an Alpha character : &lt;alpha> ::=
   * [0x41-0x5A] | [0x61-0x7A]
   * 
   * @param byteArray
   *            The buffer which contains the data
   * @param index
   *            Current position in the buffer
   * @return <code>true</code> if the current character is an Alpha
   *         character
   */
  public static final boolean isAlphaASCII( byte[] byteArray, int index )
  {
      if ( ( byteArray == null ) || ( byteArray.length == 0 ) || ( index < 0 ) || ( index >= byteArray.length ) )
      {
          return false;
      }
      else
      {
          byte c = byteArray[index++];
          if ( ( ( c | 0x7F ) != 0x7F ) || ( ALPHA[c] == false ) )
          {
              return false;
          }
          else
          {
              return true;
          }
      }
  }
}





Test if the current character is a new line.

  
import java.io.File;
import java.io.FileFilter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *  
 *    http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License. 
 *  
 */

/**
 * Various string manipulation methods that are more efficient then chaining
 * string operations: all is done in the same buffer without creating a bunch of
 * string objects.
 * 
 * @author 
 */
public class Main {
  /**
   * Test if the current character is a new line.
   * 
   * @param charArray The buffer which contains the data
   * @param pos Position in the buffer
   *
   * @return <code>true</code> if the current character is a newLine.
   */
  public static final boolean isNL( char[] charArray, int index )
  {
      if ( ( charArray == null ) || ( charArray.length == 0 ) || ( index < 0 ) || ( index >= charArray.length ) )
      {
          return false;
      }
      else
      {
          return ( ( ( charArray[ index ] == "\n" ) || 
                   ( charArray[ index ] == "\r" ) )  ? true : false );
      }
  }
}





Test if the current character is an Ident character

  
import java.io.File;
import java.io.FileFilter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *  
 *    http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License. 
 *  
 */

/**
 * Various string manipulation methods that are more efficient then chaining
 * string operations: all is done in the same buffer without creating a bunch of
 * string objects.
 * 
 * @author 
 */
public class Main {
    /** &lt;alpha> ::= [0x41-0x5A] | [0x61-0x7A] | "_"*/
    private static final boolean[] IDENT =
        { 
            false, false, false, false, false, false, false, false, 
            false, false, false, false, false, false, false, false, 
            false, false, false, false, false, false, false, false, 
            false, false, false, false, false, false, false, false, 
            false, false, false, false, false, false, false, false, 
            false, false, false, false, false, false, false, false, 
            false, false, false, false, false, false, false, false, 
            false, false, false, false, false, false, false, false, 
            false, true,  true,  true,  true,  true,  true,  true, 
            true,  true,  true,  true,  true,  true,  true,  true, 
            true,  true,  true,  true,  true,  true,  true,  true, 
            true,  true,  true,  false, false, false, false, true, 
            false, true,  true,  true,  true,  true,  true,  true, 
            true,  true,  true,  true,  true,  true,  true,  true, 
            true,  true,  true,  true,  true,  true,  true,  true, 
            true,  true,  true,  false, false, false, false, false 
        };
  /**
   * Test if the current character is an Ident character : 
   * 
   * &lt;alpha> ::= [0x41-0x5A] | [0x61-0x7A] | "_"
   * 
   * @param chars
   *            The buffer which contains the data
   * @param index
   *            Current position in the buffer
   * @return <code>true</code> if the current character is an IDENT
   *         character
   */
  public static final boolean isIdentChar( char[] chars, int index )
  {
      if ( ( chars == null ) || ( chars.length == 0 ) || ( index < 0 ) || ( index >= chars.length ) )
      {
          return false;
      }
      else
      {
          char c = chars[index++];
          if ( ( ( c | 0x7F ) != 0x7F ) || ( IDENT[c] == false ) )
          {
              return false;
          }
          else
          {
              return true;
          }
      }
  }
}





Test if the current character is a space.

  
import java.io.File;
import java.io.FileFilter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *  
 *    http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License. 
 *  
 */

/**
 * Various string manipulation methods that are more efficient then chaining
 * string operations: all is done in the same buffer without creating a bunch of
 * string objects.
 * 
 * @author 
 */
public class Main {
  /**
   * Test if the current character is a space.
   * 
   * @param charArray The buffer which contains the data
   * @param pos Position in the buffer
   *
   * @return <code>true</code> if the current character is a space.
   */
  public static final boolean isSpace( char[] charArray, int index )
  {
      if ( ( charArray == null ) || ( charArray.length == 0 ) || ( index < 0 ) || ( index >= charArray.length ) )
      {
          return false;
      }
      else
      {
          return ( Character.isWhitespace( charArray[ index ] ) ? true : false );
      }
  }
}





Test if the current character is equal to a specific character.

  
import java.io.File;
import java.io.FileFilter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *  
 *    http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License. 
 *  
 */

/**
 * Various string manipulation methods that are more efficient then chaining
 * string operations: all is done in the same buffer without creating a bunch of
 * string objects.
 * 
 * @author 
 */
public class Main {
  /**
   * Test if the current character is equal to a specific character. This
   * function works only for character between 0 and 127, as it does compare a
   * byte and a char (which is 16 bits wide)
   * 
   * @param byteArray
   *            The buffer which contains the data
   * @param index
   *            Current position in the buffer
   * @param car
   *            The character we want to compare with the current buffer
   *            position
   * @return <code>true</code> if the current character equals the given
   *         character.
   */
  public static final boolean isCharASCII( byte[] byteArray, int index, char car )
  {
      if ( ( byteArray == null ) || ( byteArray.length == 0 ) || ( index < 0 ) || ( index >= byteArray.length ) )
      {
          return false;
      }
      else
      {
          return ( ( byteArray[index] == car ) ? true : false );
      }
  }
}





Test if the current string is a digit <digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"

  
import java.io.File;
import java.io.FileFilter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *  
 *    http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License. 
 *  
 */

/**
 * Various string manipulation methods that are more efficient then chaining
 * string operations: all is done in the same buffer without creating a bunch of
 * string objects.
 * 
 * @author 
 */
public class Main {
  /** "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" */
  private static final boolean[] DIGIT =
      { 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          true,  true,  true,  true,  true,  true,  true,  true, 
          true,  true,  false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false, 
          false, false, false, false, false, false, false, false
      };

  /**
   * Test if the current string is a digit &lt;digit> ::= "0" | "1" | "2" |
   * "3" | "4" | "5" | "6" | "7" | "8" | "9"
   * 
   * @param string
   *            The string which contains the data
   * @param index
   *            Current position in the string
   * @return <code>true</code> if the current character is a Digit
   */
  public static final boolean isDigit( String string, int index )
  {
      if ( string == null )
      {
          return false;
      }
      
      int length = string.length();
      
      if ( ( length == 0 ) || ( index < 0 ) || ( index >= length ) )
      {
          return false;
      }
      else
      {
          char c = string.charAt(  index  );
          return ( ( ( ( c | 0x7F ) != 0x7F ) || !DIGIT[c] ) ? false : true );
      }
  }
}





Thansform an array of ASCII bytes to a string. the byte array should contains only values in [0, 127].

  
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *  
 *    http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License. 
 *  
 */

/**
 * Various string manipulation methods that are more efficient then chaining
 * string operations: all is done in the same buffer without creating a bunch of
 * string objects.
 * 
 * @author 
 */
public class Main {
  /**
   * Thansform an array of ASCII bytes to a string. the byte array should contains
   * only values in [0, 127].
   * 
   * @param bytes The byte array to transform
   * @return The resulting string
   */
  public static String asciiBytesToString( byte[] bytes )
  {
      if ( (bytes == null) || (bytes.length == 0 ) )
      {
          return "";
      }
      
      char[] result = new char[bytes.length];
      
      for ( int i = 0; i < bytes.length; i++ )
      {
          result[i] = (char)bytes[i];
      }
      
      return new String( result );
  }
}