Ignore:
Timestamp:
2013-12-28T00:30:15+01:00 (12 years ago)
Author:
simon04
Message:

Refactoring: introduce Utils.UTF_8 charset to avoid handling of UnsupportedEncodingException

According to the Javadoc of Charset, every implementation of the Java
platform is required to support UTF-8.

Location:
trunk/src/org/openstreetmap/josm/tools
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/BugReportExceptionHandler.java

    r6380 r6552  
    194194            ByteArrayOutputStream out = new ByteArrayOutputStream();
    195195            GZIPOutputStream gzip = new GZIPOutputStream(out);
    196             gzip.write(debugText.getBytes("UTF-8"));
     196            gzip.write(debugText.getBytes(Utils.UTF_8));
    197197            Utils.close(gzip);
    198198
  • trunk/src/org/openstreetmap/josm/tools/I18n.java

    r6380 r6552  
    548548                        if(rval != val) /* file corrupt */
    549549                            return false;
    550                         enstrings[i] = new String(str, 0, val, "utf-8");
     550                        enstrings[i] = new String(str, 0, val, Utils.UTF_8);
    551551                    }
    552552                    for(int i = 0; i < trnum; ++i)
     
    562562                        if(rval != val) /* file corrupt */
    563563                            return false;
    564                         trstrings[i] = new String(str, 0, val, "utf-8");
     564                        trstrings[i] = new String(str, 0, val, Utils.UTF_8);
    565565                    }
    566566                    if(trnum > 0 && !p.containsKey(enstrings[0])) {
     
    600600                        if(val != enval) /* file corrupt */
    601601                            return false;
    602                         String enstr = new String(str, 0, enval, "utf-8");
     602                        String enstr = new String(str, 0, enval, Utils.UTF_8);
    603603                        if(trval != 0)
    604604                        {
     
    606606                            if(val != trval) /* file corrupt */
    607607                                return false;
    608                             String trstr = new String(str, 0, trval, "utf-8");
     608                            String trstr = new String(str, 0, trval, Utils.UTF_8);
    609609                            if(!s.containsKey(enstr))
    610610                                s.put(enstr, trstr);
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r6538 r6552  
    2323import java.io.InputStreamReader;
    2424import java.io.OutputStream;
    25 import java.io.UnsupportedEncodingException;
    2625import java.net.HttpURLConnection;
    2726import java.net.URL;
    2827import java.net.URLConnection;
    2928import java.nio.channels.FileChannel;
     29import java.nio.charset.Charset;
    3030import java.security.MessageDigest;
    3131import java.security.NoSuchAlgorithmException;
     
    5656    }
    5757
     58    /**
     59     * UTF-8 (UCS Transformation Format—8-bit).
     60     *
     61     * <p>Every implementation of the Java platform is required to support UTF-8 (see {@link Charset}).</p>
     62     */
     63    public static final Charset UTF_8 = Charset.forName("UTF-8");
     64
    5865    public static <T> boolean exists(Iterable<? extends T> collection, Predicate<? super T> predicate) {
    5966        for (T item : collection) {
     
    436443     */
    437444    public static String md5Hex(String data) {
    438         byte[] byteData = null;
    439         try {
    440             byteData = data.getBytes("UTF-8");
    441         } catch (UnsupportedEncodingException e) {
    442             throw new RuntimeException();
    443         }
     445        byte[] byteData = data.getBytes(UTF_8);
    444446        MessageDigest md = null;
    445447        try {
     
    707709     */
    708710    public static BufferedReader openURLReaderAndDecompress(final URL url, final boolean decompress) throws IOException {
    709         return new BufferedReader(new InputStreamReader(openURLAndDecompress(url, decompress), "utf-8"));
     711        return new BufferedReader(new InputStreamReader(openURLAndDecompress(url, decompress), UTF_8));
    710712    }
    711713
Note: See TracChangeset for help on using the changeset viewer.