minor cleanup, encodefile command in Base64.java
[org.ibex.crypto.git] / src / org / ibex / crypto / Base64.java
index 5f66130..04ef163 100644 (file)
@@ -4,12 +4,12 @@
  */
 package org.ibex.crypto;
 
-import java.io.UnsupportedEncodingException;
+import java.io.*;
 
 public final class Base64 {
-       private Base64() { /* can't be instansiated */ }
-       
-    private static final char encodeMap[] = {
+    private Base64() { /* can't be instansiated */ }
+    
+    private static final char[] encodeMap = {
         'A','B','C','D','E','F','G','H',
         'I','J','K','L','M','N','O','P',
         'Q','R','S','T','U','V','W','X',
@@ -20,7 +20,7 @@ public final class Base64 {
         '4','5','6','7','8','9','+','/'
     };
 
-    private static final int decodeMap[] = {
+    private static final int[] decodeMap = {
         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,
@@ -31,19 +31,27 @@ public final class Base64 {
         41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1
     };
 
-       public static void main(String[] args){
+    public static void main(String[] args) throws IOException {
         if(args.length == 2 && args[0].equals("encode"))
-            System.out.println(encode(args[1].getBytes()));
+            System.out.println(encode(getBytes(args[1])));
         else if(args.length == 2 && args[0].equals("decode"))
-            System.out.println(new String(decode(args[1])));
-        else {
-            System.out.println("Usage: Base64 {encode,decode} text");
+            System.out.write(decode(args[1]));
+        else if(args.length == 2 && args[0].equals("encodefile")) {
+            FileInputStream fis = new FileInputStream(args[1]);
+            ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            byte[] buf = new byte[4096];
+            int n;
+            while((n = fis.read(buf)) != -1) baos.write(buf,0,n);
+            fis.close();
+            System.out.println(encode(baos.toByteArray()));
+        } else {
+            System.out.println("Usage: Base64 {encode,decode} filename");
             System.exit(1);
         }
         System.exit(0);
-       }
+    }
        
-       public static byte[] decode(String inString){
+    public static byte[] decode(String inString){
         char[] in = inString.toCharArray();
         int part=0;
         int theBytes = 0;
@@ -81,14 +89,15 @@ public final class Base64 {
         }
     }
 
-       public static String encode(String in) { return encode(getBytes(in)); }
+    public static String encode(String in) { return encode(getBytes(in)); }
+    
     public static String encode(byte[] in){
         int part=0;
         int theBytes = 0;
         char[] out = new char[(in.length / 3 + 1) * 4];
         int outPos = 0;
         for(int i=0;i<in.length;i++){
-            theBytes = (theBytes << 8) | in[i];
+            theBytes = (theBytes << 8) | (in[i]&0xff);
             part++;
             if(part == 3){
                 part = 0;