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

/**
 * append/*from  w w  w .j  a v a  2s .c o m*/
 * 
 * @param s 
 */
public void append(String s) {
    int length = s.length();
    if (this.length + length > this.ch.length) {
        int newLength = this.ch.length * 2;
        if (newLength < this.length + length + DEFAULT_SIZE)
            newLength = this.ch.length + length + DEFAULT_SIZE;
        char[] newch = new char[newLength];
        System.arraycopy(this.ch, 0, newch, 0, this.length);
        this.ch = newch;
    }
    s.getChars(0, length, this.ch, this.length);
    this.length += length;
}

From source file:ArabicReshaper.java

/**
 * Main Reshaping function, Doesn't Support LamAlef
 * @param unshapedWord The unReshaped Word to Reshape
 * @return The Reshaped Word without the LamAlef Support
 *///from  w  w w .j  av a  2 s .c o m
public String reshapeIt(String unshapedWord) {

    //The reshaped Word to Return
    StringBuffer reshapedWord = new StringBuffer("");

    //The Word length
    int wordLength = unshapedWord.length();

    //The Word Letters
    char[] wordLetters = new char[wordLength];

    //Copy the unreshapedWord to the WordLetters Character Array
    unshapedWord.getChars(0, wordLength, wordLetters, 0);

    //for the first letter
    reshapedWord.append(getReshapedGlphy(wordLetters[0], 2));//2 is the Form when the Letter is at the start of the word

    //iteration from the second till the second to last
    for (int i = 1; i < wordLength - 1; i++) {
        int beforeLast = i - 1;
        //Check if the Letter Before Last has only 2 Forms, for the current Letter to be as a start for a new Word!
        if (getGlphyType(wordLetters[beforeLast]) == 2) { //checking if it's only has 2 shapes
            //If the letter has only 2 shapes, then it doesnt matter which position it is, It'll be always the second form
            reshapedWord.append(getReshapedGlphy(wordLetters[i], 2));
        } else {
            //Then it should be in the middle which should be placed in its right form [3]
            reshapedWord.append(getReshapedGlphy(wordLetters[i], 3));
        }
    }

    //check for the last letter Before last has 2 forms, that means that the last Letter will be alone.
    if (getGlphyType(wordLetters[wordLength - 2]) == 2) {
        //If the letter has only 2 shapes, then it doesnt matter which position it is, It'll be always the second form
        reshapedWord.append(getReshapedGlphy(wordLetters[wordLength - 1], 1));
    } else {
        //Put the right form of the character, 4 for the last letter in the word
        reshapedWord.append(getReshapedGlphy(wordLetters[wordLength - 1], 4));
    }

    //Return the ReshapedWord
    return reshapedWord.toString();
}

From source file:xbird.xquery.misc.StringChunk.java

/**
 * Store a String and return the stored address.
 *///w w  w .  j  ava2  s  .co  m
public final long store(final String s) {
    final int strlen = s.length();
    if (strlen < CHUNKED_THRESHOLD) {
        s.getChars(0, strlen, tmpBuf, 0);
        return storeCharChunk(tmpBuf, 0, strlen);
    }
    final byte[] b = StringUtils.getBytes(s);
    return storeStringChunk(b);
}

From source file:org.ralasafe.db.sql.Query.java

/**
 * Add line breaker at propriety positions, to format for web view
 * @param script//from   www .  ja  v a 2 s .  c o  m
 * @return
 */
