Pokazywanie postów oznaczonych etykietą Base64. Pokaż wszystkie posty
Pokazywanie postów oznaczonych etykietą Base64. Pokaż wszystkie posty

24 lipca 2009

kodowanie i dekodowanie znaków Base64[JAVA]

Poniższa klasa koduje i dekoduje Stringa algorytmem Base64:

package webster.jdownloader;

/**
* A Base64 Encoder/Decoder.
*
* <p>
* This class is used to encode and decode data in Base64 
format as described in RFC 1521.
*
* <p>
* This is "Open Source" software and released under
 the <a href="http://www.gnu.org/licenses/lgpl.html">
GNU/LGPL</a> license.<br>
* It is provided "as is" without warranty of any kind.<br>
* Copyright 2003: Christian d'Heureuse, 
Inventec Informatik AG, Switzerland.<br>
* Home page: <a 
href="http://www.source-code.biz">www.source-code.biz</a><br>
*
* <p>
* Version history:<br>
* 2003-07-22 Christian d'Heureuse (chdh): Module created.<br>
* 2005-08-11 chdh: Lincense changed from GPL to LGPL.<br>
* 2006-11-21 chdh:<br>
*  &nbsp; Method encode(String) renamed to encodeString(String).<br>
*  &nbsp; Method decode(String) renamed to decodeString(String).<br>
*  &nbsp; New method encode(byte[],int) added.<br>
*  &nbsp; New method decode(String) added.<br>
*/

public class Base64 {

// Mapping table from 6-bit nibbles to Base64 characters.
private static char[]    map1 = new char[64];
static {
int i=0;
for (char c='A'; c<='Z'; c++) map1[i++] = c;
for (char c='a'; c<='z'; c++) map1[i++] = c;
for (char c='0'; c<='9'; c++) map1[i++] = c;
map1[i++] = '+'; map1[i++] = '/'; }

// Mapping table from Base64 characters to 6-bit nibbles.
private static byte[]    map2 = new byte[128];
static {
for (int i=0; i<map2.length; i++) map2[i] = -1;
for (int i=0; i<64; i++) map2[map1[i]] = (byte)i; }

/**
* Encodes a string into Base64 format.
* No blanks or line breaks are inserted.
* @param s  a String to be encoded.
* @return   A String with the Base64 encoded data.
*/