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:Base64.java

/**
 * Decode the base64 data./*from  w w w.j a va2  s. c om*/
 * @param data The base64 encoded data to be decoded
 * @return The decoded data
 */
public static byte[] decode(String data) {
    int ibufcount = 0;
    int slen = data.length();
    char[] ibuf = new char[slen < BUF_SIZE + 3 ? slen : BUF_SIZE + 3];
    byte[] obuf = new byte[(slen >> 2) * 3 + 3];
    int obufcount = 0;
    int blen;

    for (int i = 0; i < slen; i += BUF_SIZE) {
        // buffer may contain unprocessed characters from previous step
        if (i + BUF_SIZE <= slen) {
            data.getChars(i, i + BUF_SIZE, ibuf, ibufcount);
            blen = BUF_SIZE + ibufcount;
        } else {
            data.getChars(i, slen, ibuf, ibufcount);
            blen = slen - i + ibufcount;
        }

        for (int j = ibufcount; j < blen; j++) {
            char ch = ibuf[j];
            if (ch == S_BASE64PAD || ch < S_DECODETABLE.length && S_DECODETABLE[ch] != Byte.MAX_VALUE) {
                ibuf[ibufcount++] = ch;
                // as soon as we have 4 chars process them
                if (ibufcount == 4) {
                    ibufcount = 0;
                    obufcount += decode0(ibuf, obuf, obufcount);
                }
            }
        }
    }
    if (obufcount == obuf.length) {
        return obuf;
    }
    byte[] ret = new byte[obufcount];
    System.arraycopy(obuf, 0, ret, 0, obufcount);
    return ret;
}

From source file:Main.java

public static String nextVolumeName(String arcName, boolean oldNumbering) {
    if (!oldNumbering) {
        // part1.rar, part2.rar, ...
        int len = arcName.length();
        int indexR = len - 1;
        while ((indexR >= 0) && !isDigit(arcName.charAt(indexR))) {
            indexR--;// w  w w . j av a 2s .c  om
        }
        int index = indexR + 1;
        int indexL = indexR - 1;
        while ((indexL >= 0) && isDigit(arcName.charAt(indexL))) {
            indexL--;
        }
        if (indexL < 0) {
            return null;
        }
        indexL++;
        StringBuilder buffer = new StringBuilder(len);
        buffer.append(arcName, 0, indexL);
        char[] digits = new char[indexR - indexL + 1];
        arcName.getChars(indexL, indexR + 1, digits, 0);
        indexR = digits.length - 1;
        while ((indexR >= 0) && (++digits[indexR]) == '9' + 1) {
            digits[indexR] = '0';
            indexR--;
        }
        if (indexR < 0) {
            buffer.append('1');
        }
        buffer.append(digits);
        buffer.append(arcName, index, len);
        return buffer.toString();
    } else {
        // .rar, .r00, .r01, ...
        int len = arcName.length();
        if ((len <= 4) || (arcName.charAt(len - 4) != '.')) {
            return null;
        }
        StringBuilder buffer = new StringBuilder();
        int off = len - 3;
        buffer.append(arcName, 0, off);
        if (!isDigit(arcName.charAt(off + 1)) || !isDigit(arcName.charAt(off + 2))) {
            buffer.append("r00");
        } else {
            char[] ext = new char[3];
            arcName.getChars(off, len, ext, 0);
            int i = ext.length - 1;
            while ((++ext[i]) == '9' + 1) {
                ext[i] = '0';
                i--;
            }
            buffer.append(ext);
        }
        return buffer.toString();
    }
}

From source file:org.yawlfoundation.yawl.util.StringUtil.java

/**
 * Utility method to take a string and return the string in revserse sequence.
 *
 * @param inputString String to be reversed
 * @return Reversed string//from   w w w .  ja  va2s  . c  om
 */
public static String reverseString(String inputString) {
    char[] inputChars = new char[inputString.length()];
    char[] outputChars = new char[inputString.length()];

    inputString.getChars(0, inputString.length(), inputChars, 0);
    int pointer = inputChars.length - 1;

    for (int i = 0; i <= inputChars.length - 1; i++) {
        outputChars[pointer] = inputChars[i];
        pointer--;
    }

    return new String(outputChars);
}

From source file:org.owasp.webgoat.lessons.Encoding.java

/**
 * Description of the Method// w  w  w  .  j a  va 2 s  .c  o m
 * 
 * @param asciiString
 *            Description of the Parameter
 * @return Description of the Return Value
 */

