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:com.bazaarvoice.jackson.rison.RisonGenerator.java

private void _writeRaw(String text) throws IOException, JsonGenerationException {
    // Nothing to check, can just output as is
    int len = text.length();
    int room = _outputEnd - _outputTail;

    if (room == 0) {
        _flushBuffer();/*from   ww w .  ja v  a 2 s . co m*/
        room = _outputEnd - _outputTail;
    }
    // But would it nicely fit in? If yes, it's easy
    if (room >= len) {
        text.getChars(0, len, _outputBuffer, _outputTail);
        _outputTail += len;
    } else {
        _writeRawLong(text);
    }
}

From source file:com.bazaarvoice.jackson.rison.RisonGenerator.java

/**
 * Method called to write "long strings", strings whose length exceeds
 * output buffer length.//from  ww w  . ja v a2  s.  com
 */
private void _writeLongString(String text) throws IOException, JsonGenerationException {
    // First things first: let's flush the buffer to get some more room
    _flushBuffer();

    // Then we can write
    final int textLen = text.length();
    int offset = 0;
    do {
        int max = _outputEnd;
        int segmentLen = ((offset + max) > textLen) ? (textLen - offset) : max;
        text.getChars(offset, offset + segmentLen, _outputBuffer, 0);
        _writeSegment(segmentLen);
        offset += segmentLen;
    } while (offset < textLen);
}

From source file:org.jahia.services.search.jcr.JahiaJCRSearchProvider.java

/**
 * To replace accented characters in a String by unaccented equivalents.
 */// ww w.j a v a  2s. co  m
