From 71bd11c7468e1cbccc58069e70c176c67fb8de53 Mon Sep 17 00:00:00 2001 From: John Doe Date: Wed, 21 Jul 2021 12:34:35 +0200 Subject: Replaced StandardCharsets with Charset.forName java.nio.charset.StandardCharsets is available on API level >= 19. See https://developer.android.com/reference/java/nio/charset/StandardCharsets. java.nio.charset.Charset is there since java 1.4. --- .../java/org/uic/barcode/asn1/uper/Document2.txt | 34 ---------------------- .../org/uic/barcode/asn1/uper/StringCoder.java | 20 ++++++------- .../uic/barcode/staticFrame/UTLAYDataRecord.java | 6 ++-- 3 files changed, 13 insertions(+), 47 deletions(-) delete mode 100644 src/main/java/org/uic/barcode/asn1/uper/Document2.txt (limited to 'src/main/java') diff --git a/src/main/java/org/uic/barcode/asn1/uper/Document2.txt b/src/main/java/org/uic/barcode/asn1/uper/Document2.txt deleted file mode 100644 index 176ec23..0000000 --- a/src/main/java/org/uic/barcode/asn1/uper/Document2.txt +++ /dev/null @@ -1,34 +0,0 @@ - if (restrictionAnnotation.value() == CharacterRestriction.UTF8String) { - // UTF8 length - BitBuffer stringbuffer = ByteBitBuffer.createInfinite(); - - //char array replaced - begin - byte[] stringasbytearray = string.getBytes(StandardCharsets.UTF_8); - - for (byte b: stringasbytearray){ - UperEncoder.encodeConstrainedInt(stringbuffer, byte & 0xff, 0, 255); - } - //char array replaced - end - - stringbuffer.flip(); - if (stringbuffer.limit() % 8 != 0) { - throw new AssertionError("utf8 encoding resulted not in multiple of 8 bits"); - } - int numOctets = (stringbuffer.limit() + 7) / 8; // Actually +7 is not needed here, - // since we already checked with %8. - int position1 = bitbuffer.position(); - UperEncoder.encodeLengthDeterminant(bitbuffer, numOctets); - UperEncoder.logger.debug(String.format("UTF8String %s, length %d octets, encoded as %s", string, numOctets, bitbuffer.toBooleanStringFromPosition(position1))); - int position2 = bitbuffer.position(); - for (int i = 0; i < stringbuffer.limit(); i++) { - bitbuffer.put(stringbuffer.get()); - } - UperEncoder.logger.debug(String.format("UTF8String %s, encoded length %d octets, value bits: %s", string, numOctets, bitbuffer.toBooleanStringFromPosition(position2))); - return; - - - - - - -new String(bytearray, StandardCharsets.UTF_8)); \ No newline at end of file diff --git a/src/main/java/org/uic/barcode/asn1/uper/StringCoder.java b/src/main/java/org/uic/barcode/asn1/uper/StringCoder.java index fe06e3d..c842480 100644 --- a/src/main/java/org/uic/barcode/asn1/uper/StringCoder.java +++ b/src/main/java/org/uic/barcode/asn1/uper/StringCoder.java @@ -5,7 +5,7 @@ import java.lang.reflect.Field; import java.math.BigInteger; import java.nio.ByteBuffer; import java.nio.CharBuffer; -import java.nio.charset.StandardCharsets; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -87,7 +87,7 @@ class StringCoder implements Decoder, Encoder { BitBuffer stringbuffer = ByteBitBuffer.createInfinite(); //char array replaced - begin - byte[] stringasbytearray = string.getBytes(StandardCharsets.UTF_8); + byte[] stringasbytearray = string.getBytes(Charset.forName("UTF-8")); for (byte b: stringasbytearray){ UperEncoder.encodeConstrainedInt(stringbuffer, b & 0xff, 0, 255); @@ -179,7 +179,7 @@ class StringCoder implements Decoder, Encoder { } byte[] contentBytes = UperEncoder.bytesFromCollection(content); UperEncoder.logger.debug(String.format("Content bytes (hex): %s", UperEncoder.hexStringFromBytes(contentBytes))); - String resultStr = StandardCharsets.UTF_8.decode(ByteBuffer.wrap(contentBytes)).toString(); + String resultStr = Charset.forName("UTF-8").decode(ByteBuffer.wrap(contentBytes)).toString(); UperEncoder.logger.debug(String.format("Decoded as %s", resultStr)); T result = UperEncoder.instantiate(classOfT, resultStr); return result; @@ -211,7 +211,7 @@ class StringCoder implements Decoder, Encoder { } UperEncoder.encodeConstrainedInt( bitbuffer, - StandardCharsets.US_ASCII.encode(CharBuffer.wrap(new char[] { c })).get() & 0xff, + Charset.forName("US-ASCII").encode(CharBuffer.wrap(new char[] { c })).get() & 0xff, 0, 127); return; @@ -219,7 +219,7 @@ class StringCoder implements Decoder, Encoder { if (restriction.alphabet() != DefaultAlphabet.class) { throw new UnsupportedOperationException("alphabet for UTF8 is not supported yet."); } - ByteBuffer buffer = StandardCharsets.UTF_8.encode(CharBuffer.wrap(new char[] { c })); + ByteBuffer buffer = Charset.forName("UTF-8").encode(CharBuffer.wrap(new char[] { c })); for (int i = 0; i < buffer.limit(); i++) { UperEncoder.encodeConstrainedInt(bitbuffer, buffer.get() & 0xff, 0, 255); } @@ -249,7 +249,7 @@ class StringCoder implements Decoder, Encoder { } else { UperEncoder.encodeConstrainedInt( bitbuffer, - StandardCharsets.US_ASCII.encode(CharBuffer.wrap(new char[] { c })) + Charset.forName("US-ASCII").encode(CharBuffer.wrap(new char[] { c })) .get() & 0xff, 0, 126); @@ -258,7 +258,7 @@ class StringCoder implements Decoder, Encoder { } else { UperEncoder.encodeConstrainedInt( bitbuffer, - StandardCharsets.US_ASCII.encode(CharBuffer.wrap(new char[] { c })) + Charset.forName("US-ASCII").encode(CharBuffer.wrap(new char[] { c })) .get() & 0xff, 0, 126); @@ -280,7 +280,7 @@ class StringCoder implements Decoder, Encoder { } byte charByte = (byte) UperEncoder.decodeConstrainedInt(bitqueue, UperEncoder.newRange(0, 127, false)); byte[] bytes = new byte[] { charByte }; - String result = StandardCharsets.US_ASCII.decode(ByteBuffer.wrap(bytes)).toString(); + String result = Charset.forName("US-ASCII").decode(ByteBuffer.wrap(bytes)).toString(); if (result.length() != 1) { throw new AssertionError("decoded more than one char (" + result + ")"); } @@ -307,7 +307,7 @@ class StringCoder implements Decoder, Encoder { } else { // Encode normally byte charByte = (byte) UperEncoder.decodeConstrainedInt(bitqueue, UperEncoder.newRange(0, 126, false)); byte[] bytes = new byte[] { charByte }; - String result = StandardCharsets.US_ASCII.decode(ByteBuffer.wrap(bytes)).toString(); + String result = Charset.forName("US-ASCII").decode(ByteBuffer.wrap(bytes)).toString(); if (result.length() != 1) { throw new AssertionError( "decoded more than one char (" + result + ")"); } @@ -316,7 +316,7 @@ class StringCoder implements Decoder, Encoder { } else { // Encode normally byte charByte = (byte) UperEncoder.decodeConstrainedInt(bitqueue, UperEncoder.newRange(0, 126, false)); byte[] bytes = new byte[] { charByte }; - String result = StandardCharsets.US_ASCII.decode(ByteBuffer.wrap(bytes)).toString(); + String result = Charset.forName("US-ASCII").decode(ByteBuffer.wrap(bytes)).toString(); if (result.length() != 1) { throw new AssertionError("decoded more than one char (" + result + ")"); } diff --git a/src/main/java/org/uic/barcode/staticFrame/UTLAYDataRecord.java b/src/main/java/org/uic/barcode/staticFrame/UTLAYDataRecord.java index 549a2e7..2d403e4 100644 --- a/src/main/java/org/uic/barcode/staticFrame/UTLAYDataRecord.java +++ b/src/main/java/org/uic/barcode/staticFrame/UTLAYDataRecord.java @@ -7,7 +7,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; -import java.nio.charset.StandardCharsets; +import java.nio.charset.Charset; import org.uic.barcode.staticFrame.ticketLayoutBarcode.FormatType; import org.uic.barcode.staticFrame.ticketLayoutBarcode.LayoutElement; @@ -43,7 +43,7 @@ public class UTLAYDataRecord extends DataRecord { for (int i = 0; i < length; i++){ bytes[i] = byteData[i + offset]; } - return StandardCharsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString(); + return Charset.forName("UTF-8").decode(ByteBuffer.wrap(bytes)).toString(); } /** @@ -59,7 +59,7 @@ public class UTLAYDataRecord extends DataRecord { for (int i = 0; i < length; i++){ bytes[i] = byteData[i + offset]; } - return StandardCharsets.ISO_8859_1.decode(ByteBuffer.wrap(bytes)).toString(); + return Charset.forName("ISO-8859-1").decode(ByteBuffer.wrap(bytes)).toString(); } /** -- cgit v1.2.3