private String format(String script) {
    int maxInLine = 80;
    int lengthOfLine = 0;
    int indexOfTheLatestSpaceOrComma = 0;
    char[] dst = new char[script.length()];
    script.getChars(0, script.length(), dst, 0);
    StringBuffer buf = new StringBuffer();
    char replacedChar = ' ';

    for (int i = 0; i < dst.length; i++, lengthOfLine++) {
        char curChar = dst[i];
        buf.append(curChar);

        if (curChar == '\n') {
            // reset counter
            lengthOfLine = 0;
            continue;
        }

        if (curChar == ' ' || curChar == '\t' || curChar == ',') {
            replacedChar = curChar;
            indexOfTheLatestSpaceOrComma = buf.length() - 1;
        }

        if (lengthOfLine == maxInLine) {
            // break line
            if (replacedChar == ',') {
                buf.replace(indexOfTheLatestSpaceOrComma, indexOfTheLatestSpaceOrComma + 1, ",\n ");
            } else {
                buf.replace(indexOfTheLatestSpaceOrComma, indexOfTheLatestSpaceOrComma + 1, "\n ");
            }
            lengthOfLine = (buf.length() - indexOfTheLatestSpaceOrComma + 1);
        }
    }
    return buf.toString();
}

From source file:com.badugi.game.logic.model.utils.common.TextUtils.java

/**
 * Escape HTML.//from w w  w. ja  v  a  2  s. co m
 * 
 * @param s
 *            string to be escaped
 * @param escapeEmpty
 *            if true, then empty string will be escaped.
 */
public static final String escapeHTML(String s, boolean escapeEmpty) {
    if (s == null)
        return null;
    int len = s.length();

    if (len == 0) {
        return s;
    }

    if (!escapeEmpty) {
        String trimmed = s.trim();

        if ((trimmed.length() == 0) || ("\"\"").equals(trimmed)) {
            return s;
        }
    }

    int i = 0;

    // First loop through String and check if escaping is needed at all
    // No buffers are copied at this time
    do {
        int index = s.charAt(i);

        if (index >= MAX_LENGTH) {
            if (index != 0x20AC) { // If not euro symbol

                continue;
            }

            break;
        } else if (_stringChars[index] != null) {
            break;
        }
    } while (++i < len);

    // If the check went to the end with no escaping then i should be == len
    // now
    // otherwise we must continue escaping for real
    if (i == len) {
        return s;
    }

    // We found a character to escape and broke out at position i
    // Now copy all characters before that to StringBuffer sb
    // Since a char[] will be used for copying we might as well get
    // a complete copy of it so that we can use array indexing instead of
    // charAt
    StringBuffer sb = new StringBuffer(len + 40);
    char[] chars = new char[len];

    // Copy all chars from the String s to the chars buffer
    s.getChars(0, len, chars, 0);

    // Append the first i characters that we have checked to the resulting
    // StringBuffer
    sb.append(chars, 0, i);

    int last = i;
    char[] subst;

    for (; i < len; i++) {
        char c = chars[i];
        int index = c;

        if (index < MAX_LENGTH) {
            subst = _stringChars[index];

            // It is faster to append a char[] than a String which is why we
            // use this
            if (subst != null) {
                if (i > last) {
                    sb.append(chars, last, i - last);
                }

                sb.append(subst);
                last = i + 1;
            }
        }
        // Check if it is the euro symbol. This could be changed to check in
        // a second lookup
        // table in case one wants to convert more characters in that area
        else if (index == 0x20AC) {
            if (i > last) {
                sb.append(chars, last, i - last);
            }

            sb.append("&euro;");
            last = i + 1;
        }
    }

    if (i > last) {
        sb.append(chars, last, i - last);
    }

    return sb.toString();
}

From source file:com.casmall.common.dialog.MessageConfig.java

/**
 * Verify input(Only allow digits)//from  www  .j a v  a  2s.  c o  m
 * @param evt
 */
private void verifyOnlyDigits(VerifyEvent evt) {
    String string = evt.text;
    char[] chars = new char[string.length()];
    string.getChars(0, chars.length, chars, 0);
    for (int i = 0; i < chars.length; i++) {
        if (!('0' <= chars[i] && chars[i] <= '9' || chars[i] == '-')) {
            evt.doit = false;
            return;
        } // if
    } // for
}

From source file:com.matteoveroni.model.copy.FilenameUtils.java

