Skip to main content

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

Comments

Popular posts from this blog

BAD_CERTIFICATE - A corrupt or unuseable certificate...

This is a simple symptom-cause-solution blog entry only. I hope these blogs will help fellow administrators.
Symptom
In wls_utc, when trying to test a webservice using SSL, the following error message is received: javax.net.ssl.SSLKeyException: FATAL Alert:BAD_CERTIFICATE - A corrupt or unuseable certificate was received.
If SSL debugging is enabled, the following error also appears in the logs: ExecuteThread: '4' for queue: 'weblogic.kernel.Default (self-tuning)' <1254822672320>>
verification failed because RSA key public exponent [3] is too small
Cause
The certificate encryption is of a weaker strength than expected by newer versions of Java.
Solution
Add the flag "-Dweblogic.security.SSL.allowSmallRSAExponent=true" to the server startup parameters.
References


None.

Connection refused: No available router to destination

This is a simple symptom-cause-solution blog entry only. I hope these blogs will help fellow administrators.
Symptom

The following exception occurs in WebLogic server logs. Most likely to occur during WebLogic server start-up, but similar exceptions may occur at other times.
java.net.ConnectException: t3://myserver:8000: Destination unreachable; nested exception is: java.net.ConnectException: Connection refused: connect; No available router to destination] at weblogic.jndi.internal.ExceptionTranslator.toNamingException(ExceptionTranslator.java:49) at weblogic.jndi.WLInitialContextFactoryDelegate.toNamingException(WLInitialContextFactoryDelegate.java:773) at weblogic.jndi.WLInitialContextFactoryDelegate.getInitialContext(WLInitialContextFactoryDelegate.java:363) at weblogic.jndi.Environment.getContext(Environment.java:307) at weblogic.jndi.Environment.getContext(Environment.java:277)
Cause
This message (Connection refused: connect; No available router to destination) is a kind of "catch…

WebLogic Admin Console

WebLogic Admin Console
The WebLogic Admin Console is a web-based, user interface used to configure and control a set of WebLogic servers or clusters (i.e. a "domain"). In any logical group of WebLogic servers there must exist one admin server, which hosts the WebLogic Admin Console application and manages the associated configuration files.
WebLogic Administrators will use the Administration Console for a number of tasks, including:
Starting and stopping WebLogic servers or entire clusters.Configuring server parameters, security, database connections and deployed applications.Viewing server status, health and metrics. Note: It is not strictly necessary to use the Weblogic Admin Console to perform these tasks, as they can be scripted using WLST (the WebLogic Scripting Tool).
Accessing the Admin Console
WebLogic Admin Console Url: http://hostname:port/console.
To access the WebLogic Administration Console, assuming the admin server has been started, goto the above url. Where hostname…