<?xml version="1.0" encoding="UTF-8"?><rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
> <channel><title>Comments on: UnsupportedEncodingException when invoking getContent() on a bodypart that contains text data</title> <atom:link href="http://iderror.com/errors/java/java_mail/unsupportedencodingexception-when-invoking-getcontent-on-a-bodypart-that-contains-text-data/feed/" rel="self" type="application/rss+xml" /><link>http://iderror.com/errors/java/java_mail/unsupportedencodingexception-when-invoking-getcontent-on-a-bodypart-that-contains-text-data/</link> <description>You have errors, we have solutions!</description> <lastBuildDate>Fri, 10 Jun 2011 15:06:54 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.2.1</generator> <item><title>By: admin</title><link>http://iderror.com/errors/java/java_mail/unsupportedencodingexception-when-invoking-getcontent-on-a-bodypart-that-contains-text-data/comment-page-1/#comment-7942</link> <dc:creator>admin</dc:creator> <pubDate>Fri, 20 Mar 2009 10:28:59 +0000</pubDate> <guid
isPermaLink="false">http://localhost/idError/?p=8014#comment-7942</guid> <description>In this case, you can use the getInputStream() method to retrieve the content as a stream of bytes. For example:String s;
if (part.isMimeType(&quot;text/plain&quot;)) {
try {
s = part.getContent();
} catch (UnsupportedEncodingException uex) {
InputStream is = part.getInputStream();
/*
* Read the input stream into a byte array.
* Choose a charset in some heuristic manner, use
* that charset in the java.lang.String constructor
* to convert the byte array into a String.
*/
s = convert_to_string(is);
} catch (Exception ex) {
// Handle other exceptions appropriately
}
}There are some commonly used charsets that the JDK does not yet support.
You can also add an alias for an existing charset already supported by the JDK so that it will be known by an additional name. You can create a charset provider for the &quot;bad&quot; charset name that simply redirects to an existing charset provider; see the following code. Create an appropriate CharsetProvider subclass and include it along with the META-INF/services file and the JDK will find it. Obviously you could get significantly more clever and redirect all unknown charsets to &quot;us-ascii&quot;, for instance.==&gt; TestCharsetProvider.java &lt;==
import java.nio.charset.*;
import java.nio.charset.spi.*;
import java.util.*;public class TestCharsetProvider extends CharsetProvider {
private static final String badCharset = &quot;cp-1252&quot;;
private static final String goodCharset = &quot;cp1252&quot;;public Charset charsetForName(String charset) {
if (charset.equals(badCharset))
return Charset.forName(goodCharset);
return null;
}public Iterator charsets() {
return null;
}
}==&gt; META-INF/services/java.nio.charset.spi.CharsetProvider &lt;==
TestCharsetProvider </description> <content:encoded><![CDATA[<p>In this case, you can use the getInputStream() method to retrieve the content as a stream of bytes. For example:</p><p> String s;<br
/> if (part.isMimeType(&#8220;text/plain&#8221;)) {<br
/> try {<br
/> s = part.getContent();<br
/> } catch (UnsupportedEncodingException uex) {<br
/> InputStream is = part.getInputStream();<br
/> /*<br
/> * Read the input stream into a byte array.<br
/> * Choose a charset in some heuristic manner, use<br
/> * that charset in the java.lang.String constructor<br
/> * to convert the byte array into a String.<br
/> */<br
/> s = convert_to_string(is);<br
/> } catch (Exception ex) {<br
/> // Handle other exceptions appropriately<br
/> }<br
/> }</p><p>There are some commonly used charsets that the JDK does not yet support.<br
/> You can also add an alias for an existing charset already supported by the JDK so that it will be known by an additional name. You can create a charset provider for the &#8220;bad&#8221; charset name that simply redirects to an existing charset provider; see the following code. Create an appropriate CharsetProvider subclass and include it along with the META-INF/services file and the JDK will find it. Obviously you could get significantly more clever and redirect all unknown charsets to &#8220;us-ascii&#8221;, for instance.</p><p>==> TestCharsetProvider.java < ==<br
/> import java.nio.charset.*;<br
/> import java.nio.charset.spi.*;<br
/> import java.util.*;</p><p>public class TestCharsetProvider extends CharsetProvider {<br
/> private static final String badCharset = "cp-1252";<br
/> private static final String goodCharset = "cp1252";</p><p> public Charset charsetForName(String charset) {<br
/> if (charset.equals(badCharset))<br
/> return Charset.forName(goodCharset);<br
/> return null;<br
/> }</p><p> public Iterator charsets() {<br
/> return null;<br
/> }<br
/> }</p><p>==> META-INF/services/java.nio.charset.spi.CharsetProvider <==<br
/> TestCharsetProvider</p> ]]></content:encoded> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced

Served from: iderror.com @ 2012-02-10 18:38:53 -->
