The Base64 utility class implements Base-64 and Base-85 encoding and decoding algorithms. : Base64 « Development Class « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Development Class » Base64 




The Base64 utility class implements Base-64 and Base-85 encoding and decoding algorithms.
     
/*
 * Copyright © 2011 Rebecca G. Bettencourt / Kreative Software
 * <p>
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * <a href="http://www.mozilla.org/MPL/">http://www.mozilla.org/MPL/</a>
 * <p>
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 * <p>
 * Alternatively, the contents of this file may be used under the terms
 * of the GNU Lesser General Public License (the "LGPL License"), in which
 * case the provisions of LGPL License are applicable instead of those
 * above. If you wish to allow use of your version of this file only
 * under the terms of the LGPL License and not to allow others to use
 * your version of this file under the MPL, indicate your decision by
 * deleting the provisions above and replace them with the notice and
 * other provisions required by the LGPL License. If you do not delete
 * the provisions above, a recipient may use your version of this file
 * under either the MPL or the LGPL License.
 * @since OpenXION 1.3
 * @author Rebecca G. Bettencourt, Kreative Software
 */

//package com.kreative.openxion.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;

/**
 * The Base64 utility class implements Base-64 and Base-85 encoding and decoding algorithms.
 @since OpenXION 1.3
 @author Rebecca G. Bettencourt, Kreative Software
 */
public class Base64 {
  private Base64() {}
  
  private static final char[] b64e = {
    'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
    'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
    'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
    'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/',
  };
  
