Can I force an UnsupportedEncodingException to happen in MimeMessage?

I have a java application that consumes email messages which have attachments. Occasionally I see errors like this: X-iso88591
at sun.nio.cs.StreamDecoder.forInputStreamReader( ~[na:1.7.0_65]
at<init>( ~[na:1.7.0_65]
at com.sun.mail.handlers.text_plain.getContent( ~[mail-1.4.5.jar:na]
at javax.activation.DataSourceDataContentHandler.getContent( ~[na:1.7.0_65]
at javax.activation.DataHandler.getContent( ~[na:1.7.0_65]
at javax.mail.internet.MimeMessage.getContent( ~[mail-1.4.5.jar:na]

I'm trying to write a unit test to reproduce this behaviour. But I'm having a problem whereby I can't encode something "badly" so that I can attempt (and fail) to decode it later.

I've even written a "fake" Charset (called BorkBorkBork) - but that only seems to get used for encoding but not decoding

MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()));
Multipart container = new MimeMultipart();
MimeBodyPart bodyPart = new MimeBodyPart();
bodyPart.setDescription("日本語 Nihongo", "BorkBorkBork");
bodyPart.setText("日本語 Nihongo", "BorkBorkBork");
message.getContent(); // I want this to fail

This is the Charset that I've written

public class BorkBorkBorkCharset extends Charset {

    public BorkBorkBorkCharset() {
        super("BorkBorkBork", new String[]{});

    public boolean contains(Charset cs) {
        throw new UnsupportedOperationException();

    public CharsetDecoder newDecoder() {
        throw new UnsupportedOperationException();

    public CharsetEncoder newEncoder() {
        return new CharsetEncoder(Charset.forName("UTF8"), 10, 10) {
            protected CoderResult encodeLoop(CharBuffer charBuffer, ByteBuffer byteBuffer) {
                return CoderResult.UNDERFLOW;

I also wrote a CharsetProvider

public class BorkBorkBorkCharsetProvider extends CharsetProvider {
    public Iterator<Charset> charsets() {
        throw new UnsupportedOperationException();

    public Charset charsetForName(String charsetName) {
        if (StringUtils.equals("BorkBorkBork", charsetName)) {
            return new BorkBorkBorkCharset();
        return null;

I'm not sure I'm going down the right path for this. Is it possible? Is there another approach that I can take?


I got around the problem through mocking. I didn't exactly reproduce the scenario but I was able to simulate the bad way that my code deals with the Exception

when(mimeMessage.getContent()).thenThrow(new UnsupportedEncodingException("X-iso88591"));

You don't need to do any special encoding or create your own Charset. Just create a MimeMessage, use the setText method to set the body of the message, and specify a bogus charset name to the setText method. Then call MimeMessage.saveChanges and use the getContent method to read back the body of the message. You should get an UnsupportedEncodingException.

Need Your Help

Hibernate: Deadlock found when trying to obtain lock

java mysql hibernate c3p0

I am using hibernate in my project and I am getting random Apparent Deadlocks for very simple database operations.

How to destroy() my popup correctly in Sencha Touch

popup sencha-touch extjs destroy

I am having difficulties implementing the destroy method on my popup. Everything works fine, the below code works for having one popup that changes its contents depending what is clicked on. But I ...