Example usage for java.text CharacterIterator first

List of usage examples for java.text CharacterIterator first

Introduction

In this page you can find the example usage for java.text CharacterIterator first.

Prototype

public char first();

Source Link

Document

Sets the position to getBeginIndex() and returns the character at that position.

Usage

From source file:Base64.java

public static byte[] decodeKreative85(String s) {
    CharacterIterator it = new StringCharacterIterator(s.trim());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    long i = 0;/*w w  w.j  av a 2s .c  o m*/
    int j = 0;
    for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
        int v = k85d(ch);
        if (v >= 0) {
            i = i * 85L + v;
            j++;
            if (j >= 5) {
                out.write((int) (i >> 24L));
                out.write((int) (i >> 16L));
                out.write((int) (i >> 8L));
                out.write((int) i);
                i = 0;
                j = 0;
            }
        }
    }
    switch (j) {
    case 4:
        i = i * 85L + 84L;
        out.write((int) (i >> 24L));
        out.write((int) (i >> 16L));
        out.write((int) (i >> 8L));
        break;
    case 3:
        i = i * 85L + 84L;
        i = i * 85L + 84L;
        out.write((int) (i >> 24L));
        out.write((int) (i >> 16L));
        break;
    case 2:
        i = i * 85L + 84L;
        i = i * 85L + 84L;
        i = i * 85L + 84L;
        out.write((int) (i >> 24L));
        break;
    }
    return out.toByteArray();
}

From source file:org.gradle.api.internal.plugins.StartScriptTemplateBindingFactory.java

private String escapeWindowsJvmOpt(String jvmOpts) {
    boolean wasOnBackslash = false;
    StringBuilder escapedJvmOpt = new StringBuilder();
    CharacterIterator it = new StringCharacterIterator(jvmOpts);

    //argument quoting:
    // - " must be encoded as \"
    // - % must be encoded as %%
    // - pathological case: \" must be encoded as \\\", but other than that, \ MUST NOT be quoted
    // - other characters (including ') will not be quoted
    // - use a state machine rather than regexps
    for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
        String repl = Character.toString(ch);

        if (ch == '%') {
            repl = "%%";
        } else if (ch == '"') {
            repl = (wasOnBackslash ? '\\' : "") + "\\\"";
        }/* ww w.j  a  v a  2s.  c om*/
        wasOnBackslash = ch == '\\';
        escapedJvmOpt.append(repl);
    }

    return escapedJvmOpt.toString();
}

From source file:Base64.java

public static byte[] decodeASCII85(String s) {
    s = s.trim();// w w w.j  a  va 2 s. c  o  m
    if (s.startsWith("<~") && s.endsWith("~>")) {
        s = s.substring(2, s.length() - 2).trim();
    }
    CharacterIterator it = new StringCharacterIterator(s);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    long i = 0;
    int j = 0;
    for (char ch = it.first(); ch != CharacterIterator.DONE && ch != '~'; ch = it.next()) {
        if (ch == 'z' && j == 0) {
            out.write(0);
            out.write(0);
            out.write(0);
            out.write(0);
        } else if (ch == 'y' && j == 0) {
            out.write(' ');
            out.write(' ');
            out.write(' ');
            out.write(' ');
        } else if (ch == 'x' && j == 0) {
            out.write(-1);
            out.write(-1);
            out.write(-1);
            out.write(-1);
        } else if (ch >= '!' && ch <= 'u') {
            i = i * 85L + (long) (ch - '!');
            j++;
            if (j >= 5) {
                out.write((int) (i >> 24L));
                out.write((int) (i >> 16L));
                out.write((int) (i >> 8L));
                out.write((int) i);
                i = 0;
                j = 0;
            }
        }
    }
    switch (j) {
    case 4:
        i = i * 85L + 84L;
        out.write((int) (i >> 24L));
        out.write((int) (i >> 16L));
        out.write((int) (i >> 8L));
        break;
    case 3:
        i = i * 85L + 84L;
        i = i * 85L + 84L;
        out.write((int) (i >> 24L));
        out.write((int) (i >> 16L));
        break;
    case 2:
        i = i * 85L + 84L;
        i = i * 85L + 84L;
        i = i * 85L + 84L;
        out.write((int) (i >> 24L));
        break;
    }
    return out.toByteArray();
}

From source file:Base64.java

