mirror of
https://github.com/zxing/zxing.git
synced 2024-11-09 20:44:03 -08:00
Support for platforms where SJIS is unavailable (#1747)
* Add support for embedded JREs where SJIS isn't available (cherry picked from commit 67896f97042f5a9772f0ebcfdbb91f275d86e087) * Add support for embedded JREs where EUC_JP isn't available (cherry picked from commit 979d3b648982b151b7a1e779e58d445086911b31)
This commit is contained in:
parent
956bbcafcb
commit
1cf9eaaeba
|
@ -32,7 +32,17 @@ import com.google.zxing.DecodeHintType;
|
||||||
public final class StringUtils {
|
public final class StringUtils {
|
||||||
|
|
||||||
private static final Charset PLATFORM_DEFAULT_ENCODING = Charset.defaultCharset();
|
private static final Charset PLATFORM_DEFAULT_ENCODING = Charset.defaultCharset();
|
||||||
public static final Charset SHIFT_JIS_CHARSET = Charset.forName("SJIS");
|
public static final Charset SHIFT_JIS_CHARSET;
|
||||||
|
static {
|
||||||
|
Charset sjisCharset;
|
||||||
|
try {
|
||||||
|
sjisCharset = Charset.forName("SJIS");
|
||||||
|
} catch (UnsupportedCharsetException ucee) {
|
||||||
|
// Can happen on JREs without lib/charsets.jar
|
||||||
|
sjisCharset = null;
|
||||||
|
}
|
||||||
|
SHIFT_JIS_CHARSET = sjisCharset;
|
||||||
|
}
|
||||||
public static final Charset GB2312_CHARSET;
|
public static final Charset GB2312_CHARSET;
|
||||||
static {
|
static {
|
||||||
Charset gb2312Charset;
|
Charset gb2312Charset;
|
||||||
|
@ -44,10 +54,20 @@ public final class StringUtils {
|
||||||
}
|
}
|
||||||
GB2312_CHARSET = gb2312Charset;
|
GB2312_CHARSET = gb2312Charset;
|
||||||
}
|
}
|
||||||
private static final Charset EUC_JP = Charset.forName("EUC_JP");
|
private static final Charset EUC_JP;
|
||||||
|
static {
|
||||||
|
Charset eucJpCharset;
|
||||||
|
try {
|
||||||
|
eucJpCharset = Charset.forName("EUC_JP");
|
||||||
|
} catch (UnsupportedCharsetException ucee) {
|
||||||
|
// Can happen on JREs without lib/charsets.jar
|
||||||
|
eucJpCharset = null;
|
||||||
|
}
|
||||||
|
EUC_JP = eucJpCharset;
|
||||||
|
}
|
||||||
private static final boolean ASSUME_SHIFT_JIS =
|
private static final boolean ASSUME_SHIFT_JIS =
|
||||||
SHIFT_JIS_CHARSET.equals(PLATFORM_DEFAULT_ENCODING) ||
|
(SHIFT_JIS_CHARSET != null && SHIFT_JIS_CHARSET.equals(PLATFORM_DEFAULT_ENCODING)) ||
|
||||||
EUC_JP.equals(PLATFORM_DEFAULT_ENCODING);
|
(EUC_JP != null && EUC_JP.equals(PLATFORM_DEFAULT_ENCODING));
|
||||||
|
|
||||||
// Retained for ABI compatibility with earlier versions
|
// Retained for ABI compatibility with earlier versions
|
||||||
public static final String SHIFT_JIS = "SJIS";
|
public static final String SHIFT_JIS = "SJIS";
|
||||||
|
@ -101,7 +121,7 @@ public final class StringUtils {
|
||||||
// which should be by far the most common encodings.
|
// which should be by far the most common encodings.
|
||||||
int length = bytes.length;
|
int length = bytes.length;
|
||||||
boolean canBeISO88591 = true;
|
boolean canBeISO88591 = true;
|
||||||
boolean canBeShiftJIS = true;
|
boolean canBeShiftJIS = SHIFT_JIS_CHARSET != null;
|
||||||
boolean canBeUTF8 = true;
|
boolean canBeUTF8 = true;
|
||||||
int utf8BytesLeft = 0;
|
int utf8BytesLeft = 0;
|
||||||
int utf2BytesChars = 0;
|
int utf2BytesChars = 0;
|
||||||
|
|
|
@ -211,6 +211,10 @@ final class DecodedBitStreamParser {
|
||||||
private static void decodeKanjiSegment(BitSource bits,
|
private static void decodeKanjiSegment(BitSource bits,
|
||||||
StringBuilder result,
|
StringBuilder result,
|
||||||
int count) throws FormatException {
|
int count) throws FormatException {
|
||||||
|
if (StringUtils.SHIFT_JIS_CHARSET == null) {
|
||||||
|
// Not supported without charset support
|
||||||
|
throw FormatException.getFormatInstance();
|
||||||
|
}
|
||||||
// Don't crash trying to read more bits than we have available.
|
// Don't crash trying to read more bits than we have available.
|
||||||
if (count * 13 > bits.available()) {
|
if (count * 13 > bits.available()) {
|
||||||
throw FormatException.getFormatInstance();
|
throw FormatException.getFormatInstance();
|
||||||
|
|
|
@ -29,6 +29,7 @@ import com.google.zxing.qrcode.decoder.Version;
|
||||||
|
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.charset.UnsupportedCharsetException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -91,7 +92,11 @@ public final class Encoder {
|
||||||
Charset encoding = DEFAULT_BYTE_MODE_ENCODING;
|
Charset encoding = DEFAULT_BYTE_MODE_ENCODING;
|
||||||
boolean hasEncodingHint = hints != null && hints.containsKey(EncodeHintType.CHARACTER_SET);
|
boolean hasEncodingHint = hints != null && hints.containsKey(EncodeHintType.CHARACTER_SET);
|
||||||
if (hasEncodingHint) {
|
if (hasEncodingHint) {
|
||||||
|
try {
|
||||||
encoding = Charset.forName(hints.get(EncodeHintType.CHARACTER_SET).toString());
|
encoding = Charset.forName(hints.get(EncodeHintType.CHARACTER_SET).toString());
|
||||||
|
} catch (UnsupportedCharsetException ignore) {
|
||||||
|
//ignore the encodingHint and use the DEFAULT_BYTE_MODE_ENCODING
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hasCompactionHint) {
|
if (hasCompactionHint) {
|
||||||
|
@ -244,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}.
|
* 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) {
|
private static Mode chooseMode(String content, Charset encoding) {
|
||||||
if (StringUtils.SHIFT_JIS_CHARSET.equals(encoding) && isOnlyDoubleByteKanji(content)) {
|
if (StringUtils.SHIFT_JIS_CHARSET != null &&
|
||||||
|
StringUtils.SHIFT_JIS_CHARSET.equals(encoding) &&
|
||||||
|
isOnlyDoubleByteKanji(content)) {
|
||||||
// Choose Kanji mode if all input are double-byte characters
|
// Choose Kanji mode if all input are double-byte characters
|
||||||
return Mode.KANJI;
|
return Mode.KANJI;
|
||||||
}
|
}
|
||||||
|
@ -605,6 +612,10 @@ public final class Encoder {
|
||||||
}
|
}
|
||||||
|
|
||||||
static void appendKanjiBytes(String content, BitArray bits) throws WriterException {
|
static void appendKanjiBytes(String content, BitArray bits) throws WriterException {
|
||||||
|
if (StringUtils.SHIFT_JIS_CHARSET == null) {
|
||||||
|
// Not supported without charset support
|
||||||
|
throw new WriterException("SJIS Charset not supported on this platform");
|
||||||
|
}
|
||||||
byte[] bytes = content.getBytes(StringUtils.SHIFT_JIS_CHARSET);
|
byte[] bytes = content.getBytes(StringUtils.SHIFT_JIS_CHARSET);
|
||||||
if (bytes.length % 2 != 0) {
|
if (bytes.length % 2 != 0) {
|
||||||
throw new WriterException("Kanji byte size not even");
|
throw new WriterException("Kanji byte size not even");
|
||||||
|
|
Loading…
Reference in a new issue