igalshilman commented on a change in pull request #6966: [FLINK-10166] [table] 
Reduce dependencies by removing org.apache.commons
URL: https://github.com/apache/flink/pull/6966#discussion_r229481767
 
 

 ##########
 File path: 
flink-libraries/flink-table-common/src/main/java/org/apache/flink/table/utils/EncodingUtils.java
 ##########
 @@ -87,10 +95,145 @@ public static String encodeObjectToString(Serializable 
obj) {
                return loadClass(qualifiedName, 
Thread.currentThread().getContextClassLoader());
        }
 
+       public static String encodeStringToBase64(String string) {
+               return new 
String(java.util.Base64.getEncoder().encode(string.getBytes(UTF_8)), UTF_8);
+       }
+
+       public static String decodeBase64ToString(String base64) {
+               return new 
String(java.util.Base64.getDecoder().decode(base64.getBytes(UTF_8)), UTF_8);
+       }
+
+       public static byte[] md5(String string) {
+               if (MD5_MESSAGE_DIGEST == null) {
+                       throw new TableException("Unsupported MD5 algorithm.");
+               }
+               return MD5_MESSAGE_DIGEST.digest(string.getBytes(UTF_8));
+       }
+
+       public static String hex(String string) {
+               return hex(string.getBytes(UTF_8));
+       }
+
+       public static String hex(byte[] bytes) {
+               // adopted from https://stackoverflow.com/a/9855338
+               final char[] hexChars = new char[bytes.length * 2];
+               for (int j = 0; j < bytes.length; j++) {
+                       final int v = bytes[j] & 0xFF;
+                       hexChars[j * 2] = HEX_CHARS[v >>> 4];
+                       hexChars[j * 2 + 1] = HEX_CHARS[v & 0x0F];
+               }
+               return new String(hexChars);
+       }
+
+       private static MessageDigest getMd5MessageDigest() {
+               try {
+                       return MessageDigest.getInstance("MD5");
+               } catch (NoSuchAlgorithmException e) {
+                       return null;
+               }
+       }
+
+       // 
--------------------------------------------------------------------------------------------
+       // Java String Repetition
+       //
+       // copied from o.a.commons.lang3.StringUtils (commons-lang3:3.3.2)
+       // 
--------------------------------------------------------------------------------------------
+
+       private static final String EMPTY = "";
+
+       /**
+        * The maximum size to which the padding constant(s) can expand.
+        */
+       private static final int PAD_LIMIT = 8192;
+
+       /**
+        * Repeat a String {@code repeat} times to form a new String.
+        *
+        * <pre>
+        * StringUtils.repeat(null, 2) = null
+        * StringUtils.repeat("", 0)   = ""
+        * StringUtils.repeat("", 2)   = ""
+        * StringUtils.repeat("a", 3)  = "aaa"
+        * StringUtils.repeat("ab", 2) = "abab"
+        * StringUtils.repeat("a", -2) = ""
+        * </pre>
+        *
+        * @param str    the String to repeat, may be null
+        * @param repeat number of times to repeat str, negative treated as zero
+        * @return a new String consisting of the original String repeated, 
{@code null} if null String input
+        */
+       public static String repeat(final String str, final int repeat) {
+               // Performance tuned for 2.0 (JDK1.4)
+
+               if (str == null) {
+                       return null;
+               }
+               if (repeat <= 0) {
+                       return EMPTY;
+               }
+               final int inputLength = str.length();
+               if (repeat == 1 || inputLength == 0) {
+                       return str;
+               }
+               if (inputLength == 1 && repeat <= PAD_LIMIT) {
+                       return repeat(str.charAt(0), repeat);
+               }
+
+               final int outputLength = inputLength * repeat;
+               switch (inputLength) {
+                       case 1:
+                               return repeat(str.charAt(0), repeat);
+                       case 2:
+                               final char ch0 = str.charAt(0);
+                               final char ch1 = str.charAt(1);
+                               final char[] output2 = new char[outputLength];
+                               for (int i = repeat * 2 - 2; i >= 0; i--, i--) {
+                                       output2[i] = ch0;
+                                       output2[i + 1] = ch1;
+                               }
+                               return new String(output2);
+                       default:
+                               final StringBuilder buf = new 
StringBuilder(outputLength);
+                               for (int i = 0; i < repeat; i++) {
+                                       buf.append(str);
+                               }
+                               return buf.toString();
+               }
+       }
+
+       /**
+        * Returns padding using the specified delimiter repeated to a given 
length.
+        *
+        * <pre>
+        * StringUtils.repeat('e', 0)  = ""
+        * StringUtils.repeat('e', 3)  = "eee"
+        * StringUtils.repeat('e', -2) = ""
+        * </pre>
+        *
+        * <p>Note: this method doesn't not support padding with
+        * <a 
href="http://www.unicode.org/glossary/#supplementary_character";>Unicode 
Supplementary Characters</a>
+        * as they require a pair of {@code char}s to be represented.
+        * If you are needing to support full I18N of your applications
+        * consider using {@link #repeat(String, int)} instead.
+        *
+        * @param ch     character to repeat
+        * @param repeat number of times to repeat char, negative treated as 
zero
+        * @return String with repeated character
+        * @see #repeat(String, int)
+        */
+       public static String repeat(final char ch, final int repeat) {
+               final char[] buf = new char[repeat];
+               for (int i = repeat - 1; i >= 0; i--) {
 
 Review comment:
   I wonder why the original author has decided to loop backwards :-) 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to