Search in sources :

Example 31 with UnsupportedCharsetException

use of java.nio.charset.UnsupportedCharsetException in project j2objc by google.

the class ChannelsTest method testnewReaderCharsetError.

public void testnewReaderCharsetError() throws Exception {
    this.fins = new FileInputStream(tmpFile);
    ReadableByteChannel rbChannel = Channels.newChannel(this.fins);
    try {
        Channels.newReader(rbChannel, Charset.forName(BAD_CODE_SET).newDecoder(), -1);
        fail();
    } catch (UnsupportedCharsetException e) {
    // correct
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) FileInputStream(java.io.FileInputStream)

Example 32 with UnsupportedCharsetException

use of java.nio.charset.UnsupportedCharsetException in project j2objc by google.

the class Properties method storeToXML.

/**
     * Emits an XML document representing all of the properties contained
     * in this table, using the specified encoding.
     *
     * <p>The XML document will have the following DOCTYPE declaration:
     * <pre>
     * &lt;!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"&gt;
     * </pre>
     *
     *<p>If the specified comment is <code>null</code> then no comment
     * will be stored in the document.
     *
     * <p>The specified stream remains open after this method returns.
     *
     * @param os        the output stream on which to emit the XML document.
     * @param comment   a description of the property list, or <code>null</code>
     *                  if no comment is desired.
     * @param  encoding the name of a supported
     *                  <a href="../lang/package-summary.html#charenc">
     *                  character encoding</a>
     *
     * @throws IOException if writing to the specified output stream
     *         results in an <tt>IOException</tt>.
     * @throws NullPointerException if <code>os</code> is <code>null</code>,
     *         or if <code>encoding</code> is <code>null</code>.
     * @throws ClassCastException  if this <code>Properties</code> object
     *         contains any keys or values that are not
     *         <code>Strings</code>.
     * @see    #loadFromXML(InputStream)
     * @since 1.5
     */
public void storeToXML(OutputStream os, String comment, String encoding) throws IOException {
    if (os == null) {
        throw new NullPointerException("os == null");
    } else if (encoding == null) {
        throw new NullPointerException("encoding == null");
    }
    /*
         * 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 33 with UnsupportedCharsetException

use of java.nio.charset.UnsupportedCharsetException in project guava by hceylan.

the class MediaTypeTest method testGetCharset_unsupportedCharset.

public void testGetCharset_unsupportedCharset() {
    MediaType mediaType = MediaType.parse("text/plain; charset=utf-wtf");
    try {
        mediaType.charset();
        fail();
    } catch (UnsupportedCharsetException expected) {
    }
}
Also used : UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) MediaType(com.google.common.net.MediaType)

Example 34 with UnsupportedCharsetException

use of java.nio.charset.UnsupportedCharsetException in project robovm by robovm.

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) {
        throw new NullPointerException("os == null");
    } else if (encoding == null) {
        throw new NullPointerException("encoding == null");
    }
    /*
         * 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 35 with UnsupportedCharsetException

use of java.nio.charset.UnsupportedCharsetException in project robovm by robovm.

the class ChannelsTest method testnewReaderCharsetError.

public void testnewReaderCharsetError() throws Exception {
    this.fins = new FileInputStream(tmpFile);
    ReadableByteChannel rbChannel = Channels.newChannel(this.fins);
    try {
        Channels.newReader(rbChannel, Charset.forName(BAD_CODE_SET).newDecoder(), -1);
        fail();
    } catch (UnsupportedCharsetException e) {
    // correct
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) FileInputStream(java.io.FileInputStream)

Aggregations

UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)46 Charset (java.nio.charset.Charset)18 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)13 IOException (java.io.IOException)10 File (java.io.File)9 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 FileOutputStream (java.io.FileOutputStream)4 PrintStream (java.io.PrintStream)4 HashMap (java.util.HashMap)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 FileInputStream (java.io.FileInputStream)3 InputStream (java.io.InputStream)3 Test (org.junit.Test)3 FormBuilder (com.jgoodies.forms.builder.FormBuilder)2 FormLayout (com.jgoodies.forms.layout.FormLayout)2 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)2 InputStreamReader (java.io.InputStreamReader)2 StringReader (java.io.StringReader)2 ReadableByteChannel (java.nio.channels.ReadableByteChannel)2 WritableByteChannel (java.nio.channels.WritableByteChannel)2