Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to encode a binary and text file into a string

Posted on 2004-08-16
7
Medium Priority
?
229 Views
Last Modified: 2010-03-31
I'm using the java commons base64 api.  Here is what i need to do.  My program creates a pdf and html file.  I need to base64 encode each file into a string and then store it into a text field in a database.  and at a later date retrieve it and decode it and store it back into the file type it should be.   But i'm not quite to sure.  Here is what i have so far:

                   File file = new File( "D:/projects/base64code/test.pdf" );
                   File fileOut = new File( "D:/projects/base64code/test_out.pdf" );

                   byte[] scratch = new byte[8*1024];

                   FileInputStream in = new FileInputStream(file);
                   FileOutputStream out = new FileOutputStream(fileOut);

//                    These next two give you super turbo performance speed
                   BufferedInputStream bin = new BufferedInputStream(in);
                   BufferedOutputStream bout = new BufferedOutputStream(out);

//                    If you don't loop, then you're probably losing data
                   int consumed = 0;
                   do {
                        consumed = bin.read( scratch );
                        if ( consumed > 0 )
                        {
                            bout.write( scratch, 0, consumed );
                        }
                   } while ( consumed >= 0 );
                  
                   byte[] encodedArray = Base64.encodeBase64( scratch );
                  
                  
                   byte[] decodeArray = new byte[8*1024];
                   decodeArray = Base64.decodeBase64(encodedArray);


So what i'm trying to do in this example is:
1.  Encode the pdf file.
2.  Retrieve the string value so i can store it into a column in a database table ( I'm not sure how to do that here)
3.  Take the string value (just to test that i can convert back to a byte and decode) and convert into a byte then save to the appropriate file name.

Thanks in advance.
0
Comment
Question by:borg48
  • 3
7 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 11817953
You need to encode your byte array. Lets say that your byte array that holds the pdf is called "pdfInBytes" you can do:

String encodedString = BASE64Encoder().encode(pdfInBytes);

To decode it back to a bytes array you use:

bytes [] decodedPDF = BASE64Decoder().decodeBuffer(endodedString);
0
 
LVL 35

Expert Comment

by:girionis
ID: 11818079
You might also want to take a look here: http://javaalmanac.com/egs/java.net/Base64.html
0
 
LVL 9

Expert Comment

by:Venci75
ID: 11818082
If the only reason for encoding a binary file is that you have to store it into a database, you can use BLOB field
0
 

Author Comment

by:borg48
ID: 11821682
How would i then take the byte:
bytes [] decodedPDF = BASE64Decoder().decodeBuffer(endodedString);

and output it back to a file?
0
 
LVL 35

Accepted Solution

by:
girionis earned 375 total points
ID: 11827855
You can use a FileOutputStream and use one of its write methods that take a byte array as a parameter.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question