private final void writeEscaped(String s, int quot) throws IOException {
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            switch (c) {
                case '\n':
                case '\r':
                case '\t':
                    if (quot == -1) {
                        writer.write(c);
                    } else {
                        writer.write("&#" + ((int) c) + ';');
                    }
                    break;
                case '&':
                    writer.write("&amp;");
                    break;
                case '>':
                    writer.write("&gt;");
                    break;
                case '<':
                    writer.write("&lt;");
                    break;
                default:
                    if (c == quot) {
                        writer.write(c == '"' ? "&quot;" : "&apos;");
                        break;
                    }
                    // BEGIN android-changed: refuse to output invalid characters
                    // See http://www.w3.org/TR/REC-xml/#charsets for definition.
                    // Corrected for c:geo to handle utf-16 codepoint surrogates correctly
                    // See http://en.wikipedia.org/wiki/UTF-16#Code_points_U.2B10000_to_U.2B10FFFF
                    // Note: tab, newline, and carriage return have already been
                    // handled above.
                    // Check for lead surrogate
                    if (c >= 0xd800 && c <= 0xdbff) {

                        if (i + 1 < s.length()) {
                            writer.write(s.substring(i, i + 1));
                            i++;
                            break;
                        }
                        // if the lead surrogate is at the string end, it's not valid utf-16
                        reportInvalidCharacter(c);
                    }
                    boolean valid = (c >= 0x20 && c <= 0xd7ff) || (c >= 0xe000 && c <= 0xfffd);
                    if (valid) {
                        if (unicode || c < 127) {
                            writer.write(c);
                        } else {
                            writer.write("&#" + ((int) c) + ";");
                        }
                    } else {
                        reportInvalidCharacter(c);
                    }
                    // END android-changed
            }
        }
    }
size (in characters) for the write buffer
main/thirdparty/cgeo/org/kxml2/io/KXmlSerializer.java