Java Code Examples for com.sun.org.apache.xerces.internal.util.XMLChar#isLowSurrogate()

The following examples show how to use com.sun.org.apache.xerces.internal.util.XMLChar#isLowSurrogate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XML11Serializer.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
protected final void surrogates(int high, int low) throws IOException{
    if (XMLChar.isHighSurrogate(high)) {
        if (!XMLChar.isLowSurrogate(low)) {
            //Invalid XML
            fatalError("The character '"+(char)low+"' is an invalid XML character");
        }
        else {
            int supplemental = XMLChar.supplemental((char)high, (char)low);
            if (!XML11Char.isXML11Valid(supplemental)) {
                //Invalid XML
                fatalError("The character '"+(char)supplemental+"' is an invalid XML character");
            }
            else {
                if (content().inCData ) {
                    _printer.printText("]]>&#x");
                    _printer.printText(Integer.toHexString(supplemental));
                    _printer.printText(";<![CDATA[");
                }
                else {
                                            printHex(supplemental);
                }
            }
        }
    } else {
        fatalError("The character '"+(char)high+"' is an invalid XML character");
    }

}
 
Example 2
Source File: BaseMarkupSerializer.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
protected void surrogates(int high, int low) throws IOException{
    if (XMLChar.isHighSurrogate(high)) {
        if (!XMLChar.isLowSurrogate(low)) {
            //Invalid XML
            fatalError("The character '"+(char)low+"' is an invalid XML character");
        }
        else {
            int supplemental = XMLChar.supplemental((char)high, (char)low);
            if (!XMLChar.isValid(supplemental)) {
                //Invalid XML
                fatalError("The character '"+(char)supplemental+"' is an invalid XML character");
            }
            else {
                if (content().inCData ) {
                    _printer.printText("]]>&#x");
                    _printer.printText(Integer.toHexString(supplemental));
                    _printer.printText(";<![CDATA[");
                }
                else {
                    printHex(supplemental);
                }
            }
        }
    } else {
        fatalError("The character '"+(char)high+"' is an invalid XML character");
    }

}
 
Example 3
Source File: BaseMarkupSerializer.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
protected void surrogates(int high, int low) throws IOException{
    if (XMLChar.isHighSurrogate(high)) {
        if (!XMLChar.isLowSurrogate(low)) {
            //Invalid XML
            fatalError("The character '"+(char)low+"' is an invalid XML character");
        }
        else {
            int supplemental = XMLChar.supplemental((char)high, (char)low);
            if (!XMLChar.isValid(supplemental)) {
                //Invalid XML
                fatalError("The character '"+(char)supplemental+"' is an invalid XML character");
            }
            else {
                if (content().inCData ) {
                    _printer.printText("]]>&#x");
                    _printer.printText(Integer.toHexString(supplemental));
                    _printer.printText(";<![CDATA[");
                }
                else {
                    printHex(supplemental);
                }
            }
        }
    } else {
        fatalError("The character '"+(char)high+"' is an invalid XML character");
    }

}
 
Example 4
Source File: XMLScanner.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Scans surrogates and append them to the specified buffer.
 * <p>
 * <strong>Note:</strong> This assumes the current char has already been
 * identified as a high surrogate.
 *
 * @param buf The StringBuffer to append the read surrogates to.
 * @return True if it succeeded.
 */
protected boolean scanSurrogates(XMLStringBuffer buf)
throws IOException, XNIException {

    int high = fEntityScanner.scanChar();
    int low = fEntityScanner.peekChar();
    if (!XMLChar.isLowSurrogate(low)) {
        reportFatalError("InvalidCharInContent",
                new Object[] {Integer.toString(high, 16)});
                return false;
    }
    fEntityScanner.scanChar();

    // convert surrogates to supplemental character
    int c = XMLChar.supplemental((char)high, (char)low);

    // supplemental character must be a valid XML character
    if (isInvalid(c)) {
        reportFatalError("InvalidCharInContent",
                new Object[]{Integer.toString(c, 16)});
                return false;
    }

    // fill in the buffer
    buf.append((char)high);
    buf.append((char)low);

    return true;

}
 