/**
 * Internal method to perform the normalization.
 *
 * @param filename  the filename//from w  w w.  ja  v  a 2  s.  c om
 * @param separator The separator character to use
 * @param keepSeparator  true to keep the final separator
 * @return the normalized filename
 */
private static String doNormalize(String filename, char separator, boolean keepSeparator) {
    if (filename == null) {
        return null;
    }
    int size = filename.length();
    if (size == 0) {
        return filename;
    }
    int prefix = getPrefixLength(filename);
    if (prefix < 0) {
        return null;
    }

    char[] array = new char[size + 2]; // +1 for possible extra slash, +2 for arraycopy
    filename.getChars(0, filename.length(), array, 0);

    // fix separators throughout
    char otherSeparator = separator == SYSTEM_SEPARATOR ? OTHER_SEPARATOR : SYSTEM_SEPARATOR;
    for (int i = 0; i < array.length; i++) {
        if (array[i] == otherSeparator) {
            array[i] = separator;
        }
    }

    // add extra separator on the end to simplify code below
    boolean lastIsDirectory = true;
    if (array[size - 1] != separator) {
        array[size++] = separator;
        lastIsDirectory = false;
    }

    // adjoining slashes
    for (int i = prefix + 1; i < size; i++) {
        if (array[i] == separator && array[i - 1] == separator) {
            System.arraycopy(array, i, array, i - 1, size - i);
            size--;
            i--;
        }
    }

    // dot slash
    for (int i = prefix + 1; i < size; i++) {
        if (array[i] == separator && array[i - 1] == '.' && (i == prefix + 1 || array[i - 2] == separator)) {
            if (i == size - 1) {
                lastIsDirectory = true;
            }
            System.arraycopy(array, i + 1, array, i - 1, size - i);
            size -= 2;
            i--;
        }
    }

    // double dot slash
    outer: for (int i = prefix + 2; i < size; i++) {
        if (array[i] == separator && array[i - 1] == '.' && array[i - 2] == '.'
                && (i == prefix + 2 || array[i - 3] == separator)) {
            if (i == prefix + 2) {
                return null;
            }
            if (i == size - 1) {
                lastIsDirectory = true;
            }
            int j;
            for (j = i - 4; j >= prefix; j--) {
                if (array[j] == separator) {
                    // remove b/../ from a/b/../c
                    System.arraycopy(array, i + 1, array, j + 1, size - i);
                    size -= i - j;
                    i = j + 1;
                    continue outer;
                }
            }
            // remove a/../ from a/../c
            System.arraycopy(array, i + 1, array, prefix, size - i);
            size -= i + 1 - prefix;
            i = prefix + 1;
        }
    }

    if (size <= 0) { // should never be less than 0
        return "";
    }
    if (size <= prefix) { // should never be less than prefix
        return new String(array, 0, size);
    }
    if (lastIsDirectory && keepSeparator) {
        return new String(array, 0, size); // keep trailing separator
    }
    return new String(array, 0, size - 1); // lose trailing separator
}

From source file:ClosableCharArrayWriter.java

/**
 * Efficiently writes the designated portion of the designated string. 
 *
 * The operation occurs as if by calling
 * <tt>str.getChars(off, off + len, buf, count)</tt>. 
 *
 * @param str the string from which to write
 * @param off the start offset in the string.
 * @param len the number of characters to write.
 * @throws java.io.IOException if an I/O error occurs.
 *      In particular, an <tt>IOException</tt> may be thrown
 *      if this writer has been {@link #close() closed}.
 *//* w  w  w .j  a v a  2s . c  o m*/
public synchronized void write(String str, int off, int len) throws IOException {

    checkClosed();

    int strlen = str.length();

    if ((off < 0) || (off > strlen) || (len < 0) || ((off + len) > strlen) || ((off + len) < 0)) {
        throw new IndexOutOfBoundsException();
    } else if (len == 0) {
        return;
    }

    int newcount = count + len;

    if (newcount > buf.length) {
        buf = copyOf(buf, Math.max(buf.length << 1, newcount));
    }

    str.getChars(off, off + len, buf, count);

    count = newcount;
}

