{"record":{"id":"685198c064c84ec3","repo":"microg/GmsCore","slug":"cannot-remove-elements-from-a-databufferiterator","errorCode":null,"errorMessage":"Cannot remove elements from a DataBufferIterator","messagePattern":"Cannot remove elements from a DataBufferIterator","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"play-services-base/src/main/java/com/google/android/gms/common/data/DataBufferIterator.java","lineNumber":33,"sourceCode":"public class DataBufferIterator<T> implements Iterator<T> {\n    protected DataBuffer<T> dataBuffer;\n    protected int position = -1;\n\n    public DataBufferIterator(@NonNull DataBuffer<T> dataBuffer) {\n        this.dataBuffer = dataBuffer;\n    }\n\n    @Override\n    public T next() {\n        if (!hasNext()) {\n            throw new NoSuchElementException(\"Cannot advance the iterator beyond \" + position);\n        }\n        return dataBuffer.get(++position);\n    }\n\n    @Override\n    public void remove() {\n        throw new UnsupportedOperationException(\"Cannot remove elements from a DataBufferIterator\");\n    }\n\n    @Override\n    public boolean hasNext() {\n        return this.position < this.dataBuffer.getCount() - 1;\n    }\n}\n","sourceCodeStart":15,"sourceCodeEnd":41,"githubUrl":"https://github.com/microg/GmsCore/blob/157c9d86ac46c195a86c2f15ab55c84036223f95/play-services-base/src/main/java/com/google/android/gms/common/data/DataBufferIterator.java#L15-L41","documentation":"DataBufferIterator.remove() unconditionally throws UnsupportedOperationException('Cannot remove elements from a DataBufferIterator'). DataBuffers are read-only views over query results, so the optional Iterator.remove operation is not supported.","triggerScenarios":"Calling iterator.remove() inside any iteration over a DataBuffer (e.g. trying to 'delete' a row while looping); passing the iterator to code that mutates collections via Iterator.remove (like some removeAll/filter idioms).","commonSituations":"Developers assuming DataBuffer behaves like a mutable List; porting collection code that mutates during iteration; third-party utilities that call remove() opportunistically.","solutions":["Do not call remove(); delete the underlying data (e.g. via the content resolver/API that produced the DataBuffer) instead.","Collect the elements to delete during iteration, then perform deletions after releasing the buffer.","Wrap the iterator in an unmodifiable-tolerant algorithm, or copy to an ArrayList first if mutable iteration semantics are required.","Catch UnsupportedOperationException if a shared utility may call remove(), and treat it as read-only."],"exampleFix":"// before\nwhile (iterator.hasNext()) {\n    if (shouldDelete(iterator.next())) iterator.remove(); // UnsupportedOperationException\n}\n\n// after\nList<Item> toDelete = new ArrayList<>();\nwhile (iterator.hasNext()) {\n    Item item = iterator.next();\n    if (shouldDelete(item)) toDelete.add(item);\n}\ndeleteItems(toDelete); // via the data source API","handlingStrategy":"type-guard","validationCode":"// DataBufferIterator.remove always throws; treat buffers as read-only.\nList<Item> snapshot = new ArrayList<>();\nfor (Item i : dataBuffer) snapshot.add(i); // safe copy for mutation logic","typeGuard":"boolean isRemovableIterator(java.util.Iterator<?> it) {\n    return !(it instanceof DataBufferIterator);\n}","tryCatchPattern":"try {\n    iterator.remove();\n} catch (UnsupportedOperationException e) {\n    Log.w(TAG, \"DataBuffer is read-only; delete via data source instead\", e);\n}","preventionTips":["Treat all DataBuffers as immutable query results.","Never pass DataBuffer iterators to utilities that mutate via Iterator.remove().","Collect items during iteration, then perform deletions through the producing API.","Copy to a List first if downstream code needs mutable-collection semantics."],"tags":["java","iterator","unsupported-operation","read-only"],"backgroundTag":"unsupported-operation","analyzedSha":"157c9d86ac46c195a86c2f15ab55c84036223f95","analyzedAt":"2026-09-06T17:27:33.892Z","contentChangedAt":"2026-09-06T17:27:33.892Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}