下面列出了怎么用android.database.CharArrayBuffer的API类实例代码及写法,或者点击链接到github查看源代码。
public void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer) {
checkPosition();
if (columnIndex == mDeltaColumn) {
long value = getDeltaValue();
String strValue = Long.toString(value);
int len = strValue.length();
char[] data = buffer.data;
if (data == null || data.length < len) {
buffer.data = strValue.toCharArray();
} else {
strValue.getChars(0, len, data, 0);
}
buffer.sizeCopied = strValue.length();
} else {
mInnerCursor.copyStringToBuffer(columnIndex, buffer);
}
}
@Override
public void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer) {
// Default implementation, uses getString
String result = getString(columnIndex);
if (result != null) {
char[] data = buffer.data;
if (data == null || data.length < result.length()) {
buffer.data = result.toCharArray();
} else {
result.getChars(0, result.length(), data, 0);
}
buffer.sizeCopied = result.length();
} else {
buffer.sizeCopied = 0;
}
}
/**
* Retrieves the requested column text and stores it in the buffer provided.
* If the buffer size is not sufficient, a new char buffer will be allocated
* and assigned to CharArrayBuffer.data
*
* @param columnIndex
* the zero-based index of the target column. if the target
* column is null, return buffer
* @param buffer
* the buffer to copy the text into.
*/
@Override
public void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer) {
try {
if (mCursor != null) {
mCursor.copyStringToBuffer(columnIndex, buffer);
}
} catch (Exception e) {
PaLog.d("Cursor exception: " + e);
if (mCursor != null) {
try {
mCursor.close();
} catch (Exception exception) {
PaLog.d("Cursor close exception: " + e);
}
mCursor = null;
}
}
}
public void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer) {
checkPosition();
if (columnIndex == mDeltaColumn) {
long value = getDeltaValue();
String strValue = Long.toString(value);
int len = strValue.length();
char[] data = buffer.data;
if (data == null || data.length < len) {
buffer.data = strValue.toCharArray();
} else {
strValue.getChars(0, len, data, 0);
}
buffer.sizeCopied = strValue.length();
} else {
mInnerCursor.copyStringToBuffer(columnIndex, buffer);
}
}
@Override
public void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer) {
for (int chance = 0; chance < LOCK_RETRY_CHANCES; chance++) {
try {
cursor.copyStringToBuffer(columnIndex, buffer);
} catch (RuntimeException e) {
if (!isSQLiteDatabaseLockedException(e)) {
throw e;
}
}
}
}
public void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer) {
// Default implementation, uses getString
String result = getString(columnIndex);
if (result != null) {
char[] data = buffer.data;
if (data == null || data.length < result.length()) {
buffer.data = result.toCharArray();
} else {
result.getChars(0, result.length(), data, 0);
}
buffer.sizeCopied = result.length();
} else {
buffer.sizeCopied = 0;
}
}
@Override
public void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer) {
mWindow.copyStringToBuffer(mPos, columnIndex, buffer);
}
@MediumTest
@Test
public void testCopyString() throws Exception {
mDatabase.execSQL("CREATE TABLE guess (numi INTEGER, numf FLOAT, str TEXT);");
mDatabase.execSQL(
"INSERT INTO guess (numi,numf,str) VALUES (0,0.0,'ZoomZoomZoomZoom');");
mDatabase.execSQL("INSERT INTO guess (numi,numf,str) VALUES (2000000000,3.1415926535,'');");
String chinese = "\u4eac\u4ec5 \u5c3d\u5f84\u60ca";
String[] arr = new String[1];
arr[0] = chinese;
mDatabase.execSQL("INSERT INTO guess (numi,numf,str) VALUES (-32768,-1.0,?)", arr);
Cursor c;
c = mDatabase.rawQuery("SELECT * FROM guess", null);
c.moveToFirst();
CharArrayBuffer buf = new CharArrayBuffer(14);
String compareTo = c.getString(c.getColumnIndexOrThrow("numi"));
int numiIdx = c.getColumnIndexOrThrow("numi");
int numfIdx = c.getColumnIndexOrThrow("numf");
int strIdx = c.getColumnIndexOrThrow("str");
c.copyStringToBuffer(numiIdx, buf);
assertEquals(1, buf.sizeCopied);
assertEquals(compareTo, new String(buf.data, 0, buf.sizeCopied));
c.copyStringToBuffer(strIdx, buf);
assertEquals("ZoomZoomZoomZoom", new String(buf.data, 0, buf.sizeCopied));
c.moveToNext();
compareTo = c.getString(numfIdx);
c.copyStringToBuffer(numfIdx, buf);
assertEquals(compareTo, new String(buf.data, 0, buf.sizeCopied));
c.copyStringToBuffer(strIdx, buf);
assertEquals(0, buf.sizeCopied);
c.moveToNext();
c.copyStringToBuffer(numfIdx, buf);
assertEquals(new Double(-1.0), Double.valueOf(
new String(buf.data, 0, buf.sizeCopied)));
c.copyStringToBuffer(strIdx, buf);
compareTo = c.getString(strIdx);
assertEquals(chinese, compareTo);
assertEquals(chinese, new String(buf.data, 0, buf.sizeCopied));
c.close();
}
@Override
public void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer) {
backingCursor.copyStringToBuffer(columnIndex, buffer);
}
private static void setTextWithBuffer(TextView textView, CharArrayBuffer buffer) {
textView.setText(buffer.data, 0, buffer.sizeCopied);
}
@Override
public void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer) {
// TODO Auto-generated method stub
}
public void copyStringToBuffer(int i, CharArrayBuffer chararraybuffer)
{
throw new UnsupportedOperationException();
}
@Override
public void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer) {
throw new IllegalArgumentException("CursorList is not backed by columns.");
}
@Override
public void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer) {
cursor.copyStringToBuffer(columnIndex, buffer);
}
@Override
public void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer) {
// DUMMY
}
@Override
public void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer) {
// no implementation needed, proxy will request the original cursor
}
@Override
public void copyStringToBuffer(int arg0, CharArrayBuffer arg1) {}
@Override
public void copyStringToBuffer(int i, CharArrayBuffer charArrayBuffer) {
baseCursor.copyStringToBuffer(i, charArrayBuffer);
}
/**
* Copies the text of the field at the specified row and column index into
* a {@link CharArrayBuffer}.
* <p>
* The buffer is populated as follows:
* <ul>
* <li>If the buffer is too small for the value to be copied, then it is
* automatically resized.</li>
* <li>If the field is of type {@link Cursor#FIELD_TYPE_NULL}, then the buffer
* is set to an empty string.</li>
* <li>If the field is of type {@link Cursor#FIELD_TYPE_STRING}, then the buffer
* is set to the contents of the string.</li>
* <li>If the field is of type {@link Cursor#FIELD_TYPE_INTEGER}, then the buffer
* is set to a string representation of the integer in decimal, obtained by formatting the
* value with the <code>printf</code> family of functions using
* format specifier <code>%lld</code>.</li>
* <li>If the field is of type {@link Cursor#FIELD_TYPE_FLOAT}, then the buffer is
* set to a string representation of the floating-point value in decimal, obtained by
* formatting the value with the <code>printf</code> family of functions using
* format specifier <code>%g</code>.</li>
* <li>If the field is of type {@link Cursor#FIELD_TYPE_BLOB}, then a
* {@link SQLiteException} is thrown.</li>
* </ul>
* </p>
*
* @param row The zero-based row index.
* @param column The zero-based column index.
* @param buffer The {@link CharArrayBuffer} to hold the string. It is automatically
* resized if the requested string is larger than the buffer's current capacity.
*/
public void copyStringToBuffer(int row, int column, CharArrayBuffer buffer) {
if (buffer == null) {
throw new IllegalArgumentException("CharArrayBuffer should not be null");
}
// TODO not as optimal as the original code
char[] chars = getString(row, column).toCharArray();
buffer.data = chars;
buffer.sizeCopied = chars.length;
}
@Override public void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer)
{
}
@Override public void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer)
{
}