You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by ma...@apache.org on 2019/11/12 21:37:23 UTC

[james-project] 01/15: [Refactoring] use StandardCharsets in more places

This is an automated email from the ASF dual-hosted git repository.

matthieu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit b137370dce2d36aab3067c341b7398c9ae8b5f2a
Author: Matthieu Baechler <ma...@apache.org>
AuthorDate: Thu Nov 7 21:54:46 2019 +0100

    [Refactoring] use StandardCharsets in more places
---
 .gitignore                                         |  1 +
 .../mailbox/store/streaming/FullByteContent.java   |  5 +-
 .../protocols/api/AbstractProtocolTransport.java   |  8 +---
 .../parser/FetchCommandParserPartialFetchTest.java |  7 +--
 .../SearchCommandParserAndParenthesesTest.java     |  7 +--
 .../parser/SearchCommandParserCharsetTest.java     | 19 ++++----
 .../decode/parser/SearchCommandParserNotTest.java  |  5 +-
 .../decode/parser/SearchCommandParserOrTest.java   |  3 +-
 .../SearchCommandParserQuotedCharsetTest.java      | 55 ++++++++++------------
 ...earchCommandParserSearchKeySequenceSetTest.java |  3 +-
 .../parser/SearchCommandParserSearchKeyTest.java   |  5 +-
 .../parser/SearchCommandParserTopLevelAndTest.java |  7 ++-
 .../imap/decode/parser/StoreCommandParserTest.java |  3 +-
 .../james/protocols/pop3/utils/MockMailbox.java    | 20 ++++----
 .../server/core/InternetHeadersInputStream.java    | 30 +++++-------
 .../apache/james/server/core/MimeMessageTest.java  |  3 +-
 .../imapserver/netty/ImapHeartbeatHandler.java     |  4 +-
 17 files changed, 90 insertions(+), 95 deletions(-)

diff --git a/.gitignore b/.gitignore
index eefcea2..67357ab 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,3 +5,4 @@ dockerfiles/run/**/*.jar
 dockerfiles/run/**/keystore
 .m2
 test-run.log
+build
\ No newline at end of file
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/streaming/FullByteContent.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/streaming/FullByteContent.java
index 758db90..8864182 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/streaming/FullByteContent.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/streaming/FullByteContent.java
@@ -23,6 +23,7 @@ import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.SequenceInputStream;
+import java.nio.charset.StandardCharsets;
 import java.util.List;
 
 import org.apache.james.mailbox.exception.MailboxException;
@@ -64,10 +65,10 @@ public class FullByteContent implements Content {
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         for (Header header : headers) {
             if (header != null) {
-                out.write((header.getName() + ": " + header.getValue() + "\r\n").getBytes("US-ASCII"));
+                out.write((header.getName() + ": " + header.getValue() + "\r\n").getBytes(StandardCharsets.US_ASCII));
             }
         }
-        out.write("\r\n".getBytes("US-ASCII"));
+        out.write("\r\n".getBytes(StandardCharsets.US_ASCII));
         out.flush();
         return new SequenceInputStream(new ByteArrayInputStream(out.toByteArray()), new ByteArrayInputStream(body));
     }
diff --git a/protocols/api/src/main/java/org/apache/james/protocols/api/AbstractProtocolTransport.java b/protocols/api/src/main/java/org/apache/james/protocols/api/AbstractProtocolTransport.java
index d3f814b..3aab58b 100644
--- a/protocols/api/src/main/java/org/apache/james/protocols/api/AbstractProtocolTransport.java
+++ b/protocols/api/src/main/java/org/apache/james/protocols/api/AbstractProtocolTransport.java
@@ -20,7 +20,7 @@
 package org.apache.james.protocols.api;
 
 import java.io.InputStream;
-import java.io.UnsupportedEncodingException;
+import java.nio.charset.StandardCharsets;
 import java.util.List;
 
 
@@ -75,11 +75,7 @@ public abstract class AbstractProtocolTransport implements ProtocolTransport {
                 builder.append(CRLF);
             }
         }
