{"record":{"id":"fb394894fe9aa355","repo":"MuntashirAkon/AppManager","slug":"result-tostring","errorCode":null,"errorMessage":"result.toString()","messagePattern":"result\\.toString\\(\\)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"libcore/compat/src/main/java/io/github/muntashirakon/compat/xml/FastXmlSerializer.java","lineNumber":249,"sourceCode":"\n    private void flushBytes() throws IOException {\n        int position;\n        if ((position = mBytes.position()) > 0) {\n            mBytes.flip();\n            mOutputStream.write(mBytes.array(), 0, position);\n            mBytes.clear();\n        }\n    }\n\n    public void flush() throws IOException {\n        //Log.i(\"PackageManager\", \"flush mPos=\" + mPos);\n        if (mPos > 0) {\n            if (mOutputStream != null) {\n                CharBuffer charBuffer = CharBuffer.wrap(mText, 0, mPos);\n                CoderResult result = mCharset.encode(charBuffer, mBytes, true);\n                while (true) {\n                    if (result.isError()) {\n                        throw new IOException(result.toString());\n                    } else if (result.isOverflow()) {\n                        flushBytes();\n                        result = mCharset.encode(charBuffer, mBytes, true);\n                        continue;\n                    }\n                    break;\n                }\n                flushBytes();\n                mOutputStream.flush();\n            } else {\n                mWriter.write(mText, 0, mPos);\n                mWriter.flush();\n            }\n            mPos = 0;\n        }\n    }\n\n    public int getDepth() {","sourceCodeStart":231,"sourceCodeEnd":267,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/libcore/compat/src/main/java/io/github/muntashirakon/compat/xml/FastXmlSerializer.java#L231-L267","documentation":"FastXmlSerializer.flush() throws IOException when the CharsetEncoder reports a CoderResult error during encoding the buffered text to the output stream; the message is the CoderResult's toString (e.g. malformed/unmappable character information).","triggerScenarios":"Writing text containing characters not encodable in the configured charset (typically malformed surrogate pairs from unpaired high/low surrogates) before flushBytes/out stream flush runs.","commonSituations":"Serializing strings built from invalid char data (truncated emoji, hand-spliced char arrays), or a non-UTF-8 charset that cannot represent the content.","solutions":["Use UTF-8 for the serializer's charset so all valid Unicode is encodable.","Fix the source string: remove/replace unpaired surrogates (e.g. via String.codePoints() filtering).","Catch IOException around serialization and identify the offending buffer content.","Ensure the writer never splices strings at arbitrary char indexes that can split surrogate pairs."],"exampleFix":"// before\nString s = new String(new char[]{0xD83D}); // unpaired surrogate -> IOException on flush\n// after\nString s = input.codePoints()\n        .filter(cp -> Character.isValidCodePoint(cp))\n        .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)\n        .toString();","handlingStrategy":"try-catch","validationCode":"boolean valid = s.chars().allMatch(c ->\n        !(Character.isHighSurrogate((char) c)) || /* next is low surrogate */ true);\n// simpler: ensure no unpaired surrogates\nboolean wellFormed = s.codePoints().allMatch(cp -> Character.isValidCodePoint(cp));","typeGuard":null,"tryCatchPattern":"try {\n    serializer.flush();\n} catch (IOException e) {\n    throw new IOException(\"Character encoding failed in serializer: \" + e.getMessage(), e);\n}","preventionTips":["Always use UTF-8 as the serializer charset.","Sanitize input strings for unpaired surrogates before writing.","Avoid splitting surrogate pairs when slicing strings by char index.","Test serialization with emoji and supplementary-plane characters."],"tags":["xml","serializer","encoding"],"backgroundTag":"invalid-argument-format","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"}