Clover Coverage Report - itracker
Coverage timestamp: Tue May 1 2012 16:42:12 CEST
55   144   22   7.86
28   69   0.4   7
7     3.14  
1    
 
 
  Base64Coder       Line # 25 55 22 0% 0.0
 
No Tests
 
1    package org.itracker.services.util;
2    /**
3    * A Base64 Encoder/Decoder.
4    *
5    * <p>
6    * This class is used to encode and decode data in Base64 format as described in RFC 1521.
7    *
8    * <p>
9    * This is "Open Source" software and released under the <a href="http://www.gnu.org/licenses/lgpl.html">GNU/LGPL</a> license.<br>
10    * It is provided "as is" without warranty of any kind.<br>
11    * Copyright 2003: Christian d'Heureuse, Inventec Informatik AG, Switzerland.<br>
12    * Home page: <a href="http://www.source-code.biz">www.source-code.biz</a><br>
13    *
14    * <p>
15    * Version history:<br>
16    * 2003-07-22 Christian d'Heureuse (chdh): Module created.<br>
17    * 2005-08-11 chdh: Lincense changed from GPL to LGPL.<br>
18    * 2006-11-21 chdh:<br>
19    * &nbsp; Method encode(String) renamed to encodeString(String).<br>
20    * &nbsp; Method decode(String) renamed to decodeString(String).<br>
21    * &nbsp; New method encode(byte[],int) added.<br>
22    * &nbsp; New method decode(String) added.<br>
23    */
24   
 
25    public class Base64Coder {
26   
27    // Mapping table from 6-bit nibbles to Base64 characters.
28    private static char[] map1 = new char[64];
 
29    toggle static {
30    int i=0;
31    for (char c='A'; c<='Z'; c++) map1[i++] = c;
32    for (char c='a'; c<='z'; c++) map1[i++] = c;
33    for (char c='0'; c<='9'; c++) map1[i++] = c;
34    map1[i++] = '+'; map1[i++] = '/'; }
35   
36    // Mapping table from Base64 characters to 6-bit nibbles.
37    private static byte[] map2 = new byte[128];
 
38    toggle static {
39    for (int i=0; i<map2.length; i++) map2[i] = -1;
40    for (int i=0; i<64; i++) map2[map1[i]] = (byte)i; }
41   
42    /**
43    * Encodes a string into Base64 format.
44    * No blanks or line breaks are inserted.
45    * @param s a String to be encoded.
46    * @return A String with the Base64 encoded data.
47    */
 
48  0 togglepublic static String encodeString (String s) {
49  0 return new String(encode(s.getBytes())); }
50   
51    /**
52    * Encodes a byte array into Base64 format.
53    * No blanks or line breaks are inserted.
54    * @param in an array containing the data bytes to be encoded.
55    * @return A character array with the Base64 encoded data.
56    */
 
57  0 togglepublic static char[] encode (byte[] in) {
58  0 return encode(in,in.length); }
59   
60    /**
61    * Encodes a byte array into Base64 format.
62    * No blanks or line breaks are inserted.
63    * @param in an array containing the data bytes to be encoded.
64    * @param iLen number of bytes to process in <code>in</code>.
65    * @return A character array with the Base64 encoded data.
66    */
 
67  0 togglepublic static char[] encode (byte[] in, int iLen) {
68  0 int oDataLen = (iLen*4+2)/3; // output length without padding
69  0 int oLen = ((iLen+2)/3)*4; // output length including padding
70  0 char[] out = new char[oLen];
71  0 int ip = 0;
72  0 int op = 0;
73  0 while (ip < iLen) {
74  0 int i0 = in[ip++] & 0xff;
75  0 int i1 = ip < iLen ? in[ip++] & 0xff : 0;
76  0 int i2 = ip < iLen ? in[ip++] & 0xff : 0;
77  0 int o0 = i0 >>> 2;
78  0 int o1 = ((i0 & 3) << 4) | (i1 >>> 4);
79  0 int o2 = ((i1 & 0xf) << 2) | (i2 >>> 6);
80  0 int o3 = i2 & 0x3F;
81  0 out[op++] = map1[o0];
82  0 out[op++] = map1[o1];
83  0 out[op] = op < oDataLen ? map1[o2] : '='; op++;
84  0 out[op] = op < oDataLen ? map1[o3] : '='; op++; }
85  0 return out; }
86   
87    /**
88    * Decodes a string from Base64 format.
89    * @param s a Base64 String to be decoded.
90    * @return A String containing the decoded data.
91    * @throws IllegalArgumentException if the input is not valid Base64 encoded data.
92    */
 
93  0 togglepublic static String decodeString (String s) {
94  0 return new String(decode(s)); }
95   
96    /**
97    * Decodes a byte array from Base64 format.
98    * @param s a Base64 String to be decoded.
99    * @return An array containing the decoded data bytes.
100    * @throws IllegalArgumentException if the input is not valid Base64 encoded data.
101    */
 
102  0 togglepublic static byte[] decode (String s) {
103  0 return decode(s.toCharArray()); }
104   
105    /**
106    * Decodes a byte array from Base64 format.
107    * No blanks or line breaks are allowed within the Base64 encoded data.
108    * @param in a character array containing the Base64 encoded data.
109    * @return An array containing the decoded data bytes.
110    * @throws IllegalArgumentException if the input is not valid Base64 encoded data.
111    */
 
112  0 togglepublic static byte[] decode (char[] in) {
113  0 int iLen = in.length;
114  0 if (iLen%4 != 0) throw new IllegalArgumentException ("Length of Base64 encoded input string is not a multiple of 4.");
115  0 while (iLen > 0 && in[iLen-1] == '=') iLen--;
116  0 int oLen = (iLen*3) / 4;
117  0 byte[] out = new byte[oLen];
118  0 int ip = 0;
119  0 int op = 0;
120  0 while (ip < iLen) {
121  0 int i0 = in[ip++];
122  0 int i1 = in[ip++];
123  0 int i2 = ip < iLen ? in[ip++] : 'A';
124  0 int i3 = ip < iLen ? in[ip++] : 'A';
125  0 if (i0 > 127 || i1 > 127 || i2 > 127 || i3 > 127)
126  0 throw new IllegalArgumentException ("Illegal character in Base64 encoded data.");
127  0 int b0 = map2[i0];
128  0 int b1 = map2[i1];
129  0 int b2 = map2[i2];
130  0 int b3 = map2[i3];
131  0 if (b0 < 0 || b1 < 0 || b2 < 0 || b3 < 0)
132  0 throw new IllegalArgumentException ("Illegal character in Base64 encoded data.");
133  0 int o0 = ( b0 <<2) | (b1>>>4);
134  0 int o1 = ((b1 & 0xf)<<4) | (b2>>>2);
135  0 int o2 = ((b2 & 3)<<6) | b3;
136  0 out[op++] = (byte)o0;
137  0 if (op<oLen) out[op++] = (byte)o1;
138  0 if (op<oLen) out[op++] = (byte)o2; }
139  0 return out; }
140   
141    // Dummy constructor.
 
142  0 toggleprivate Base64Coder() {}
143   
144    } // end class Base64Coder