-        try {
-            return builder.toString().getBytes("US-ASCII");
-        } catch (UnsupportedEncodingException e) {
-            throw new RuntimeException("No US-ASCII ?");
-        }
+        return builder.toString().getBytes(StandardCharsets.US_ASCII);
     }
 
     /**
diff --git a/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/FetchCommandParserPartialFetchTest.java b/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/FetchCommandParserPartialFetchTest.java
index ebc079d..cc8a589 100644
--- a/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/FetchCommandParserPartialFetchTest.java
+++ b/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/FetchCommandParserPartialFetchTest.java
@@ -24,6 +24,7 @@ import static org.mockito.Mockito.mock;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.nio.charset.StandardCharsets;
 
 import org.apache.james.imap.api.ImapCommand;
 import org.apache.james.imap.api.message.BodyFetchElement;
@@ -68,10 +69,10 @@ public class FetchCommandParserPartialFetchTest  {
     }
 
     @Test
-    public void testShouldNotParseZeroLength() throws Exception {
+    public void testShouldNotParseZeroLength() {
         ImapRequestLineReader reader = new ImapRequestStreamLineReader(
                 new ByteArrayInputStream("1 (BODY[]<20.0>)\r\n"
-                        .getBytes("US-ASCII")), new ByteArrayOutputStream());
+                        .getBytes(StandardCharsets.US_ASCII)), new ByteArrayOutputStream());
 
         assertThatThrownBy(() -> parser.decode(command, reader, "A01", false, session))
             .isInstanceOf(DecodingException.class);
@@ -80,7 +81,7 @@ public class FetchCommandParserPartialFetchTest  {
     private void check(String input, IdRange[] idSet,
             boolean useUids, FetchData data, String tag) throws Exception {
         ImapRequestLineReader reader = new ImapRequestStreamLineReader(
-                new ByteArrayInputStream(input.getBytes("US-ASCII")),
+                new ByteArrayInputStream(input.getBytes(StandardCharsets.US_ASCII)),
                 new ByteArrayOutputStream());
 
         parser.decode(command, reader, tag, useUids, session);
diff --git a/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/SearchCommandParserAndParenthesesTest.java b/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/SearchCommandParserAndParenthesesTest.java
index 0e81a16..95c1d78 100644
--- a/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/SearchCommandParserAndParenthesesTest.java
+++ b/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/SearchCommandParserAndParenthesesTest.java
@@ -24,6 +24,7 @@ import static org.assertj.core.api.Assertions.assertThat;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.UnsupportedEncodingException;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -182,11 +183,11 @@ public class SearchCommandParserAndParenthesesTest {
         check(and(top, false));
     }
 
-    private void check(Input in) throws UnsupportedEncodingException,
-            DecodingException {
+    private void check(Input in) throws
+        DecodingException {
         String input = in.input + "\r\n";
         ImapRequestLineReader reader = new ImapRequestStreamLineReader(
-                new ByteArrayInputStream(input.getBytes("US-ASCII")),
+                new ByteArrayInputStream(input.getBytes(StandardCharsets.US_ASCII)),
                 new ByteArrayOutputStream());
 
         final SearchKey result = parser.decode(null, reader);
diff --git a/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/SearchCommandParserCharsetTest.java b/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/SearchCommandParserCharsetTest.java
index c866b23..80b772e 100644
--- a/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/SearchCommandParserCharsetTest.java
+++ b/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/SearchCommandParserCharsetTest.java
@@ -30,6 +30,7 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 
 import org.apache.james.imap.api.ImapCommand;
 import org.apache.james.imap.api.ImapMessage;
@@ -87,7 +88,7 @@ public class SearchCommandParserCharsetTest {
     @Test
     public void testBadCharset() throws Exception {
         ImapRequestLineReader reader = new ImapRequestStreamLineReader(
-                new ByteArrayInputStream("CHARSET BOGUS ".getBytes("US-ASCII")),
+                new ByteArrayInputStream("CHARSET BOGUS ".getBytes(StandardCharsets.US_ASCII)),
                 new ByteArrayOutputStream());
         parser.decode(command, reader, TAG, false, session);
 
@@ -103,50 +104,50 @@ public class SearchCommandParserCharsetTest {
     @Test
     public void testBCCShouldConvertCharset() throws Exception {
         SearchKey key = SearchKey.buildBcc(NON_ASCII_SEARCH_TERM);
-        checkUTF8Valid("BCC".getBytes("US-ASCII"), key);
+        checkUTF8Valid("BCC".getBytes(StandardCharsets.US_ASCII), key);
     }
 
     @Test
     public void testBODYShouldConvertCharset() throws Exception {
         SearchKey key = SearchKey.buildBody(NON_ASCII_SEARCH_TERM);
-        checkUTF8Valid("BODY".getBytes("US-ASCII"), key);
+        checkUTF8Valid("BODY".getBytes(StandardCharsets.US_ASCII), key);
     }
 
     @Test
     public void testCCShouldConvertCharset() throws Exception {
         SearchKey key = SearchKey.buildCc(NON_ASCII_SEARCH_TERM);
-        checkUTF8Valid("CC".getBytes("US-ASCII"), key);
+        checkUTF8Valid("CC".getBytes(StandardCharsets.US_ASCII), key);
     }
 
     @Test
     public void testFROMShouldConvertCharset() throws Exception {
         SearchKey key = SearchKey.buildFrom(NON_ASCII_SEARCH_TERM);
-        checkUTF8Valid("FROM".getBytes("US-ASCII"), key);
+        checkUTF8Valid("FROM".getBytes(StandardCharsets.US_ASCII), key);
     }
 
     @Test
     public void testHEADERShouldConvertCharset() throws Exception {
         SearchKey key = SearchKey
                 .buildHeader("whatever", NON_ASCII_SEARCH_TERM);
-        checkUTF8Valid("HEADER whatever".getBytes("US-ASCII"), key);
+        checkUTF8Valid("HEADER whatever".getBytes(StandardCharsets.US_ASCII), key);
     }
 
     @Test
     public void testSUBJECTShouldConvertCharset() throws Exception {
         SearchKey key = SearchKey.buildSubject(NON_ASCII_SEARCH_TERM);
-        checkUTF8Valid("SUBJECT".getBytes("US-ASCII"), key);
+        checkUTF8Valid("SUBJECT".getBytes(StandardCharsets.US_ASCII), key);
     }
 
     @Test
     public void testTEXTShouldConvertCharset() throws Exception {
         SearchKey key = SearchKey.buildText(NON_ASCII_SEARCH_TERM);
-        checkUTF8Valid("TEXT".getBytes("US-ASCII"), key);
+        checkUTF8Valid("TEXT".getBytes(StandardCharsets.US_ASCII), key);
     }
 
     @Test
     public void testTOShouldConvertCharset() throws Exception {
         SearchKey key = SearchKey.buildTo(NON_ASCII_SEARCH_TERM);
-        checkUTF8Valid("TO".getBytes("US-ASCII"), key);
+        checkUTF8Valid("TO".getBytes(StandardCharsets.US_ASCII), key);
     }
 
     @Test
diff --git a/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/SearchCommandParserNotTest.java b/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/SearchCommandParserNotTest.java
index 86ec857..9b51acf 100644
--- a/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/SearchCommandParserNotTest.java
+++ b/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/SearchCommandParserNotTest.java
@@ -23,6 +23,7 @@ import static org.assertj.core.api.Assertions.assertThat;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
 import java.util.List;
 
@@ -113,7 +114,7 @@ public class SearchCommandParserNotTest {
     @Test 
     public void testUserFlagsParsing() throws Exception { 
         ImapRequestLineReader reader = new ImapRequestStreamLineReader(
-                new ByteArrayInputStream("NOT (KEYWORD bar KEYWORD foo)".getBytes("US-ASCII")), 
+                new ByteArrayInputStream("NOT (KEYWORD bar KEYWORD foo)".getBytes(StandardCharsets.US_ASCII)),
                 new ByteArrayOutputStream()); 
         SearchKey key = parser.searchKey(null, reader, null, false); 
         List<SearchKey> keys = key.getKeys().get(0).getKeys(); 
@@ -124,7 +125,7 @@ public class SearchCommandParserNotTest {
 
     private void checkValid(String input, SearchKey key) throws Exception {
         ImapRequestLineReader reader = new ImapRequestStreamLineReader(
-                new ByteArrayInputStream(input.getBytes("US-ASCII")),
+                new ByteArrayInputStream(input.getBytes(StandardCharsets.US_ASCII)),
                 new ByteArrayOutputStream());
 
         assertThat(parser.searchKey(null, reader, null, false)).isEqualTo(key);
diff --git a/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/SearchCommandParserOrTest.java b/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/SearchCommandParserOrTest.java
index b5bb13f..eae0525 100644
--- a/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/SearchCommandParserOrTest.java
+++ b/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/SearchCommandParserOrTest.java
@@ -23,6 +23,7 @@ import static org.assertj.core.api.Assertions.assertThat;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
 
 import org.apache.james.imap.api.ImapCommand;
@@ -172,7 +173,7 @@ public class SearchCommandParserOrTest {
         String input = "OR " + one.input + " " + two.input + "\r\n";
         SearchKey key = SearchKey.buildOr(one.key, two.key);
         ImapRequestLineReader reader = new ImapRequestStreamLineReader(
-                new ByteArrayInputStream(input.getBytes("US-ASCII")),
+                new ByteArrayInputStream(input.getBytes(StandardCharsets.US_ASCII)),
                 new ByteArrayOutputStream());
 
         assertThat(parser.searchKey(null, reader, null, false)).isEqualTo(key);
diff --git a/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/SearchCommandParserQuotedCharsetTest.java b/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/SearchCommandParserQuotedCharsetTest.java
index 1fe6191..2533f77 100644
--- a/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/SearchCommandParserQuotedCharsetTest.java
+++ b/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/SearchCommandParserQuotedCharsetTest.java
@@ -31,6 +31,7 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 
 import org.apache.james.imap.api.ImapCommand;
 import org.apache.james.imap.api.ImapMessage;
@@ -48,10 +49,6 @@ import org.junit.Test;
 
 public class SearchCommandParserQuotedCharsetTest {
 
-    private static final Charset UTF8 = Charset.forName("UTF-8");
-
-    private static final Charset ASCII = Charset.forName("US-ASCII");
-
     private static final String TAG = "A1";
 
     private static final String ASCII_SEARCH_TERM = "A Search Term";
@@ -104,25 +101,25 @@ public class SearchCommandParserQuotedCharsetTest {
             + NON_ASCII_SEARCH_TERM;
 
     private static final byte[] BYTES_LENGTHY_NON_ASCII_SEARCH_TERM = NioUtils
-            .toBytes(LENGTHY_NON_ASCII_SEARCH_TERM, UTF8);
+            .toBytes(LENGTHY_NON_ASCII_SEARCH_TERM, StandardCharsets.UTF_8);
 
     private static final byte[] BYTES_NON_ASCII_SEARCH_TERM = NioUtils.toBytes(
-            NON_ASCII_SEARCH_TERM, UTF8);
+            NON_ASCII_SEARCH_TERM, StandardCharsets.UTF_8);
 
     private static final byte[] BYTES_QUOTED_UTF8_LENGTHY_NON_ASCII_SEARCH_TERM = add(
-            add(NioUtils.toBytes(" \"", ASCII),
+            add(NioUtils.toBytes(" \"", StandardCharsets.US_ASCII),
                     BYTES_LENGTHY_NON_ASCII_SEARCH_TERM), NioUtils.toBytes(
-                    "\"", ASCII));
+                    "\"", StandardCharsets.US_ASCII));
 
     private static final byte[] BYTES_QUOTED_UTF8_NON_ASCII_SEARCH_TERM = add(
-            add(NioUtils.toBytes(" \"", ASCII), BYTES_NON_ASCII_SEARCH_TERM),
-            NioUtils.toBytes("\"", ASCII));
+            add(NioUtils.toBytes(" \"", StandardCharsets.US_ASCII), BYTES_NON_ASCII_SEARCH_TERM),
+            NioUtils.toBytes("\"", StandardCharsets.US_ASCII));
 
     private static final byte[] BYTES_UTF8_NON_ASCII_SEARCH_TERM = add(NioUtils
-            .toBytes(" {16}\r\n", ASCII), BYTES_NON_ASCII_SEARCH_TERM);
+            .toBytes(" {16}\r\n", StandardCharsets.US_ASCII), BYTES_NON_ASCII_SEARCH_TERM);
 
     private static final byte[] CHARSET = NioUtils.toBytes("CHARSET UTF-8 ",
-            ASCII);
+        StandardCharsets.US_ASCII);
 
     private static byte[] add(byte[] one, byte[] two) {
         byte[] results = new byte[one.length + two.length];
@@ -154,7 +151,7 @@ public class SearchCommandParserQuotedCharsetTest {
         SearchKey key = SearchKey.buildBcc(LENGTHY_NON_ASCII_SEARCH_TERM);
         ImapRequestLineReader reader = new ImapRequestStreamLineReader(
                 new ByteArrayInputStream(add(add(CHARSET, "BCC"
-                        .getBytes("US-ASCII")),
+                        .getBytes(StandardCharsets.US_ASCII)),
                         BYTES_QUOTED_UTF8_LENGTHY_NON_ASCII_SEARCH_TERM)),
                 new ByteArrayOutputStream());
         final SearchKey searchKey = parser.searchKey(null, reader, null, true);
@@ -166,7 +163,7 @@ public class SearchCommandParserQuotedCharsetTest {
         SearchKey key = SearchKey.buildBcc(NON_ASCII_SEARCH_TERM);
         ImapRequestLineReader reader = new ImapRequestStreamLineReader(
                 new ByteArrayInputStream(add(add(CHARSET, "BCC"
-                        .getBytes("US-ASCII")),
+                        .getBytes(StandardCharsets.US_ASCII)),
                         BYTES_QUOTED_UTF8_NON_ASCII_SEARCH_TERM)),
                 new ByteArrayOutputStream());
         final SearchKey searchKey = parser.searchKey(null, reader, null, true);
@@ -176,7 +173,7 @@ public class SearchCommandParserQuotedCharsetTest {
     @Test
     public void testBadCharset() throws Exception {
         ImapRequestLineReader reader = new ImapRequestStreamLineReader(
-                new ByteArrayInputStream("CHARSET BOGUS ".getBytes("US-ASCII")),
+                new ByteArrayInputStream("CHARSET BOGUS ".getBytes(StandardCharsets.US_ASCII)),
                 new ByteArrayOutputStream());
         parser.decode(command, reader, TAG, false, session);
 
@@ -189,12 +186,11 @@ public class SearchCommandParserQuotedCharsetTest {
     }
 
     @Test
-    public void testShouldThrowProtocolExceptionWhenBytesAreNotEncodedByCharset()
-            throws Exception {
+    public void testShouldThrowProtocolExceptionWhenBytesAreNotEncodedByCharset() {
         try {
             ImapRequestLineReader reader = new ImapRequestStreamLineReader(
                     new ByteArrayInputStream(add("CHARSET US-ASCII BCC "
-                            .getBytes("US-ASCII"), BYTES_NON_ASCII_SEARCH_TERM)),
+                            .getBytes(StandardCharsets.US_ASCII), BYTES_NON_ASCII_SEARCH_TERM)),
                     new ByteArrayOutputStream());
             parser.decode(command, reader, TAG, false, session);
             fail("A protocol exception should be thrown when charset is incompatible with input");
@@ -206,64 +202,64 @@ public class SearchCommandParserQuotedCharsetTest {
     @Test
     public void testBCCShouldConvertCharset() throws Exception {
         SearchKey key = SearchKey.buildBcc(NON_ASCII_SEARCH_TERM);
-        checkUTF8Valid("BCC".getBytes("US-ASCII"), key);
+        checkUTF8Valid("BCC".getBytes(StandardCharsets.US_ASCII), key);
     }
 
     @Test
     public void testBODYShouldConvertCharset() throws Exception {
         SearchKey key = SearchKey.buildBody(NON_ASCII_SEARCH_TERM);
-        checkUTF8Valid("BODY".getBytes("US-ASCII"), key);
+        checkUTF8Valid("BODY".getBytes(StandardCharsets.US_ASCII), key);
     }
 
     @Test
     public void testCCShouldConvertCharset() throws Exception {
         SearchKey key = SearchKey.buildCc(NON_ASCII_SEARCH_TERM);
-        checkUTF8Valid("CC".getBytes("US-ASCII"), key);
+        checkUTF8Valid("CC".getBytes(StandardCharsets.US_ASCII), key);
     }
 
     @Test
     public void testFROMShouldConvertCharset() throws Exception {
         SearchKey key = SearchKey.buildFrom(NON_ASCII_SEARCH_TERM);
-        checkUTF8Valid("FROM".getBytes("US-ASCII"), key);
+        checkUTF8Valid("FROM".getBytes(StandardCharsets.US_ASCII), key);
     }
 
     @Test
     public void testHEADERShouldConvertCharset() throws Exception {
         SearchKey key = SearchKey
                 .buildHeader("whatever", NON_ASCII_SEARCH_TERM);
-        checkUTF8Valid("HEADER whatever".getBytes("US-ASCII"), key);
+        checkUTF8Valid("HEADER whatever".getBytes(StandardCharsets.US_ASCII), key);
     }
 
     @Test
     public void testSUBJECTShouldConvertCharset() throws Exception {
         SearchKey key = SearchKey.buildSubject(NON_ASCII_SEARCH_TERM);
-        checkUTF8Valid("SUBJECT".getBytes("US-ASCII"), key);
+        checkUTF8Valid("SUBJECT".getBytes(StandardCharsets.US_ASCII), key);
     }
 
     @Test
     public void testTEXTShouldConvertCharset() throws Exception {
         SearchKey key = SearchKey.buildText(NON_ASCII_SEARCH_TERM);
-        checkUTF8Valid("TEXT".getBytes("US-ASCII"), key);
+        checkUTF8Valid("TEXT".getBytes(StandardCharsets.US_ASCII), key);
     }
 
     @Test
     public void testTOShouldConvertCharset() throws Exception {
         SearchKey key = SearchKey.buildTo(NON_ASCII_SEARCH_TERM);
-        checkUTF8Valid("TO".getBytes("US-ASCII"), key);
+        checkUTF8Valid("TO".getBytes(StandardCharsets.US_ASCII), key);
     }
 
     @Test
     public void testASCIICharset() throws Exception {
         SearchKey key = SearchKey.buildBcc(ASCII_SEARCH_TERM);
         checkValid("CHARSET US-ASCII BCC \"" + ASCII_SEARCH_TERM + "\"", key,
-                true, "US-ASCII");
+                true, StandardCharsets.US_ASCII);
     }
 
     @Test
     public void testSimpleUTF8Charset() throws Exception {
         SearchKey key = SearchKey.buildBcc(ASCII_SEARCH_TERM);
         checkValid("CHARSET UTF-8 BCC \"" + ASCII_SEARCH_TERM + "\"", key,
-                true, "US-ASCII");
+                true, StandardCharsets.US_ASCII);
     }
 
     private void checkUTF8Valid(byte[] term, SearchKey key)
@@ -276,8 +272,7 @@ public class SearchCommandParserQuotedCharsetTest {
         assertThat(searchKey).isEqualTo(key);
     }
 
-    private void checkValid(String input, SearchKey key, boolean isFirst,
-            String charset) throws Exception {
+    private void checkValid(String input, SearchKey key, boolean isFirst, Charset charset) throws Exception {
         ImapRequestLineReader reader = new ImapRequestStreamLineReader(
                 new ByteArrayInputStream(input.getBytes(charset)),
                 new ByteArrayOutputStream());
diff --git a/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/SearchCommandParserSearchKeySequenceSetTest.java b/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/SearchCommandParserSearchKeySequenceSetTest.java
index fa6eebb..1287082 100644
--- a/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/SearchCommandParserSearchKeySequenceSetTest.java
+++ b/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/SearchCommandParserSearchKeySequenceSetTest.java
@@ -23,6 +23,7 @@ import static org.assertj.core.api.Assertions.assertThat;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
 
 import org.apache.james.imap.api.ImapCommand;
@@ -130,7 +131,7 @@ public class SearchCommandParserSearchKeySequenceSetTest {
     private void checkValid(String input, SearchKey key) throws Exception {
         input = input + "\r\n";
         ImapRequestLineReader reader = new ImapRequestStreamLineReader(
-                new ByteArrayInputStream(input.getBytes("US-ASCII")),
+                new ByteArrayInputStream(input.getBytes(StandardCharsets.US_ASCII)),
                 new ByteArrayOutputStream());
 
         final SearchKey searchKey = parser.searchKey(null, reader, null, false);
diff --git a/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/SearchCommandParserSearchKeyTest.java b/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/SearchCommandParserSearchKeyTest.java
index 774914c..2534ab0 100644
--- a/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/SearchCommandParserSearchKeyTest.java
+++ b/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/SearchCommandParserSearchKeyTest.java
@@ -24,6 +24,7 @@ import static org.assertj.core.api.Fail.fail;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.nio.charset.StandardCharsets;
 
 import org.apache.james.imap.api.ImapCommand;
 import org.apache.james.imap.api.ImapMessage;
@@ -374,7 +375,7 @@ public class SearchCommandParserSearchKeyTest {
    
     private void checkValid(String input, SearchKey key) throws Exception {
         ImapRequestLineReader reader = new ImapRequestStreamLineReader(
-                new ByteArrayInputStream(input.getBytes("US-ASCII")),
+                new ByteArrayInputStream(input.getBytes(StandardCharsets.US_ASCII)),
                 new ByteArrayOutputStream());
 
         assertThat(parser.searchKey(null, reader, null, false)).isEqualTo(key);
@@ -727,7 +728,7 @@ public class SearchCommandParserSearchKeyTest {
     private void checkInvalid(String input, SearchKey key)
             throws Exception {
         ImapRequestLineReader reader = new ImapRequestStreamLineReader(
-                new ByteArrayInputStream(input.getBytes("US-ASCII")),
+                new ByteArrayInputStream(input.getBytes(StandardCharsets.US_ASCII)),
                 new ByteArrayOutputStream());
 
         try {
diff --git a/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/SearchCommandParserTopLevelAndTest.java b/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/SearchCommandParserTopLevelAndTest.java
index 4417f38..4e779cf 100644
--- a/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/SearchCommandParserTopLevelAndTest.java
+++ b/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/SearchCommandParserTopLevelAndTest.java
@@ -23,7 +23,7 @@ import static org.assertj.core.api.Assertions.assertThat;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
-import java.io.UnsupportedEncodingException;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -183,13 +183,12 @@ public class SearchCommandParserTopLevelAndTest {
         }
     }
 
-    private void check(List<SearchKey> keys, StringBuffer buffer)
-            throws UnsupportedEncodingException, DecodingException {
+    private void check(List<SearchKey> keys, StringBuffer buffer) throws DecodingException {
         buffer.append("\r\n");
         String input = buffer.toString();
         SearchKey key = SearchKey.buildAnd(keys);
         ImapRequestLineReader reader = new ImapRequestStreamLineReader(
-                new ByteArrayInputStream(input.getBytes("US-ASCII")),
+                new ByteArrayInputStream(input.getBytes(StandardCharsets.US_ASCII)),
                 new ByteArrayOutputStream());
 
         assertThat(parser.decode(null, reader)).describedAs(input).isEqualTo(key);
diff --git a/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/StoreCommandParserTest.java b/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/StoreCommandParserTest.java
index 865a2bc..5e07a19 100644
--- a/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/StoreCommandParserTest.java
+++ b/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/StoreCommandParserTest.java
@@ -23,6 +23,7 @@ import static org.mockito.Mockito.mock;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.nio.charset.StandardCharsets;
 
 import javax.mail.Flags;
 
@@ -75,7 +76,7 @@ public class StoreCommandParserTest {
             final Boolean sign, Flags flags, boolean useUids, String tag)
             throws Exception {
         ImapRequestLineReader reader = new ImapRequestStreamLineReader(
-                new ByteArrayInputStream(input.getBytes("US-ASCII")),
+                new ByteArrayInputStream(input.getBytes(StandardCharsets.US_ASCII)),
                 new ByteArrayOutputStream());
 
         parser.decode(command, reader, tag, useUids, session);
diff --git a/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/utils/MockMailbox.java b/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/utils/MockMailbox.java
index bc6a846..e0f014b 100644
--- a/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/utils/MockMailbox.java
+++ b/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/utils/MockMailbox.java
@@ -19,9 +19,9 @@
 package org.apache.james.protocols.pop3.utils;
 
 import java.io.ByteArrayInputStream;
-import java.io.IOException;
 import java.io.InputStream;
 import java.io.SequenceInputStream;
+import java.nio.charset.StandardCharsets;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -51,25 +51,25 @@ public class MockMailbox extends ImapMailbox {
     }
 
     @Override
-    public InputStream getMessageBody(long uid) throws IOException {
+    public InputStream getMessageBody(long uid) {
         Message m = messages.get(uid);
         if (m == null) {
             return null;
         }
-        return new ByteArrayInputStream(m.body.getBytes("US-ASCII"));
+        return new ByteArrayInputStream(m.body.getBytes(StandardCharsets.US_ASCII));
     }
 
     @Override
-    public InputStream getMessageHeaders(long uid) throws IOException {
+    public InputStream getMessageHeaders(long uid) {
         Message m = messages.get(uid);
         if (m == null) {
             return null;
         }
-        return new ByteArrayInputStream((m.headers + "\r\n").getBytes("US-ASCII"));
+        return new ByteArrayInputStream((m.headers + "\r\n").getBytes(StandardCharsets.US_ASCII));
     }
 
     @Override
-    public InputStream getMessage(long uid) throws IOException {
+    public InputStream getMessage(long uid) {
         InputStream body = getMessageBody(uid);
         InputStream headers = getMessageHeaders(uid);
         if (body == null || headers == null) {
@@ -79,7 +79,7 @@ public class MockMailbox extends ImapMailbox {
     }
 
     @Override
-    public List<MessageMetaData> getMessages() throws IOException {
+    public List<MessageMetaData> getMessages() {
         return messages.values()
             .stream()
             .map(m -> m.meta)
@@ -87,19 +87,19 @@ public class MockMailbox extends ImapMailbox {
     }
 
     @Override
-    public void remove(long... uids) throws IOException {
+    public void remove(long... uids) {
         for (long uid: uids) {
             messages.remove(uid);
         }
     }
 
     @Override
-    public String getIdentifier() throws IOException {
+    public String getIdentifier() {
         return identifier;
     }
 
     @Override
-    public void close() throws IOException {
+    public void close() {
         // nothing
     }
     
diff --git a/server/container/core/src/main/java/org/apache/james/server/core/InternetHeadersInputStream.java b/server/container/core/src/main/java/org/apache/james/server/core/InternetHeadersInputStream.java
index f6911f8..c73f890 100644
--- a/server/container/core/src/main/java/org/apache/james/server/core/InternetHeadersInputStream.java
+++ b/server/container/core/src/main/java/org/apache/james/server/core/InternetHeadersInputStream.java
@@ -19,9 +19,8 @@
 
 package org.apache.james.server.core;
 
-import java.io.IOException;
 import java.io.InputStream;
-import java.io.UnsupportedEncodingException;
+import java.nio.charset.StandardCharsets;
 import java.util.Enumeration;
 
 import javax.mail.internet.InternetHeaders;
@@ -48,7 +47,7 @@ public class InternetHeadersInputStream extends InputStream {
     }
 
     @Override
-    public int read() throws IOException {
+    public int read() {
         if (currLine == null || pos == currLine.length) {
             if (!readNextLine()) {
                 return -1;
@@ -61,31 +60,24 @@ public class InternetHeadersInputStream extends InputStream {
      * Load the next header line if possible
      * 
      * @return true if there was an headerline which could be read
-     * 
-     * @throws IOException
      */