private String removeAccents(String term) {
    int length = term.length();
    char[] input = new char[length];
    term.getChars(0, length, input, 0);

    char[] output = new char[length];
    // Worst-case length required:
    final int maxSizeNeeded = 2 * length;

    int size = output.length;
    while (size < maxSizeNeeded)
        size *= 2;

    if (size != output.length)
        output = new char[size];

    int outputPos = 0;

    int pos = 0;

    for (int i = 0; i < length; i++, pos++) {
        final char c = input[pos];

        // Quick test: if it's not in range then just keep
        // current character
        if (c < '\u00c0' || c > '\uFB06')
            output[outputPos++] = c;
        else {
            switch (c) {
            case '\u00C0': // 
            case '\u00C1': // 
            case '\u00C2': // 
            case '\u00C3': // 
            case '\u00C4': // 
            case '\u00C5': // 
                output[outputPos++] = 'A';
                break;
            case '\u00C6': // 
                output[outputPos++] = 'A';
                output[outputPos++] = 'E';
                break;
            case '\u00C7': // 
                output[outputPos++] = 'C';
                break;
            case '\u00C8': // 
            case '\u00C9': // 
            case '\u00CA': // 
            case '\u00CB': // 
                output[outputPos++] = 'E';
                break;
            case '\u00CC': // 
            case '\u00CD': // 
            case '\u00CE': // 
            case '\u00CF': // 
                output[outputPos++] = 'I';
                break;
            case '\u0132': // 
                output[outputPos++] = 'I';
                output[outputPos++] = 'J';
                break;
            case '\u00D0': // 
                output[outputPos++] = 'D';
                break;
            case '\u00D1': // 
                output[outputPos++] = 'N';
                break;
            case '\u00D2': // 
            case '\u00D3': // 
            case '\u00D4': // ?
            case '\u00D5': // 
            case '\u00D6': // 
            case '\u00D8': // 
                output[outputPos++] = 'O';
                break;
            case '\u0152': // 
                output[outputPos++] = 'O';
                output[outputPos++] = 'E';
                break;
            case '\u00DE': // 
                output[outputPos++] = 'T';
                output[outputPos++] = 'H';
                break;
            case '\u00D9': // 
            case '\u00DA': // 
            case '\u00DB': // 
            case '\u00DC': // 
                output[outputPos++] = 'U';
                break;
            case '\u00DD': // 
            case '\u0178': // 
                output[outputPos++] = 'Y';
                break;
            case '\u00E0': // 
            case '\u00E1': // 
            case '\u00E2': // 
            case '\u00E3': // 
            case '\u00E4': // 
            case '\u00E5': // 
                output[outputPos++] = 'a';
                break;
            case '\u00E6': // 
                output[outputPos++] = 'a';
                output[outputPos++] = 'e';
                break;
            case '\u00E7': // 
                output[outputPos++] = 'c';
                break;
            case '\u00E8': // 
            case '\u00E9': // 
            case '\u00EA': // 
            case '\u00EB': // 
                output[outputPos++] = 'e';
                break;
            case '\u00EC': // 
            case '\u00ED': // 
            case '\u00EE': // 
            case '\u00EF': // 
                output[outputPos++] = 'i';
                break;
            case '\u0133': // 
                output[outputPos++] = 'i';
                output[outputPos++] = 'j';
                break;
            case '\u00F0': // 
                output[outputPos++] = 'd';
                break;
            case '\u00F1': // 
                output[outputPos++] = 'n';
                break;
            case '\u00F2': // 
            case '\u00F3': // 
            case '\u00F4': // 
            case '\u00F5': // 
            case '\u00F6': // 
            case '\u00F8': // 
                output[outputPos++] = 'o';
                break;
            case '\u0153': // 
                output[outputPos++] = 'o';
                output[outputPos++] = 'e';
                break;
            case '\u00DF': // 
                output[outputPos++] = 's';
                output[outputPos++] = 's';
                break;
            case '\u00FE': // 
                output[outputPos++] = 't';
                output[outputPos++] = 'h';
                break;
            case '\u00F9': // 
            case '\u00FA': // 
            case '\u00FB': // 
            case '\u00FC': // 
                output[outputPos++] = 'u';
                break;
            case '\u00FD': // 
            case '\u00FF': // 
                output[outputPos++] = 'y';
                break;
            case '\uFB00': // 
                output[outputPos++] = 'f';
                output[outputPos++] = 'f';
                break;
            case '\uFB01': // 
                output[outputPos++] = 'f';
                output[outputPos++] = 'i';
                break;
            case '\uFB02': // 
                output[outputPos++] = 'f';
                output[outputPos++] = 'l';
                break;
            // following 2 are commented as they can break the maxSizeNeeded (and doing *3 could be expensive)
            //          case '\uFB03': // 
            //              output[outputPos++] = 'f';
            //              output[outputPos++] = 'f';
            //              output[outputPos++] = 'i';
            //              break;
            //          case '\uFB04': // 
            //              output[outputPos++] = 'f';
            //              output[outputPos++] = 'f';
            //              output[outputPos++] = 'l';
            //              break;
            case '\uFB05': // 
                output[outputPos++] = 'f';
                output[outputPos++] = 't';
                break;
            case '\uFB06': // 
                output[outputPos++] = 's';
                output[outputPos++] = 't';
                break;
            default:
                output[outputPos++] = c;
                break;
            }
        }
    }
    return (new String(output)).trim();
}

From source file:org.apache.axiom.om.impl.llom.OMStAXWrapper.java

/**
 * @param sourceStart//from  w ww.  j  a va  2s.  co  m
 * @param target
 * @param targetStart
 * @param length
 * @return Returns int.
 * @throws XMLStreamException
 * @see javax.xml.stream.XMLStreamReader#getTextCharacters(int, char[], int, int)
 */
public int getTextCharacters(int sourceStart, char[] target, int targetStart, int length)
        throws XMLStreamException {
    if (parser != null) {
        try {
            return parser.getTextCharacters(sourceStart, target, targetStart, length);
        } catch (XMLStreamException e) {
            throw new OMStreamingException(e);
        }
    } else {
        String text = getTextFromNode();
        if (text != null) {
            int copied = Math.min(length, text.length() - sourceStart);
            text.getChars(sourceStart, sourceStart + copied, target, targetStart);
            return copied;
        } else {
            return 0;
        }
    }
}

From source file:org.apache.axiom.om.impl.SwitchingWrapper.java