Example 5
Source File: XML11Serializer.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
protected final void surrogates(int high, int low, boolean inContent) throws IOException{
    if (XMLChar.isHighSurrogate(high)) {
        if (!XMLChar.isLowSurrogate(low)) {
            //Invalid XML
            fatalError("The character '"+(char)low+"' is an invalid XML character");
        }
        else {
            int supplemental = XMLChar.supplemental((char)high, (char)low);
            if (!XML11Char.isXML11Valid(supplemental)) {
                //Invalid XML
                fatalError("The character '"+(char)supplemental+"' is an invalid XML character");
            }
            else {
                if (inContent && content().inCData) {
                    _printer.printText("]]>&#x");
                    _printer.printText(Integer.toHexString(supplemental));
                    _printer.printText(";<![CDATA[");
                }
                else {
                                            printHex(supplemental);
                }
            }
        }
    }
    else {
        fatalError("The character '"+(char)high+"' is an invalid XML character");
    }

}
 
Example 6
Source File: XML11Serializer.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
protected final void surrogates(int high, int low) throws IOException{
    if (XMLChar.isHighSurrogate(high)) {
        if (!XMLChar.isLowSurrogate(low)) {
            //Invalid XML
            fatalError("The character '"+(char)low+"' is an invalid XML character");
        }
        else {
            int supplemental = XMLChar.supplemental((char)high, (char)low);
            if (!XML11Char.isXML11Valid(supplemental)) {
                //Invalid XML
                fatalError("The character '"+(char)supplemental+"' is an invalid XML character");
            }
            else {
                if (content().inCData ) {
                    _printer.printText("]]>&#x");
                    _printer.printText(Integer.toHexString(supplemental));
                    _printer.printText(";<![CDATA[");
                }
                else {
                                            printHex(supplemental);
                }
            }
        }
    } else {
        fatalError("The character '"+(char)high+"' is an invalid XML character");
    }

}
 
Example 7
Source File: BaseMarkupSerializer.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
protected void surrogates(int high, int low) throws IOException{
    if (XMLChar.isHighSurrogate(high)) {
        if (!XMLChar.isLowSurrogate(low)) {
            //Invalid XML
            fatalError("The character '"+(char)low+"' is an invalid XML character");
        }
        else {
            int supplemental = XMLChar.supplemental((char)high, (char)low);
            if (!XMLChar.isValid(supplemental)) {
                //Invalid XML
                fatalError("The character '"+(char)supplemental+"' is an invalid XML character");
            }
            else {
                if (content().inCData ) {
                    _printer.printText("]]>&#x");
                    _printer.printText(Integer.toHexString(supplemental));
                    _printer.printText(";<![CDATA[");
                }
                else {
                    printHex(supplemental);
                }
            }
        }
    } else {
        fatalError("The character '"+(char)high+"' is an invalid XML character");
    }

}
 
Example 8
Source File: BaseMarkupSerializer.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
protected void surrogates(int high, int low) throws IOException{
    if (XMLChar.isHighSurrogate(high)) {
        if (!XMLChar.isLowSurrogate(low)) {
            //Invalid XML
            fatalError("The character '"+(char)low+"' is an invalid XML character");
        }
        else {
            int supplemental = XMLChar.supplemental((char)high, (char)low);
            if (!XMLChar.isValid(supplemental)) {
                //Invalid XML
                fatalError("The character '"+(char)supplemental+"' is an invalid XML character");
            }
            else {
                if (content().inCData ) {
                    _printer.printText("]]>&#x");
                    _printer.printText(Integer.toHexString(supplemental));
                    _printer.printText(";<![CDATA[");
                }
                else {
                    printHex(supplemental);
                }
            }
        }
    } else {
        fatalError("The character '"+(char)high+"' is an invalid XML character");
    }

}
 
Example 9
Source File: XMLScanner.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * Scans surrogates and append them to the specified buffer.
 * <p>
 * <strong>Note:</strong> This assumes the current char has already been
 * identified as a high surrogate.
 *
 * @param buf The StringBuffer to append the read surrogates to.
 * @return True if it succeeded.
 */
