Fixed bug in JSONObjectOutputStream and Base64Encoder

This commit is contained in:
Ziver Koc 2015-11-18 17:25:14 +01:00
parent 034166b413
commit 66bebc6f52
4 changed files with 7 additions and 7 deletions

View file

@ -140,7 +140,7 @@ public class Base64Encoder {
case (byte)(46 & 0xff): return 'u'; case (byte)(46 & 0xff): return 'u';
case (byte)(47 & 0xff): return 'v'; case (byte)(47 & 0xff): return 'v';
case (byte)(48 & 0xff): return 'w'; case (byte)(48 & 0xff): return 'w';
case (byte)(49 & 0xff): return 'w'; case (byte)(49 & 0xff): return 'x';
case (byte)(50 & 0xff): return 'y'; case (byte)(50 & 0xff): return 'y';
case (byte)(51 & 0xff): return 'z'; case (byte)(51 & 0xff): return 'z';

View file

@ -137,7 +137,7 @@ public class JSONObjectInputStream extends InputStream implements ObjectInput, C
return readPrimitive(type, json); return readPrimitive(type, json);
} }
else if(type.isArray()){ else if(type.isArray()){
if(type.getComponentType() == Byte.class) if(type.getComponentType() == byte.class)
return Base64Decoder.decodeToByte(json.getString()); return Base64Decoder.decodeToByte(json.getString());
else{ else{
Object array = Array.newInstance(type.getComponentType(), json.size()); Object array = Array.newInstance(type.getComponentType(), json.size());

View file

@ -88,7 +88,7 @@ public class JSONObjectOutputStream extends OutputStream implements ObjectOutput
// Add an array // Add an array
else if(objClass.isArray()){ else if(objClass.isArray()){
// Special case for byte arrays // Special case for byte arrays
if(objClass.getComponentType() == byte[].class) { if(objClass.getComponentType() == byte.class) {
root = new DataNode(DataType.String); root = new DataNode(DataType.String);
root.set(Base64Encoder.encode((byte[])obj)); root.set(Base64Encoder.encode((byte[])obj));
} }

View file

@ -54,9 +54,9 @@ public class Base64Test {
@Test @Test
public void encode() { public void encode() {
/*assertEquals("TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=" , 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.") 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("YW55IGNhcm5hbCBwbGVhc3VyZQ==", Base64Encoder.encode("any carnal pleasure"));
assertEquals("bGVhc3VyZS4=", Base64Encoder.encode("leasure.")); assertEquals("bGVhc3VyZS4=", Base64Encoder.encode("leasure."));
@ -65,9 +65,9 @@ public class Base64Test {
@Test @Test
public void encodeJavaUtil() { public void encodeJavaUtil() {
/*assertEquals("TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=" , 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()) 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("YW55IGNhcm5hbCBwbGVhc3VyZQ==", Base64.getEncoder().encodeToString("any carnal pleasure".getBytes()));
assertEquals("bGVhc3VyZS4=", Base64.getEncoder().encodeToString("leasure.".getBytes())); assertEquals("bGVhc3VyZS4=", Base64.getEncoder().encodeToString("leasure.".getBytes()));