[GitHub] igalshilman commented on a change in pull request #6966: [FLINK-10166] [table] Reduce dependencies by removing org.apache.commons

2018-10-31 Thread GitBox
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_r229603180
 
 

 ##
 File path: 
flink-libraries/flink-table-common/src/main/java/org/apache/flink/table/utils/EncodingUtils.java
 ##
 @@ -40,14 +44,18 @@
 
private static final Base64.Decoder BASE64_DECODER = 
java.util.Base64.getUrlDecoder();
 
+   private static final MessageDigest MD5_MESSAGE_DIGEST = 
getMd5MessageDigest();
 
 Review comment:
   Thanks for addressing this.


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


[GitHub] igalshilman commented on a change in pull request #6966: [FLINK-10166] [table] Reduce dependencies by removing org.apache.commons

2018-10-31 Thread GitBox
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_r229603056
 
 

 ##
 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.
+*
+* 
+* 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) = ""
+* 
+*
+* @param strthe 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);
 
 Review comment:
   Got it. makes senes.


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 

[GitHub] igalshilman commented on a change in pull request #6966: [FLINK-10166] [table] Reduce dependencies by removing org.apache.commons

2018-10-30 Thread GitBox
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.
+*
+* 
+* 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) = ""
+* 
+*
+* @param strthe 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();
+   }
+   }
+
+  

[GitHub] igalshilman commented on a change in pull request #6966: [FLINK-10166] [table] Reduce dependencies by removing org.apache.commons

2018-10-30 Thread GitBox
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_r229473954
 
 

 ##
 File path: 
flink-libraries/flink-table-common/src/main/java/org/apache/flink/table/utils/EncodingUtils.java
 ##
 @@ -40,14 +44,18 @@
 
private static final Base64.Decoder BASE64_DECODER = 
java.util.Base64.getUrlDecoder();
 
+   private static final MessageDigest MD5_MESSAGE_DIGEST = 
getMd5MessageDigest();
 
 Review comment:
   Would MD5_MESSAGE_DIGEST be accessed concurrently? afaik this is not thread 
safe.


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


[GitHub] igalshilman commented on a change in pull request #6966: [FLINK-10166] [table] Reduce dependencies by removing org.apache.commons

2018-10-30 Thread GitBox
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_r229487810
 
 

 ##
 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.
+*
+* 
+* 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) = ""
+* 
+*
+* @param strthe 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);
 
 Review comment:
   I know that you've just copied it, but honestly, I think that the default 
case is the only one that makes senes and all the complication in that method 
is a bit wired (as the comment above states - it is optimized for jdk1.4)