sdedic commented on a change in pull request #2874:
URL: https://github.com/apache/netbeans/pull/2874#discussion_r611934939



##########
File path: 
ide/db/test/unit/src/org/netbeans/modules/db/explorer/DatabaseConnectionConvertorTest.java
##########
@@ -195,6 +196,16 @@ public void testDecodePassword() throws Exception {
         } catch (CharacterCodingException e) {}
     }
     
+    public void testDecodeBase64() {
+        final byte[] data = "P4sswørd".getBytes(StandardCharsets.UTF_8);

Review comment:
       I'd recommend to stick with ANSI charset for source. Pls. replace with 
an Unicode escape.

##########
File path: ide/versioning.util/src/org/netbeans/modules/proxy/Base64Encoder.java
##########
@@ -16,102 +16,54 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-
 package org.netbeans.modules.proxy;
 
-import java.io.ByteArrayOutputStream;
+import java.util.Base64;
 
 /**
  * Bas64 encode utility class.
  *
  * @author Maros Sandor
+ * @deprecated prefer java.util.Base64 instead
  */
+@Deprecated

Review comment:
       May be appropriate to add `apichanges.xml` and bump module's spec 
version (API change - deprecation).

##########
File path: ide/diff/src/org/netbeans/modules/diff/builtin/Base64.java
##########
@@ -19,60 +19,34 @@
 package org.netbeans.modules.diff.builtin;
 
 import java.io.ByteArrayOutputStream;
-import java.util.*;
+import java.util.List;
 
 /**
  * Base64 utility methods.
  *
  * @author Maros Sandor
  */
 class Base64 {
-    
+
+    private static final java.util.Base64.Decoder DECODER = 
java.util.Base64.getDecoder();
+
     private Base64() {
     }
-    
-    public static byte [] decode(List<String> ls) {
+
+    /**
+     * Decodes multiple Base64 strings into a single byteArray
+     */
+    public static byte[] decode(List<String> ls) {
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         for (String s : ls) {
             decode(s, bos);
         }
         return bos.toByteArray();
     }
-  
+
     private static void decode(String s, ByteArrayOutputStream bos) {
-        int i = 0;
-        int len = s.length();
-        while (true) {
-            while (i < len && s.charAt(i) <= ' ') i++;
-            if (i == len) break;
-            int tri = (decode(s.charAt(i)) << 18)
-            + (decode(s.charAt(i+1)) << 12)
-            + (decode(s.charAt(i+2)) << 6)
-            + (decode(s.charAt(i+3)));
-          
-            bos.write((tri >> 16) & 255);
-            if (s.charAt(i+2) == '=') break;
-            bos.write((tri >> 8) & 255);
-            if (s.charAt(i+3) == '=') break;
-            bos.write(tri & 255);
-          
-            i += 4;
-        }
+        final byte[] decoded = DECODER.decode(s);

Review comment:
       The original method ignored nonprintable (< 0x20) chars and spaces; it 
seems that `.getDecoder()` returns RFC4648 that throws an error on characters 
outside of Bas64 charset. Consider change to `Base64.getMimeDecoder`.
   

##########
File path: ide/versioning.util/src/org/netbeans/modules/proxy/Base64Encoder.java
##########
@@ -16,102 +16,54 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-
 package org.netbeans.modules.proxy;
 
-import java.io.ByteArrayOutputStream;
+import java.util.Base64;
 
 /**
  * Bas64 encode utility class.
  *
  * @author Maros Sandor
+ * @deprecated prefer java.util.Base64 instead
  */
+@Deprecated
 public class Base64Encoder {
 
-    private static final char [] characters = 
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
-
     private Base64Encoder() {
     }
 
-    public static String encode(byte [] data) {
+    public static String encode(byte[] data) {
         return encode(data, false);
     }
-    
-    public static String encode(byte [] data, boolean useNewlines) {
-        int length = data.length;
-        StringBuffer sb = new StringBuffer(data.length * 3 / 2);
-
-        int end = length - 3;
-        int i = 0;
-        int lineCount = 0;
-
-        while (i <= end) {
-            int d = ((((int) data[i]) & 0xFF) << 16) | ((((int) data[i + 1]) & 
0xFF) << 8) | (((int) data[i + 2]) & 0xFF);
-            sb.append(characters[(d >> 18) & 0x3F]);
-            sb.append(characters[(d >> 12) & 0x3F]);
-            sb.append(characters[(d >> 6) & 0x3F]);
-            sb.append(characters[d & 0x3F]);
-            i += 3;
-            
-            if (useNewlines && lineCount++ >= 14) {
-                lineCount = 0;
-                sb.append(System.getProperty("line.separator"));
-            }
-        }
 
-        if (i == length - 2) {
-            int d = ((((int) data[i]) & 0xFF) << 16) | ((((int) data[i + 1]) & 
0xFF) << 8);
-            sb.append(characters[(d >> 18) & 0x3F]);
-            sb.append(characters[(d >> 12) & 0x3F]);
-            sb.append(characters[(d >> 6) & 0x3F]);
-            sb.append("=");
-        } else if (i == length - 1) {
-            int d = (((int) data[i]) & 0xFF) << 16;
-            sb.append(characters[(d >> 18) & 0x3F]);
-            sb.append(characters[(d >> 12) & 0x3F]);
-            sb.append("==");
+    public static String encode(byte[] data, boolean useNewlines) {
+        final String encoded = Base64.getEncoder().encodeToString(data);
+        if (useNewlines) {
+            return wrapText(encoded, 60, System.getProperty("line.separator"));
+        } else {
+            return encoded;
         }
-        return sb.toString();
     }
-    
-    public static byte [] decode(String s) {
-        ByteArrayOutputStream bos = new ByteArrayOutputStream();
-        decode(s, bos);
-        return bos.toByteArray();
-}
-  
-    private static void decode(String s, ByteArrayOutputStream bos) {
-        int i = 0;
-        int len = s.length();
-        for (;;) {
-            while (i < len && s.charAt(i) <= ' ') i++;
-            if (i == len) break;
-            int tri = (decode(s.charAt(i)) << 18) + (decode(s.charAt(i+1)) << 
12) + (decode(s.charAt(i+2)) << 6) + (decode(s.charAt(i+3)));
-            bos.write((tri >> 16) & 255);
-            if (s.charAt(i+2) == '=') break;
-            bos.write((tri >> 8) & 255);
-            if (s.charAt(i+3) == '=') break;
-            bos.write(tri & 255);
-            i += 4;
+
+    static String wrapText(String text, int length, String separator) {
+        if (length > 0) {
+            StringBuilder sb = new StringBuilder(text.length() + 
(((text.length() - 1) / length) * separator.length()));
+            int idx = 0;
+            while (idx < text.length()) {
+                if (idx > 0) {
+                    sb.append(separator);
+                }
+                sb.append(text.substring(idx, Math.min(idx + length, 
text.length())));
+                idx += length;
+            }
+            return sb.toString();
+        } else {
+            return text;
         }
     }
 
-    private static int decode(char c) {
-        if (c >= 'A' && c <= 'Z') {
-            return ((int) c) - 65;
-        } else if (c >= 'a' && c <= 'z') {
-            return ((int) c) - 97 + 26;
-        } else if (c >= '0' && c <= '9') {
-            return ((int) c) - 48 + 26 + 26;
-        } else switch (c) {
-            case '+': 
-                return 62;
-            case '/': 
-                return 63;
-            case '=': 
-                return 0;
-            default:
-                throw new RuntimeException("Base64: unexpected code: " + c);
-        }
+    public static byte[] decode(String s) {
+        return Base64.getDecoder().decode(s);

Review comment:
       Original impl ignores spaces and nonprintables; consider using 
`getMimeDecoder()`

##########
File path: ide/versioning.util/src/org/netbeans/modules/proxy/Base64Encoder.java
##########
@@ -16,102 +16,54 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-
 package org.netbeans.modules.proxy;
 
-import java.io.ByteArrayOutputStream;
+import java.util.Base64;
 
 /**
  * Bas64 encode utility class.
  *
  * @author Maros Sandor
+ * @deprecated prefer java.util.Base64 instead
  */
+@Deprecated
 public class Base64Encoder {
 
-    private static final char [] characters = 
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
-
     private Base64Encoder() {
     }
 
-    public static String encode(byte [] data) {
+    public static String encode(byte[] data) {
         return encode(data, false);
     }
-    
-    public static String encode(byte [] data, boolean useNewlines) {
-        int length = data.length;
-        StringBuffer sb = new StringBuffer(data.length * 3 / 2);
-
-        int end = length - 3;
-        int i = 0;
-        int lineCount = 0;
-
-        while (i <= end) {
-            int d = ((((int) data[i]) & 0xFF) << 16) | ((((int) data[i + 1]) & 
0xFF) << 8) | (((int) data[i + 2]) & 0xFF);
-            sb.append(characters[(d >> 18) & 0x3F]);
-            sb.append(characters[(d >> 12) & 0x3F]);
-            sb.append(characters[(d >> 6) & 0x3F]);
-            sb.append(characters[d & 0x3F]);
-            i += 3;
-            
-            if (useNewlines && lineCount++ >= 14) {
-                lineCount = 0;
-                sb.append(System.getProperty("line.separator"));
-            }
-        }
 
-        if (i == length - 2) {
-            int d = ((((int) data[i]) & 0xFF) << 16) | ((((int) data[i + 1]) & 
0xFF) << 8);
-            sb.append(characters[(d >> 18) & 0x3F]);
-            sb.append(characters[(d >> 12) & 0x3F]);
-            sb.append(characters[(d >> 6) & 0x3F]);
-            sb.append("=");
-        } else if (i == length - 1) {
-            int d = (((int) data[i]) & 0xFF) << 16;
-            sb.append(characters[(d >> 18) & 0x3F]);
-            sb.append(characters[(d >> 12) & 0x3F]);
-            sb.append("==");
+    public static String encode(byte[] data, boolean useNewlines) {
+        final String encoded = Base64.getEncoder().encodeToString(data);
+        if (useNewlines) {
+            return wrapText(encoded, 60, System.getProperty("line.separator"));

Review comment:
       Hm, I wonder if `Base64.getMimeEncoder(60, 
System.getProperty("line.separator").getBytes(StandardCharsets.ISO_8859_1)` 
could be used.

##########
File path: 
ide/subversion/src/org/netbeans/modules/subversion/ui/diff/ExportDiffAction.java
##########
@@ -351,8 +350,29 @@ private String exportBinaryFile(File file) throws 
IOException {
         }
         sb.append("MIME: application/octet-stream; encoding: Base64; length: 
").append(file.canRead() ? file.length() : -1); // NOI18N
         sb.append(System.getProperty("line.separator")); // NOI18N
-        sb.append(Base64Encoder.encode(baos.toByteArray(), true));
+        sb.append(encodeToWrappedBase64(baos.toByteArray()));
         sb.append(System.getProperty("line.separator")); // NOI18N
         return sb.toString();
     }
+
+    static String encodeToWrappedBase64(byte[] data) {

Review comment:
       Consider using MIME encoder with specified length.

##########
File path: 
ide/mercurial/src/org/netbeans/modules/mercurial/config/Scrambler.java
##########
@@ -304,6 +304,6 @@ private char scramble(char ch) {
     }     
     
     private byte[] decode(String str) {        
-            return Base64Encoder.decode(str);
+        return Base64.getDecoder().decode(str);

Review comment:
       `getMimeDecoder` ?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to