@@ -72,13 +72,16 @@ private static B64Result tryDecode(String str, B64DeobfuscateOptions options) {
7272 return null ;
7373 }
7474
75+ private static CharsetDecoder newUtf8Decoder (CodingErrorAction action ) {
76+ return StandardCharsets .UTF_8 .newDecoder ()
77+ .onMalformedInput (action )
78+ .onUnmappableCharacter (action );
79+ }
80+
7581 private static B64Result attemptDecode (Base64 .Decoder decoder , String str , B64DeobfuscateOptions options , String tag ) {
7682 try {
7783 byte [] bytes = decoder .decode (str );
78- CharsetDecoder utf8 = StandardCharsets .UTF_8 .newDecoder ()
79- .onMalformedInput (CodingErrorAction .REPORT )
80- .onUnmappableCharacter (CodingErrorAction .REPORT );
81- String decoded = utf8 .decode (ByteBuffer .wrap (bytes )).toString ();
84+ String decoded = newUtf8Decoder (CodingErrorAction .REPORT ).decode (ByteBuffer .wrap (bytes )).toString ();
8285 if (!isPrintable (decoded , options .getMinPrintableRatio ())) {
8386 return null ;
8487 }
@@ -143,10 +146,7 @@ public static B64Result decodeIfValid(String str, int maxCommentLength) {
143146 if (bytes .length == 0 ) {
144147 return null ;
145148 }
146- CharsetDecoder utf8 = StandardCharsets .UTF_8 .newDecoder ()
147- .onMalformedInput (CodingErrorAction .REPORT )
148- .onUnmappableCharacter (CodingErrorAction .REPORT );
149- String decoded = utf8 .decode (ByteBuffer .wrap (bytes )).toString ();
149+ String decoded = newUtf8Decoder (CodingErrorAction .REPORT ).decode (ByteBuffer .wrap (bytes )).toString ();
150150 return new B64Result (truncate (decoded , maxCommentLength ), tags [i ]);
151151 } catch (IllegalArgumentException | CharacterCodingException ignored ) {
152152 // decoder.decode() rejects malformed Base64; utf8.decode() rejects invalid UTF-8
@@ -168,10 +168,7 @@ public static B64Result decodeForced(String str, int maxCommentLength) {
168168 if (bytes .length == 0 ) {
169169 return null ;
170170 }
171- CharsetDecoder utf8 = StandardCharsets .UTF_8 .newDecoder ()
172- .onMalformedInput (CodingErrorAction .REPLACE )
173- .onUnmappableCharacter (CodingErrorAction .REPLACE );
174- String decoded = utf8 .decode (ByteBuffer .wrap (bytes )).toString ();
171+ String decoded = newUtf8Decoder (CodingErrorAction .REPLACE ).decode (ByteBuffer .wrap (bytes )).toString ();
175172 return new B64Result (truncate (decoded , maxCommentLength ), ALL_DECODER_TAGS [i ]);
176173 } catch (IllegalArgumentException | CharacterCodingException ignored ) {
177174 // decoder.decode() rejects malformed Base64; utf8.decode() on REPLACE mode won't throw, but guard anyway
0 commit comments