diff options
| author | Mygod <contact-github@mygod.be> | 2026-05-27 17:25:52 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-05-27 17:25:52 +0300 |
| commit | 4c7fba8de664ada5da1fa545856704a534d08f95 (patch) | |
| tree | 1dac2a79693ca45ca4c451d42e3b021628739a49 /core/src/main/java | |
| parent | 62a33ca7a328907dee602798043692ff1f83b0c0 (diff) | |
| download | zxing-4c7fba8de664ada5da1fa545856704a534d08f95.tar.xz | |
Avoid charset provider lookups for UTF-8 QR encoding (#2077)
* Avoid charset provider lookups for UTF-8 QR encoding
* Simplify charset lookup handling
Diffstat (limited to 'core/src/main/java')
| -rw-r--r-- | core/src/main/java/com/google/zxing/common/CharacterSetECI.java | 54 | ||||
| -rw-r--r-- | core/src/main/java/com/google/zxing/qrcode/encoder/Encoder.java | 6 |
2 files changed, 39 insertions, 21 deletions
diff --git a/core/src/main/java/com/google/zxing/common/CharacterSetECI.java b/core/src/main/java/com/google/zxing/common/CharacterSetECI.java index 6ea51f1e6..ae89e9bd1 100644 --- a/core/src/main/java/com/google/zxing/common/CharacterSetECI.java +++ b/core/src/main/java/com/google/zxing/common/CharacterSetECI.java @@ -19,7 +19,7 @@ package com.google.zxing.common; import com.google.zxing.FormatException; import java.nio.charset.Charset; - +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; @@ -33,7 +33,7 @@ public enum CharacterSetECI { // Enum name is a Java encoding valid for java.lang and java.io Cp437(new int[]{0,2}), - ISO8859_1(new int[]{1,3}, "ISO-8859-1"), + ISO8859_1(new int[]{1,3}, StandardCharsets.ISO_8859_1, "ISO-8859-1"), ISO8859_2(4, "ISO-8859-2"), ISO8859_3(5, "ISO-8859-3"), ISO8859_4(6, "ISO-8859-4"), @@ -53,31 +53,29 @@ public enum CharacterSetECI { Cp1251(22, "windows-1251"), Cp1252(23, "windows-1252"), Cp1256(24, "windows-1256"), - UnicodeBigUnmarked(25, "UTF-16BE", "UnicodeBig"), - UTF8(26, "UTF-8"), - ASCII(new int[] {27, 170}, "US-ASCII"), + UnicodeBigUnmarked(25, StandardCharsets.UTF_16BE, "UTF-16BE", "UnicodeBig"), + UTF8(26, StandardCharsets.UTF_8, "UTF-8"), + ASCII(new int[] {27, 170}, StandardCharsets.US_ASCII, "US-ASCII"), Big5(28), GB18030(29, "GB2312", "EUC_CN", "GBK"), EUC_KR(30, "EUC-KR"); - // only character sets supported by the current JVM are registered here private static final Map<Integer,CharacterSetECI> VALUE_TO_ECI = new HashMap<>(); private static final Map<String,CharacterSetECI> NAME_TO_ECI = new HashMap<>(); static { for (CharacterSetECI eci : values()) { - if (Charset.isSupported(eci.name())) { - for (int value : eci.values) { - VALUE_TO_ECI.put(value, eci); - } - NAME_TO_ECI.put(eci.name(), eci); - for (String name : eci.otherEncodingNames) { - NAME_TO_ECI.put(name, eci); - } + for (int value : eci.values) { + VALUE_TO_ECI.put(value, eci); + } + NAME_TO_ECI.put(eci.name(), eci); + for (String name : eci.otherEncodingNames) { + NAME_TO_ECI.put(name, eci); } } } private final int[] values; + private final Charset standardCharset; private final String[] otherEncodingNames; CharacterSetECI(int value) { @@ -85,12 +83,20 @@ public enum CharacterSetECI { } CharacterSetECI(int value, String... otherEncodingNames) { - this.values = new int[] {value}; - this.otherEncodingNames = otherEncodingNames; + this(new int[] {value}, null, otherEncodingNames); + } + + CharacterSetECI(int value, Charset standardCharset, String... otherEncodingNames) { + this(new int[] {value}, standardCharset, otherEncodingNames); } CharacterSetECI(int[] values, String... otherEncodingNames) { + this(values, null, otherEncodingNames); + } + + CharacterSetECI(int[] values, Charset standardCharset, String... otherEncodingNames) { this.values = values; + this.standardCharset = standardCharset; this.otherEncodingNames = otherEncodingNames; } @@ -99,16 +105,24 @@ public enum CharacterSetECI { } public Charset getCharset() { + if (standardCharset != null) { + return standardCharset; + } return Charset.forName(name()); } + private boolean isCharsetSupported() { + return standardCharset != null || Charset.isSupported(name()); + } + /** * @param charset Java character set object * @return CharacterSetECI representing ECI for character encoding, or null if it is legal * but unsupported */ public static CharacterSetECI getCharacterSetECI(Charset charset) { - return NAME_TO_ECI.get(charset.name()); + CharacterSetECI eci = NAME_TO_ECI.get(charset.name()); + return eci == null || eci.isCharsetSupported() ? eci : null; } /** @@ -121,7 +135,8 @@ public enum CharacterSetECI { if (value < 0 || value >= 900) { throw FormatException.getFormatInstance(); } - return VALUE_TO_ECI.get(value); + CharacterSetECI eci = VALUE_TO_ECI.get(value); + return eci == null || eci.isCharsetSupported() ? eci : null; } /** @@ -130,7 +145,8 @@ public enum CharacterSetECI { * but unsupported */ public static CharacterSetECI getCharacterSetECIByName(String name) { - return NAME_TO_ECI.get(name); + CharacterSetECI eci = NAME_TO_ECI.get(name); + return eci == null || eci.isCharsetSupported() ? eci : null; } } diff --git a/core/src/main/java/com/google/zxing/qrcode/encoder/Encoder.java b/core/src/main/java/com/google/zxing/qrcode/encoder/Encoder.java index 97f316ddd..ad02a5829 100644 --- a/core/src/main/java/com/google/zxing/qrcode/encoder/Encoder.java +++ b/core/src/main/java/com/google/zxing/qrcode/encoder/Encoder.java @@ -19,8 +19,8 @@ package com.google.zxing.qrcode.encoder; import com.google.zxing.EncodeHintType; import com.google.zxing.WriterException; import com.google.zxing.common.BitArray; -import com.google.zxing.common.StringUtils; import com.google.zxing.common.CharacterSetECI; +import com.google.zxing.common.StringUtils; import com.google.zxing.common.reedsolomon.GenericGF; import com.google.zxing.common.reedsolomon.ReedSolomonEncoder; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; @@ -249,7 +249,9 @@ public final class Encoder { * if it is Shift_JIS, and the input is only double-byte Kanji, then we return {@link Mode#KANJI}. */ private static Mode chooseMode(String content, Charset encoding) { - if (StringUtils.SHIFT_JIS_CHARSET != null && + if (encoding != null && + ("Shift_JIS".equalsIgnoreCase(encoding.name()) || "SJIS".equalsIgnoreCase(encoding.name())) && + StringUtils.SHIFT_JIS_CHARSET != null && StringUtils.SHIFT_JIS_CHARSET.equals(encoding) && isOnlyDoubleByteKanji(content)) { // Choose Kanji mode if all input are double-byte characters |
