Example usage for java.lang CharSequence charAt

List of usage examples for java.lang CharSequence charAt

Introduction

In this page you can find the example usage for java.lang CharSequence charAt.

Prototype

char charAt(int index);

Source Link

Document

Returns the char value at the specified index.

Usage

From source file:be.milieuinfo.core.proxy.controller.ProxyServlet.java

/**
 * <p>Encodes characters in the query or fragment part of the URI.
 *
 * <p>Unfortunately, an incoming URI sometimes has characters disallowed by the spec.  HttpClient
 * insists that the outgoing proxied request has a valid URI because it uses Java's {@link URI}. To be more
 * forgiving, we must escape the problematic characters.  See the URI class for the spec.
 *
 * @param in example: name=value&foo=bar#fragment
 *//* www .  ja  v a2  s.c o m*/
static CharSequence encodeUriQuery(CharSequence in) {
    //Note that I can't simply use URI.java to encode because it will escape pre-existing escaped things.
    StringBuilder outBuf = null;
    Formatter formatter = null;
    for (int i = 0; i < in.length(); i++) {
        char c = in.charAt(i);
        boolean escape = true;
        if (c < 128) {
            if (asciiQueryChars.get((int) c)) {
                escape = false;
            }
        } else if (!Character.isISOControl(c) && !Character.isSpaceChar(c)) {//not-ascii
            escape = false;
        }
        if (!escape) {
            if (outBuf != null)
                outBuf.append(c);
        } else {
            //escape
            if (outBuf == null) {
                outBuf = new StringBuilder(in.length() + 5 * 3);
                outBuf.append(in, 0, i);
                formatter = new Formatter(outBuf);
            }
            //leading %, 0 padded, width 2, capital hex
            formatter.format("%%%02X", (int) c);//TODO
        }
    }
    return outBuf != null ? outBuf : in;
}

From source file:com.tecapro.inventory.common.util.StringUtil.java

public static boolean isMobileNumber(final CharSequence cs) {

    final int seqLength = cs.length();

    if (seqLength == 0) {
        return false;
    }//from w w w.ja va2  s .c  o  m

    boolean hasMobiPrefix = false;
    char firstChar = cs.charAt(0);

    if (cs.charAt(0) == '8' && cs.charAt(1) == '4') { // two first digits are country code
        firstChar = cs.charAt(2);
    }

    for (int i = 0; i < Constants.MOBI_PREFIX.length; i++) {
        if (firstChar == Constants.MOBI_PREFIX[i]) {
            hasMobiPrefix = true;
            break;
        }
    }

    if (!hasMobiPrefix) {
        return false;
    }

    for (int i = 1; i < seqLength; i++) {
        if (!Character.isDigit(cs.charAt(i))) {
            return false;
        }
    }

    return true;
}

From source file:org.realityforge.proxy_servlet.AbstractProxyServlet.java

/**
 * Encodes characters in the query or fragment part of the URI.
 * <p/>/*  w  w  w .  j  a  v  a 2s  . c om*/
 * <p>Unfortunately, an incoming URI sometimes has characters disallowed by the spec.  HttpClient
 * insists that the outgoing proxied request has a valid URI because it uses Java's {@link java.net.URI}.
 * To be more forgiving, we must escape the problematic characters.  See the URI class for the
 * spec.
 *
 * @param in example: name=value&foo=bar#fragment
 */
private static CharSequence encodeUriQuery(final CharSequence in) {
    //Note that I can't simply use URI.java to encode because it will escape pre-existing escaped things.
    StringBuilder sb = null;
    Formatter formatter = null;
    for (int i = 0; i < in.length(); i++) {
        char c = in.charAt(i);
        boolean escape = true;
        if (c < MAX_ASCII_VALUE) {
            if (ASCII_QUERY_CHARS.get((int) c)) {
                escape = false;
            }
        } else if (!Character.isISOControl(c) && !Character.isSpaceChar(c)) {
            //not-ascii
            escape = false;
        }
        if (!escape) {
            if (null != sb) {
                sb.append(c);
            }
        } else {
            //escape
            if (null == sb) {
                final int formatLength = 5 * 3;
                sb = new StringBuilder(in.length() + formatLength);
                sb.append(in, 0, i);
                formatter = new Formatter(sb);
            }
            //leading %, 0 padded, width 2, capital hex
            formatter.format("%%%02X", (int) c);
        }
    }
    return sb != null ? sb : in;
}

From source file:uk.ac.ebi.phenotype.web.proxy.ExternalUrlConfiguratbleProxyServlet.java

/**
 * <p>//from   w  w w.j  a  va 2s .  c om
 * Encodes characters in the query or fragment part of the URI.
 * 
 * <p>
 * Unfortunately, an incoming URI sometimes has characters disallowed by the
 * spec. HttpClient insists that the outgoing proxied request has a valid
 * URI because it uses Java's {@link URI}. To be more forgiving, we must
 * escape the problematic characters. See the URI class for the spec.
 * 
 * @param in
 *            example: name=value&foo=bar#fragment
 */
static CharSequence encodeUriQuery(CharSequence in) {
    // Note that I can't simply use URI.java to encode because it will
    // escape pre-existing escaped things.
    StringBuilder outBuf = null;
    Formatter formatter = null;
    for (int i = 0; i < in.length(); i++) {
        char c = in.charAt(i);
        boolean escape = true;
        if (c < 128) {
            if (asciiQueryChars.get((int) c)) {
                escape = false;
            }
        } else if (!Character.isISOControl(c) && !Character.isSpaceChar(c)) {// not-ascii
            escape = false;
        }
        if (!escape) {
            if (outBuf != null)
                outBuf.append(c);
        } else {
            // escape
            if (outBuf == null) {
                outBuf = new StringBuilder(in.length() + 5 * 3);
                outBuf.append(in, 0, i);
                formatter = new Formatter(outBuf);
            }
            // leading %, 0 padded, width 2, capital hex
            formatter.format("%%%02X", (int) c);// TODO
        }
    }
    return outBuf != null ? outBuf : in;
}

