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

NullPointerException

java.lang.NullPointerException NullPointerException is described in the javadoc comments as: Thrown when an application attempts to use null in a case where an object is required. These include: Calling the instance method of a null object. Accessing or modifying the field of a null object. Taking the length of null as if it were an array. Accessing or modifying the slots of null as if it were an array. Throwing null as if it were a Throwable value. Applications should throw instances of this class to indicate other illegal uses of the null object. author: unascribed version: 1.19, 12/19/03 since: JDK1.0 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. The message ' java.lang.NullPointerException: ' is thrown within the method: com.sun.corba.se.impl.interceptors.ClientRequestInfoImpl.get_r

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

Recovering WebLogic Passwords

In one of my previous articles ( here ) I explained that the SerializedSystemIni.dat file in WebLogic contains the key used to encrypt and decrypt passwords. If you're not currently keeping this file secure I suggest you do, as with it someone can (to name a few things): Decrypt the WebLogic admin username and password from boot.properties. Recover database passwords, if JDBC Connection pools are configured, from config.xml. Recover the keystore passwords from config.xml and obtain SSL certificates stored in the jks keystores. Essentially, they can do whatever they want, so if you don't know who can read your SerializedSystemIni.dat files, look... now. In this article I will show how easy it is for this file to be used to recover lost passwords via a simple WLST script. The Script The script I use to decrypt passwords is incredibly short, and it works with WebLogic 8, 9 and 10 (probably for version 7 too). To use it, just create a new file called decryptpwd.py and paste the fol