/**
 * @param sourceStart/*from  ww  w .ja v  a 2 s.c  o m*/
 * @param target
 * @param targetStart
 * @param length
 * @return Returns int.
 * @throws XMLStreamException
 * @see javax.xml.stream.XMLStreamReader#getTextCharacters(int, char[], int, int)
 */
public int getTextCharacters(int sourceStart, char[] target, int targetStart, int length)
        throws XMLStreamException {
    if (parser != null) {
        try {
            return parser.getTextCharacters(sourceStart, target, targetStart, length);
        } catch (XMLStreamException e) {
            throw new OMStreamingException(e);
        }
    } else {
        if (currentEvent == DTD) {
            // Not sure if that conforms to the StAX spec, but it is what Woodstox does
            throw new IllegalStateException();
        } else {
            String text = getTextFromNode();
            int copied = Math.min(length, text.length() - sourceStart);
            text.getChars(sourceStart, sourceStart + copied, target, targetStart);
            return copied;
        }
    }
}

From source file:com.bazaarvoice.jackson.rison.RisonGenerator.java

private void _writeString(String text) throws IOException, JsonGenerationException {
    /* One check first: if String won't fit in the buffer, let's
     * segment writes. No point in extending buffer to huge sizes
     * (like if someone wants to include multi-megabyte base64
     * encoded stuff or such)/*from  w w w  . jav  a  2s  .  co  m*/
     */
    final int len = text.length();
    if (len > _outputEnd) { // Let's reserve space for entity at begin/end
        _writeLongString(text);
        return;
    }

    // Ok: we know String will fit in buffer ok
    // But do we need to flush first?
    if ((_outputTail + len) > _outputEnd) {
        _flushBuffer();
    }
    text.getChars(0, len, _outputBuffer, _outputTail);

    _writeString2(len);
}

From source file:FastStringBuffer.java

/**
 * Append the contents of a String onto the FastStringBuffer,
 * growing the storage if necessary.//from w  ww .  j a  v a2s .co  m
 * <p>
 * NOTE THAT after calling append(), previously obtained
 * references to m_array[] may no longer be valid.
 *
 * @param value String whose contents are to be appended.
 */
public final void append(String value) {

    if (value == null)
        return;
    int strlen = value.length();

    if (0 == strlen)
        return;

    int copyfrom = 0;
    char[] chunk = m_array[m_lastChunk];
    int available = m_chunkSize - m_firstFree;

    // Repeat while data remains to be copied
    while (strlen > 0) {

        // Copy what fits
        if (available > strlen)
            available = strlen;

        value.getChars(copyfrom, copyfrom + available, m_array[m_lastChunk], m_firstFree);

        strlen -= available;
        copyfrom += available;

        // If there's more left, allocate another chunk and continue
        if (strlen > 0) {

            // Extend array?
            int i = m_array.length;

            if (m_lastChunk + 1 == i) {
                char[][] newarray = new char[i + 16][];

                System.arraycopy(m_array, 0, newarray, 0, i);

                m_array = newarray;
            }

            // Advance one chunk
            chunk = m_array[++m_lastChunk];

            if (chunk == null) {

                // Hierarchical encapsulation
                if (m_lastChunk == 1 << m_rebundleBits && m_chunkBits < m_maxChunkBits) {

                    // Should do all the work of both encapsulating
                    // existing data and establishing new sizes/offsets
                    m_innerFSB = new FastStringBuffer(this);
                }

                // Add a chunk. 
                chunk = m_array[m_lastChunk] = new char[m_chunkSize];
            }

            available = m_chunkSize;
            m_firstFree = 0;
        }
    }

    // Adjust the insert point in the last chunk, when we've reached it.
    m_firstFree += available;
}

From source file:se.sawano.java.security.otp.google.keyuri.UnicodeEscaper.java