public static String hexEncode(String asciiString) {
    char[] ascii = new char[asciiString.length()];
    asciiString.getChars(0, asciiString.length(), ascii, 0);
    StringBuffer hexBuff = new StringBuffer();
    for (int i = 0; i < asciiString.length(); i++) {
        hexBuff.append("%");
        hexBuff.append(Integer.toHexString(ascii[i]));
    }
    return hexBuff.toString().toUpperCase();
}

From source file:com.laxser.blitz.web.impl.mapping.MappingFactory.java

public static List<Mapping> parse(String path) {

    ///*from  w  w  w. j a v  a2  s.  c  om*/
    if (path.equals("/")) {
        path = "";
    } else if (path.length() > 0 && path.charAt(0) != '/') {
        path = "/" + path;
    }
    if (path.length() > 1 && path.endsWith("/")) {
        if (path.endsWith("//")) {
            throw new IllegalArgumentException(
                    "invalid path '" + path + "' : don't end with more than one '/'");
        }
        path = path.substring(0, path.length() - 1);
    }

    if (path.length() == 0) {
        return Collections.emptyList();
    }

    List<Mapping> mappings = new ArrayList<Mapping>(8);

    //

    char[] chars = new char[path.length()];
    path.getChars(0, path.length(), chars, 0);
    int paramBegin = -1;
    int paramNameEnd = -1;
    int constantBegin = -1;
    int bracketDeep = 0; // > 0 ?{}?
    boolean startsWithBracket = true;
    for (int i = 0; i < chars.length; i++) {
        switch (chars[i]) {
        case '$':
            if (i + 1 >= chars.length) {
                throw new IllegalArgumentException(//
                        "invalid string '" + path + "', don't end with '$'");
            }
            // constains$name"constains"
            if (constantBegin >= 0) {
                mappings.add(createConstantMapping(path, constantBegin, i));
                constantBegin = -1;
            }
            // $name$xyz"$name"
            if (paramBegin >= 0) {
                mappings.add(createRegexMapping(path, paramBegin, paramNameEnd, i));
                paramBegin = -1;
                paramNameEnd = -1;
            }
            if (chars[i + 1] != '{') {
                paramBegin = i + 1;
                bracketDeep = 1;
                startsWithBracket = false;
            }
            break;
        case '{':
            if (bracketDeep++ > 0) { //++?
                break;
            }
            if (paramBegin < 0) {
                paramBegin = i + 1;
            }
            // $name{xyz}"$name"
            else {
                mappings.add(createRegexMapping(path, paramBegin, paramNameEnd, i));
                paramBegin = -1;
                paramNameEnd = -1;
            }
            // constains{name}"constains"
            if (constantBegin >= 0) {
                mappings.add(createConstantMapping(path, constantBegin, i));
                constantBegin = -1;
            }
            break;
        case ':':
            if (paramBegin < 0) {
                throw new IllegalArgumentException(//
                        "invalid string '" + path + "', wrong ':' at position " + i);
            } else if (paramNameEnd > 0) {
                throw new IllegalArgumentException(//
                        "invalid string '" + path + "', duplicated ':' at position " + i);
            } else {
                paramNameEnd = i;
            }
            break;
        case '}':
            if (--bracketDeep > 0) { // --?
                break;
            }
            if (paramBegin < 0) {
                throw new IllegalArgumentException(//
                        "invalid string '" + path + "', wrong '}' at position " + i);
            } else {
                mappings.add(createRegexMapping(path, paramBegin, paramNameEnd, i));
                paramBegin = -1;
                paramNameEnd = -1;
            }
            break;
        case '/':
            if (paramBegin < 0) {
                if (constantBegin >= 0) {
                    mappings.add(createConstantMapping(path, constantBegin, i));
                }
                constantBegin = i;
                break;
            } else if (bracketDeep == 0 || (bracketDeep == 1 && !startsWithBracket)) {
                mappings.add(createRegexMapping(path, paramBegin, paramNameEnd, i));
                paramBegin = -1;
                constantBegin = i;
                paramNameEnd = -1;
                bracketDeep = 0;
                break;
            }

        default:
            if (constantBegin == -1 && paramBegin == -1) {
                constantBegin = i;
            }
            break;
        }
    }
    if (constantBegin >= 0) {
        mappings.add(createConstantMapping(path, constantBegin, chars.length));
        constantBegin = -1;
    }
    if (paramBegin >= 0) {
        mappings.add(createRegexMapping(path, paramBegin, paramNameEnd, chars.length));
        paramBegin = -1;
        paramNameEnd = -1;
    }
    return mappings;
}

From source file:ParseNonXML.java

