{"record":{"id":"5ba087990043a945","repo":"MuntashirAkon/AppManager","slug":"buffer-size-d-must-be-at-least-s-for-a-charsetencoder-s","errorCode":null,"errorMessage":"Buffer size %,d must be at least %s for a CharsetEncoder %s.","messagePattern":"Buffer size %,d must be at least (.+?) for a CharsetEncoder (.+?)\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"libcore/io/src/main/java/io/github/muntashirakon/io/CharSequenceInputStream.java","lineNumber":250,"sourceCode":"\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++;\n        }\n        return skipped;\n    }\n\n    private static int checkMinBufferSize(final CharsetEncoder charsetEncoder, final int bufferSize) {\n        final float minRequired = minBufferSize(charsetEncoder);\n        if (bufferSize < minRequired) {\n            throw new IllegalArgumentException(String.format(\"Buffer size %,d must be at least %s for a CharsetEncoder %s.\", bufferSize, minRequired,\n                    charsetEncoder.charset().displayName()));\n        }\n        return bufferSize;\n    }\n\n    private static float minBufferSize(final CharsetEncoder charsetEncoder) {\n        return charsetEncoder.maxBytesPerChar() * 2;\n    }\n}\n","sourceCodeStart":232,"sourceCodeEnd":260,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/libcore/io/src/main/java/io/github/muntashirakon/io/CharSequenceInputStream.java#L232-L260","documentation":"checkMinBufferSize() verifies the caller-provided buffer size is at least maxBytesPerChar as reported by the CharsetEncoder (minBufferSize). A smaller buffer cannot guarantee a single character always fits, so the constructor throws IllegalArgumentException with the required minimum and charset name.","triggerScenarios":"Constructing CharSequenceInputStream with an explicit bufferSize smaller than minBufferSize(charsetEncoder) — e.g. a 16-byte buffer with a UTF-16 encoder whose max per-char size is larger.","commonSituations":"Hand-tuning tiny buffers for memory savings without checking the charset's maxBytesPerChar; switching charsets (e.g. UTF-8 to UTF-16) while keeping the same buffer size.","solutions":["Use the CharSequenceInputStream(CharSequence, Charset) constructor that computes a safe default buffer size.","Query the required minimum via the library's minBufferSize logic (encoder.maxBytesPerChar()) and pass at least that value.","Use a comfortably larger buffer (e.g. 8192) instead of tuning minimal sizes."],"exampleFix":"// before\nnew CharSequenceInputStream(cs, charset, 16);\n// after\nint min = (int) charset.newEncoder().maxBytesPerChar();\nnew CharSequenceInputStream(cs, charset, Math.max(min, 8192));","handlingStrategy":"validation","validationCode":"float min = charset.newEncoder().maxBytesPerChar();\nif (bufferSize < min) bufferSize = Math.max((int) min, 8192);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use constructors without explicit bufferSize","Recompute buffer size when changing charset","Use standard 8KB buffers rather than minimal ones"],"tags":["io","charset","argument-validation"],"backgroundTag":"invalid-argument-value","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"}