protected boolean scanSurrogates(XMLStringBuffer buf)
throws IOException, XNIException {

    int high = fEntityScanner.scanChar(null);
    int low = fEntityScanner.peekChar();
    if (!XMLChar.isLowSurrogate(low)) {
        reportFatalError("InvalidCharInContent",
                new Object[] {Integer.toString(high, 16)});
                return false;
    }
    fEntityScanner.scanChar(null);

    // convert surrogates to supplemental character
    int c = XMLChar.supplemental((char)high, (char)low);

    // supplemental character must be a valid XML character
    if (isInvalid(c)) {
        reportFatalError("InvalidCharInContent",
                new Object[]{Integer.toString(c, 16)});
                return false;
    }

    // fill in the buffer
    buf.append((char)high);
    buf.append((char)low);

    return true;

}
 
Example 10
Source File: XMLScanner.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Scans surrogates and append them to the specified buffer.
 * <p>
 * <strong>Note:</strong> This assumes the current char has already been
 * identified as a high surrogate.
 *
 * @param buf The StringBuffer to append the read surrogates to.
 * @return True if it succeeded.
 */
protected boolean scanSurrogates(XMLStringBuffer buf)
throws IOException, XNIException {

    int high = fEntityScanner.scanChar();
    int low = fEntityScanner.peekChar();
    if (!XMLChar.isLowSurrogate(low)) {
        reportFatalError("InvalidCharInContent",
                new Object[] {Integer.toString(high, 16)});
                return false;
    }
    fEntityScanner.scanChar();

    // convert surrogates to supplemental character
    int c = XMLChar.supplemental((char)high, (char)low);

    // supplemental character must be a valid XML character
    if (isInvalid(c)) {
        reportFatalError("InvalidCharInContent",
                new Object[]{Integer.toString(c, 16)});
                return false;
    }

    // fill in the buffer
    buf.append((char)high);
    buf.append((char)low);

    return true;

}
 
Example 11
Source File: XML11Serializer.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
protected final void surrogates(int high, int low) throws IOException{
    if (XMLChar.isHighSurrogate(high)) {
        if (!XMLChar.isLowSurrogate(low)) {
            //Invalid XML
            fatalError("The character '"+(char)low+"' is an invalid XML character");
        }
        else {
            int supplemental = XMLChar.supplemental((char)high, (char)low);
            if (!XML11Char.isXML11Valid(supplemental)) {
                //Invalid XML
                fatalError("The character '"+(char)supplemental+"' is an invalid XML character");
            }
            else {
                if (content().inCData ) {
                    _printer.printText("]]>&#x");
                    _printer.printText(Integer.toHexString(supplemental));
                    _printer.printText(";<![CDATA[");
                }
                else {
                                            printHex(supplemental);
                }
            }
        }
    } else {
        fatalError("The character '"+(char)high+"' is an invalid XML character");
    }

}
 
Example 12
Source File: XMLScanner.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Scans surrogates and append them to the specified buffer.
 * <p>
 * <strong>Note:</strong> This assumes the current char has already been
 * identified as a high surrogate.
 *
 * @param buf The StringBuffer to append the read surrogates to.
 * @return True if it succeeded.
 */
protected boolean scanSurrogates(XMLStringBuffer buf)
throws IOException, XNIException {

    int high = fEntityScanner.scanChar(null);
    int low = fEntityScanner.peekChar();
    if (!XMLChar.isLowSurrogate(low)) {
        reportFatalError("InvalidCharInContent",
                new Object[] {Integer.toString(high, 16)});
                return false;
    }
    fEntityScanner.scanChar(null);

    // convert surrogates to supplemental character
    int c = XMLChar.supplemental((char)high, (char)low);

    // supplemental character must be a valid XML character
    if (isInvalid(c)) {
        reportFatalError("InvalidCharInContent",
                new Object[]{Integer.toString(c, 16)});
                return false;
    }

    // fill in the buffer
    buf.append((char)high);
    buf.append((char)low);

    return true;

}
 
Example 13
Source File: BaseMarkupSerializer.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
protected void surrogates(int high, int low) throws IOException{
    if (XMLChar.isHighSurrogate(high)) {
        if (!XMLChar.isLowSurrogate(low)) {
            //Invalid XML
            fatalError("The character '"+(char)low+"' is an invalid XML character");
        }
        else {
            int supplemental = XMLChar.supplemental((char)high, (char)low);
            if (!XMLChar.isValid(supplemental)) {
                //Invalid XML
                fatalError("The character '"+(char)supplemental+"' is an invalid XML character");
            }
            else {
                if (content().inCData ) {
                    _printer.printText("]]>&#x");
                    _printer.printText(Integer.toHexString(supplemental));
                    _printer.printText(";<![CDATA[");
                }
                else {
                    printHex(supplemental);
                }
            }
        }
    } else {
        fatalError("The character '"+(char)high+"' is an invalid XML character");
    }

}
 
