Search in sources :

Example 36 with IllegalCharsetNameException

use of java.nio.charset.IllegalCharsetNameException in project intellij-community by JetBrains.

the class BinaryContent method getDocument.

@Override
@Nullable
public Document getDocument() {
    if (myDocument == null) {
        if (isBinary())
            return null;
        String text = null;
        try {
            Charset charset = ObjectUtils.notNull(myCharset, EncodingProjectManager.getInstance(myProject).getDefaultCharset());
            text = CharsetToolkit.bytesToString(myBytes, charset);
        } catch (IllegalCharsetNameException ignored) {
        }
        //  Decode a string using the truly default encoding.
        if (text == null)
            text = new String(myBytes);
        text = LineTokenizer.correctLineSeparators(text);
        myDocument = EditorFactory.getInstance().createDocument(text);
        myDocument.setReadOnly(true);
    }
    return myDocument;
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) Charset(java.nio.charset.Charset) Nullable(org.jetbrains.annotations.Nullable)

Example 37 with IllegalCharsetNameException

use of java.nio.charset.IllegalCharsetNameException in project XobotOS by xamarin.

the class Properties method storeToXML.

/**
     * Writes all properties stored in this instance into the {@code
     * OutputStream} in XML representation. The DOCTYPE is
     *
     * <pre>
     * &lt;!DOCTYPE properties SYSTEM &quot;http://java.sun.com/dtd/properties.dtd&quot;&gt;
     * </pre>
     *
     * If the comment is null, no comment is added to the output. The parameter
     * {@code encoding} defines which encoding should be used. The {@code
     * OutputStream} is not closed at the end.
     *
     * @param os the {@code OutputStream} to write to.
     * @param comment the comment to add. If null, no comment is added.
     * @param encoding the code identifying the encoding that should be used to
     *            write into the {@code OutputStream}.
     * @throws IOException if an error occurs during writing to the output.
     */
public synchronized void storeToXML(OutputStream os, String comment, String encoding) throws IOException {
    if (os == null || encoding == null) {
        throw new NullPointerException();
    }
    /*
         * We can write to XML file using encoding parameter but note that some
         * aliases for encodings are not supported by the XML parser. Thus we
         * have to know canonical name for encoding used to store data in XML
         * since the XML parser must recognize encoding name used to store data.
         */
    String encodingCanonicalName;
    try {
        encodingCanonicalName = Charset.forName(encoding).name();
    } catch (IllegalCharsetNameException e) {
        System.out.println("Warning: encoding name " + encoding + " is illegal, using UTF-8 as default encoding");
        encodingCanonicalName = "UTF-8";
    } catch (UnsupportedCharsetException e) {
        System.out.println("Warning: encoding " + encoding + " is not supported, using UTF-8 as default encoding");
        encodingCanonicalName = "UTF-8";
    }
    PrintStream printStream = new PrintStream(os, false, encodingCanonicalName);
    printStream.print("<?xml version=\"1.0\" encoding=\"");
    printStream.print(encodingCanonicalName);
    printStream.println("\"?>");
    printStream.print("<!DOCTYPE properties SYSTEM \"");
    printStream.print(PROP_DTD_NAME);
    printStream.println("\">");
    printStream.println("<properties>");
    if (comment != null) {
        printStream.print("<comment>");
        printStream.print(substitutePredefinedEntries(comment));
        printStream.println("</comment>");
    }
    for (Map.Entry<Object, Object> entry : entrySet()) {
        String keyValue = (String) entry.getKey();
        String entryValue = (String) entry.getValue();
        printStream.print("<entry key=\"");
        printStream.print(substitutePredefinedEntries(keyValue));
        printStream.print("\">");
        printStream.print(substitutePredefinedEntries(entryValue));
        printStream.println("</entry>");
    }
    printStream.println("</properties>");
    printStream.flush();
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) PrintStream(java.io.PrintStream) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException)

