harishalwala
asked on
Base64 encoding & decoding
I am working with Base64 encoding & Decoding of all types of files, I get the stream from socket. I tried using Base64 encoder & Base64Decoder class from Java2s website. It works fine encoding & decoding for the txt file & png files. But when I try with doc/xls/pdf it fails, it says file corrupted, after decoding.
I tried even with Apache common codec jar Base64. I didnot understand how to use with InputStreams, I see it works for String.
Any help please.
I tried even with Apache common codec jar Base64. I didnot understand how to use with InputStreams, I see it works for String.
Any help please.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I tested the above code for several types of files - .xls, .jpg, .txt - and it worked fine for all of them
It is not a good practice to use classes from sun.misc. Because they are not documented, and they can be changed/removed at any time.
Also, reading the whole file in memory to pass to coder/decoder is not a good idea, as there can be a possibility of out of memory exceptions.
Also, reading the whole file in memory to pass to coder/decoder is not a good idea, as there can be a possibility of out of memory exceptions.
Right on both counts!
The code below will allow you to read as big files as you want, though it is not often that you'll probably
be reading files which will casue you out of memory exceptions on the previous version.
(make sure to increase BUFFER_SIZE to something reasonable - I made it very small for experiments).
My IDE imedniately recognized BASE64Encoder in Java 6
For BASE64InputStream you need to carry additional jar.
I';m doing Java for almost as many years as Java exists and don't recall a single case where
I needed to remove some older class from my old program because it stopped being actually supported.
import sun.misc.BASE64Encoder;
import java.io.*;
public class EncodeDecodeBinary {
public static void main(String[] args) {
try{
BufferedInputStream ba = new BufferedInputStream(new FileInputStream("test1.xls"));
BufferedOutputStream baout = new BufferedOutputStream(new FileOutputStream("test4.xls"));
int BUFFER_SIZE=100;
byte[] buffer = new byte[BUFFER_SIZE];
while(true){
int av = ba.available();
if(av == 0)break;
int readB = Math.min(av, BUFFER_SIZE);
int n = ba.read(buffer, 0, readB);
byte [] buffer2 = new byte[readB];
for(int j=0; j<readB; j++)buffer2[j]=buffer[j];
String encode = new sun.misc.BASE64Encoder().encode(buffer2);
System.out.println(encode);
byte [] buffer1 = new sun.misc.BASE64Decoder().decodeBuffer(encode);
baout.write(buffer1,0,buffer1.length);
}
ba.close();
baout.close();
} catch(Exception ex){
ex.printStackTrace();
}
}
}
Check this..
"I tried even with Apache common codec jar Base64. I didnot understand how to use with InputStreams"
"I tried even with Apache common codec jar Base64. I didnot understand how to use with InputStreams"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
closed
:)
This code reads Excel file encodes it into string then
decodes the string and writes it back to another file -
which I opened with Excel and found it the same:
Open in new window
Encoded String printouut (truncated):
Open in new window