Example 14
Source File: XML11Serializer.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
protected final void surrogates(int high, int low) throws IOException{
    if (XMLChar.isHighSurrogate(high)) {
        if (!XMLChar.isLowSurrogate(low)) {
            //Invalid XML
            fatalError("The character '"+(char)low+"' is an invalid XML character");
        }
        else {
            int supplemental = XMLChar.supplemental((char)high, (char)low);
            if (!XML11Char.isXML11Valid(supplemental)) {
                //Invalid XML
                fatalError("The character '"+(char)supplemental+"' is an invalid XML character");
            }
            else {
                if (content().inCData ) {
                    _printer.printText("]]>&#x");
                    _printer.printText(Integer.toHexString(supplemental));
                    _printer.printText(";<![CDATA[");
                }
                else {
                                            printHex(supplemental);
                }
            }
        }
    } else {
        fatalError("The character '"+(char)high+"' is an invalid XML character");
    }

}
 
Example 15
Source File: BaseMarkupSerializer.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
protected void surrogates(int high, int low, boolean inContent) throws IOException{
    if (XMLChar.isHighSurrogate(high)) {
        if (!XMLChar.isLowSurrogate(low)) {
            //Invalid XML
            fatalError("The character '"+(char)low+"' is an invalid XML character");
        }
        else {
            int supplemental = XMLChar.supplemental((char)high, (char)low);
            if (!XMLChar.isValid(supplemental)) {
                //Invalid XML
                fatalError("The character '"+(char)supplemental+"' is an invalid XML character");
            }
            else {
                if (inContent && content().inCData) {
                    _printer.printText("]]>&#x");
                    _printer.printText(Integer.toHexString(supplemental));
                    _printer.printText(";<![CDATA[");
                }
                else {
                    printHex(supplemental);
                }
            }
        }
    } else {
        fatalError("The character '"+(char)high+"' is an invalid XML character");
    }

}
 
Example 16
Source File: UTF8OutputStreamWriter.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public void write(int c) throws IOException {
    // Check in we are encoding at high and low surrogates
    if (lastUTF16CodePoint != 0) {
        final int uc =
            (((lastUTF16CodePoint & 0x3ff) << 10) | (c & 0x3ff)) + 0x10000;

        if (uc < 0 || uc >= 0x200000) {
            throw new IOException("Atttempting to write invalid Unicode code point '" + uc + "'");
        }

        out.write(0xF0 | (uc >> 18));
        out.write(0x80 | ((uc >> 12) & 0x3F));
        out.write(0x80 | ((uc >> 6) & 0x3F));
        out.write(0x80 | (uc & 0x3F));

        lastUTF16CodePoint = 0;
        return;
    }

    // Otherwise, encode char as defined in UTF-8
    if (c < 0x80) {
        // 1 byte, 7 bits
        out.write((int) c);
    }
    else if (c < 0x800) {
        // 2 bytes, 11 bits
        out.write(0xC0 | (c >> 6));    // first 5
        out.write(0x80 | (c & 0x3F));  // second 6
    }
    else if (c <= '\uFFFF') {
        if (!XMLChar.isHighSurrogate(c) && !XMLChar.isLowSurrogate(c)) {
            // 3 bytes, 16 bits
            out.write(0xE0 | (c >> 12));   // first 4
            out.write(0x80 | ((c >> 6) & 0x3F));  // second 6
            out.write(0x80 | (c & 0x3F));  // third 6
        }
        else {
            lastUTF16CodePoint = c;
        }
    }
}
 
