Thursday, March 12, 2015

UnsupportedEncodingException

java.io.UnsupportedEncodingException

UnsupportedEncodingException is described in the javadoc comments as:

The Character Encoding is not supported.
author: Asmus Freytag version: 1.16, 12/19/03 since: JDK1.1

Where is this exception thrown?

Following, is a list of exception messages cross-referenced to the source code responsible for throwing them. Click on the method link to view the code and see how the exception is thrown.

How is this exception thrown?

The following sub-sections identify where this exception is thrown, and how (or why) the code is throwing the exception.

Any source code quoted in this section is subject to the Java Research License unless stated otherwise.

com.sun.org.apache.xml.internal.serialize.Encodings.getEncodingInfo(String, boolean)

/**
     * @param encoding a MIME charset name, or null.
     */
static EncodingInfo getEncodingInfo(String encoding, boolean allowJavaNames) throws UnsupportedEncodingException {
    EncodingInfo eInfo = null;
    if (encoding == null) {
        if ((eInfo = (EncodingInfo) _encodings.get(DEFAULT_ENCODING)) != null) return eInfo;
        eInfo = new EncodingInfo(EncodingMap.getJava2IANAMapping(DEFAULT_ENCODING), DEFAULT_ENCODING, LAST_PRINTABLE_UNICODE);
        _encodings.put(DEFAULT_ENCODING, eInfo);
        return eInfo;
    }
    encoding = encoding.toUpperCase(Locale.ENGLISH);
    String jName = EncodingMap.getIANA2JavaMapping(encoding);
    if (jName == null) {
        if (allowJavaNames) {
            EncodingInfo.testJavaEncodingName(encoding);
            if ((eInfo = (EncodingInfo) _encodings.get(encoding)) != null) return eInfo;
            int i = 0;
            for (; i < UNICODE_ENCODINGS.length; i++) {
                if (UNICODE_ENCODINGS[i].equalsIgnoreCase(encoding)) {
                    eInfo = new EncodingInfo(EncodingMap.getJava2IANAMapping(encoding), encoding, LAST_PRINTABLE_UNICODE);
                    break;
                }
            }
            if (i == UNICODE_ENCODINGS.length) {
                eInfo = new EncodingInfo(EncodingMap.getJava2IANAMapping(encoding), encoding, DEFAULT_LAST_PRINTABLE);
            }
            _encodings.put(encoding, eInfo);
            return eInfo;
        } else {
            throw new UnsupportedEncodingException(encoding);
        }
    }
    if ((eInfo = (EncodingInfo) _encodings.get(jName)) != null) return eInfo;
    int i = 0;
    for (; i < UNICODE_ENCODINGS.length; i++) {
        if (UNICODE_ENCODINGS[i].equalsIgnoreCase(jName)) {
            eInfo = new EncodingInfo(encoding, jName, LAST_PRINTABLE_UNICODE);
            break;
        }
    }
    if (i == UNICODE_ENCODINGS.length) {
        eInfo = new EncodingInfo(encoding, jName, DEFAULT_LAST_PRINTABLE);
    }
    _encodings.put(jName, eInfo);
    return eInfo;
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

com.sun.org.apache.xml.internal.serializer.Encodings.getWriter(OutputStream, String)

/**
     * Returns a writer for the specified encoding based on
     * an output stream.
     * @param output The output stream
     * @param encoding The encoding
     * @return A suitable writer
     * @throws UnsupportedEncodingException There is no convertor
     *  to support this encoding
     */
public static Writer getWriter(OutputStream output, String encoding) throws UnsupportedEncodingException {
    for (int i = 0; i < _encodings.length; ++i) {
        if (_encodings[i].name.equalsIgnoreCase(encoding)) {
            try {
                return new OutputStreamWriter(output, _encodings[i].javaName);
            } catch (java.lang.IllegalArgumentException iae) {
            } catch (UnsupportedEncodingException usee) {
            }
        }
    }
    try {
        return new OutputStreamWriter(output, encoding);
    } catch (java.lang.IllegalArgumentException iae) {
        throw new UnsupportedEncodingException(encoding);
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.net.URLEncoder.encode(String, String)

/**
     * Translates a string into <code>application/x-www-form-urlencoded</code>
     * format using a specific encoding scheme. This method uses the
     * supplied encoding scheme to obtain the bytes for unsafe
     * characters.
     * <em><strong>Note:</strong> The <a href=
     * 'http://www.w3.org/TR/html40/appendix/notes.html#non-ascii-chars'>
     * World Wide Web Consortium Recommendation</a> states that
     * UTF-8 should be used. Not doing so may introduce
     * incompatibilites.</em>
     * @param   s   <code>String</code> to be translated.
     * @param   enc   The name of a supported 
     *    <a href='../lang/package-summary.html#charenc'>character
     *    encoding</a>.
     * @return  the translated <code>String</code>.
     * @exception  UnsupportedEncodingException
     *             If the named encoding is not supported
     * @see URLDecoder#decode(java.lang.String, java.lang.String)
     * @since 1.4
     */
public static String encode(String s, String enc) throws UnsupportedEncodingException {
    boolean needToChange = false;
    StringBuffer out = new StringBuffer(s.length());
    Charset charset;
    CharArrayWriter charArrayWriter = new CharArrayWriter();
    if (enc == null) throw new NullPointerException('charsetName');
    try {
        charset = Charset.forName(enc);
    } catch (IllegalCharsetNameException e) {
        throw new UnsupportedEncodingException(enc);
    } catch (UnsupportedCharsetException e) {
        throw new UnsupportedEncodingException(enc);
    }
    for (int i = 0; i < s.length(); ) {
        int c = (int) s.charAt(i);
        if (dontNeedEncoding.get(c)) {
            if (c == ' ') {
                c = '+';
                needToChange = true;
            }
            out.append((char) c);
            i++;
        } else {
            do {
                charArrayWriter.write(c);
                if (c >= 0xD800 && c <= 0xDBFF) {
                    if ((i + 1) < s.length()) {
                        int d = (int) s.charAt(i + 1);
                        if (d >= 0xDC00 && d <= 0xDFFF) {
                            charArrayWriter.write(d);
                            i++;
                        }
                    }
                }
                i++;
            } while (i < s.length() && !dontNeedEncoding.get((c = (int) s.charAt(i))));
            charArrayWriter.flush();
            String str = new String(charArrayWriter.toCharArray());
            byte[] ba = str.getBytes(charset.name());
            for (int j = 0; j < ba.length; j++) {
                out.append('%');
                char ch = Character.forDigit((ba[j] >> 4) & 0xF, 16);
                if (Character.isLetter(ch)) {
                    ch -= caseDiff;
                }
                out.append(ch);
                ch = Character.forDigit(ba[j] & 0xF, 16);
                if (Character.isLetter(ch)) {
                    ch -= caseDiff;
                }
                out.append(ch);
            }
            charArrayWriter.reset();
            needToChange = true;
        }
    }
    return (needToChange ? out.toString() : s);
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.net.URLDecoder.decode(String, String)

/**
     * Decodes a <code>application/x-www-form-urlencoded</code> string using a specific 
     * encoding scheme.
     * The supplied encoding is used to determine
     * what characters are represented by any consecutive sequences of the
     * form '<code>%<i>xy</i></code>'.
     * <em><strong>Note:</strong> The <a href=
     * 'http://www.w3.org/TR/html40/appendix/notes.html#non-ascii-chars'>
     * World Wide Web Consortium Recommendation</a> states that
     * UTF-8 should be used. Not doing so may introduce
     * incompatibilites.</em>
     * @param s the <code>String</code> to decode
     * @param enc   The name of a supported 
     *    <a href='../lang/package-summary.html#charenc'>character
     *    encoding</a>. 
     * @return the newly decoded <code>String</code>
     * @exception  UnsupportedEncodingException
     *             If character encoding needs to be consulted, but
     *             named character encoding is not supported
     * @see URLEncoder#encode(java.lang.String, java.lang.String)
     * @since 1.4
     */
public static String decode(String s, String enc) throws UnsupportedEncodingException {
    boolean needToChange = false;
    int numChars = s.length();
    StringBuffer sb = new StringBuffer(numChars > 500 ? numChars / 2 : numChars);
    int i = 0;
    if (enc.length() == 0) {
        throw new UnsupportedEncodingException('URLDecoder: empty string enc parameter');
    }
    char c;
    byte[] bytes = null;
    while (i < numChars) {
        c = s.charAt(i);
        switch(c) {
            case '+':
                sb.append(' ');
                i++;
                needToChange = true;
                break;
            case '%':
                try {
                    if (bytes == null) bytes = new byte[(numChars - i) / 3];
                    int pos = 0;
                    while (((i + 2) < numChars) && (c == '%')) {
                        bytes[pos++] = (byte) Integer.parseInt(s.substring(i + 1, i + 3), 16);
                        i += 3;
                        if (i < numChars) c = s.charAt(i);
                    }
                    if ((i < numChars) && (c == '%')) throw new IllegalArgumentException('URLDecoder: Incomplete trailing escape (%) pattern');
                    sb.append(new String(bytes, 0, pos, enc));
                } catch (NumberFormatException e) {
                    throw new IllegalArgumentException('URLDecoder: Illegal hex characters in escape (%) pattern - ' + e.getMessage());
                }
                needToChange = true;
                break;
            default:
                sb.append(c);
                i++;
                break;
        }
    }
    return (needToChange ? sb.toString() : s);
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

No comments:

Post a Comment