View Javadoc

1   package es.caib.signatura.impl;
2   
3   public class Base64 {
4   
5       // Codificar BASE 64
6       static private byte base64Array [] = 
7       	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".getBytes();
8   
9       public static byte [] toBase64 (byte source [], int src, int len)
10      {
11      	int i ;
12      	int max = src+len;
13      	int j = 0;
14      	int resultLength = 4 * ( (len + 2) / 3);
15      	byte result[] = new byte [2 + resultLength];
16      	
17      	for ( i = src; i < max; i+=3)
18      	{
19      		int c1, c2, c3;
20      		int index;
21      		c1 = source[i];
22      		if ( i+1 < max ) c2 = (char) source[i+1];
23      		else c2 = 0;
24      		if ( i+2 < max ) c3 = (char) source [i+2];
25      		else c3 = 0;
26      		index = (c1 & 0xfc) >> 2;
27      		result [j++] = base64Array[ index ];
28      		index = ((c1 & 0x03) << 4) | ((c2 & 0xf0) >> 4);
29      		result [j++] = base64Array[ index ];
30      		if ( i+1 >= max ) result [j ++ ] = '=';
31      		else
32      		{
33      			index = ((c2 & 0x0f) << 2) | ((c3 & 0xc0) >> 6);
34      			result [j++] = base64Array[ index ];
35      		}
36      		if ( i+2 >= max ) result [ j ++ ] = '=';
37      		else
38      		{
39      			index = (c3 & 0x3f);
40      			result [j++] = base64Array[ index ];
41      		}
42      	}
43      	result [j++] = '\r';
44      	result [j++] = '\n';
45      	return result;
46      }
47  
48  }