Search in sources :

Example 6 with IllegalCharsetNameException

use of java.nio.charset.IllegalCharsetNameException in project jdk8u_jdk by JetBrains.

the class Test4625418 method test.

private void test(String string) {
    try {
        File file = new File("4625418." + this.encoding + ".xml");
        FileOutputStream output = new FileOutputStream(file);
        XMLEncoder encoder = new XMLEncoder(output, this.encoding, true, 0);
        encoder.setExceptionListener(this);
        encoder.writeObject(string);
        encoder.close();
        FileInputStream input = new FileInputStream(file);
        XMLDecoder decoder = new XMLDecoder(input);
        decoder.setExceptionListener(this);
        Object object = decoder.readObject();
        decoder.close();
        if (!string.equals(object))
            throw new Error(this.encoding + " - can't read properly");
        file.delete();
    } catch (FileNotFoundException exception) {
        throw new Error(this.encoding + " - file not found", exception);
    } catch (IllegalCharsetNameException exception) {
        throw new Error(this.encoding + " - illegal charset name", exception);
    } catch (UnsupportedCharsetException exception) {
        throw new Error(this.encoding + " - unsupported charset", exception);
    } catch (UnsupportedOperationException exception) {
        throw new Error(this.encoding + " - unsupported encoder", exception);
    }
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) XMLEncoder(java.beans.XMLEncoder) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) FileOutputStream(java.io.FileOutputStream) XMLDecoder(java.beans.XMLDecoder) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 7 with IllegalCharsetNameException

use of java.nio.charset.IllegalCharsetNameException in project Asqatasun by Asqatasun.

the class CSSJsoupPhlocContentAdapterImpl method createNewExternalStyleSheet.

/**
     * 
     * @param cssAbsolutePath
     * @return 
     */
private StylesheetContent createNewExternalStyleSheet(String cssAbsolutePath) {
    LOGGER.debug("createNewExternalStyleSheet " + cssAbsolutePath);
    String cssSourceCode;
    try {
        cssSourceCode = HttpRequestHandler.getInstance().getHttpContent(cssAbsolutePath);
    } catch (URISyntaxException ex) {
        LOGGER.debug("the resource " + cssAbsolutePath + " can't be retrieved : URISyntaxException");
        cssSourceCode = CSS_ON_ERROR;
    } catch (UnknownHostException uhe) {
        LOGGER.debug("the resource " + cssAbsolutePath + " can't be retrieved : UnknownHostException");
        cssSourceCode = CSS_ON_ERROR;
    } catch (IOException ioe) {
        LOGGER.debug("the resource " + cssAbsolutePath + " can't be retrieved : IOException");
        try {
            cssSourceCode = FileUtils.readFileToString(new File(cssAbsolutePath));
        } catch (IOException ioe2) {
            LOGGER.debug("the resource " + cssAbsolutePath + " can't be retrieved : IOException");
            cssSourceCode = CSS_ON_ERROR;
        }
    } catch (IllegalCharsetNameException icne) {
        LOGGER.debug("the resource " + cssAbsolutePath + " can't be retrieved : IllegalCharsetNameException");
        cssSourceCode = CSS_ON_ERROR;
    } catch (IllegalStateException ise) {
        LOGGER.debug("the resource " + cssAbsolutePath + " can't be retrieved : IllegalStateException");
        cssSourceCode = CSS_ON_ERROR;
    }
    if (StringUtils.isBlank(cssSourceCode)) {
        LOGGER.debug("the resource " + cssAbsolutePath + " has an empty content");
        cssSourceCode = CSS_ON_ERROR;
    }
    StylesheetContent cssContent = getContentDataService().getStylesheetContent(new Date(), cssAbsolutePath, getSSP(), cssSourceCode, 200);
    cssContent.setAudit(getSSP().getAudit());
    externalCssSet.add(cssContent);
    // Some stylesheet may be retrieved during the adaptation. In this case
    // these new css are added "manually" to the externalCssRetriever which
    // is supposed to request the bdd once at the beginning of the adapting
    // phasis.
    externalCSSRetriever.addNewStylesheetContent(getSSP(), cssContent);
    return cssContent;
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) StylesheetContent(org.asqatasun.entity.audit.StylesheetContent) UnknownHostException(java.net.UnknownHostException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) File(java.io.File)

Example 8 with IllegalCharsetNameException

use of java.nio.charset.IllegalCharsetNameException in project jdk8u_jdk by JetBrains.

the class URLEncoder method encode.

