{"record":{"id":"873b6f71361bfec0","repo":"MuntashirAkon/AppManager","slug":"unexpected-charbuffer-position-actual-mcharbuffer-position","errorCode":null,"errorMessage":"Unexpected CharBuffer position: actual=\" + mCharBuffer.position() + \" expected=\" + mCharBufferMark","messagePattern":"Unexpected CharBuffer position: actual=\" \\+ mCharBuffer\\.position\\(\\) \\+ \" expected=\" \\+ mCharBufferMark","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"libcore/io/src/main/java/io/github/muntashirakon/io/CharSequenceInputStream.java","lineNumber":224,"sourceCode":"        // the mark. The code would have to catch InvalidMarkException - does not seem possible to check if mark is\n        // valid otherwise. + Try saving the state of the cBuf before each fillBuffer; it might be possible to\n        // restart from there.\n        //\n        if (mCharBufferMark != NO_MARK) {\n            // if cBuf is at 0, we have not started reading anything, so skip re-encoding\n            if (mCharBuffer.position() != 0) {\n                mCharsetEncoder.reset();\n                mCharBuffer.rewind();\n                mByteBuffer.rewind();\n                mByteBuffer.limit(0); // rewind does not clear the buffer\n                while (mCharBuffer.position() < mCharBufferMark) {\n                    mByteBuffer.rewind(); // empty the buffer (we only refill when empty during normal processing)\n                    mByteBuffer.limit(0);\n                    fillBuffer();\n                }\n            }\n            if (mCharBuffer.position() != mCharBufferMark) {\n                throw new IllegalStateException(\"Unexpected CharBuffer position: actual=\" + mCharBuffer.position() + \" \" +\n                        \"expected=\" + mCharBufferMark);\n            }\n            mByteBuffer.position(mByteBufferMark);\n            mCharBufferMark = NO_MARK;\n            mByteBufferMark = NO_MARK;\n        }\n    }\n\n    @Override\n    public long skip(long n) throws IOException {\n        //\n        // This could be made more efficient by using position to skip within the current buffer.\n        //\n        long skipped = 0;\n        while (n > 0 && available() > 0) {\n            read();\n            n--;\n            skipped++;","sourceCodeStart":206,"sourceCodeEnd":242,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/libcore/io/src/main/java/io/github/muntashirakon/io/CharSequenceInputStream.java#L206-L242","documentation":"reset() restores the stream to a previously set mark. The library throws IllegalStateException when, after refilling buffers, the CharBuffer position does not match the recorded mark (mCharBufferMark). This indicates an internal decoder buffering invariant was broken, typically because the internal buffers were consumed beyond the marked position.","triggerScenarios":"Calling reset() after reading past the marked region so far that the internal CharBuffer was refilled/rewound and its position can no longer be restored to mCharBufferMark (mark invalidated by too much reading since mark()).","commonSituations":"Calling reset() without a preceding mark(); reading more bytes than readLimit allows between mark() and reset(); calling reset() twice.","solutions":["Call mark(readLimit) with a readLimit large enough to cover all bytes read before reset().","Always pair mark()/reset() and never reset() after the mark has been invalidated by excessive reads.","Re-create the CharSequenceInputStream from the source CharSequence instead of resetting an exhausted mark."],"exampleFix":"// before\nstream.mark(8); stream.read(buf, 0, 1024); stream.reset();\n// after\nstream.mark(4096); stream.read(buf, 0, 1024); stream.reset();","handlingStrategy":"validation","validationCode":"boolean canReset = !resetCalledOnce && bytesReadSinceMark < markedReadLimit;\nif (!canReset) throw new IllegalStateException(\"Mark no longer valid\");","typeGuard":null,"tryCatchPattern":"try {\n    stream.reset();\n} catch (IllegalStateException e) {\n    stream = new CharSequenceInputStream(source, charset); // recreate instead\n}","preventionTips":["Set mark(readLimit) generously larger than expected reads","Never call reset() without a prior mark()","Track bytes read between mark and reset"],"tags":["io","stream-state","mark-reset"],"backgroundTag":"internal-invariant-violation","analyzedSha":"0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5","analyzedAt":"2026-09-12T14:03:37.243Z","contentChangedAt":"2026-09-12T14:03:37.243Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}