From source file:org.apache.axis2.jaxws.message.util.impl.XMLStreamReaderFromDOM.java

public int getTextCharacters(int sourceStart, char[] target, int targetStart, int length)
        throws XMLStreamException {
    String value = getText();
    // Calculate the sourceEnd index
    int sourceEnd = sourceStart + length;
    if (value.length() < sourceEnd) {
        sourceEnd = value.length();//  w w w.  ja  v  a  2 s  .c o m
    }
    value.getChars(sourceStart, sourceEnd, target, targetStart);
    return sourceEnd - sourceStart;
}

From source file:FilenameUtils.java

/**
 * Internal method to perform the normalization.
 *
 * @param filename  the filename/* w  w  w  .jav a2s  . c o m*/
 * @param keepSeparator  true to keep the final separator
 * @return the normalized filename
 */
private static String doNormalize(String filename, boolean keepSeparator) {
    if (filename == null) {
        return null;
    }
    int size = filename.length();
    if (size == 0) {
        return filename;
    }
    int prefix = getPrefixLength(filename);
    if (prefix < 0) {
        return null;
    }

    char[] array = new char[size + 2]; // +1 for possible extra slash, +2 for arraycopy
    filename.getChars(0, filename.length(), array, 0);

    // fix separators throughout
    for (int i = 0; i < array.length; i++) {
        if (array[i] == OTHER_SEPARATOR) {
            array[i] = SYSTEM_SEPARATOR;
        }
    }

    // add extra separator on the end to simplify code below
    boolean lastIsDirectory = true;
    if (array[size - 1] != SYSTEM_SEPARATOR) {
        array[size++] = SYSTEM_SEPARATOR;
        lastIsDirectory = false;
    }

    // adjoining slashes
    for (int i = prefix + 1; i < size; i++) {
        if (array[i] == SYSTEM_SEPARATOR && array[i - 1] == SYSTEM_SEPARATOR) {
            System.arraycopy(array, i, array, i - 1, size - i);
            size--;
            i--;
        }
    }

    // dot slash
    for (int i = prefix + 1; i < size; i++) {
        if (array[i] == SYSTEM_SEPARATOR && array[i - 1] == '.'
                && (i == prefix + 1 || array[i - 2] == SYSTEM_SEPARATOR)) {
            if (i == size - 1) {
                lastIsDirectory = true;
            }
            System.arraycopy(array, i + 1, array, i - 1, size - i);
            size -= 2;
            i--;
        }
    }

    // double dot slash
    outer: for (int i = prefix + 2; i < size; i++) {
        if (array[i] == SYSTEM_SEPARATOR && array[i - 1] == '.' && array[i - 2] == '.'
                && (i == prefix + 2 || array[i - 3] == SYSTEM_SEPARATOR)) {
            if (i == prefix + 2) {
                return null;
            }
            if (i == size - 1) {
                lastIsDirectory = true;
            }
            int j;
            for (j = i - 4; j >= prefix; j--) {
                if (array[j] == SYSTEM_SEPARATOR) {
                    // remove b/../ from a/b/../c
                    System.arraycopy(array, i + 1, array, j + 1, size - i);
                    size -= (i - j);
                    i = j + 1;
                    continue outer;
                }
            }
            // remove a/../ from a/../c
            System.arraycopy(array, i + 1, array, prefix, size - i);
            size -= (i + 1 - prefix);
            i = prefix + 1;
        }
    }

    if (size <= 0) { // should never be less than 0
        return "";
    }
    if (size <= prefix) { // should never be less than prefix
        return new String(array, 0, size);
    }
    if (lastIsDirectory && keepSeparator) {
        return new String(array, 0, size); // keep trailing separator
    }
    return new String(array, 0, size - 1); // lose trailing separator
}