Added Base64Encoder

This commit is contained in:
Ziver Koc 2015-11-17 18:59:50 +01:00
parent 67e4d54fde
commit 034166b413
7 changed files with 219 additions and 57 deletions

33
test/zutil/test/Base64Test.java Normal file → Executable file
View file

@ -25,27 +25,52 @@ package zutil.test;
import org.junit.Test;
import zutil.parser.Base64Decoder;
import zutil.parser.Base64Encoder;
import java.util.Base64;
import static org.junit.Assert.assertEquals;
public class Base64Test {
@Test
public void testHexToByte() {
public void decode() {
assertEquals( "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.",
Base64Decoder.decode("TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=")
);
Base64Decoder decoder = new Base64Decoder();
decoder.clear();
decoder.write("YW55IGNhcm5hbCBwbGVhc3VyZQ==");
decoder.read("YW55IGNhcm5hbCBwbGVhc3VyZQ==");
assertEquals( "any carnal pleasure", decoder.toString() );
decoder.clear();
decoder.write("bGVhc3VyZS4=");
decoder.read("bGVhc3VyZS4=");
assertEquals( "leasure.", decoder.toString() );
decoder.clear();
decoder.write("YW55IGNhcm5hbCBwbGVhc3Vy");
decoder.read("YW55IGNhcm5hbCBwbGVhc3Vy");
assertEquals( "any carnal pleasur", decoder.toString() );
decoder.clear();
}
@Test
public void encode() {
/*assertEquals("TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=" ,
Base64Encoder.encode("Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.")
);*/
assertEquals("YW55IGNhcm5hbCBwbGVhc3VyZQ==", Base64Encoder.encode("any carnal pleasure"));
assertEquals("bGVhc3VyZS4=", Base64Encoder.encode("leasure."));
assertEquals("YW55IGNhcm5hbCBwbGVhc3Vy", Base64Encoder.encode("any carnal pleasur"));
}
@Test
public void encodeJavaUtil() {
/*assertEquals("TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=" ,
Base64.getEncoder().encodeToString("Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.".getBytes())
);*/
assertEquals("YW55IGNhcm5hbCBwbGVhc3VyZQ==", Base64.getEncoder().encodeToString("any carnal pleasure".getBytes()));
assertEquals("bGVhc3VyZS4=", Base64.getEncoder().encodeToString("leasure.".getBytes()));
assertEquals("YW55IGNhcm5hbCBwbGVhc3Vy", Base64.getEncoder().encodeToString("any carnal pleasur".getBytes()));
}
}