| 1 |
|
package org.itracker.services.util; |
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
|
|
|
| 0% |
Uncovered Elements: 90 (90) |
Complexity: 22 |
Complexity Density: 0.4 |
|
| 25 |
|
public class Base64Coder { |
| 26 |
|
|
| 27 |
|
|
| 28 |
|
private static char[] map1 = new char[64]; |
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 29 |
|
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 |
|
|
| 37 |
|
private static byte[] map2 = new byte[128]; |
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 38 |
|
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 |
|
|
| 44 |
|
|
| 45 |
|
@param |
| 46 |
|
@return |
| 47 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 48 |
0
|
public static String encodeString (String s) {... |
| 49 |
0
|
return new String(encode(s.getBytes())); } |
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
@param |
| 55 |
|
@return |
| 56 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 57 |
0
|
public static char[] encode (byte[] in) {... |
| 58 |
0
|
return encode(in,in.length); } |
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
@param |
| 64 |
|
@param |
| 65 |
|
@return |
| 66 |
|
|
|
|
|
| 0% |
Uncovered Elements: 30 (30) |
Complexity: 2 |
Complexity Density: 0.1 |
|
| 67 |
0
|
public static char[] encode (byte[] in, int iLen) {... |
| 68 |
0
|
int oDataLen = (iLen*4+2)/3; |
| 69 |
0
|
int oLen = ((iLen+2)/3)*4; |
| 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 |
|
|
| 89 |
|
@param |
| 90 |
|
@return |
| 91 |
|
@throws |
| 92 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 93 |
0
|
public static String decodeString (String s) {... |
| 94 |
0
|
return new String(decode(s)); } |
| 95 |
|
|
| 96 |
|
|
| 97 |
|
|
| 98 |
|
@param |
| 99 |
|
@return |
| 100 |
|
@throws |
| 101 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 102 |
0
|
public static byte[] decode (String s) {... |
| 103 |
0
|
return decode(s.toCharArray()); } |
| 104 |
|
|
| 105 |
|
|
| 106 |
|
|
| 107 |
|
|
| 108 |
|
@param |
| 109 |
|
@return |
| 110 |
|
@throws |
| 111 |
|
|
|
|
|
| 0% |
Uncovered Elements: 49 (49) |
Complexity: 15 |
Complexity Density: 0.48 |
|
| 112 |
0
|
public 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 |
|
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 142 |
0
|
private Base64Coder() {}... |
| 143 |
|
|
| 144 |
|
} |