Copies the given cursor into a in-memory cursor and then closes it. This is useful for iterating over a cursor multiple times without the cost of JNI while reading or IO while filling the window at the expense of memory consumption. @param c the cursor to copy. @return a new cursor containing t
(@NonNull Cursor c)
| 45 | * @return a new cursor containing the same data as the given cursor. |
| 46 | */ |
| 47 | @NonNull |
| 48 | public static Cursor copyAndClose(@NonNull Cursor c) { |
| 49 | final MatrixCursor matrixCursor; |
| 50 | try { |
| 51 | matrixCursor = new MatrixCursor(c.getColumnNames(), c.getCount()); |
| 52 | while (c.moveToNext()) { |
| 53 | final Object[] row = new Object[c.getColumnCount()]; |
| 54 | for (int i = 0; i < c.getColumnCount(); i++) { |
| 55 | switch (c.getType(i)) { |
| 56 | case Cursor.FIELD_TYPE_NULL: |
| 57 | row[i] = null; |
| 58 | break; |
| 59 | case Cursor.FIELD_TYPE_INTEGER: |
| 60 | row[i] = c.getLong(i); |
| 61 | break; |
| 62 | case Cursor.FIELD_TYPE_FLOAT: |
| 63 | row[i] = c.getDouble(i); |
| 64 | break; |
| 65 | case Cursor.FIELD_TYPE_STRING: |
| 66 | row[i] = c.getString(i); |
| 67 | break; |
| 68 | case Cursor.FIELD_TYPE_BLOB: |
| 69 | row[i] = c.getBlob(i); |
| 70 | break; |
| 71 | default: |
| 72 | throw new IllegalStateException(); |
| 73 | } |
| 74 | } |
| 75 | matrixCursor.addRow(row); |
| 76 | } |
| 77 | } finally { |
| 78 | c.close(); |
| 79 | } |
| 80 | return matrixCursor; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Patches {@link Cursor#getColumnIndex(String)} to work around issues on older devices. |
no test coverage detected