|
11 | 11 |
|
12 | 12 | package io.vertx.mysqlclient.impl; |
13 | 13 |
|
14 | | -import io.netty.util.collection.IntObjectHashMap; |
15 | | -import io.netty.util.collection.IntObjectMap; |
16 | 14 | import io.vertx.core.internal.logging.Logger; |
17 | 15 | import io.vertx.core.internal.logging.LoggerFactory; |
18 | 16 |
|
19 | 17 | import java.nio.charset.Charset; |
20 | 18 | import java.nio.charset.StandardCharsets; |
21 | | -import java.util.*; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
22 | 23 | import java.util.stream.Collectors; |
23 | 24 |
|
24 | 25 | /** |
@@ -255,7 +256,7 @@ public enum MySQLCollation { |
255 | 256 | public static final List<String> SUPPORTED_CHARSET_NAMES = Arrays.stream(values()).map(MySQLCollation::mysqlCharsetName).distinct().collect(Collectors.toList()); |
256 | 257 |
|
257 | 258 | private static final Map<String, String> charsetToDefaultCollationMapping = new HashMap<>(); |
258 | | - private static final IntObjectMap<Charset> idToJavaCharsetMapping = new IntObjectHashMap<>(); |
| 259 | + private static final Charset[] idToJavaCharsetMapping = new Charset[256]; |
259 | 260 |
|
260 | 261 | static { |
261 | 262 | charsetToDefaultCollationMapping.put("big5", "big5_chinese_ci"); |
@@ -303,11 +304,21 @@ public enum MySQLCollation { |
303 | 304 | for (MySQLCollation collation : MySQLCollation.values()) { |
304 | 305 | try { |
305 | 306 | Charset charset = Charset.forName(collation.mappedJavaCharsetName); |
306 | | - idToJavaCharsetMapping.put(collation.collationId, charset); |
| 307 | + if (idToJavaCharsetMapping[collation.collationId] != null) { |
| 308 | + // This shouldn't happen unless the enum values are modified and the collation id gets mistaken |
| 309 | + throw new IllegalArgumentException("Duplicate collation id: " + collation.collationId); |
| 310 | + } |
| 311 | + idToJavaCharsetMapping[collation.collationId] = charset; |
307 | 312 | } catch (Exception e) { |
308 | 313 | LOGGER.warn(String.format("Java charset: [%s] is not supported by this platform, data with collation[%s] will be decoded in UTF-8 instead.", collation.mysqlCharsetName, collation.name())); |
309 | 314 | } |
310 | 315 | } |
| 316 | + // set the remaining missing ones to the default charset |
| 317 | + for (int i = 0; i < idToJavaCharsetMapping.length; i++) { |
| 318 | + if (idToJavaCharsetMapping[i] == null) { |
| 319 | + idToJavaCharsetMapping[i] = StandardCharsets.UTF_8; |
| 320 | + } |
| 321 | + } |
311 | 322 | } |
312 | 323 |
|
313 | 324 | public static final MySQLCollation DEFAULT_COLLATION = utf8mb4_general_ci; |
@@ -337,12 +348,10 @@ public static MySQLCollation valueOfName(String collationName) throws IllegalArg |
337 | 348 | * @return the charset |
338 | 349 | */ |
339 | 350 | public static Charset getJavaCharsetByCollationId(int collationId) { |
340 | | - Charset charset = idToJavaCharsetMapping.get(collationId); |
341 | | - if (charset == null) { |
| 351 | + if (collationId >= idToJavaCharsetMapping.length) { |
342 | 352 | return StandardCharsets.UTF_8; |
343 | | - } else { |
344 | | - return charset; |
345 | 353 | } |
| 354 | + return idToJavaCharsetMapping[collationId]; |
346 | 355 | } |
347 | 356 |
|
348 | 357 | public static String getDefaultCollationFromCharsetName(String charset) { |
|
0 commit comments