public static byte[] decodeLegacy85(String s) {
    int targetLength = -1;
    s = s.trim();//from  w ww .  j  a  v a 2s. c  om
    if (s.length() >= 7 && s.charAt(0) == '<' && s.charAt(6) == '>') {
        targetLength = k85d(s.charAt(1)) + k85d(s.charAt(2)) * 85 + k85d(s.charAt(3)) * 7225
                + k85d(s.charAt(4)) * 614125 + k85d(s.charAt(5)) * 52200625;
        s = s.substring(7).trim();
    }
    CharacterIterator it = new StringCharacterIterator(s);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    long i = 0;
    int j = 0;
    long k = 1;
    for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
        int v = k85d(ch);
        if (v >= 0) {
            i += v * k;
            j++;
            k *= 85;
            if (j >= 5) {
                out.write((int) i);
                out.write((int) (i >> 8L));
                out.write((int) (i >> 16L));
                out.write((int) (i >> 24L));
                i = 0;
                j = 0;
                k = 1;
            }
        }
    }
    if (j > 0) {
        out.write((int) i);
        out.write((int) (i >> 8L));
        out.write((int) (i >> 16L));
        out.write((int) (i >> 24L));
    }
    if (targetLength >= 0) {
        byte[] b = out.toByteArray();
        byte[] bt = new byte[targetLength];
        for (int x = 0; x < targetLength && x < b.length; x++) {
            bt[x] = b[x];
        }
        return bt;
    } else {
        return out.toByteArray();
    }
}

From source file:XmlNameEncoder.java

/**
 * {@inheritDoc}//from  w w w .  j a  va 2 s .c  o m
 * 
 * @see org.jboss.dna.common.text.TextEncoder#encode(java.lang.String)
 */
public String encode(String text) {
    if (text == null)
        return null;
    if (text.length() == 0)
        return text;
    StringBuilder sb = new StringBuilder();
    String hex = null;
    CharacterIterator iter = new StringCharacterIterator(text);
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        if (c == '_') {
            // Read the next character (if there is one) ...
            char next = iter.next();
            if (next == CharacterIterator.DONE) {
                sb.append(c);
                break;
            }
            // If the next character is not 'x', then these are just regular characters ...
            if (next != 'x') {
                sb.append(c).append(next);
                continue;
            }
            // The next character is 'x', so write out the '_' character in encoded form ...
            sb.append("_x005f_");
            // And then write out the next character ...
            sb.append(next);
        } else if (XML_NAME_ALLOWED_CHARACTERS.get(c)) {
            // Legal characters for an XML Name ...
            sb.append(c);
        } else {
            // All other characters must be escaped with '_xHHHH_' where 'HHHH' is the hex string for the code point
            hex = Integer.toHexString(c);
            // The hex string excludes the leading '0's, so check the character values so we know how many to prepend
            if (c >= '\u0000' && c <= '\u000f') {
                sb.append("_x000").append(hex);
            } else if (c >= '\u0010' && c <= '\u00ff') {
                sb.append("_x00").append(hex);
            } else if (c >= '\u0100' && c <= '\u0fff') {
                sb.append("_x0").append(hex);
            } else {
                sb.append("_x").append(hex);
            }
            sb.append('_');
        }
    }
    return sb.toString();
}

From source file:com.googlecode.jsonplugin.JSONWriter.java

/**
 * escape characters/*from  w  ww  .j a  v a2 s  .  c o  m*/
 */
private void string(Object obj) {
    this.add('"');

    CharacterIterator it = new StringCharacterIterator(obj.toString());

    for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {
        if (c == '"') {
            this.add("\\\"");
        } else if (c == '\\') {
            this.add("\\\\");
        } else if (c == '/') {
            this.add("\\/");
        } else if (c == '\b') {
            this.add("\\b");
        } else if (c == '\f') {
            this.add("\\f");
        } else if (c == '\n') {
            this.add("\\n");
        } else if (c == '\r') {
            this.add("\\r");
        } else if (c == '\t') {
            this.add("\\t");
        } else if (Character.isISOControl(c)) {
            this.unicode(c);
        } else {
            this.add(c);
        }
    }

    this.add('"');
}

From source file:org.semanticscience.narf.structures.parts.Sequence.java

/**
 * Create the set of nucleotides from a nucleic acid sequence string. A
 * mapping is constructed of the residue position of the nucleotides to the
 * nucleotides. The method enables the sequence to commence at an arbitrary
 * sequence position./*from  www.ja v  a  2  s  . com*/
 * 
 * @param aSequence
 *            sequence string
 * @param aStartingPosition
 *            the residue position of the first nucleotide
 * @return a mapping of the nucleotide residue positions to their respective
 *         nucleotide residue position
 * @throws InvalidSequenceException
 *             if any of the characters in the sequence string is not a
 *             valid residue identifier
 * @throws InvalidResidueException
 *             if the created nucleotide contains a residue exception
 * @since 1.6
 */