/**
 * Returns the escaped form of a given literal string, starting at the given index. This method is
 * called by the {@link #escape(String)} method when it discovers that escaping is required. It is
 * protected to allow subclasses to override the fastpath escaping function to inline their
 * escaping test. See {@link CharEscaperBuilder} for an example usage.
 *
 * <p>This method is not reentrant and may only be invoked by the top level
 * {@link #escape(String)} method.//from   w w  w. ja v a  2 s  .  c o m
 *
 * @param s
 *         the literal string to be escaped
 * @param index
 *         the index to start escaping from
 *
 * @return the escaped form of {@code string}
 *
 * @throws NullPointerException
 *         if {@code string} is null
 * @throws IllegalArgumentException
 *         if invalid surrogate characters are encountered
 */
protected final String escapeSlow(String s, int index) {
    int end = s.length();

    // Get a destination buffer and setup some loop variables.
    char[] dest = new char[1024];
    int destIndex = 0;
    int unescapedChunkStart = 0;

    while (index < end) {
        int cp = codePointAt(s, index, end);
        if (cp < 0) {
            throw new IllegalArgumentException("Trailing high surrogate at end of input");
        }
        // It is possible for this to return null because nextEscapeIndex() may
        // (for performance reasons) yield some false positives but it must never
        // give false negatives.
        char[] escaped = escape(cp);
        int nextIndex = index + (Character.isSupplementaryCodePoint(cp) ? 2 : 1);
        if (escaped != null) {
            int charsSkipped = index - unescapedChunkStart;

            // This is the size needed to add the replacement, not the full
            // size needed by the string. We only regrow when we absolutely must.
            int sizeNeeded = destIndex + charsSkipped + escaped.length;
            if (dest.length < sizeNeeded) {
                int destLength = sizeNeeded + (end - index) + DEST_PAD;
                dest = growBuffer(dest, destIndex, destLength);
            }
            // If we have skipped any characters, we need to copy them now.
            if (charsSkipped > 0) {
                s.getChars(unescapedChunkStart, index, dest, destIndex);
                destIndex += charsSkipped;
            }
            if (escaped.length > 0) {
                System.arraycopy(escaped, 0, dest, destIndex, escaped.length);
                destIndex += escaped.length;
            }
            // If we dealt with an escaped character, reset the unescaped range.
            unescapedChunkStart = nextIndex;
        }
        index = nextEscapeIndex(s, nextIndex, end);
    }

    // Process trailing unescaped characters - no need to account for escaped
    // length or padding the allocation.
    int charsSkipped = end - unescapedChunkStart;
    if (charsSkipped > 0) {
        int endIndex = destIndex + charsSkipped;
        if (dest.length < endIndex) {
            dest = growBuffer(dest, destIndex, endIndex);
        }
        s.getChars(unescapedChunkStart, end, dest, destIndex);
        destIndex = endIndex;
    }
    return new String(dest, 0, destIndex);
}

From source file:com.bazaarvoice.jackson.rison.RisonParser.java

@Override
public char[] getTextCharacters() throws IOException, JsonParseException {
    if (_currToken != null) { // null only before/after document
        switch (_currToken) {

        case FIELD_NAME:
            if (!_nameCopied) {
                String name = _parsingContext.getCurrentName();
                int nameLen = name.length();
                if (_nameCopyBuffer == null) {
                    _nameCopyBuffer = _ioContext.allocNameCopyBuffer(nameLen);
                } else if (_nameCopyBuffer.length < nameLen) {
                    _nameCopyBuffer = new char[nameLen];
                }/*from   ww  w . j  av a 2s  .c  om*/
                name.getChars(0, nameLen, _nameCopyBuffer, 0);
                _nameCopied = true;
            }
            return _nameCopyBuffer;

        case VALUE_STRING:
            if (_tokenIncomplete) {
                _tokenIncomplete = false;
                _finishString(); // only strings can be incomplete
            }
            // fall through
        case VALUE_NUMBER_INT:
        case VALUE_NUMBER_FLOAT:
            return _textBuffer.getTextBuffer();

        default:
            return _currToken.asCharArray();
        }
    }
    return null;
}

From source file:org.apache.noggit.JSONParser.java

public void write(String s, int stringOffset, int len) {
        reserve(len);// ww w . j  a v a  2 s . c o  m
        s.getChars(stringOffset, len, buf, end);
        end += len;
    }