  public static String encodeBase64(byte[] b, int off, int len) {
    int i = 0;
    StringBuffer s = new StringBuffer();
    while (len >= 3) {
      i = ((b[off0xFF<< 16((b[off+10xFF<< 8(b[off+20xFF);
      s.append(b64e[(i >> 180x3F]);
      s.append(b64e[(i >> 120x3F]);
      s.append(b64e[(i >> 60x3F]);
      s.append(b64e[i & 0x3F]);
      off += 3;
      len -= 3;
    }
    switch (len) {
    case 2:
      i = ((b[off0xFF<< 16((b[off+10xFF<< 8);
      s.append(b64e[(i >> 180x3F]);
      s.append(b64e[(i >> 120x3F]);
      s.append(b64e[(i >> 60x3F]);
      s.append('=');
      break;
    case 1:
      i = ((b[off0xFF<< 16);
      s.append(b64e[(i >> 180x3F]);
      s.append(b64e[(i >> 120x3F]);
      s.append('=');
      s.append('=');
      break;
    }
    return s.toString();
  }
  
  public static String encodeBase64(byte[] b) {
    return encodeBase64(b, 0, b.length);
  }
  
  private static int b64d(char i) {
    switch (i) {
    case 'A'return 0case 'B'return 1case 'C'return 2case 'D'return 3;
    case 'E'return 4case 'F'return 5case 'G'return 6case 'H'return 7;
    case 'I'return 8case 'J'return 9case 'K'return 10case 'L'return 11;
    case 'M'return 12case 'N'return 13case 'O'return 14case 'P'return 15;
    case 'Q'return 16case 'R'return 17case 'S'return 18case 'T'return 19;
    case 'U'return 20case 'V'return 21case 'W'return 22case 'X'return 23;
    case 'Y'return 24case 'Z'return 25case 'a'return 26case 'b'return 27;
    case 'c'return 28case 'd'return 29case 'e'return 30case 'f'return 31;
    case 'g'return 32case 'h'return 33case 'i'return 34case 'j'return 35;
    case 'k'return 36case 'l'return 37case 'm'return 38case 'n'return 39;
    case 'o'return 40case 'p'return 41case 'q'return 42case 'r'return 43;
    case 's'return 44case 't'return 45case 'u'return 46case 'v'return 47;
    case 'w'return 48case 'x'return 49case 'y'return 50case 'z'return 51;
    case '0'return 52case '1'return 53case '2'return 54case '3'return 55;
    case '4'return 56case '5'return 57case '6'return 58case '7'return 59;
    case '8'return 60case '9'return 61case '+'return 62case '/'return 63;
    defaultreturn -1;
    }
  }
  
  public static byte[] decodeBase64(String s) {
    CharacterIterator it = new StringCharacterIterator(s.trim());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int i = 0, j = 0;
    for (char ch = it.first(); ch != CharacterIterator.DONE && ch != '='; ch = it.next()) {
      int v = b64d(ch);
      if (v >= 0) {
        i = (i << 6| v;
        j++;
        if (j >= 4) {
          out.write(i >> 16);
          out.write(i >> 8);
          out.write(i);
          i = 0; j = 0;
        }
      }
    }
    switch (j) {
    case 3:
      out.write(i >> 10);
      out.write(i >> 2);
      break;
    case 2:
      out.write(i >> 4);
      break;
    }
    return out.toByteArray();
  }
  
  public static String encodeUU(byte[] b, int off, int len) {
    int i = 0;
    StringBuffer s = new StringBuffer();
    while (len > 45) {
      s.append('M');
      for (int j = 0; j < 45; j += 3) {
        i = ((b[off0xFF<< 16((b[off+10xFF<< 8(b[off+20xFF);
        s.append((char)(' '+((i >> 180x3F)));
        s.append((char)(' '+((i >> 120x3F)));
        s.append((char)(' '+((i >> 60x3F)));
        s.append((char)(' '+(i & 0x3F)));
        off += 3;
        len -= 3;
      }
      s.append('\n');
    }
    s.append((char)(' ' + len));
    while (len >= 3) {
      i = ((b[off0xFF<< 16((b[off+10xFF<< 8(b[off+20xFF);
      s.append((char)(' '+((i >> 180x3F)));
      s.append((char)(' '+((i >> 120x3F)));
      s.append((char)(' '+((i >> 60x3F)));
      s.append((char)(' '+(i & 0x3F)));
      off += 3;
      len -= 3;
    }
    switch (len) {
    case 2:
      i = ((b[off0xFF<< 16((b[off+10xFF<< 8);
      s.append((char)(' '+((i >> 180x3F)));
      s.append((char)(' '+((i >> 120x3F)));
      s.append((char)(' '+((i >> 60x3F)));
      break;
    case 1:
      i = ((b[off0xFF<< 16);
      s.append((char)(' '+((i >> 180x3F)));
      s.append((char)(' '+((i >> 120x3F)));
      break;
    }
    return s.toString();
  }
  
  public static String encodeUU(byte[] b) {
    return encodeUU(b, 0, b.length);
  }
  
  public static byte[] decodeUU(String s) {
    s = s.replaceAll("\r\n|\r|\n|\u2028|\u2029""\n").trim();
    if (s.startsWith("begin "&& s.endsWith("\nend")) {
      int o = s.indexOf('\n');
      int e = s.length()-4;
      s = s.substring(o, e).trim();
    }
    CharacterIterator it = new StringCharacterIterator(s);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int i = 0, j = 0;
    for (char ch = it.first(); ch != CharacterIterator.DONE && ch != '`'; ch = it.next()) {
      if (ch <= ' ' || ch >= '`'continue;
      while (true) {
        int v = (int)(it.next() ' ');
        if (v >= && v < 64) {
          i = (i << 6| v;
          j++;
          if (j >= 4) {
            out.write(i >> 16);
            out.write(i >> 8);
            out.write(i);
            i = 0; j = 0;
          }
        else {
          break;
        }
      }
    }
    switch (j) {
    case 3:
      out.write(i >> 10);
      out.write(i >> 2);
      break;
    case 2:
      out.write(i >> 4);
      break;
    }
    return out.toByteArray();
  }
  
  private static final char[] xxe = {
    '+','-','0','1','2','3','4','5','6','7','8','9','A','B','C','D',
    'E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T',
    'U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j',
    'k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
  };
  
  public static String encodeXX(byte[] b, int off, int len) {
    int i = 0;
    StringBuffer s = new StringBuffer();
    while (len > 45) {
      s.append(xxe[45]);
      for (int j = 0; j < 45; j += 3) {
        i = ((b[off0xFF<< 16((b[off+10xFF<< 8(b[off+20xFF);
        s.append(xxe[(i >> 180x3F]);
        s.append(xxe[(i >> 120x3F]);
        s.append(xxe[(i >> 60x3F]);
        s.append(xxe[i & 0x3F]);
        off += 3;
        len -= 3;
      }
      s.append('\n');
    }
    s.append(xxe[len]);
    while (len >= 3) {
      i = ((b[off0xFF<< 16((b[off+10xFF<< 8(b[off+20xFF);
      s.append(xxe[(i >> 180x3F]);
      s.append(xxe[(i >> 120x3F]);
      s.append(xxe[(i >> 60x3F]);
      s.append(xxe[i & 0x3F]);
      off += 3;
      len -= 3;
    }
    switch (len) {
    case 2:
      i = ((b[off0xFF<< 16((b[off+10xFF<< 8);
      s.append(xxe[(i >> 180x3F]);
      s.append(xxe[(i >> 120x3F]);
      s.append(xxe[(i >> 60x3F]);
      break;
    case 1:
      i = ((b[off0xFF<< 16);
      s.append(xxe[(i >> 180x3F]);
      s.append(xxe[(i >> 120x3F]);
      break;
    }
    return s.toString();
  }
  
  public static String encodeXX(byte[] b) {
    return encodeXX(b, 0, b.length);
  }
  
  private static int xxd(char i) {
    switch (i) {
    case '+'return 0case '-'return 1case '0'return 2case '1'return 3;
    case '2'return 4case '3'return 5case '4'return 6case '5'return 7;
    case '6'return 8case '7'return 9case '8'return 10case '9'return 11;
    case 'A'return 12case 'B'return 13case 'C'return 14case 'D'return 15;
    case 'E'return 16case 'F'return 17case 'G'return 18case 'H'return 19;
    case 'I'return 20case 'J'return 21case 'K'return 22case 'L'return 23;
    case 'M'return 24case 'N'return 25case 'O'return 26case 'P'return 27;
    case 'Q'return 28case 'R'return 29case 'S'return 30case 'T'return 31;
    case 'U'return 32case 'V'return 33case 'W'return 34case 'X'return 35;
    case 'Y'return 36case 'Z'return 37case 'a'return 38case 'b'return 39;
    case 'c'return 40case 'd'return 41case 'e'return 42case 'f'return 43;
    case 'g'return 44case 'h'return 45case 'i'return 46case 'j'return 47;
    case 'k'return 48case 'l'return 49case 'm'return 50case 'n'return 51;
    case 'o'return 52case 'p'return 53case 'q'return 54case 'r'return 55;
    case 's'return 56case 't'return 57case 'u'return 58case 'v'return 59;
    case 'w'return 60case 'x'return 61case 'y'return 62case 'z'return 63;
    defaultreturn -1;
    }
  }
  
  public static byte[] decodeXX(String s) {
    s = s.replaceAll("\r\n|\r|\n|\u2028|\u2029""\n").trim();
    if (s.startsWith("begin "&& s.endsWith("\nend")) {
      int o = s.indexOf('\n');
      int e = s.length()-4;
      s = s.substring(o, e).trim();
    }
    CharacterIterator it = new StringCharacterIterator(s);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int i = 0, j = 0;
    for (char ch = it.first(); ch != CharacterIterator.DONE && ch != '+'; ch = it.next()) {
      if (xxd(ch0continue;
      while (true) {
        int v = xxd(it.next());
        if (v >= 0) {
          i = (i << 6| v;
          j++;
          if (j >= 4) {
            out.write(i >> 16);
            out.write(i >> 8);
            out.write(i);
            i = 0; j = 0;
          }
        else {
          break;
        }
      }
    }
    switch (j) {
    case 3:
      out.write(i >> 10);
      out.write(i >> 2);
      break;
    case 2:
      out.write(i >> 4);
      break;
    }
    return out.toByteArray();
  }
  
  private static final char[] hqxe = {
    '!','"','#','$','%','&','\'','(',')','*','+',',','-','0','1','2',
    '3','4','5','6','8','9','@','A','B','C','D','E','F','G','H','I',
    'J','K','L','M','N','P','Q','R','S','T','U','V','X','Y','Z','[',
    '`','a','b','c','d','e','f','h','i','j','k','l','m','p','q','r',
  };
  
  public static String encodeBinHex(byte[] b, int off, int len) {
    // phase 1 - RLE
    ByteArrayOutputStream iout = new ByteArrayOutputStream();
    while (len > 0) {
      byte v = b[off++];
      int r = 1;
      len--;
      while (len > && b[off== v && r < 255) {
        off++;
        r++;
        len--;
      }
      if (r > 2) {
        if (v == (byte)0x90) {
          iout.write(0x90);
          iout.write(0);
        else {
          iout.write(v);
        }
        iout.write(0x90);
        iout.write(r);
      else while (r-->0) {
        if (v == (byte)0x90) {
          iout.write(0x90);
          iout.write(0);
        else {
          iout.write(v);
        }
      }
    }
    b = iout.toByteArray();
    off = 0;
    len = b.length;
    // phase 2 - base64 encoding
    int i = 0;
    StringBuffer s = new StringBuffer();
    s.append(':');
    while (len >= 3) {
      i = ((b[off0xFF<< 16((b[off+10xFF<< 8(b[off+20xFF);
      s.append(hqxe[(i >> 180x3F]);
      s.append(hqxe[(i >> 120x3F]);
      s.append(hqxe[(i >> 60x3F]);
      s.append(hqxe[i & 0x3F]);
      off += 3;
      len -= 3;
    }
    switch (len) {
    case 2:
      i = ((b[off0xFF<< 16((b[off+10xFF<< 8);
      s.append(hqxe[(i >> 180x3F]);
      s.append(hqxe[(i >> 120x3F]);
      s.append(hqxe[(i >> 60x3F]);
      break;
    case 1:
      i = ((b[off0xFF<< 16);
      s.append(hqxe[(i >> 180x3F]);
      s.append(hqxe[(i >> 120x3F]);
      break;
    }
    s.append(':');
    return s.toString();
  }
  
  public static String encodeBinHex(byte[] b) {
    return encodeBinHex(b, 0, b.length);
  }
  
  private static int hqxd(char i) {
    switch (i) {
    case '!'return 0case '"'return 1case '#'return 2case '$'return 3;
    case '%'return 4case '&'return 5case '\''return 6case '('return 7;
    case ')'return 8case '*'return 9case '+'return 10case ','return 11;
    case '-'return 12case '0'return 13case '1'return 14case '2'return 15;
    case '3'return 16case '4'return 17case '5'return 18case '6'return 19;
    case '8'return 20case '9'return 21case '@'return 22case 'A'return 23;
    case 'B'return 24case 'C'return 25case 'D'return 26case 'E'return 27;
    case 'F'return 28case 'G'return 29case 'H'return 30case 'I'return 31;
    case 'J'return 32case 'K'return 33case 'L'return 34case 'M'return 35;
    case 'N'return 36case 'P'return 37case 'Q'return 38case 'R'return 39;
    case 'S'return 40case 'T'return 41case 'U'return 42case 'V'return 43;
    case 'X'return 44case 'Y'return 45case 'Z'return 46case '['return 47;
    case '`'return 48case 'a'return 49case 'b'return 50case 'c'return 51;
    case 'd'return 52case 'e'return 53case 'f'return 54case 'h'return 55;
    case 'i'return 56case 'j'return 57case 'k'return 58case 'l'return 59;
    case 'm'return 60case 'p'return 61case 'q'return 62case 'r'return 63;
    defaultreturn -1;
    }
  }
  
  public static byte[] decodeBinHex(String s) {
    // phase 1 - base64 encoding
    CharacterIterator it = new StringCharacterIterator(s.trim());
    ByteArrayOutputStream iout = new ByteArrayOutputStream();
    int i = 0, j = 0;
    char ch = it.first();
    if (ch == ':'ch = it.next();
    while (ch != CharacterIterator.DONE && ch != ':') {
      int v = hqxd(ch);
      if (v >= 0) {
        i = (i << 6| v;
        j++;
        if (j >= 4) {
          iout.write(i >> 16);
          iout.write(i >> 8);
          iout.write(i);
          i = 0; j = 0;
        }
      }
      ch = it.next();
    }
    switch (j) {
    case 3:
      iout.write(i >> 10);
      iout.write(i >> 2);
      break;
    case 2:
      iout.write(i >> 4);
      break;
    }
    byte[] b = iout.toByteArray();
    // phase 2 - RLE
    int off = 0;
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte last = 0;
    while (off < b.length) {
      byte v = b[off++];
      if (v == (byte)0x90 && off < b.length) {
        int r = b[off++0xFF;
        if (r == 0) {
          out.write(last = (byte)0x90);
        else {
          r--;
          while (r-->0) {
            out.write(last);
          }
        }
      else {
        out.write(last = v);
      }
    }
    return out.toByteArray();
  }
  
  public static String encodeASCII85(byte[] b, int off, int len) {
    long i = 0;
    StringBuffer s = new StringBuffer();
    s.append("<~");
    while (len >= 4) {
      i = ((b[off0xFFL<< 24L((b[off+10xFFL<< 16L((b[off+20xFFL<< 8L(b[off+30xFFL);
      if (i == 0) {
        s.append('z');
      else {
        s.append((char)('!' ((i / 5220062585)));
        s.append((char)('!' ((i / 61412585)));
        s.append((char)('!' ((i / 722585)));
        s.append((char)('!' ((i / 8585)));
        s.append((char)('!' (i % 85)));
      }
      off += 4;
      len -= 4;
    }
    switch (len) {
    case 3:
      i = ((b[off0xFFL<< 24L((b[off+10xFFL<< 16L((b[off+20xFFL<< 8L);
      s.append((char)('!' ((i / 5220062585)));
      s.append((char)('!' ((i / 61412585)));
      s.append((char)('!' ((i / 722585)));
      s.append((char)('!' ((i / 8585)));
      break;
    case 2:
      i = ((b[off0xFFL<< 24L((b[off+10xFFL<< 16L);
      s.append((char)('!' ((i / 5220062585)));
      s.append((char)('!' ((i / 61412585)));
      s.append((char)('!' ((i / 722585)));
      break;
    case 1:
      i = ((b[off0xFFL<< 24L);
      s.append((char)('!' ((i / 5220062585)));
      s.append((char)('!' ((i / 61412585)));
      break;
    }
    s.append("~>");
    return s.toString();
  }
  
  public static String encodeASCII85(byte[] b) {
    return encodeASCII85(b, 0, b.length);
  }
  
  public static byte[] decodeASCII85(String s) {
    s = s.trim();
    if (s.startsWith("<~"&& s.endsWith("~>")) {
      s = s.substring(2, s.length()-2).trim();
    }
    CharacterIterator it = new StringCharacterIterator(s);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    long i = 0int j = 0;
    for (char ch = it.first(); ch != CharacterIterator.DONE && ch != '~'; ch = it.next()) {
      if (ch == 'z' && j == 0) {
        out.write(0);
        out.write(0);
        out.write(0);
        out.write(0);
      else if (ch == 'y' && j == 0) {
        out.write(' ');
        out.write(' ');
        out.write(' ');
        out.write(' ');
      else if (ch == 'x' && j == 0) {
        out.write(-1);
        out.write(-1);
        out.write(-1);
        out.write(-1);
      else if (ch >= '!' && ch <= 'u') {
        i = i * 85L (long)(ch - '!');
        j++;
        if (j >= 5) {
          out.write((int)(i >> 24L));
          out.write((int)(i >> 16L));
          out.write((int)(i >> 8L));
          out.write((int)i);
          i = 0; j = 0;
        }
      }
    }
    switch (j) {
    case 4:
      i = i * 85L 84L;
      out.write((int)(i >> 24L));
      out.write((int)(i >> 16L));
      out.write((int)(i >> 8L));
      break;
    case 3:
      i = i * 85L 84L;
      i = i * 85L 84L;
      out.write((int)(i >> 24L));
      out.write((int)(i >> 16L));
      break;
    case 2:
      i = i * 85L 84L;
      i = i * 85L 84L;
      i = i * 85L 84L;
      out.write((int)(i >> 24L));
      break;
    }
    return out.toByteArray();
  }
  
  private static final char[] k85e = {
    '!','#','$','%','&','(',')','+',',','-','.','0','1','2','3','4','5',
    '6','7','8','9',':',';','=','@','A','B','C','D','E','F','G','H','I',
    'J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
    '[',']','^','_','`','a','b','c','d','e','f','g','h','i','j','k','l',
    'm','n','o','p','q','r','s','t','u','v','w','x','y','z','{','}','~',
  };
  
  public static String encodeKreative85(byte[] b, int off, int len) {
    long i = 0;
    StringBuffer s = new StringBuffer();
    while (len >= 4) {
      i = ((b[off0xFFL<< 24L((b[off+10xFFL<< 16L((b[off+20xFFL<< 8L(b[off+30xFFL);
      s.append(k85e[(int)((i / 5220062585)]);
      s.append(k85e[(int)((i / 61412585)]);
      s.append(k85e[(int)((i / 722585)]);
      s.append(k85e[(int)((i / 8585)]);
      s.append(k85e[(int)(i % 85)]);
      off += 4;
      len -= 4;
    }
    switch (len) {
    case 3:
      i = ((b[off0xFFL<< 24L((b[off+10xFFL<< 16L((b[off+20xFFL<< 8L);
      s.append(k85e[(int)((i / 5220062585)]);
      s.append(k85e[(int)((i / 61412585)]);
      s.append(k85e[(int)((i / 722585)]);
      s.append(k85e[(int)((i / 8585)]);
      break;
    case 2:
      i = ((b[off0xFFL<< 24L((b[off+10xFFL<< 16L);
      s.append(k85e[(int)((i / 5220062585)]);
      s.append(k85e[(int)((i / 61412585)]);
      s.append(k85e[(int)((i / 722585)]);
      break;
    case 1:
      i = ((b[off0xFFL<< 24L);
      s.append(k85e[(int)((i / 5220062585)]);
      s.append(k85e[(int)((i / 61412585)]);
      break;
    }
    return s.toString();
  }
  
  public static String encodeKreative85(byte[] b) {
    return encodeKreative85(b, 0, b.length);
  }
  
  public static String encodeLegacy85(byte[] b, int off, int len) {
    long i = 0;
    StringBuffer s = new StringBuffer();
    s.append('<');
    s.append(k85e[(int)((long)len % 85L)]);
    s.append(k85e[(int)(((long)len / 85L85L)]);
    s.append(k85e[(int)(((long)len / 7225L85L)]);
    s.append(k85e[(int)(((long)len / 614125L85L)]);
    s.append(k85e[(int)(((long)len / 52200625L85L)]);
    s.append('>');
    while (len >= 4) {
      i = ((b[off+30xFFL<< 24L((b[off+20xFFL<< 16L((b[off+10xFFL<< 8L(b[off0xFFL);
      s.append(k85e[(int)(i % 85L)]);
      s.append(k85e[(int)((i / 85L85L)]);
      s.append(k85e[(int)((i / 7225L85L)]);
      s.append(k85e[(int)((i / 614125L85L)]);
      s.append(k85e[(int)((i / 52200625L85L)]);
      off += 4;
      len -= 4;
    }
    if (len > 0) {
      switch (len) {
      case 3:
        i = ((b[off+20xFFL<< 16L((b[off+10xFFL<< 8L(b[off0xFFL);
        break;
      case 2:
        i = ((b[off+10xFFL<< 8L(b[off0xFFL);
        break;
      case 1:
        i = (b[off0xFFL);
        break;
      }
      s.append(k85e[(int)(i % 85L)]);
      s.append(k85e[(int)((i / 85L85L)]);
      s.append(k85e[(int)((i / 7225L85L)]);
      s.append(k85e[(int)((i / 614125L85L)]);
      s.append(k85e[(int)((i / 52200625L85L)]);
    }
    return s.toString();
  }
  
  public static String encodeLegacy85(byte[] b) {
    return encodeLegacy85(b, 0, b.length);
  }
  
  private static int k85d(char i) {
    switch (i) {
    case '!'return 0case '#'return 1case '$'return 2case '%'return 3case '&'return 4;
    case '('return 5case ')'return 6case '+'return 7case ','return 8case '-'return 9;
    case '.'return 10case '0'return 11case '1'return 12case '2'return 13case '3'return 14;
    case '4'return 15case '5'return 16case '6'return 17case '7'return 18case '8'return 19;
    case '9'return 20case ':'return 21case ';'return 22case '='return 23case '@'return 24;
    case 'A'return 25case 'B'return 26case 'C'return 27case 'D'return 28case 'E'return 29;
    case 'F'return 30case 'G'return 31case 'H'return 32case 'I'return 33case 'J'return 34;
    case 'K'return 35case 'L'return 36case 'M'return 37case 'N'return 38case 'O'return 39;
    case 'P'return 40case 'Q'return 41case 'R'return 42case 'S'return 43case 'T'return 44;
    case 'U'return 45case 'V'return 46case 'W'return 47case 'X'return 48case 'Y'return 49;
    case 'Z'return 50case '['return 51case ']'return 52case '^'return 53case '_'return 54;
    case '`'return 55case 'a'return 56case 'b'return 57case 'c'return 58case 'd'return 59;
    case 'e'return 60case 'f'return 61case 'g'return 62case 'h'return 63case 'i'return 64;
    case 'j'return 65case 'k'return 66case 'l'return 67case 'm'return 68case 'n'return 69;
    case 'o'return 70case 'p'return 71case 'q'return 72case 'r'return 73case 's'return 74;
    case 't'return 75case 'u'return 76case 'v'return 77case 'w'return 78case 'x'return 79;
    case 'y'return 80case 'z'return 81case '{'return 82case '}'return 83case '~'return 84;
    defaultreturn -1;
    }
  }
  
  public static byte[] decodeKreative85(String s) {
    CharacterIterator it = new StringCharacterIterator(s.trim());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    long i = 0int j = 0;
    for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
      int v = k85d(ch);
      if (v >= 0) {
        i = i * 85L + v;
        j++;
        if (j >= 5) {
          out.write((int)(i >> 24L));
          out.write((int)(i >> 16L));
          out.write((int)(i >> 8L));
          out.write((int)i);
          i = 0; j = 0;
        }
      }
    }
    switch (j) {
    case 4:
      i = i * 85L 84L;
      out.write((int)(i >> 24L));
      out.write((int)(i >> 16L));
      out.write((int)(i >> 8L));
      break;
    case 3:
      i = i * 85L 84L;
      i = i * 85L 84L;
      out.write((int)(i >> 24L));
      out.write((int)(i >> 16L));
      break;
    case 2:
      i = i * 85L 84L;
      i = i * 85L 84L;
      i = i * 85L 84L;
      out.write((int)(i >> 24L));
      break;
    }
    return out.toByteArray();
  }
  
  public static byte[] decodeLegacy85(String s) {
    int targetLength = -1;
    s = s.trim();
    if (s.length() >= && s.charAt(0== '<' && s.charAt(6== '>') {
      targetLength = 
        k85d(s.charAt(1)) +
        k85d(s.charAt(2)) 85 +
        k85d(s.charAt(3)) 7225 +
        k85d(s.charAt(4)) 614125 +
        k85d(s.charAt(5)) 52200625;
      s = s.substring(7).trim();
    }
    CharacterIterator it = new StringCharacterIterator(s);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    long i = 0int j = 0long k = 1;
    for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
      int v = k85d(ch);
      if (v >= 0) {
        i += v * k;
        j++;
        k *= 85;
        if (j >= 5) {
          out.write((int)i);
          out.write((int)(i >> 8L));
          out.write((int)(i >> 16L));
          out.write((int)(i >> 24L));
          i = 0; j = 0; k = 1;
        }
      }
    }
    if (j > 0) {
      out.write((int)i);
      out.write((int)(i >> 8L));
      out.write((int)(i >> 16L));
      out.write((int)(i >> 24L));
    }
    if (targetLength >= 0) {
      byte[] b = out.toByteArray();
      byte[] bt = new byte[targetLength];
      for (int x = 0; x < targetLength && x < b.length; x++) {
        bt[x= b[x];
      }
      return bt;
    else {
      return out.toByteArray();
    }
  }
  
  public static void main(String[] argsthrows IOException {
    boolean decode = false;
    int mode = 0;
    for (String arg : args) {
      if (arg.equals("-e")) {
        decode = false;
      else if (arg.equals("-d")) {
        decode = true;
      else if (arg.equals("-b64")) {
        mode = 0;
      else if (arg.equals("-hqx")) {
        mode = 1;
      else if (arg.equals("-a85")) {
        mode = 2;
      else if (arg.equals("-l85")) {
        mode = 3;
      else if (arg.equals("-k85")) {
        mode = 4;
      else if (arg.equals("-uue")) {
        mode = 5;
      else if (arg.equals("-xxe")) {
        mode = 6;
      else if (arg.equals("--")) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buf = new byte[1048576];
        int len = 0;
        while ((len = System.in.read(buf)) >= 0) {
          out.write(buf, 0, len);
        }
        out.close();
        if (decode) {
          switch (mode) {
          case 0: System.out.println(new String(decodeBase64(out.toString())))break;
          case 1: System.out.println(new String(decodeBinHex(out.toString())))break;
          case 2: System.out.println(new String(decodeASCII85(out.toString())))break;
          case 3: System.out.println(new String(decodeLegacy85(out.toString())))break;
          case 4: System.out.println(new String(decodeKreative85(out.toString())))break;
          case 5: System.out.println(new String(decodeUU(out.toString())))break;
          case 6: System.out.println(new String(decodeXX(out.toString())))break;
          }
        else {
          switch (mode) {
          case 0: System.out.println(encodeBase64(out.toByteArray()))break;
          case 1: System.out.println(encodeBinHex(out.toByteArray()))break;
          case 2: System.out.println(encodeASCII85(out.toByteArray()))break;
          case 3: System.out.println(encodeLegacy85(out.toByteArray()))break;
          case 4: System.out.println(encodeKreative85(out.toByteArray()))break;
          case 5: System.out.println(encodeUU(out.toByteArray()))break;
          case 6: System.out.println(encodeXX(out.toByteArray()))break;
          }
        }
      else if (decode) {
        switch (mode) {
        case 0: System.out.println(new String(decodeBase64(arg)))break;
        case 1: System.out.println(new String(decodeBinHex(arg)))break;
        case 2: System.out.println(new String(decodeASCII85(arg)))break;
        case 3: System.out.println(new String(decodeLegacy85(arg)))break;
        case 4: System.out.println(new String(decodeKreative85(arg)))break;
        case 5: System.out.println(new String(decodeUU(arg)))break;
        case 6: System.out.println(new String(decodeXX(arg)))break;
        }
      else {
        switch (mode) {
        case 0: System.out.println(encodeBase64(arg.getBytes()))break;
        case 1: System.out.println(encodeBinHex(arg.getBytes()))break;
        case 2: System.out.println(encodeASCII85(arg.getBytes()))break;
        case 3: System.out.println(encodeLegacy85(arg.getBytes()))break;
        case 4: System.out.println(encodeKreative85(arg.getBytes()))break;
        case 5: System.out.println(encodeUU(arg.getBytes()))break;
        case 6: System.out.println(encodeXX(arg.getBytes()))break;
        }
      }
    }
  }
}

   
    
    
    
    
  














Related examples in the same category
1.Base64 encoding/decoding.
2.Decodes Base64 data into octects
3.Implementation of MIME's Base64 encoding and decoding conversions.
4.Encode/decode for RFC 2045 Base64 as defined by RFC 2045
5.Encode/decode for RFC 2045 Base64 as defined by RFC 2045, N. Freed and N. Borenstein.
6.Encodes and decodes to and from Base64 notation.
7.Encodes hex octects into Base64
8.Helper class to provide Base64 encoding routines.
9.Represents a collection of 64 boolean (on/off) flags.
10.byte to be tested if it is Base64 alphabet
11.to Base64
12.One of the fastest implementation of the Base64 encoding. Jakarta and others are slower
13.array of byte to encode
14.Codes number up to radix 62
15.A Base64 Encoder/Decoder
16.A fast and memory efficient class to encode and decode to and from BASE64 in full accordance with RFC 2045
17.BASE64 encoder implementation
18.Base-64 Encoder - translates from base-64 text into binary
19.Base64 Character encoder as specified in RFC1113
20.Base64 Utils
21.Base64 encoder/decoder
22.Base64 from by Funambol, Inc.
23.Convert to hex from byte arrays and back
24.Converting hexadecimal strings
25.Encode and decode data in Base64 format as described in RFC 1521
26.Encode and decode integers, times, and internationalized strings to and from popular binary formats
27.Encoding of raw bytes to base64-encoded characters, and decoding of base64 characters to raw bytes
28.Performs Base64 encoding and/or decoding
29.Provides Base64 encoding and decoding as defined by RFC 2045
30.Provides Base64 encoding and decoding with URL and filename safe alphabet as defined by RFC 3548, section 4.
31.Provides utility methods to Base64 encode data
32.QP Decoder Stream
33.QP Encoder Stream
34.A class to decode Base64 streams and strings.
35.A class to encode Base64 streams and strings.
36.Encodes binary data to plain text as Base64
37.A very fast and memory efficient class to encode and decode to and from BASE64 in full accordance with RFC 2045.
38.Decodes InputStreams which contain binary data in base64 form
39.Base 64 Converter
40.Base64 from org.cspoker.common.util
41.Base64 converted from code at http://iharder.sourceforge.net/base64/
42.Encodes and decodes to and from Base64 notation.
43.Simple Base64 string decoding function
44.Class to represent unsigned 64-bit numbers.
45.A Base64 encoder/decoder.
46.Provides Base64 encoding and decoding
47.Code to read and write Base64-encoded text.
48.Base32 encoding/decoding class.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.