From source file:org.artifactory.util.PathUtils.java

public static CharSequence trimLeadingSlashChars(CharSequence path) {
    if (path == null) {
        return null;
    }//from   w  ww  . j  av a2s.c  o m
    //Trim leading '/' (caused by webdav requests)
    if (path.length() > 0 && path.charAt(0) == '/') {
        path = path.subSequence(1, path.length());
        return trimLeadingSlashChars(path);
    }
    return path;
}

From source file:edu.cornell.med.icb.goby.reads.ColorSpaceConverter.java

/**
 * Converts a sequence into the equivalent sequence in color space.
 *
 * @param input  The sequence to be converted
 * @param output where the converted sequence should be placed
 * @param anyN   if true, converts color space codes larger or equal to 4 to 'N' characters.
 *///  www  .j a  v  a 2 s  . c  o  m
public static void convert(final CharSequence input, final MutableString output, final boolean anyN) {
    assert output != null : "The output location must not be null";

    output.setLength(0);
    if (input != null) {
        int position = 0;
        final int length = input.length() - 1; // -1 since we enumerate digrams
        if (input.length() > 0) {

            output.setLength(position + 1);
            output.setCharAt(position++, input.charAt(0));
        }
        for (int index = 0; index < length; ++index) {
            final char code = Character.forDigit(getColorCode(input.charAt(index), input.charAt(index + 1)),
                    10);
            output.setLength(position + 1);
            output.setCharAt(position++, (code >= '4') ? (anyN ? 'N' : code) : code);
        }
        output.setLength(position);
    }
}

From source file:org.jets3t.service.utils.SignatureUtils.java

/**
 * Slightly modified version of "uri-encode" from:
 * {@link "http://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-header-based-auth.html"}
 *
 * @param input/*from ww w  .jav  a  2 s.co  m*/
 * URI or URI-fragment string to encode.
 * @param encodeSlash
 * true if slash (/) character should be encoded.
 * @return URI string encoded per recommendations from AWS.
 */
public static String awsV4EncodeURI(CharSequence input, boolean encodeSlash) {
    StringBuilder result = new StringBuilder();
    for (int i = 0; i < input.length(); i++) {
        char ch = input.charAt(i);
        if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') || ch == '_'
                || ch == '-' || ch == '~' || ch == '.') {
            result.append(ch);
        } else if (ch == '/') {
            result.append(encodeSlash ? "%2F" : ch);
        } else {
            String hex = RestUtils.encodeUrlString(String.valueOf(ch));
            result.append(hex);
        }
    }
    return result.toString();
}

From source file:jp.co.ctc_g.jse.core.validation.util.Validators.java

/**
 * ????'\u0020'?'\u007e'??????????//from w  w  w  . ja  v a  2s  .c om
 * @param suspect 
 * @return ???true
 */
public static boolean isASCII(CharSequence suspect) {
    for (int i = 0; i < suspect.length(); i++) {
        char c = suspect.charAt(i);
        if (c < '\u0020' || c > '\u007e') {
            return false;
        }
    }
    return true;
}

From source file:com.dnw.json.J.java

/**
 * Escapes each character to make the string can be denoted as a JSON string.
 * //from ww w.  j a v  a 2  s. c  o  m
 * @author manbaum
 * @since Oct 11, 2014
 * @param text a string to have all characters to be escaped.
 * @return the escaped string.
 */
private final static String escape(final CharSequence text) {
    final StringBuffer sb = new StringBuffer();
    for (int i = 0; i < text.length(); i++) {
        char ch = text.charAt(i);
        if (ch == 0) {
            sb.append("\\0");
        } else if (ch == '\n') {
            sb.append("\\n");
        } else if (ch == '\r') {
            sb.append("\\r");
        } else if (ch < 32) {
            sb.append("\\x");
            if (ch < 16) {
                sb.append('0');
            }
            sb.append(Integer.toHexString(ch));
        } else if (ch == '\\' || ch == '\'' || ch == '\"') {
            sb.append("\\");
            sb.append(ch);
        } else if (ch <= 126) {
            sb.append(ch);
        } else {
            int n = Character.codePointAt(text, i);
            sb.append("\\u");
            if (n < 16) {
                sb.append("000");
            } else if (n < 256) {
                sb.append("00");
            } else if (n < 4096) {
                sb.append("0");
            }
            sb.append(Integer.toHexString(n));
        }
    }
    return sb.toString();
}

From source file:org.apache.sshd.common.util.net.SshdSocketAddress.java

/**
 * <P>Checks if the provided argument is a valid IPv4 address component:</P></BR>
 * <UL>/*from  w  w w.j av a2 s .  c  om*/
 *     <LI>Not {@code null}/empty</LI>
 *     <LI>Has at most 3 <U>digits</U></LI>
 *     <LI>Its value is &le; 255</LI>
 * </UL>
 * @param c The {@link CharSequence} to be validate
 * @return {@code true} if valid IPv4 address component
 */
public static boolean isValidIPv4AddressComponent(CharSequence c) {
    if (GenericUtils.isEmpty(c) || (c.length() > 3)) {
        return false;
    }

    char ch = c.charAt(0);
    if ((ch < '0') || (ch > '9')) {
        return false;
    }

    if (!NumberUtils.isIntegerNumber(c)) {
        return false;
    }

    int v = Integer.parseInt(c.toString());
    return (v >= 0) && (v <= 255);
}