@@ -14,13 +14,15 @@ public final class B64Detector {
1414 private static final Pattern BASE64_URL_SAFE = Pattern .compile ("^[A-Za-z0-9_-]+=*$" );
1515 private static final int MIN_LENGTH = 8 ;
1616 private static final double MIN_PRINTABLE_RATIO = 0.75 ;
17- static final int MAX_COMMENT_LENGTH = 100 ;
1817
1918 private B64Detector () {
2019 }
2120
22- /** Returns the decoded+truncated string if {@code str} looks like Base64, otherwise null. */
23- public static String detect (String str ) {
21+ /**
22+ * Returns the decoded string if {@code str} looks like Base64, otherwise null.
23+ * The result is truncated to {@code maxLength} chars (0 = unlimited).
24+ */
25+ public static String detect (String str , int maxLength ) {
2426 if (str .length () < MIN_LENGTH ) {
2527 return null ;
2628 }
@@ -31,7 +33,7 @@ public static String detect(String str) {
3133 if (decoded == null ) {
3234 return null ;
3335 }
34- return truncate (decoded );
36+ return truncate (decoded , maxLength );
3537 }
3638
3739 private static String tryDecode (String str ) {
@@ -69,10 +71,10 @@ private static boolean isPrintable(String str) {
6971 return (double ) printableCount / str .length () >= MIN_PRINTABLE_RATIO ;
7072 }
7173
72- static String truncate (String str ) {
74+ static String truncate (String str , int maxLength ) {
7375 String safe = str .replace ("\n " , "\\ n" ).replace ("\r " , "\\ r" );
74- if (safe .length () > MAX_COMMENT_LENGTH ) {
75- return safe .substring (0 , MAX_COMMENT_LENGTH ) + "..." ;
76+ if (maxLength > 0 && safe .length () > maxLength ) {
77+ return safe .substring (0 , maxLength ) + "..." ;
7678 }
7779 return safe ;
7880 }
0 commit comments