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

public static String concat(String[] ss, char delim) {
    int n = ss.length;
    if (n == 0)//from w ww .  j a v  a  2  s. co m
        return "";

    if (n == 1) {
        String s = ss[0];
        return s != null ? s : "";
    }
    int len = n - 1;
    for (String s : ss)
        if (s != null)
            len += s.length();

    char[] cs = new char[len];
    for (int i = 0, off = 0; i < n; ++i) {
        if (i != 0)
            cs[off++] = delim;
        String s = ss[i];
        if (s != null) {
            int l = s.length();
            s.getChars(0, l, cs, off);
            off += l;
        }
    }
    return new String(cs);
}

From source file:ma.glasnost.orika.impl.util.StringUtil.java

/**
 * Remark: Copied from commons-lang3 v3.5
 *
 * <p>Uncapitalizes a String, changing the first character to lower case as per {@link
 * Character#toLowerCase(char)}. No other characters are changed.</p>
 *
 * <p>For a word based algorithm, see {@link org.apache.commons.lang3.text.WordUtils#uncapitalize(String)}.
 * A {@code null} input String returns {@code null}.</p>
 *
 * <pre>//from ww  w. j  a v  a2  s.  c  o m
 * StringUtils.uncapitalize(null)  = null
 * StringUtils.uncapitalize("")    = ""
 * StringUtils.uncapitalize("cat") = "cat"
 * StringUtils.uncapitalize("Cat") = "cat"
 * StringUtils.uncapitalize("CAT") = "cAT"
 * </pre>
 *
 * @param str the String to uncapitalize, may be null
 * @return the uncapitalized String, {@code null} if null String input
 * @see org.apache.commons.lang3.text.WordUtils#uncapitalize(String)
 * @see #capitalize(String)
 * @since 2.0
 */
public static String uncapitalize(final String str) {
    int strLen;
    if (str == null || (strLen = str.length()) == 0) {
        return str;
    }

    final char firstChar = str.charAt(0);
    final char newChar = Character.toLowerCase(firstChar);
    if (firstChar == newChar) {
        // already uncapitalized
        return str;
    }

    char[] newChars = new char[strLen];
    newChars[0] = newChar;
    str.getChars(1, strLen, newChars, 1);
    return String.valueOf(newChars);
}

From source file:com.jaspersoft.ireport.designer.data.fieldsproviders.ejbql.EJBQLFieldsReader.java

/**
* Takes a name and returns the same if it is a Java identifier;
* else it substitutes the illegal characters so that it can be an identifier
*
* @param name/*ww w.  j a  v  a 2s.c  o m*/
*/
public static String getLiteral(String name) {
    if (isValidLiteral(name)) {
        return name;
    }

    StringBuffer buffer = new StringBuffer(name.length() + 5);

    char[] literalChars = new char[name.length()];
    name.getChars(0, literalChars.length, literalChars, 0);

    for (int i = 0; i < literalChars.length; i++) {
        if (i == 0 && !Character.isJavaIdentifierStart(literalChars[i])) {
            buffer.append((int) literalChars[i]);
        } else if (i != 0 && !Character.isJavaIdentifierPart(literalChars[i])) {
            buffer.append((int) literalChars[i]);
        } else {
            buffer.append(literalChars[i]);
        }
    }

    return buffer.toString();
}

From source file:Main.java

/**
 * Convert Base64 string to byte array.// www.  j  a va2s.com
 * 
 * @param input
 *            Base64 string.
 * @return Converted byte array.
 */
public static byte[] fromBase64(final String input) {
    String tmp = input.replace("\r\n", "");
    if (tmp.length() % 4 != 0) {
        throw new IllegalArgumentException("Invalid base64 input");
    }
    int len = (tmp.length() * 3) / 4;
    int pos = tmp.indexOf('=');
    if (pos > 0) {
        len -= tmp.length() - pos;
    }
    byte[] decoded = new byte[len];
    char[] inChars = new char[4];
    int j = 0;
    int[] b = new int[4];
    for (pos = 0; pos != tmp.length(); pos += 4) {
        tmp.getChars(pos, pos + 4, inChars, 0);
        b[0] = getIndex(inChars[0]);
        b[1] = getIndex(inChars[1]);
        b[2] = getIndex(inChars[2]);
        b[3] = getIndex(inChars[3]);
        decoded[j++] = (byte) ((b[0] << 2) | (b[1] >> 4));
        if (b[2] < 64) {
            decoded[j++] = (byte) ((b[1] << 4) | (b[2] >> 2));
            if (b[3] < 64) {
                decoded[j++] = (byte) ((b[2] << 6) | b[3]);
            }
        }
    }
    return decoded;
}

From source file:org.talend.commons.utils.StringUtils.java