/**
     * Translates a string into {@code application/x-www-form-urlencoded}
     * format using a specific encoding scheme. This method uses the
     * supplied encoding scheme to obtain the bytes for unsafe
     * characters.
     * <p>
     * <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
     * incompatibilities.</em>
     *
     * @param   s   {@code String} 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}.
     * @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);
        //System.out.println("Examining character: " + c);
        if (dontNeedEncoding.get(c)) {
            if (c == ' ') {
                c = '+';
                needToChange = true;
            }
            //System.out.println("Storing: " + c);
            out.append((char) c);
            i++;
        } else {
            // convert to external encoding before hex conversion
            do {
                charArrayWriter.write(c);
                /*
                     * If this character represents the start of a Unicode
                     * surrogate pair, then pass in two characters. It's not
                     * clear what should be done if a bytes reserved in the
                     * surrogate pairs range occurs outside of a legal
                     * surrogate pair. For now, just treat it as if it were
                     * any other character.
                     */
                if (c >= 0xD800 && c <= 0xDBFF) {
                    /*
                          System.out.println(Integer.toHexString(c)
                          + " is high surrogate");
                        */
                    if ((i + 1) < s.length()) {
                        int d = (int) s.charAt(i + 1);
                        /*
                              System.out.println("\tExamining "
                              + Integer.toHexString(d));
                            */
                        if (d >= 0xDC00 && d <= 0xDFFF) {
                            /*
                                  System.out.println("\t"
                                  + Integer.toHexString(d)
                                  + " is low surrogate");
                                */
                            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);
            for (int j = 0; j < ba.length; j++) {
                out.append('%');
                char ch = Character.forDigit((ba[j] >> 4) & 0xF, 16);
                // the hex value if ch is a letter.
                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);
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) Charset(java.nio.charset.Charset) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CharArrayWriter(java.io.CharArrayWriter)

Example 9 with IllegalCharsetNameException

use of java.nio.charset.IllegalCharsetNameException in project jdk8u_jdk by JetBrains.

the class Main method initializeConverter.

private static void initializeConverter() throws UnsupportedEncodingException {
    Charset cs = null;
    try {
        cs = (encodingString == null) ? lookupCharset(defaultEncoding) : lookupCharset(encodingString);
        encoder = (cs != null) ? cs.newEncoder() : null;
    } catch (IllegalCharsetNameException e) {
        throw new Error(e);
    }
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) Charset(java.nio.charset.Charset)

Example 10 with IllegalCharsetNameException

use of java.nio.charset.IllegalCharsetNameException in project tika by apache.

the class CharsetUtils method forName.

/** Returns Charset impl, if one exists.  This method
     *  optionally uses ICU4J's CharsetICU.forNameICU,
     *  if it is found on the classpath, else only uses
     *  JDK's builtin Charset.forName. */
public static Charset forName(String name) {
    if (name == null) {
        throw new IllegalArgumentException();
    }
    // Get rid of cruft around names, like <>, trailing commas, etc.
    Matcher m = CHARSET_NAME_PATTERN.matcher(name);
    if (!m.matches()) {
        throw new IllegalCharsetNameException(name);
    }
    name = m.group(1);
    String lower = name.toLowerCase(Locale.ENGLISH);
    Charset charset = COMMON_CHARSETS.get(lower);
    if (charset != null) {
        return charset;
    } else if ("none".equals(lower) || "no".equals(lower)) {
        throw new IllegalCharsetNameException(name);
    } else {
        Matcher iso = ISO_NAME_PATTERN.matcher(lower);
        Matcher cp = CP_NAME_PATTERN.matcher(lower);
        Matcher win = WIN_NAME_PATTERN.matcher(lower);
        if (iso.matches()) {
            // Handle "iso 8859-x" error
            name = "iso-8859-" + iso.group(1);
            charset = COMMON_CHARSETS.get(name);
        } else if (cp.matches()) {
            // Handle "cp-xxx" error
            name = "cp" + cp.group(1);
            charset = COMMON_CHARSETS.get(name);
        } else if (win.matches()) {
            // Handle "winxxx" and "win-xxx" errors
            name = "windows-" + win.group(1);
            charset = COMMON_CHARSETS.get(name);
        }
        if (charset != null) {
            return charset;
        }
    }
    if (getCharsetICU != null) {
        try {
            Charset cs = (Charset) getCharsetICU.invoke(null, name);
            if (cs != null) {
                return cs;
            }
        } catch (Exception e) {
        // ignore
        }
    }
    return Charset.forName(name);
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) Matcher(java.util.regex.Matcher) Charset(java.nio.charset.Charset) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException)

Aggregations

IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)24 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)13 Charset (java.nio.charset.Charset)10 File (java.io.File)3 IOException (java.io.IOException)3 PrintStream (java.io.PrintStream)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 CharBuffer (java.nio.CharBuffer)2 CharacterCodingException (java.nio.charset.CharacterCodingException)2 CharsetDecoder (java.nio.charset.CharsetDecoder)2 Matcher (java.util.regex.Matcher)2 CoreException (org.eclipse.core.runtime.CoreException)2 IStatus (org.eclipse.core.runtime.IStatus)2 Status (org.eclipse.core.runtime.Status)2 HistoricallyNamedCharset (sun.nio.cs.HistoricallyNamedCharset)2 MediaType (com.google.common.net.MediaType)1 DefaultToolOptions (com.redhat.ceylon.common.config.DefaultToolOptions)1 Document (com.smartandroid.sa.tag.nodes.Document)1 Element (com.smartandroid.sa.tag.nodes.Element)1 Lint (com.sun.tools.javac.code.Lint)1