SortedMap<Integer, Nucleotide> makeNucleotides(String aSequence, int aStartingPosition)
        throws InvalidSequenceException, InvalidResidueException {
    //first make the sequence all upper case
    aSequence = aSequence.toUpperCase();
    if ((aSequence == null) || (aSequence.length() == 0)) {
        throw new InvalidSequenceException("The provided sequence has no characters.");
    }

    if (!this.checkStringSequence(aSequence)) {
        throw new InvalidSequenceException(
                "The inputted sequence has a invalid sequence character.\n" + aSequence + "\n");
    }

    SortedMap<Integer, Nucleotide> returnMe = new TreeMap<Integer, Nucleotide>();
    int currentPosition = aStartingPosition;
    CharacterIterator it = new StringCharacterIterator(aSequence);

    for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
        returnMe.put(currentPosition, new Nucleotide(currentPosition, String.valueOf(ch)));
        currentPosition++;
    }
    return returnMe;
}

From source file:XmlNameEncoder.java

/**
 * {@inheritDoc}/*  w  w  w. j a v  a2  s  . com*/
 * 
 * @see org.jboss.dna.common.text.TextDecoder#decode(java.lang.String)
 */
public String decode(String encodedText) {
    if (encodedText == null)
        return null;
    if (encodedText.length() < 7) {
        // Not big enough to have an encoded sequence
        return encodedText;
    }
    StringBuilder sb = new StringBuilder();
    char[] digits = new char[4];
    CharacterIterator iter = new StringCharacterIterator(encodedText);
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        if (c == '_') {
            // Read the next character, if there is one ...
            char next = iter.next();
            if (next == CharacterIterator.DONE) {
                sb.append(c);
                break;
            }
            // If the next character is not 'x', then these are just regular characters ...
            if (next != 'x') {
                sb.append(c).append(next);
                continue;
            }
            // Read the next 4 characters (digits) and another '_' character ...
            digits[0] = iter.next();
            if (digits[0] == CharacterIterator.DONE) {
                sb.append(c).append(next);
                break;
            }
            digits[1] = iter.next();
            if (digits[1] == CharacterIterator.DONE) {
                sb.append(c).append(next).append(digits, 0, 1);
                break;
            }
            digits[2] = iter.next();
            if (digits[2] == CharacterIterator.DONE) {
                sb.append(c).append(next).append(digits, 0, 2);
                break;
            }
            digits[3] = iter.next();
            if (digits[3] == CharacterIterator.DONE) {
                sb.append(c).append(next).append(digits, 0, 3);
                break;
            }
            char underscore = iter.next();
            if (underscore != '_') { // includes DONE
                sb.append(c).append(next).append(digits, 0, 4);
                if (underscore == CharacterIterator.DONE)
                    break;
                sb.append(underscore);
                continue;
            }
            // We've read all 4 digits, including the trailing '_'
            // Now parse into the resulting character
            try {
                sb.appendCodePoint(Integer.parseInt(new String(digits), 16));
            } catch (NumberFormatException e) {
                // code was not hexadecimal, so just write out the characters as is ...
                sb.append(c).append(next).append(digits).append(underscore);
            }
        } else {
            // Just append other characters ...
            sb.append(c);
        }
    }
    return sb.toString();
}

From source file:stg.utils.RandomStringGenerator.java

/**
 * Validates the given format.//www. j  a  va 2  s .  c o  m
 * Valid characters are :
 * <li>a<dt>For lower case alphabets.
 * <li>A<dt>For upper case alphabets.
 * <li>9<dt>For numbers.
 * <li>#<dt>For special characters.
 * The format characters can be repeated in whatever sequence. 
 * The random string generated will have the same length as that of the format.
 * 
 * @param format
 */
private void validate(String format) {
    CharacterIterator iter = new StringCharacterIterator(format);
    char c = iter.first();
    boolean constantStarted = false;
    while (c != CharacterIterator.DONE) {
        switch (c) {
        case ESCAPE:
            c = iter.next();
            if (c == CharacterIterator.DONE) {
                throw new IllegalArgumentException(
                        "Invalid format! Escape character found without any associated character that was to be escaped.");
            }
            break;
        case LOWER_CASE:
            break;
        case UPPER_CASE:
            break;
        case NUMBER:
            break;
        case SPECIAL:
            break;
        case START_CONSTANT:
            if (!constantStarted)
                constantStarted = true;
            break;
        case END_CONSTANT:
            if (constantStarted)
                constantStarted = false;
            break;
        default:
            if (!constantStarted)
                throw new IllegalArgumentException("Invalid format character found '" + c + "'");
        }
        c = iter.next();
    }
}