public static String loadConvert(String inputStr, String language) {
    if (inputStr == null) {
        return null;
    }/*from  ww w.  j  a v a  2  s . c  o  m*/
    char[] inputChars = new char[inputStr.length()];
    inputStr.getChars(0, inputStr.length(), inputChars, 0);
    String loadConvert = null;
    if (language.equalsIgnoreCase("perl")) { //$NON-NLS-1$
        loadConvert = loadConvert(inputChars, 0, inputStr.length(), new char[inputStr.length()], 'x');
    } else {
        loadConvert = loadConvert(inputChars, 0, inputStr.length(), new char[inputStr.length()], 'u');
    }
    return loadConvert;
}

From source file:org.eclipse.swt.snippets.Snippet186.java

static Variant writeSafeArray(String string) {
    // Create a one dimensional safearray containing two VT_UI1 values
    // where VT_UI1 is an unsigned char

    // Define cDims, fFeatures and cbElements
    short cDims = 1;
    short FADF_FIXEDSIZE = 0x10;
    short FADF_HAVEVARTYPE = 0x80;
    short fFeatures = (short) (FADF_FIXEDSIZE | FADF_HAVEVARTYPE);
    int cbElements = 1;
    // Create a pointer and copy the data into it
    int count = string.length();
    char[] chars = new char[count + 1];
    string.getChars(0, count, chars, 0);
    int cchMultiByte = OS.WideCharToMultiByte(CodePage, 0, chars, -1, null, 0, null, null);
    if (cchMultiByte == 0)
        return null;
    long /*int*/ pvData = OS.GlobalAlloc(OS.GMEM_FIXED | OS.GMEM_ZEROINIT, cchMultiByte);
    OS.WideCharToMultiByte(CodePage, 0, chars, -1, pvData, cchMultiByte, null, null);
    int cElements1 = cchMultiByte;
    int lLbound1 = 0;
    // Create a safearray in memory
    long /*int*/ pSafeArray = OS.GlobalAlloc(OS.GMEM_FIXED | OS.GMEM_ZEROINIT, SAFEARRAY.sizeof);
    SAFEARRAY safeArray = new SAFEARRAY();
    safeArray.cDims = cDims;/*from   w  w  w  .  j  av a  2  s.  c  om*/
    safeArray.fFeatures = fFeatures;
    safeArray.cbElements = cbElements;
    safeArray.pvData = pvData;
    SAFEARRAYBOUND safeArrayBound = new SAFEARRAYBOUND();
    safeArray.rgsabound = safeArrayBound;
    safeArrayBound.cElements = cElements1;
    safeArrayBound.lLbound = lLbound1;
    OS.MoveMemory(pSafeArray, safeArray, SAFEARRAY.sizeof);
    // Create a variant in memory to hold the safearray
    long /*int*/ pVariant = OS.GlobalAlloc(OS.GMEM_FIXED | OS.GMEM_ZEROINIT, Variant.sizeof);
    short vt = (short) (OLE.VT_ARRAY | OLE.VT_UI1);
    OS.MoveMemory(pVariant, new short[] { vt }, 2);
    OS.MoveMemory(pVariant + 8, new long /*int*/[] { pSafeArray }, OS.PTR_SIZEOF);
    // Create a by ref variant
    Variant variantByRef = new Variant(pVariant, (short) (OLE.VT_BYREF | OLE.VT_VARIANT));
    return variantByRef;
}

From source file:Snippet186.java

