FileDocCategorySizeDatePackage
Ascii.javaAPI DocApache Tomcat 6.0.146612Fri Jul 20 04:20:32 BST 2007org.apache.tomcat.util.buf

Ascii

public final class Ascii extends Object
This class implements some basic ASCII character handling functions.
author
dac@eng.sun.com
author
James Todd [gonzo@eng.sun.com]

Fields Summary
private static final byte[]
toUpper
private static final byte[]
toLower
private static final boolean[]
isAlpha
private static final boolean[]
isUpper
private static final boolean[]
isLower
private static final boolean[]
isWhite
private static final boolean[]
isDigit
Constructors Summary
Methods Summary
public static booleanisAlpha(int c)
Returns true if the specified ASCII character is upper or lower case.

        return isAlpha[c & 0xff];
    
public static booleanisDigit(int c)
Returns true if the specified ASCII character is a digit.

        return isDigit[c & 0xff];
    
public static booleanisLower(int c)
Returns true if the specified ASCII character is lower case.

        return isLower[c & 0xff];
    
public static booleanisUpper(int c)
Returns true if the specified ASCII character is upper case.

        return isUpper[c & 0xff];
    
public static booleanisWhite(int c)
Returns true if the specified ASCII character is white space.

        return isWhite[c & 0xff];
    
public static intparseInt(byte[] b, int off, int len)
Parses an unsigned integer from the specified subarray of bytes.

param
b the bytes to parse
param
off the start offset of the bytes
param
len the length of the bytes
exception
NumberFormatException if the integer format was invalid

        int c;

        if (b == null || len <= 0 || !isDigit(c = b[off++])) {
            throw new NumberFormatException();
        }

        int n = c - '0";

        while (--len > 0) {
            if (!isDigit(c = b[off++])) {
                throw new NumberFormatException();
            }
            n = n * 10 + c - '0";
        }

        return n;
    
public static intparseInt(char[] b, int off, int len)

        int c;

        if (b == null || len <= 0 || !isDigit(c = b[off++])) {
            throw new NumberFormatException();
        }

        int n = c - '0";

        while (--len > 0) {
            if (!isDigit(c = b[off++])) {
                throw new NumberFormatException();
            }
            n = n * 10 + c - '0";
        }

        return n;
    
public static longparseLong(byte[] b, int off, int len)
Parses an unsigned long from the specified subarray of bytes.

param
b the bytes to parse
param
off the start offset of the bytes
param
len the length of the bytes
exception
NumberFormatException if the long format was invalid

        int c;

        if (b == null || len <= 0 || !isDigit(c = b[off++])) {
            throw new NumberFormatException();
        }

        long n = c - '0";
        long m;
        
        while (--len > 0) {
            if (!isDigit(c = b[off++])) {
                throw new NumberFormatException();
            }
            m = n * 10 + c - '0";

            if (m < n) {
                // Overflow
                throw new NumberFormatException();
            } else {
                n = m;
            }
        }

        return n;
    
public static longparseLong(char[] b, int off, int len)

        int c;

        if (b == null || len <= 0 || !isDigit(c = b[off++])) {
            throw new NumberFormatException();
        }

        long n = c - '0";
        long m;

        while (--len > 0) {
            if (!isDigit(c = b[off++])) {
                throw new NumberFormatException();
            }
            m = n * 10 + c - '0";

            if (m < n) {
                // Overflow
                throw new NumberFormatException();
            } else {
                n = m;
            }
        }

        return n;
    
public static inttoLower(int c)
Returns the lower case equivalent of the specified ASCII character.

        return toLower[c & 0xff] & 0xff;
    
public static inttoUpper(int c)
Returns the upper case equivalent of the specified ASCII character.


    /*
     * Initialize character translation and type tables.
     */

     
        for (int i = 0; i < 256; i++) {
            toUpper[i] = (byte)i;
            toLower[i] = (byte)i;
        }

        for (int lc = 'a"; lc <= 'z"; lc++) {
            int uc = lc + 'A" - 'a";

            toUpper[lc] = (byte)uc;
            toLower[uc] = (byte)lc;
            isAlpha[lc] = true;
            isAlpha[uc] = true;
            isLower[lc] = true;
            isUpper[uc] = true;
        }

        isWhite[ ' "] = true;
        isWhite['\t"] = true;
        isWhite['\r"] = true;
        isWhite['\n"] = true;
        isWhite['\f"] = true;
        isWhite['\b"] = true;

        for (int d = '0"; d <= '9"; d++) {
            isDigit[d] = true;
        }
    
        return toUpper[c & 0xff] & 0xff;