Example usage for java.lang String codePointAt

List of usage examples for java.lang String codePointAt

Introduction

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

Prototype

public int codePointAt(int index) 

Source Link

Document

Returns the character (Unicode code point) at the specified index.

Usage

From source file:com.bytecode.util.Crypto.java

private static Key generateKey(String keystring, int bits) throws Exception {
    byte[] keyBytes = new byte[bits];
    byte[] key = new byte[bits];
    for (int i = 0; i < bits; i++) {
        keyBytes[i] = (byte) keystring.codePointAt(i);
    }/*from  www.  j  ava  2s . com*/
    SecretKey secretKey = new SecretKeySpec(keyBytes, "AES");
    Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    key = cipher.doFinal(keyBytes);
    for (int i = 0; i < bits - 16; i++) {
        key[16 + i] = key[i];
    }

    return new SecretKeySpec(key, "AES");
}

From source file:Main.java

/**
 * This method ensures that the output String has only valid XML unicode characters as specified by the
 * XML 1.0 standard. For reference, please see the
 * standard. This method will return an empty String if the input is null or empty.
 *
 * @param s - The String whose non-valid characters we want to replace.
 * @return The in String, where non-valid characters are replace by spaces.
 * @author Nuno Freire/*from   ww  w .j av a 2 s  .  co  m*/
 */
public static String removeInvalidXMLCharacters(String s) {

    StringBuilder out = new StringBuilder(); // Used to hold the output.
    int codePoint; // Used to reference the current character.
    int i = 0;
    while (i < s.length()) {
        codePoint = s.codePointAt(i); // This is the unicode code of the character.
        if ((codePoint == 0x9) || // Consider testing larger ranges first to improve speed.
                (codePoint == 0xA) || (codePoint == 0xD) || ((codePoint >= 0x20) && (codePoint <= 0xD7FF))
                || ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))
                || ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF))) {
            out.append(Character.toChars(codePoint));
        } else {
            out.append(' ');
        }
        i += Character.charCount(codePoint); // Increment with the number of code units(java chars) needed to represent a Unicode char.
    }
    return out.toString();
}

From source file:com.evolveum.midpoint.common.Utils.java

/**
 * Removing non-printable UTF characters from the string.
 * //from w w w.  ja  v a 2  s.  c  o  m
 * This is not really used now. It was done as a kind of prototype for
 * filters. But may come handy and it in fact tests that the pattern is
 * doing what expected, so it may be useful.
 * 
 * @param bad
 *            string with bad chars
 * @return string without bad chars
 */
public static String cleanupUtf(String bad) {

    StringBuilder sb = new StringBuilder(bad.length());

    for (int cp, i = 0; i < bad.length(); i += Character.charCount(cp)) {
        cp = bad.codePointAt(i);
        if (isValidXmlCodepoint(cp)) {
            sb.append(Character.toChars(cp));
        }
    }

    return sb.toString();
}

From source file:Main.java

public static String stripNonValidXMLCharacters(String text) {
    if (text == null || ("".equals(text))) {
        return "";
    }//  w w w  .j  a v  a 2  s.  c o m

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < text.length(); i++) {
        int codePoint = text.codePointAt(i);
        if (codePoint > 0xFFFF) {
            i++;
        }
        if ((codePoint == 0x9) || (codePoint == 0xA) || (codePoint == 0xD)
                || ((codePoint >= 0x20) && (codePoint <= 0xD7FF))
                || ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))
                || ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF))) {
            sb.appendCodePoint(codePoint);
        }
    }
    return sb.toString();
}

From source file:info.novatec.testit.livingdoc.util.NameUtils.java

public static String removeNonJavaIdentifierCharacters(String name) {
    StringBuilder javaIdentifier = new StringBuilder();

    for (int index = 0; index < name.length(); index++) {
        if (Character.isJavaIdentifierPart(name.codePointAt(index))) {
            javaIdentifier.append(name.charAt(index));
        }/*from  ww w .j a  v  a 2 s  .c  om*/
    }

    return javaIdentifier.toString();
}

From source file:Main.java

public static boolean containsOnlyWhiteSpaces(final Collection<String> values) {
    if (values == null) {
        return true;
    }/*w w  w .j  av  a 2s.  c  o  m*/
    for (final String str : values) {
        if (TextUtils.isEmpty(str)) {
            continue;
        }
        final int length = str.length();
        for (int i = 0; i < length; i = str.offsetByCodePoints(i, 1)) {
            if (!Character.isWhitespace(str.codePointAt(i))) {
                return false;
            }
        }
    }
    return true;
}

From source file:SpinnerTest.java

private static int[] toCodePointArray(String str) {
    int[] codePoints = new int[str.codePointCount(0, str.length())];
    for (int i = 0, j = 0; i < str.length(); i++, j++) {
        int cp = str.codePointAt(i);
        if (Character.isSupplementaryCodePoint(cp))
            i++;/*from   w  ww  .  j  av a2  s  .c o  m*/
        codePoints[j] = cp;
    }
    return codePoints;
}

From source file:Main.java

public static boolean containsOnlyNonCrLfPrintableAscii(final Collection<String> values) {
    if (values == null) {
        return true;
    }//from   ww  w  . j ava2 s. c o m
    final int asciiFirst = 0x20;
    final int asciiLast = 0x7E; // included
    for (final String value : values) {
        if (TextUtils.isEmpty(value)) {
            continue;
        }
        final int length = value.length();
        for (int i = 0; i < length; i = value.offsetByCodePoints(i, 1)) {
            final int c = value.codePointAt(i);
            if (!(asciiFirst <= c && c <= asciiLast)) {
                return false;
            }
        }
    }
    return true;
}

From source file:Main.java

public static boolean containsOnlyPrintableAscii(String str) {
    if (TextUtils.isEmpty(str)) {
        return true;
    }/*from  w  w  w.j a va 2 s .  com*/

    final int length = str.length();
    final int asciiFirst = 0x20;
    final int asciiLast = 0x126;
    for (int i = 0; i < length; i = str.offsetByCodePoints(i, 1)) {
        int c = str.codePointAt(i);
        if (c < asciiFirst || asciiLast < c) {
            return false;
        }
    }
    return true;
}

From source file:de.tudarmstadt.ukp.dkpro.tc.api.features.util.FeatureUtil.java

/**
 * Escapes the names, as Weka does not seem to like special characters in attribute names.
 * @param name/*from w  ww  .  j  a  v a2 s  .  c  o m*/
 * @return
 */
public static String escapeFeatureName(String name) {

    // TODO Issue 120: improve the escaping
    // the fix was necessary due to Issue 32
    // http://code.google.com/p/dkpro-tc/issues/detail?id=32
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < name.length(); i++) {
        String c = name.substring(i, i + 1);
        if (StringUtils.isAlphanumeric(c) || c.equals("_")) {
            sb.append(c);
        } else {
            sb.append("u");
            sb.append(c.codePointAt(0));
        }
    }
    return sb.toString();
}