private char[] getChars(String value) {
    char[] chars = new char[value.length()];
    value.getChars(0, value.length(), chars, 0);
    return chars;
}

From source file:de.thischwa.pmcms.tool.swt.FileNameVerifier.java

@Override
public void verifyText(VerifyEvent e) {
    String string = e.text;
    char[] chars = new char[string.length()];
    string.getChars(0, chars.length, chars, 0);
    for (char chr : chars) {
        if (!(StringUtils.contains(Constants.ALLOWED_CHARS_FOR_FILES, chr))) {
            e.doit = false;/*from  w ww .  ja  v a2 s  .  co m*/
            return;
        }
    }
}

From source file:at.tuwien.minimee.migration.parser.Jip_Parser.java

private double interpretLine(String line) {
    char[] chars = new char[line.length()];
    line.getChars(0, line.length() - 1, chars, 0);

    String tot_time = "";

    int countWord = 0;
    int wordEntered = 0;

    for (char c : chars) {
        if (c == ' ') {
            wordEntered = 0;//from  ww  w  .ja v a2s. c  om
            continue;
        }
        if (wordEntered == 0)
            countWord++;
        wordEntered = 1;
        if (countWord == 2)
            tot_time += c;
    }
    tot_time = tot_time.replace(',', '.');
    return Double.parseDouble(tot_time);
}

From source file:org.owasp.webgoat.lessons.Encoding.java

/**
 * Description of the Method/*  w  ww.j  a v a  2 s  .  c om*/
 * 
 * @param hexString
 *            Description of the Parameter
 * @return Description of the Return Value
 */

public static String hexDecode(String hexString) {
    try {
        if ((hexString.length() % 3) != 0) {
            return ("String not comprised of Hex digit pairs.");
        }
        char[] chars = new char[hexString.length()];
        char[] convChars = new char[hexString.length() / 3];
        hexString.getChars(0, hexString.length(), chars, 0);
        for (int i = 1; i < hexString.length(); i += 3) {
            String hexToken = new String(chars, i, 2);
            convChars[i / 3] = (char) Integer.parseInt(hexToken, 16);
        }
        return new String(convChars);
    } catch (NumberFormatException nfe) {
        return ("String not comprised of Hex digits");
    }
}

From source file:org.springsource.ide.eclipse.commons.core.JdtUtils.java

public static int getLineNumber(IJavaElement element) {
    if (element != null && element instanceof IMethod) {
        try {//from w  ww  . ja  v a  2s . co  m
            IMethod method = (IMethod) element;
            int lines = 0;
            if (method.getDeclaringType() != null && method.getDeclaringType().getCompilationUnit() != null) {
                String targetsource = method.getDeclaringType().getCompilationUnit().getSource();
                if (targetsource != null) {
                    String sourceuptomethod = targetsource.substring(0, method.getNameRange().getOffset());

                    char[] chars = new char[sourceuptomethod.length()];
                    sourceuptomethod.getChars(0, sourceuptomethod.length(), chars, 0);
                    for (char element0 : chars) {
                        if (element0 == '\n') {
                            lines++;
                        }
                    }
                    return new Integer(lines + 1);
                }
            }
        } catch (JavaModelException e) {
        }
    } else if (element != null && element instanceof IType && ((IType) element).getCompilationUnit() != null) {
        try {
            IType type = (IType) element;
            int lines = 0;
            String targetsource = type.getCompilationUnit().getSource();
            if (targetsource != null) {
                String sourceuptomethod = targetsource.substring(0, type.getNameRange().getOffset());

                char[] chars = new char[sourceuptomethod.length()];
                sourceuptomethod.getChars(0, sourceuptomethod.length(), chars, 0);
                for (char element0 : chars) {
                    if (element0 == '\n') {
                        lines++;
                    }
                }
                return new Integer(lines + 1);
            }
        } catch (JavaModelException e) {
        }
    } else if (element != null && element instanceof IField) {
        try {
            IField type = (IField) element;
            int lines = 0;
            ICompilationUnit cu = type.getCompilationUnit();
            if (cu != null) {
                String targetsource = cu.getSource();
                if (targetsource != null) {
                    String sourceuptomethod = targetsource.substring(0, type.getNameRange().getOffset());

                    char[] chars = new char[sourceuptomethod.length()];
                    sourceuptomethod.getChars(0, sourceuptomethod.length(), chars, 0);
                    for (char element0 : chars) {
                        if (element0 == '\n') {
                            lines++;
                        }
                    }
                    return new Integer(lines + 1);
                }
            }
        } catch (JavaModelException e) {
        }
    }
    return new Integer(-1);
}