Merge branch 'Jalview-BH/JAL-3026-JAL-3063-JAXB' of
[jalview.git] / srcjar / javajs / util / Base64.java
1 // Version 1.0a
2 // Copyright (C) 1998, James R. Weeks and BioElectroMech.
3 // Visit BioElectroMech at www.obrador.com.  Email James@obrador.com.
4
5 // See license.txt for details about the allowed used of this software.
6 // This software is based in part on the work of the Independent JPEG Group.
7 // See IJGreadme.txt for details about the Independent JPEG Group's license.
8
9 // This encoder is inspired by the Java Jpeg encoder by Florian Raemy,
10 // studwww.eurecom.fr/~raemy.
11 // It borrows a great deal of code and structure from the Independent
12 // Jpeg Group's Jpeg 6a library, Copyright Thomas G. Lane.
13 // See license.txt for details.
14
15 package javajs.util;
16
17
18 public class Base64 {
19
20   //                              0         1         2         3         4         5         6
21   //                              0123456789012345678901234567890123456789012345678901234567890123
22   private static String base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
23   //                              41----------------------5A
24   //                                                        61----------------------7A
25   //                                                                                  30------39    
26   //                                                                                            2B  
27   //                                                                                             2F
28   //                                                 alternative "URL-SAFE"     2D and 5F       -_
29   
30   private static int[] decode64 = new int[] {
31     0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,      //0x00-0x0F
32     0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,      //0x10-0x1F
33     0,0,0,0,     0,0,0,0,     0,0,0,62,    0,62,0,63,    //0x20-0x2F
34     52,53,54,55, 56,57,58,59, 60,61,0,0,   0,0,0,0,      //0x30-0x3F
35     0,0,1,2,     3,4,5,6,     7,8,9,10,    11,12,13,14,  //0x40-0x4F
36     15,16,17,18, 19,20,21,22, 23,24,25,0,  0,0,0,63,     //0x50-0x5F
37     0,26,27,28,  29,30,31,32, 33,34,35,36, 37,38,39,40,  //0x60-0x6F
38     41,42,43,44, 45,46,47,48, 49,50,51,0,  0,0,0,0,      //0x70-0x7F
39   };
40     
41 //  public static void write(byte[] bytes, OutputChannel out) {
42 //    SB sb = getBase64(bytes);
43 //    int len = sb.length();
44 //    byte[] b = new byte[1];
45 //    for (int i = 0; i < len; i++) {
46 //      b[0] = (byte) sb.charAt(i);
47 //      out.write(b, 0, 1);
48 //    }
49 //  }
50
51   public static byte[] getBytes64(byte[] bytes) {
52     return getBase64(bytes).toBytes(0, -1);
53   }
54
55   /**
56    * 
57    * @param bytes
58    * @return BASE64-encoded string, without ";base64,"
59    */
60   public static SB getBase64(byte[] bytes) {
61     long nBytes = bytes.length;
62     SB sout = new SB();
63     if (nBytes == 0)
64       return sout;
65     for (int i = 0, nPad = 0; i < nBytes && nPad == 0;) {
66       if (i % 75 == 0 && i != 0)
67         sout.append("\r\n");
68       nPad = (i + 2 == nBytes ? 1 : i + 1 == nBytes ? 2 : 0);
69       int outbytes = ((bytes[i++] << 16) & 0xFF0000)
70           | ((nPad == 2 ? 0 : bytes[i++] << 8) & 0x00FF00)
71           | ((nPad >= 1 ? 0 : (int) bytes[i++]) & 0x0000FF);
72       //System.out.println(Integer.toHexString(outbytes));
73       sout.appendC(base64.charAt((outbytes >> 18) & 0x3F));
74       sout.appendC(base64.charAt((outbytes >> 12) & 0x3F));
75       sout.appendC(nPad == 2 ? '=' : base64.charAt((outbytes >> 6) & 0x3F));
76       sout.appendC(nPad >= 1 ? '=' : base64.charAt(outbytes & 0x3F));
77     }
78     return sout;
79   }
80
81   //Note: Just a simple decoder here. Nothing fancy at all
82   //      Because of the 0s in decode64, this is not a VERIFIER
83   //      Rather, it may decode even bad Base64-encoded data
84   //
85   // Bob Hanson 4/2007
86   
87   public static byte[] decodeBase64(String strBase64) {
88     int nBytes = 0;
89     int ch;
90     int pt0 = strBase64.indexOf(";base64,") + 1;
91     if (pt0 > 0)
92       pt0 += 7;
93     char[] chars64 = strBase64.toCharArray();
94     int len64 = chars64.length;
95     if (len64 == 0)
96       return new byte[0];
97     for (int i = len64; --i >= pt0;)
98       nBytes += ((ch = chars64[i] & 0x7F) == 'A' || decode64[ch] > 0 ? 3 : 0);
99     nBytes = nBytes >> 2;
100     byte[] bytes = new byte[nBytes];
101     int offset = 18;
102     for (int i = pt0, pt = 0, b = 0; i < len64; i++) {
103       if (decode64[ch = chars64[i] & 0x7F] > 0 || ch == 'A' || ch == '=') {
104         b |= decode64[ch] << offset;
105         //System.out.println(chars64[i] + " " + decode64[ch] + " " + offset + " " + Integer.toHexString(b));
106         offset -= 6;
107         if (offset < 0) {
108           bytes[pt++] = (byte) ((b & 0xFF0000) >> 16);
109           if (pt < nBytes)
110             bytes[pt++] = (byte) ((b & 0xFF00) >> 8);
111           if (pt < nBytes)
112             bytes[pt++] = (byte) (b & 0xFF);
113           offset = 18;
114           b =  0;
115         }
116       }
117     }
118     return bytes;
119   }    
120 }