-    private boolean readNextLine() throws IOException {
+    private boolean readNextLine() {
         if (headerLines.hasMoreElements()) {
-            try {
-                pos = 0;
-                String line = (headerLines.nextElement() + LINE_SEPERATOR);
-                // Add seperator to show that headers are complete
-                if (!headerLines.hasMoreElements()) {
-                    line += LINE_SEPERATOR;
-                }
-                currLine = line.getBytes("US-ASCII");
-                return true;
-            } catch (UnsupportedEncodingException e) {
-                // should never happen
-                throw new IOException("US-ASCII encoding not supported by this platform ?!");
+            pos = 0;
+            String line = (headerLines.nextElement() + LINE_SEPERATOR);
+            // Add seperator to show that headers are complete
+            if (!headerLines.hasMoreElements()) {
+                line += LINE_SEPERATOR;
             }
+            currLine = line.getBytes(StandardCharsets.US_ASCII);
+            return true;
         } else {
             return false;
         }
     }
 
     @Override
-    public void close() throws IOException {
+    public void close() {
         currLine = null;
     }
 
diff --git a/server/container/core/src/test/java/org/apache/james/server/core/MimeMessageTest.java b/server/container/core/src/test/java/org/apache/james/server/core/MimeMessageTest.java
index eab98ac..3e02407 100644
--- a/server/container/core/src/test/java/org/apache/james/server/core/MimeMessageTest.java
+++ b/server/container/core/src/test/java/org/apache/james/server/core/MimeMessageTest.java
@@ -24,6 +24,7 @@ import static org.assertj.core.api.Fail.fail;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
 import java.util.Enumeration;
 import java.util.Properties;
@@ -401,7 +402,7 @@ public class MimeMessageTest {
     public void testGeronimoIndexOutOfBounds() throws Exception {
         String message = "                  \r\n" + "Subject: test\r\n" + "\r\n" + "Body\r\n";
 
-        byte[] messageBytes = message.getBytes("US-ASCII");
+        byte[] messageBytes = message.getBytes(StandardCharsets.US_ASCII);
         new MimeMessage(null, new ByteArrayInputStream(messageBytes));
     }
 }
diff --git a/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/ImapHeartbeatHandler.java b/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/ImapHeartbeatHandler.java
index ae44ab7..29e085e 100644
--- a/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/ImapHeartbeatHandler.java
+++ b/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/ImapHeartbeatHandler.java
@@ -18,6 +18,8 @@
  ****************************************************************/
 package org.apache.james.imapserver.netty;
 
+import java.nio.charset.StandardCharsets;
+
 import org.jboss.netty.buffer.ChannelBuffers;
 import org.jboss.netty.channel.ChannelHandlerContext;
 import org.jboss.netty.handler.timeout.IdleState;
@@ -29,7 +31,7 @@ public class ImapHeartbeatHandler extends IdleStateAwareChannelHandler {
     @Override
     public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) throws Exception {
         if (e.getState().equals(IdleState.WRITER_IDLE)) {
-            e.getChannel().write(ChannelBuffers.wrappedBuffer("* OK Hang in there..\r\n".getBytes("US-ASCII")));
+            e.getChannel().write(ChannelBuffers.wrappedBuffer("* OK Hang in there..\r\n".getBytes(StandardCharsets.US_ASCII)));
         }
         super.channelIdle(ctx, e);
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org