|
40 | 40 | import java.util.NoSuchElementException; |
41 | 41 | import java.util.Properties; |
42 | 42 | import java.util.StringTokenizer; |
| 43 | +import java.util.logging.Level; |
| 44 | +import java.util.logging.Logger; |
43 | 45 |
|
44 | 46 | /** |
45 | 47 | * This is a utility class that provides various MIME related |
@@ -139,9 +141,12 @@ public class MimeUtility { |
139 | 141 | private MimeUtility() { |
140 | 142 | } |
141 | 143 |
|
| 144 | + private static final Logger LOGGER = Logger.getLogger(MimeUtility.class.getName()); |
| 145 | + |
142 | 146 | public static final int ALL = -1; |
143 | 147 |
|
144 | 148 | // This is for backwards compatibility. Eventually we have to remove the second value of the next array. |
| 149 | + // Note this order is different than in Session arrays. This is because here the key-value pair is overwritten. |
145 | 150 | private static final String[] CHARSET_MAP_RESOURCES = new String[] {"/META-INF/jakarta.charset.map", "/META-INF/javamail.charset.map"}; |
146 | 151 |
|
147 | 152 | // cached map of whether a charset is compatible with ASCII |
@@ -1353,29 +1358,27 @@ static String getDefaultMIMECharset() { |
1353 | 1358 | java2mime = new HashMap<>(40); |
1354 | 1359 | mime2java = new HashMap<>(14); |
1355 | 1360 |
|
1356 | | - try { |
1357 | | - // Use this class's classloader to load the mapping file |
1358 | | - // XXX - we should use SecuritySupport, but it's in another package |
1359 | | - InputStream is = resource(CHARSET_MAP_RESOURCES); |
1360 | | - |
1361 | | - if (is != null) { |
1362 | | - try { |
| 1361 | + // Use this class's classloader to load the mapping file |
| 1362 | + // XXX - we should use SecuritySupport, but it's in another package |
| 1363 | + for (int i = 0; i < CHARSET_MAP_RESOURCES.length; i++) { |
| 1364 | + String charsetResource = CHARSET_MAP_RESOURCES[i]; |
| 1365 | + try (InputStream is = MimeUtility.class.getResourceAsStream(charsetResource)) { |
| 1366 | + if (is != null) { |
1363 | 1367 | LineInputStream lineInput = StreamProvider.provider().inputLineStream(is, false); |
1364 | 1368 |
|
1365 | 1369 | // Load the JDK-to-MIME charset mapping table |
1366 | 1370 | loadMappings(lineInput, java2mime); |
1367 | 1371 |
|
1368 | 1372 | // Load the MIME-to-JDK charset mapping table |
1369 | 1373 | loadMappings(lineInput, mime2java); |
1370 | | - } finally { |
1371 | | - try { |
1372 | | - is.close(); |
1373 | | - } catch (Exception cex) { |
1374 | | - // ignore |
1375 | | - } |
1376 | 1374 | } |
| 1375 | + // We already know size is two, but just in case, we don't want to fail here for a logger reason |
| 1376 | + if (i == 0 && CHARSET_MAP_RESOURCES.length == 2) { |
| 1377 | + LOGGER.log(Level.WARNING, "[DEPRECATED] {0} is deprecated and will be removed. Future versions will require {1}.", |
| 1378 | + new Object[]{CHARSET_MAP_RESOURCES[0], CHARSET_MAP_RESOURCES[1]}); |
| 1379 | + } |
| 1380 | + } catch (Exception ex) { |
1377 | 1381 | } |
1378 | | - } catch (Exception ex) { |
1379 | 1382 | } |
1380 | 1383 |
|
1381 | 1384 | // If we didn't load the tables, e.g., because we didn't have |
@@ -1444,16 +1447,6 @@ static String getDefaultMIMECharset() { |
1444 | 1447 | mime2java.put("gbk", "GB18030"); |
1445 | 1448 | } |
1446 | 1449 | } |
1447 | | - |
1448 | | - private static InputStream resource(String[] resources) { |
1449 | | - for (String resource : resources) { |
1450 | | - InputStream is = MimeUtility.class.getResourceAsStream(resource); |
1451 | | - if (is != null) { |
1452 | | - return is; |
1453 | | - } |
1454 | | - } |
1455 | | - return null; |
1456 | | - } |
1457 | 1450 |
|
1458 | 1451 | private static void loadMappings(LineInputStream is, |
1459 | 1452 | Map<String, String> table) { |
|
0 commit comments