static Variant writeSafeArray(String string) {
    // Create a one dimensional safearray containing two VT_UI1 values
    // where VT_UI1 is an unsigned char

    // Define cDims, fFeatures and cbElements
    short cDims = 1;
    short FADF_FIXEDSIZE = 0x10;
    short FADF_HAVEVARTYPE = 0x80;
    short fFeatures = (short) (FADF_FIXEDSIZE | FADF_HAVEVARTYPE);
    int cbElements = 1;
    // Create a pointer and copy the data into it
    int count = string.length();
    char[] chars = new char[count + 1];
    string.getChars(0, count, chars, 0);
    int cchMultiByte = OS.WideCharToMultiByte(CodePage, 0, chars, -1, null, 0, null, null);
    if (cchMultiByte == 0)
        return null;
    int pvData = OS.GlobalAlloc(OS.GMEM_FIXED | OS.GMEM_ZEROINIT, cchMultiByte);
    OS.WideCharToMultiByte(CodePage, 0, chars, -1, pvData, cchMultiByte, null, null);
    int cElements1 = cchMultiByte;
    int lLbound1 = 0;
    // Create a safearray in memory
    // 12 bytes for cDims, fFeatures and cbElements + 4 bytes for pvData +
    // number of dimensions * (size of safearraybound)
    int sizeofSafeArray = 12 + 4 + 1 * 8;
    int pSafeArray = OS.GlobalAlloc(OS.GMEM_FIXED | OS.GMEM_ZEROINIT, sizeofSafeArray);
    // Copy the data into the safe array
    int offset = 0;
    OS.MoveMemory(pSafeArray + offset, new short[] { cDims }, 2);
    offset += 2;/*from ww  w . ja va 2 s  .  com*/
    OS.MoveMemory(pSafeArray + offset, new short[] { fFeatures }, 2);
    offset += 2;
    OS.MoveMemory(pSafeArray + offset, new int[] { cbElements }, 4);
    offset += 4;
    OS.MoveMemory(pSafeArray + offset, new int[] { 0 }, 4);
    offset += 4;
    OS.MoveMemory(pSafeArray + offset, new int[] { pvData }, 4);
    offset += 4;
    OS.MoveMemory(pSafeArray + offset, new int[] { cElements1 }, 4);
    offset += 4;
    OS.MoveMemory(pSafeArray + offset, new int[] { lLbound1 }, 4);
    offset += 4;
    // Create a variant in memory to hold the safearray
    int pVariant = OS.GlobalAlloc(OS.GMEM_FIXED | OS.GMEM_ZEROINIT, Variant.sizeof);
    short vt = (short) (OLE.VT_ARRAY | OLE.VT_UI1);
    OS.MoveMemory(pVariant, new short[] { vt }, 2);
    OS.MoveMemory(pVariant + 8, new int[] { pSafeArray }, 4);
    // Create a by ref variant
    Variant variantByRef = new Variant(pVariant, (short) (OLE.VT_BYREF | OLE.VT_VARIANT));
    return variantByRef;
}

From source file:org.etudes.jforum.dao.oracle.OracleUtils.java

/**
 * Writes clob to database //  w  w  w  .  ja va 2 s. c  om
 * @param query
 * @param idForQuery
 * @param value
 * @throws IOException
 * @throws SQLException
 */
public static void writeClobUTF16BinaryStream(String query, int idForQuery, String value)
        throws IOException, SQLException {
    PreparedStatement p = JForum.getConnection().prepareStatement(query);
    p.setInt(1, idForQuery);

    ResultSet rs = p.executeQuery();
    rs.next();
    Clob clobval = rs.getClob(1);

    if (logger.isDebugEnabled())
        logger.debug("clobval is " + clobval.getClass().getName());

    Writer clobWriter = clobval.setCharacterStream(0L);

    char[] cbuffer = new char[value.length()];
    int srcBegin = 0, dstBegin = 0;
    value.getChars(srcBegin, value.length(), cbuffer, dstBegin);
    clobWriter.write(cbuffer);

    clobWriter.close();

    rs.close();
    p.close();
}

From source file:Base64.java

/**
 * Decode the base64 data./*w  w  w .  j a  va  2  s.  com*/
 * @param data The base64 encoded data to be decoded
 * @param ostream The OutputStream to which the decoded data should be
 *                written
 */
public static void decode(String data, OutputStream ostream) throws IOException {
    char[] ibuf = new char[BUF_SIZE + 4];
    byte[] obuf = new byte[3];
    int slen = data.length();
    int blen;
    int ibufcount = 0;

    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 sson as we have 4 chars process them
                if (ibufcount == 4) {
                    ibufcount = 0;
                    int obufcount = decode0(ibuf, obuf, 0);
                    ostream.write(obuf, 0, obufcount);
                }
            }
        }
    }
}

From source file:com.shishu.utility.string.StringUtil.java

/**
 * ????/*from  www .j  a  va  2 s .co  m*/
 * 
 * @param s1
 *            1
 * @param s2
 *            2
 * @return 
 * 
 * @author wangtao
 */
public static String prepareURLString(String s1, String s2) {
    int len1 = s1.length();
    int len2 = s2.length();

    char[] b1 = new char[len1];
    char[] b2 = new char[len2];

    s1.getChars(0, len1, b1, 0);
    s2.getChars(0, len2, b2, 0);

    StringBuilder stringBuilder = new StringBuilder("");

    // ???@
    int dis = Math.abs(len1 - len2);
    int j = 0;
    char[] high, low;

    if (len1 > len2) {
        high = b1;
        low = b2;
    } else {
        high = b2;
        low = b1;
    }

    for (int i = 0; i < high.length; i++) {

        if (high[i] == low[j]) {
            stringBuilder.append(high[i]);
        } else if (stringBuilder.charAt(stringBuilder.length() - 1) != '@') {
            stringBuilder.append("@");
        }

        if (stringBuilder.charAt(stringBuilder.length() - 1) == '@' && dis > 0) {
            dis--;
        } else {
            j++;
        }
    }

    return stringBuilder.toString();
}