Example usage for java.lang String getChars

List of usage examples for java.lang String getChars

Introduction

In this page you can find the example usage for java.lang String getChars.

Prototype

public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) 

Source Link

Document

Copies characters from this string into the destination character array.

Usage

From source file:org.apache.shindig.gadgets.uri.UriUtils.java

/**
 * Returns true if the header value is valid.
 * NOTE: RFC 822 section 3.1.2 describes the structure of header fields.
 * According to the RFC, a header value (or field-body) may be composed of any ASCII characters,
 * except CR or LF.//from  w  ww .ja  v a2s .c  o m
 * @param val The header value.
 * @return True if the header value is valid, false otherwise.
 */
public static boolean isValidHeaderValue(String val) {
    char[] dst = new char[val.length()];
    val.getChars(0, val.length(), dst, 0);

    for (char c : dst) {
        if (c == 13 || c == 10) {
            // CR and LF.
            return false;
        }
        if (c > 127) {
            return false;
        }
    }
    return true;
}

From source file:org.apache.shindig.gadgets.uri.UriUtils.java

/**
 * Returns true if the header name is valid.
 * NOTE: RFC 822 section 3.1.2 describes the structure of header fields.
 * According to the RFC, a header name (or field-name) must be composed of printable ASCII
 * characters (i.e., characters that have values between 33. and 126. decimal, except colon).
 * @param name The header name.//from w ww.  jav a 2s.co m
 * @return True if the header name is valid, false otherwise.
 */
public static boolean isValidHeaderName(String name) {
    char[] dst = new char[name.length()];
    name.getChars(0, name.length(), dst, 0);

    for (char c : dst) {
        if (c < 33 || c > 126) {
            return false;
        }
        if (c == ':') {
            return false;
        }
    }
    return true;
}

From source file:Main.java

/**
 * Decode a base64 string into a byte array.
 *
 * @param data the encoded data.//from  ww  w.  j  av  a 2 s.co  m
 * @return a byte array.
 * @see #fromBase64(String)
 */
public static byte[] fromBase64(String data) {
    if (data == null) {
        return null;
    }

    int len = data.length();
    assert (len % 4) == 0;

    if (len == 0) {
        return new byte[0];
    }

    char[] chars = new char[len];
    data.getChars(0, len, chars, 0);

    int olen = 3 * (len / 4);
    if (chars[len - 2] == '=') {
        --olen;
    }
    if (chars[len - 1] == '=') {
        --olen;
    }

    byte[] bytes = new byte[olen];

    int iidx = 0;
    int oidx = 0;
    while (iidx < len) {
        int c0 = base64Values[chars[iidx++] & 0xff];
        int c1 = base64Values[chars[iidx++] & 0xff];
        int c2 = base64Values[chars[iidx++] & 0xff];
        int c3 = base64Values[chars[iidx++] & 0xff];
        int c24 = (c0 << 18) | (c1 << 12) | (c2 << 6) | c3;

        bytes[oidx++] = (byte) (c24 >> 16);
        if (oidx == olen) {
            break;
        }
        bytes[oidx++] = (byte) (c24 >> 8);
        if (oidx == olen) {
            break;
        }
        bytes[oidx++] = (byte) c24;
    }

    return bytes;
}

From source file:IPDomainValidator.java

/**
 * Returns 0 if the string {@code domain} is invalid.
 *//*from   www  .j  a v a 2s .  c  o  m*/
public static int validate(String domain, int start, int end) {
    char[] ch = new char[end - start];
    domain.getChars(start, end, ch, 0);
    return validate(ch);
}

From source file:org.seasar.mayaa.impl.engine.specification.QNameImpl.java

/**
 * "{URI}localName"?????/* w ww.j a v a 2 s  .com*/
 *
 * @param namespaceURI ????URI
 * @param localName ??
 * @return "{URI}localName"??
 */
private static String forQNameString(URI namespaceURI, String localName) {
    String namespace = namespaceURI.getValue();
    int namespaceLength = namespace.length();
    int localNameLength = localName.length();
    char[] buffer = new char[namespaceLength + localNameLength + 2];
    buffer[0] = '{';
    namespace.getChars(0, namespaceLength, buffer, 1);
    buffer[namespaceLength + 1] = '}';
    localName.getChars(0, localNameLength, buffer, namespaceLength + 2);
    return new String(buffer);
}

