android cursor getString vs copyStringToBuffer为什么copyStringToBuffer优化了?

时间:2014-01-17 03:48:15

标签: android

我看到android代码DownloadInfo.java,代码块如下,注释意味着它已优化了getString,但我不认为。谁能解释为什么它已经优化? 代码是:

info.mUri = getString(info.mUri, Downloads.COLUMN_URI);// call the getString method

/**
     * Returns a String that holds the current value of the column, optimizing for the case
     * where the value hasn't changed.
     */
    private String getString(String old, String column) {
        int index = mCursor.getColumnIndexOrThrow(column);
        if (old == null) {
            return mCursor.getString(index);
        }
        if (mNewChars == null) {
            mNewChars = new CharArrayBuffer(128);
        }
        mCursor.copyStringToBuffer(index, mNewChars);
        int length = mNewChars.sizeCopied;
        if (length != old.length()) {
            return new String(mNewChars.data, 0, length);
        }
        if (mOldChars == null || mOldChars.sizeCopied < length) {
            mOldChars = new CharArrayBuffer(length);
        }
        char[] oldArray = mOldChars.data;
        char[] newArray = mNewChars.data;
        old.getChars(0, length, oldArray, 0);
        for (int i = length - 1; i >= 0; --i) {
            if (oldArray[i] != newArray[i]) {
                return new String(newArray, 0, length);
            }
        }
        return old;
    }

0 个答案:

没有答案