Link to home
Start Free TrialLog in
Avatar of harishalwala
harishalwalaFlag for Afghanistan

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.
SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

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:


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"));
            int av = ba.available();
              byte[] buffer = new byte[av];
            int n = ba.read(buffer, 0, av);
            ba.close();
            ByteArrayInputStream bas = new ByteArrayInputStream(buffer);


            String encode = new sun.misc.BASE64Encoder().encode(buffer);

           System.out.println(encode);

            byte [] buffer1 = new sun.misc.BASE64Decoder().decodeBuffer(encode);

                  BufferedOutputStream baout = new BufferedOutputStream(new FileOutputStream("test2.xls"));
                  baout.write(buffer1,0,buffer1.length);
                  baout.close();






        }   catch(Exception ex){
            ex.printStackTrace();
        }

    }

}

Open in new window



Encoded String printouut (truncated):
0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAAAAABAAAAAQAAAAAAAAAA
EAAAHAAAAAEAAAD+////AAAAAAAAAAD/////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////9
////HwAAAAMAAAAEAAAABQAAAAYAAAAHAAAACAAAAAkAAAAKAAAACwAAAAwAAAANAAAADgAAAA8A
AAAQAAAAEQAAABIAAAATAAAAFAAAABUAAAAWAAAAFwAAABgAAAAZAAAAGgAAABsAAAD+/////v//
/x4AAAD+/////v//////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////1IA

Open in new window

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.
Avatar of CEHJ
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();
        }





    }

}

Open in new window

Check this..

"I tried even with Apache common codec jar Base64. I didnot understand how to use with InputStreams"
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of harishalwala

ASKER

closed
:)