From source file:jp.terasoluna.fw.web.taglib.TagUtil.java

/**
 * HTML?oTj^CY?s?B/*from  w  ww.  jav a2 s . c om*/
 * ?u<?v?u>?v?u&?v?u"?v?u'?vu?B
 * 
 * @param value Tj^CY?
 * @return ?W?
 */
public static String filter(String value) {

    if (value == null) {
        return null;
    }

    char[] content = new char[value.length()];
    value.getChars(0, value.length(), content, 0);
    StringBuilder result = new StringBuilder();

    for (int i = 0; i < content.length; i++) {
        switch (content[i]) {
        case '<':
            result.append("&lt;");
            break;
        case '>':
            result.append("&gt;");
            break;
        case '&':
            result.append("&amp;");
            break;
        case '"':
            result.append("&quot;");
            break;
        case '\'':
            result.append("&#39;");
            break;
        default:
            result.append(content[i]);
            break;
        }
    }

    return result.toString();
}

From source file:com.jaspersoft.ireport.designer.data.fieldsproviders.ejbql.EJBQLFieldsReader.java

/**
 * Checks if the input is a valid Java literal
 * @param literal//from w  w  w  .j  av a  2s  .  c  om
 * @author Gaganis Giorgos (gaganis@users.sourceforge.net) 
 */
private static boolean isValidLiteral(String literal) {
    boolean result = true;

    char[] literalChars = new char[literal.length()];
    literal.getChars(0, literalChars.length, literalChars, 0);

    for (int i = 0; i < literalChars.length; i++) {
        if (i == 0 && !Character.isJavaIdentifierStart(literalChars[i])) {
            result = false;
            break;
        }

        if (i != 0 && !Character.isJavaIdentifierPart(literalChars[i])) {
            result = false;
            break;
        }
    }

    return result;
}

From source file:Main.java

public static Document loadString(String paramString) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setIgnoringComments(false);//from  ww w  .ja  va2s .  com
    factory.setIgnoringElementContentWhitespace(false);
    factory.setValidating(false);
    factory.setCoalescing(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    char[] arrayOfChar = new char[paramString.length()];
    paramString.getChars(0, paramString.length(), arrayOfChar, 0);
    InputSource input = new InputSource(new CharArrayReader(arrayOfChar));
    return builder.parse(input);
}

From source file:Main.java

public static Document loadString(String domContent) throws Exception {
    javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
    factory.setIgnoringComments(false);// w  w  w. ja  v  a 2  s .co m
    factory.setIgnoringElementContentWhitespace(false);
    factory.setValidating(false);
    factory.setCoalescing(false);
    DocumentBuilder builder = factory.newDocumentBuilder();

    char[] chars = new char[domContent.length()];
    domContent.getChars(0, domContent.length(), chars, 0);
    InputSource is = new InputSource(new CharArrayReader(chars));
    return (builder.parse(is));
}

From source file:de.iteratec.iteraplan.presentation.tags.TagUtils.java

/**
 * Filter the specified string for characters that are sensitive to HTML interpreters, returning
 * the string with these characters replaced by the corresponding character entities.
 * //www  .  j av  a2 s.  co  m
 * @param value
 *          The string to be filtered and returned
 */
public static String filter(String value) {

    if (value == null) {
        return (null);
    }

    char content[] = new char[value.length()];
    value.getChars(0, value.length(), content, 0);
    StringBuffer result = new StringBuffer(content.length + 50);
    for (int i = 0; i < content.length; i++) {
        switch (content[i]) {
        case '<':
            result.append("&lt;");
            break;
        case '>':
            result.append("&gt;");
            break;
        case '&':
            result.append("&amp;");
            break;
        case '"':
            result.append("&quot;");
            break;
        case '\'':
            result.append("&#39;");
            break;
        default:
            result.append(content[i]);
        }
    }
    return (result.toString());
}