From source file:stg.utils.RandomStringGenerator.java

/**
 * Generates the random string as per the format.
 * /* ww w. j av  a 2s  . c om*/
 * @return String
 */
public String generate() {
    CharacterIterator iter = new StringCharacterIterator(format);
    char c = iter.first();
    StringBuilder sb = new StringBuilder();
    List<Character> tempLowerCaseCharsList = cloneList(lowerCaseCharsList);
    List<Character> tempUpperCaseCharsList = cloneList(upperCaseCharsList);
    List<Character> tempNumericCharsList = cloneList(numericCharsList);
    List<Character> tempSpecialCharsList = cloneList(specialCharsList);

    boolean constantStarted = false;
    while (c != CharacterIterator.DONE) {
        switch (c) {
        case ESCAPE:
            c = iter.next();
            if (c == CharacterIterator.DONE) {
                throw new IllegalArgumentException(
                        "Invalid format, escape character found without any associated character that was to be escaped.");
            }
            sb.append(c);
            break;
        case LOWER_CASE:
            if (!constantStarted) {
                switch (option) {
                case NON_UNIQUE:
                    sb.append(RandomStringUtils.random(1, toCharArray(lowerCaseCharsList)));
                    break;
                case UNIQUE_CASE_SENSITIVE:
                    sb.append(generateUniqueCharacter(c, tempLowerCaseCharsList));
                    break;
                default:
                    String str = generateUniqueCharacter(c, tempLowerCaseCharsList);
                    sb.append(str);
                    if (!tempUpperCaseCharsList.contains(Character.valueOf(str.toUpperCase().charAt(0)))) {
                        System.out.println(tempLowerCaseCharsList + " \t " + str);
                    }
                    if (!tempUpperCaseCharsList.remove(Character.valueOf(str.toUpperCase().charAt(0)))) { //remove it from the upper case char set.
                        System.out.println("Problem unable to remove " + tempUpperCaseCharsList + "\t" + str);
                    }
                    break;
                }
            } else {
                sb.append(c);
            }
            break;
        case UPPER_CASE:
            if (!constantStarted) {
                switch (option) {
                case NON_UNIQUE:
                    sb.append(RandomStringUtils.random(1, toCharArray(upperCaseCharsList)));
                    break;
                case UNIQUE_CASE_SENSITIVE:
                    sb.append(generateUniqueCharacter(c, tempUpperCaseCharsList));
                    break;
                default:
                    String str = generateUniqueCharacter(c, tempUpperCaseCharsList);
                    sb.append(str);
                    if (!tempLowerCaseCharsList.contains(Character.valueOf(str.toLowerCase().charAt(0)))) {
                        System.out.println(tempLowerCaseCharsList + " \t " + str);
                    }
                    if (!tempLowerCaseCharsList.remove(Character.valueOf(str.toLowerCase().charAt(0)))) {
                        System.out.println("Problem unable to remove " + tempLowerCaseCharsList + "\t" + str);
                    }
                    break;
                }
            } else {
                sb.append(c);
            }
            break;
        case NUMBER:
            if (!constantStarted) {
                switch (option) {
                case NON_UNIQUE:
                    sb.append(RandomStringUtils.random(1, toCharArray(numericCharsList)));
                    break;
                default:
                    sb.append(generateUniqueCharacter(c, tempNumericCharsList));
                    break;
                }
            } else {
                sb.append(c);
            }
            break;
        case SPECIAL:
            if (!constantStarted) {
                switch (option) {
                case NON_UNIQUE:
                    sb.append(RandomStringUtils.random(1, toCharArray(specialCharsList)));
                    break;
                default:
                    sb.append(generateUniqueCharacter(c, tempSpecialCharsList));
                    break;
                }
            } else {
                sb.append(c);
            }
            break;
        case START_CONSTANT:
            if (constantStarted) {
                throw new IllegalArgumentException("Special { character found without an escape character");
            }
            if (!constantStarted)
                constantStarted = true;
            break;
        case END_CONSTANT:
            if (!constantStarted)
                throw new IllegalArgumentException("Special } character found without an escape character");
            if (constantStarted)
                constantStarted = false;
            break;
        default:
            sb.append(c);
        }
        c = iter.next();
    }
    return sb.toString();
}