From afb65d5ad7ec2b773480dd674dbc8819e952934d Mon Sep 17 00:00:00 2001 From: Art O Cathain Date: Sun, 28 Sep 2014 11:39:32 +0100 Subject: [PATCH] remove some try-catch cruft --- src/com/fsck/k9/PRNGFixes.java | 9 ++--- src/com/fsck/k9/activity/MessageCompose.java | 9 ++--- src/com/fsck/k9/helper/Utility.java | 12 +++---- src/com/fsck/k9/mail/filter/Base64.java | 10 ++---- .../fsck/k9/mail/internet/DecoderUtil.java | 17 +++------- src/com/fsck/k9/mail/store/ImapStore.java | 33 +++++-------------- src/com/fsck/k9/view/MessageOpenPgpView.java | 8 ++--- 7 files changed, 28 insertions(+), 70 deletions(-) diff --git a/src/com/fsck/k9/PRNGFixes.java b/src/com/fsck/k9/PRNGFixes.java index e1b20d23b..33d4ff6c9 100644 --- a/src/com/fsck/k9/PRNGFixes.java +++ b/src/com/fsck/k9/PRNGFixes.java @@ -19,7 +19,7 @@ import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; -import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; import java.security.NoSuchAlgorithmException; import java.security.Provider; import java.security.SecureRandom; @@ -284,10 +284,7 @@ public final class PRNGFixes { if (serial != null) { result.append(serial); } - try { - return result.toString().getBytes("UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException("UTF-8 encoding not supported"); - } + + return result.toString().getBytes(Charset.forName("UTF-8")); } } diff --git a/src/com/fsck/k9/activity/MessageCompose.java b/src/com/fsck/k9/activity/MessageCompose.java index 1bda6cb47..05170db2e 100644 --- a/src/com/fsck/k9/activity/MessageCompose.java +++ b/src/com/fsck/k9/activity/MessageCompose.java @@ -5,6 +5,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; import java.text.DateFormat; import java.util.ArrayList; import java.util.Arrays; @@ -1890,13 +1891,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, private InputStream getOpenPgpInputStream() { String text = buildText(false).getText(); - InputStream is = null; - try { - is = new ByteArrayInputStream(text.getBytes("UTF-8")); - } catch (UnsupportedEncodingException e) { - Log.e(K9.LOG_TAG, "UnsupportedEncodingException.", e); - } - return is; + return new ByteArrayInputStream(text.getBytes(Charset.forName("UTF-8"))); } private void executeOpenPgpMethod(Intent intent) { diff --git a/src/com/fsck/k9/helper/Utility.java b/src/com/fsck/k9/helper/Utility.java index a5bea3448..ae8d9bb71 100644 --- a/src/com/fsck/k9/helper/Utility.java +++ b/src/com/fsck/k9/helper/Utility.java @@ -20,7 +20,7 @@ import com.fsck.k9.mail.filter.Base64; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; -import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -189,8 +189,8 @@ public class Utility { * hundreds of times in places that slow down the UI, so it helps. */ public static String fastUrlDecode(String s) { - try { - byte[] bytes = s.getBytes("UTF-8"); + + byte[] bytes = s.getBytes(Charset.forName("UTF-8")); byte ch; int length = 0; for (int i = 0, count = bytes.length; i < count; i++) { @@ -213,10 +213,8 @@ public class Utility { } length++; } - return new String(bytes, 0, length, "UTF-8"); - } catch (UnsupportedEncodingException uee) { - return null; - } + return new String(bytes, 0, length, Charset.forName("UTF-8")); + } /* diff --git a/src/com/fsck/k9/mail/filter/Base64.java b/src/com/fsck/k9/mail/filter/Base64.java index ffc7abf94..934c3b222 100644 --- a/src/com/fsck/k9/mail/filter/Base64.java +++ b/src/com/fsck/k9/mail/filter/Base64.java @@ -17,8 +17,8 @@ package com.fsck.k9.mail.filter; -import java.io.UnsupportedEncodingException; import java.math.BigInteger; +import java.nio.charset.Charset; /** * Provides Base64 encoding and decoding as defined by RFC 2045. @@ -225,12 +225,8 @@ public class Base64 { } this.decodeSize = encodeSize - 1; if (containsBase64Byte(lineSeparator)) { - String sep; - try { - sep = new String(lineSeparator, "UTF-8"); - } catch (UnsupportedEncodingException uee) { - sep = new String(lineSeparator); - } + String sep = new String(lineSeparator, Charset.forName("UTF-8")); + throw new IllegalArgumentException("lineSeperator must not contain base64 characters: [" + sep + "]"); } } diff --git a/src/com/fsck/k9/mail/internet/DecoderUtil.java b/src/com/fsck/k9/mail/internet/DecoderUtil.java index 5c7d5034f..80d5b573e 100644 --- a/src/com/fsck/k9/mail/internet/DecoderUtil.java +++ b/src/com/fsck/k9/mail/internet/DecoderUtil.java @@ -7,7 +7,8 @@ import com.fsck.k9.mail.Message; import com.fsck.k9.mail.MessagingException; import java.io.ByteArrayInputStream; import java.io.IOException; -import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; + import org.apache.james.mime4j.codec.Base64InputStream; import org.apache.james.mime4j.codec.QuotedPrintableInputStream; import org.apache.james.mime4j.util.CharsetUtil; @@ -30,12 +31,7 @@ public class DecoderUtil { * @return the decoded string. */ private static String decodeB(String encodedWord, String charset) { - byte[] bytes; - try { - bytes = encodedWord.getBytes("US-ASCII"); - } catch (UnsupportedEncodingException e) { - return null; - } + byte[] bytes = encodedWord.getBytes(Charset.forName("US-ASCII")); Base64InputStream is = new Base64InputStream(new ByteArrayInputStream(bytes)); try { @@ -68,12 +64,7 @@ public class DecoderUtil { } } - byte[] bytes; - try { - bytes = sb.toString().getBytes("US-ASCII"); - } catch (UnsupportedEncodingException e) { - return null; - } + byte[] bytes = sb.toString().getBytes(Charset.forName("US-ASCII")); QuotedPrintableInputStream is = new QuotedPrintableInputStream(new ByteArrayInputStream(bytes)); try { diff --git a/src/com/fsck/k9/mail/store/ImapStore.java b/src/com/fsck/k9/mail/store/ImapStore.java index 1874d91b6..0f33a473e 100644 --- a/src/com/fsck/k9/mail/store/ImapStore.java +++ b/src/com/fsck/k9/mail/store/ImapStore.java @@ -757,18 +757,10 @@ public class ImapStore extends Store { } private String encodeFolderName(String name) { - try { - ByteBuffer bb = mModifiedUtf7Charset.encode(name); - byte[] b = new byte[bb.limit()]; - bb.get(b); - return new String(b, "US-ASCII"); - } catch (UnsupportedEncodingException uee) { - /* - * The only thing that can throw this is getBytes("US-ASCII") and if US-ASCII doesn't - * exist we're totally screwed. - */ - throw new RuntimeException("Unable to encode folder name: " + name, uee); - } + ByteBuffer bb = mModifiedUtf7Charset.encode(name); + byte[] b = new byte[bb.limit()]; + bb.get(b); + return new String(b, Charset.forName("US-ASCII")); } private String decodeFolderName(String name) throws CharacterCodingException { @@ -776,18 +768,11 @@ public class ImapStore extends Store { * Convert the encoded name to US-ASCII, then pass it through the modified UTF-7 * decoder and return the Unicode String. */ - try { - // Make sure the decoder throws an exception if it encounters an invalid encoding. - CharsetDecoder decoder = mModifiedUtf7Charset.newDecoder().onMalformedInput(CodingErrorAction.REPORT); - CharBuffer cb = decoder.decode(ByteBuffer.wrap(name.getBytes("US-ASCII"))); - return cb.toString(); - } catch (UnsupportedEncodingException uee) { - /* - * The only thing that can throw this is getBytes("US-ASCII") and if US-ASCII doesn't - * exist we're totally screwed. - */ - throw new RuntimeException("Unable to decode folder name: " + name, uee); - } + // Make sure the decoder throws an exception if it encounters an invalid encoding. + CharsetDecoder decoder = mModifiedUtf7Charset.newDecoder().onMalformedInput(CodingErrorAction.REPORT); + CharBuffer cb = decoder.decode(ByteBuffer.wrap(name.getBytes(Charset.forName("US-ASCII")))); + return cb.toString(); + } @Override diff --git a/src/com/fsck/k9/view/MessageOpenPgpView.java b/src/com/fsck/k9/view/MessageOpenPgpView.java index dd2cbb40b..1b224112f 100644 --- a/src/com/fsck/k9/view/MessageOpenPgpView.java +++ b/src/com/fsck/k9/view/MessageOpenPgpView.java @@ -5,6 +5,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; import android.app.Activity; import android.app.Fragment; @@ -279,12 +280,7 @@ public class MessageOpenPgpView extends LinearLayout { String accName = OpenPgpApiHelper.buildAccountName(identity); intent.putExtra(OpenPgpApi.EXTRA_ACCOUNT_NAME, accName); - InputStream is = null; - try { - is = new ByteArrayInputStream(mData.getBytes("UTF-8")); - } catch (UnsupportedEncodingException e) { - Log.e(K9.LOG_TAG, "UnsupportedEncodingException.", e); - } + InputStream is = new ByteArrayInputStream(mData.getBytes(Charset.forName("UTF-8"))); final ByteArrayOutputStream os = new ByteArrayOutputStream(); DecryptVerifyCallback callback = new DecryptVerifyCallback(os, REQUEST_CODE_DECRYPT_VERIFY);