BASE64Encoder and BASE64Decoder

V

Vallabha

Hello All,

I am trying to understand how BASE64Encoder() and BASE64Decoder()
work. I wrote a small sample for that:

==========================================

import java.io.IOException;
import java.util.*;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Base64 {
public static void main(String[] args)throws IOException{
String a = new String("TEST");
System.out.println("Original string: " + a);
String encode = new sun.misc.BASE64Encoder().encode(a.getBytes());
System.out.println("Encoded String: " + encode);
byte[] decode = new sun.misc.BASE64Decoder().decodeBuffer(encode);
System.out.println("Decoded String: " + decode);
}
}

========================================
Original string: TEST
Encoded String: VEVTVA==
Decoded String: [B@addbf1
=========================================

Here I was expecting the original string after the decoding. However
decoded string is different from the original one. Now how do I get
back my original string.

Any clue on this will be of great help.

Thanks
-Vallabha
 
S

Stefan Rybacki

Vallabha said:
Hello All,

I am trying to understand how BASE64Encoder() and BASE64Decoder()
work. I wrote a small sample for that:

==========================================

import java.io.IOException;
import java.util.*;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Base64 {
public static void main(String[] args)throws IOException{
String a = new String("TEST");
System.out.println("Original string: " + a);
String encode = new sun.misc.BASE64Encoder().encode(a.getBytes());
System.out.println("Encoded String: " + encode);
byte[] decode = new sun.misc.BASE64Decoder().decodeBuffer(encode);
System.out.println("Decoded String: " + decode);
}
}

========================================
Original string: TEST
Encoded String: VEVTVA==
Decoded String: [B@addbf1
=========================================

Here I was expecting the original string after the decoding. However
decoded string is different from the original one. Now how do I get
back my original string.

What if you do this

System.out.println("Decoded String: " + String.valueOf(decode));
 
J

Joshua Cranmer

Vallabha said:
byte[] decode = new sun.misc.BASE64Decoder().decodeBuffer(encode);
System.out.println("Decoded String: " + decode);

What type is decode?
Here I was expecting the original string after the decoding. However
decoded string is different from the original one. Now how do I get
back my original string.

Do a little experiment: Tell me what (new byte[0]).toString() is.

Converting an array to a string via Object.toString() is not going to
give you what you expect... you'll want to actually pass it into a
byte[]->String converter, like |new String(decode)| or
|String.valueOf(decode)|.

What you're actually seeing is Object's toString implementation:
getClass().getName() + "@" + Integer.toHexString(hashCode());
 
A

Arne Vajhøj

Vallabha said:
I am trying to understand how BASE64Encoder() and BASE64Decoder()
work. I wrote a small sample for that:

==========================================

import java.io.IOException;
import java.util.*;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Base64 {
public static void main(String[] args)throws IOException{
String a = new String("TEST");
System.out.println("Original string: " + a);
String encode = new sun.misc.BASE64Encoder().encode(a.getBytes());
System.out.println("Encoded String: " + encode);
byte[] decode = new sun.misc.BASE64Decoder().decodeBuffer(encode);
System.out.println("Decoded String: " + decode);
}
}

========================================
Original string: TEST
Encoded String: VEVTVA==
Decoded String: [B@addbf1
=========================================

Here I was expecting the original string after the decoding. However
decoded string is different from the original one. Now how do I get
back my original string.

Other have already pointed out the type issue.

I will just add that:
- I think you should use the Java EE javax.mail.internet classes
instead of the sun.misc classes
- I think you should consider an explicit encoding

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top