Example 38 with IllegalCharsetNameException

use of java.nio.charset.IllegalCharsetNameException in project intellij-community by JetBrains.

the class JdkUtil method appendEncoding.

private static void appendEncoding(SimpleJavaParameters javaParameters, GeneralCommandLine commandLine, ParametersList parametersList) {
    // Value of file.encoding and charset of GeneralCommandLine should be in sync in order process's input and output be correctly handled.
    String encoding = parametersList.getPropertyValue("file.encoding");
    if (encoding == null) {
        Charset charset = javaParameters.getCharset();
        if (charset == null)
            charset = EncodingManager.getInstance().getDefaultCharset();
        commandLine.addParameter("-Dfile.encoding=" + charset.name());
        commandLine.withCharset(charset);
    } else {
        try {
            Charset charset = Charset.forName(encoding);
            commandLine.withCharset(charset);
        } catch (UnsupportedCharsetException | IllegalCharsetNameException ignore) {
        }
    }
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) Charset(java.nio.charset.Charset)

Example 39 with IllegalCharsetNameException

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

the class StringCoding method decode.

static char[] decode(String charsetName, byte[] ba, int off, int len) throws UnsupportedEncodingException {
    StringDecoder sd = deref(decoder);
    String csn = (charsetName == null) ? "ISO-8859-1" : charsetName;
    if ((sd == null) || !(csn.equals(sd.requestedCharsetName()) || csn.equals(sd.charsetName()))) {
        sd = null;
        try {
            Charset cs = lookupCharset(csn);
            if (cs != null)
                sd = new StringDecoder(cs, csn);
        } catch (IllegalCharsetNameException x) {
        }
        if (sd == null)
            throw new UnsupportedEncodingException(csn);
        set(decoder, sd);
    }
    return sd.decode(ba, off, len);
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) HistoricallyNamedCharset(sun.nio.cs.HistoricallyNamedCharset) Charset(java.nio.charset.Charset) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 40 with IllegalCharsetNameException

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

the class StringCoding method encode.

static byte[] encode(String charsetName, char[] ca, int off, int len) throws UnsupportedEncodingException {
    StringEncoder se = deref(encoder);
    String csn = (charsetName == null) ? "ISO-8859-1" : charsetName;
    if ((se == null) || !(csn.equals(se.requestedCharsetName()) || csn.equals(se.charsetName()))) {
        se = null;
        try {
            Charset cs = lookupCharset(csn);
            if (cs != null)
                se = new StringEncoder(cs, csn);
        } catch (IllegalCharsetNameException x) {
        }
        if (se == null)
            throw new UnsupportedEncodingException(csn);
        set(encoder, se);
    }
    return se.encode(ca, off, len);
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) HistoricallyNamedCharset(sun.nio.cs.HistoricallyNamedCharset) Charset(java.nio.charset.Charset) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)69 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)44 Charset (java.nio.charset.Charset)34 IOException (java.io.IOException)13 UnsupportedEncodingException (java.io.UnsupportedEncodingException)10 File (java.io.File)9 ByteBuffer (java.nio.ByteBuffer)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStream (java.io.InputStream)6 CharacterCodingException (java.nio.charset.CharacterCodingException)6 CoreException (org.eclipse.core.runtime.CoreException)6 IStatus (org.eclipse.core.runtime.IStatus)6 Status (org.eclipse.core.runtime.Status)6 HistoricallyNamedCharset (sun.nio.cs.HistoricallyNamedCharset)6 CharsetEncoder (java.nio.charset.CharsetEncoder)5 UnmappableCharacterException (java.nio.charset.UnmappableCharacterException)5 SequenceInputStream (java.io.SequenceInputStream)4 XmlPullParserException (org.codehaus.plexus.util.xml.pull.XmlPullParserException)4 CharArrayWriter (java.io.CharArrayWriter)3 FileInputStream (java.io.FileInputStream)3