Example 17
Source File: XIncludeHandler.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private String escapeHref(String href) {
    int len = href.length();
    int ch;
    final StringBuilder buffer = new StringBuilder(len*3);

    // for each character in the href
    int i = 0;
    for (; i < len; i++) {
        ch = href.charAt(i);
        // if it's not an ASCII character (excluding 0x7F), break here, and use UTF-8 encoding
        if (ch > 0x7E) {
            break;
        }
        // abort: href does not allow this character
        if (ch < 0x20) {
            return href;
        }
        if (gNeedEscaping[ch]) {
            buffer.append('%');
            buffer.append(gAfterEscaping1[ch]);
            buffer.append(gAfterEscaping2[ch]);
        }
        else {
            buffer.append((char)ch);
        }
    }

    // we saw some non-ascii character
    if (i < len) {
        // check if remainder of href contains any illegal characters before proceeding
        for (int j = i; j < len; ++j) {
            ch = href.charAt(j);
            if ((ch >= 0x20 && ch <= 0x7E) ||
                (ch >= 0xA0 && ch <= 0xD7FF) ||
                (ch >= 0xF900 && ch <= 0xFDCF) ||
                (ch >= 0xFDF0 && ch <= 0xFFEF)) {
                continue;
            }
            if (XMLChar.isHighSurrogate(ch) && ++j < len) {
                int ch2 = href.charAt(j);
                if (XMLChar.isLowSurrogate(ch2)) {
                    ch2 = XMLChar.supplemental((char)ch, (char)ch2);
                    if (ch2 < 0xF0000 && (ch2 & 0xFFFF) <= 0xFFFD) {
                        continue;
                    }
                }
            }
            // abort: href does not allow this character
            return href;
        }

        // get UTF-8 bytes for the remaining sub-string
        byte[] bytes = null;
        byte b;
        try {
            bytes = href.substring(i).getBytes("UTF-8");
        } catch (java.io.UnsupportedEncodingException e) {
            // should never happen
            return href;
        }
        len = bytes.length;

        // for each byte
        for (i = 0; i < len; i++) {
            b = bytes[i];
            // for non-ascii character: make it positive, then escape
            if (b < 0) {
                ch = b + 256;
                buffer.append('%');
                buffer.append(gHexChs[ch >> 4]);
                buffer.append(gHexChs[ch & 0xf]);
            }
            else if (gNeedEscaping[b]) {
                buffer.append('%');
                buffer.append(gAfterEscaping1[b]);
                buffer.append(gAfterEscaping2[b]);
            }
            else {
                buffer.append((char)b);
            }
        }
    }

    // If escaping happened, create a new string;
    // otherwise, return the orginal one.
    if (buffer.length() != len) {
        return buffer.toString();
    }
    else {
        return href;
    }
}
 
Example 18
Source File: XIncludeHandler.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
private String escapeHref(String href) {
    int len = href.length();
    int ch;
    final StringBuilder buffer = new StringBuilder(len*3);

    // for each character in the href
    int i = 0;
    for (; i < len; i++) {
        ch = href.charAt(i);
        // if it's not an ASCII character (excluding 0x7F), break here, and use UTF-8 encoding
        if (ch > 0x7E) {
            break;
        }
        // abort: href does not allow this character
        if (ch < 0x20) {
            return href;
        }
        if (gNeedEscaping[ch]) {
            buffer.append('%');
            buffer.append(gAfterEscaping1[ch]);
            buffer.append(gAfterEscaping2[ch]);
        }
        else {
            buffer.append((char)ch);
        }
    }

    // we saw some non-ascii character
    if (i < len) {
        // check if remainder of href contains any illegal characters before proceeding
        for (int j = i; j < len; ++j) {
            ch = href.charAt(j);
            if ((ch >= 0x20 && ch <= 0x7E) ||
                (ch >= 0xA0 && ch <= 0xD7FF) ||
                (ch >= 0xF900 && ch <= 0xFDCF) ||
                (ch >= 0xFDF0 && ch <= 0xFFEF)) {
                continue;
            }
            if (XMLChar.isHighSurrogate(ch) && ++j < len) {
                int ch2 = href.charAt(j);
                if (XMLChar.isLowSurrogate(ch2)) {
                    ch2 = XMLChar.supplemental((char)ch, (char)ch2);
                    if (ch2 < 0xF0000 && (ch2 & 0xFFFF) <= 0xFFFD) {
                        continue;
                    }
                }
            }
            // abort: href does not allow this character
            return href;
        }

        // get UTF-8 bytes for the remaining sub-string
        byte[] bytes = null;
        byte b;
        try {
            bytes = href.substring(i).getBytes("UTF-8");
        } catch (java.io.UnsupportedEncodingException e) {
            // should never happen
            return href;
        }
        len = bytes.length;

        // for each byte
        for (i = 0; i < len; i++) {
            b = bytes[i];
            // for non-ascii character: make it positive, then escape
            if (b < 0) {
                ch = b + 256;
                buffer.append('%');
                buffer.append(gHexChs[ch >> 4]);
                buffer.append(gHexChs[ch & 0xf]);
            }
            else if (gNeedEscaping[b]) {
                buffer.append('%');
                buffer.append(gAfterEscaping1[b]);
                buffer.append(gAfterEscaping2[b]);
            }
            else {
                buffer.append((char)b);
            }
        }
    }

    // If escaping happened, create a new string;
    // otherwise, return the orginal one.
    if (buffer.length() != len) {
        return buffer.toString();
    }
    else {
        return href;
    }
}
 
Example 19
Source File: UTF8OutputStreamWriter.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public void write(int c) throws IOException {
    // Check in we are encoding at high and low surrogates
    if (lastUTF16CodePoint != 0) {
        final int uc =
            (((lastUTF16CodePoint & 0x3ff) << 10) | (c & 0x3ff)) + 0x10000;

        if (uc < 0 || uc >= 0x200000) {
            throw new IOException("Atttempting to write invalid Unicode code point '" + uc + "'");
        }

        out.write(0xF0 | (uc >> 18));
        out.write(0x80 | ((uc >> 12) & 0x3F));
        out.write(0x80 | ((uc >> 6) & 0x3F));
        out.write(0x80 | (uc & 0x3F));

        lastUTF16CodePoint = 0;
        return;
    }

    // Otherwise, encode char as defined in UTF-8
    if (c < 0x80) {
        // 1 byte, 7 bits
        out.write((int) c);
    }
    else if (c < 0x800) {
        // 2 bytes, 11 bits
        out.write(0xC0 | (c >> 6));    // first 5
        out.write(0x80 | (c & 0x3F));  // second 6
    }
    else if (c <= '\uFFFF') {
        if (!XMLChar.isHighSurrogate(c) && !XMLChar.isLowSurrogate(c)) {
            // 3 bytes, 16 bits
            out.write(0xE0 | (c >> 12));   // first 4
            out.write(0x80 | ((c >> 6) & 0x3F));  // second 6
            out.write(0x80 | (c & 0x3F));  // third 6
        }
        else {
            lastUTF16CodePoint = c;
        }
    }
}
 
Example 20
Source File: UTF8OutputStreamWriter.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public void write(int c) throws IOException {
    // Check in we are encoding at high and low surrogates
    if (lastUTF16CodePoint != 0) {
        final int uc =
            (((lastUTF16CodePoint & 0x3ff) << 10) | (c & 0x3ff)) + 0x10000;

        if (uc < 0 || uc >= 0x200000) {
            throw new IOException("Atttempting to write invalid Unicode code point '" + uc + "'");
        }

        out.write(0xF0 | (uc >> 18));
        out.write(0x80 | ((uc >> 12) & 0x3F));
        out.write(0x80 | ((uc >> 6) & 0x3F));
        out.write(0x80 | (uc & 0x3F));

        lastUTF16CodePoint = 0;
        return;
    }

    // Otherwise, encode char as defined in UTF-8
    if (c < 0x80) {
        // 1 byte, 7 bits
        out.write((int) c);
    }
    else if (c < 0x800) {
        // 2 bytes, 11 bits
        out.write(0xC0 | (c >> 6));    // first 5
        out.write(0x80 | (c & 0x3F));  // second 6
    }
    else if (c <= '\uFFFF') {
        if (!XMLChar.isHighSurrogate(c) && !XMLChar.isLowSurrogate(c)) {
            // 3 bytes, 16 bits
            out.write(0xE0 | (c >> 12));   // first 4
            out.write(0x80 | ((c >> 6) & 0x3F));  // second 6
            out.write(0x80 | (c & 0x3F));  // third 6
        }
        else {
            lastUTF16CodePoint = c;
        }
    }
}