Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

base64binary

Thread view: 
lord.zoltar@gmail.com - 18 Oct 2007 18:53 GMT
Hello,
I have a class that receives some data encoded as a base64binary
string from a SOAP request, and stores it in a byte array. I need to
write it to a binary file on disc. I thought that just writing the
byte array with a FileOutputStream would do this, but this seems to
result in writing the base64 encoding which was received.
How do I convert this array into the binary data?

Thanks.
Arne Vajhøj - 19 Oct 2007 00:18 GMT
> I have a class that receives some data encoded as a base64binary
> string from a SOAP request, and stores it in a byte array. I need to
> write it to a binary file on disc. I thought that just writing the
> byte array with a FileOutputStream would do this, but this seems to
> result in writing the base64 encoding which was received.
> How do I convert this array into the binary data?

Decode it before writing.

Base64 suppurt are in the Java Mail API.

Code snippets:

   public static byte[] b64encode(byte[] b) throws Exception {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      OutputStream b64os = MimeUtility.encode(baos, "base64");
      b64os.write(b);
      b64os.close();
      return baos.toByteArray();
   }
   public static byte[] b64decode(byte[] b) throws Exception {
      ByteArrayInputStream bais = new ByteArrayInputStream(b);
      InputStream b64is = MimeUtility.decode(bais, "Base64");
      byte[] tmp = new byte[b.length];
      int n = b64is.read(tmp);
      byte[] res = new byte[n];
      System.arraycopy(tmp, 0, res, 0, n);
      return res;
   }

Arne

PS: Don't your web service toolkit decode automatically ??
lord.zoltar@gmail.com - 19 Oct 2007 18:36 GMT
On Oct 18, 7:18 pm, Arne Vajh?j <a...@vajhoej.dk> wrote:

> PS: Don't your web service toolkit decode automatically ??

Actually, it was! I've gotten around the whole encoding/decoding
issue, but for some reason it's not writing the bytes out correctly.
I'm using an ObjectOutputStream to write to a FileOutputStream. The
problem is there is extra data at the begining of the file. Not sure
why. Nothing is prepending the array with extra data... Is there
something better than ObjectOutputStream to write an array of bytes?
Gordon Beaton - 19 Oct 2007 18:39 GMT
> Actually, it was! I've gotten around the whole encoding/decoding
> issue, but for some reason it's not writing the bytes out correctly.
> I'm using an ObjectOutputStream to write to a FileOutputStream.

If you've got an array of bytes that you want to write verbatim to the
file, then you should use the write methods provided by the
FileOutputStream itself. Don't wrap it in any kind of Writer, and
certainly not an ObjectOutputStream.

/gordon

--
Christian - 19 Oct 2007 18:43 GMT
lord.zoltar@gmail.com schrieb:

>> PS: Don't your web service toolkit decode automatically ??
>
[quoted text clipped - 4 lines]
> why. Nothing is prepending the array with extra data... Is there
> something better than ObjectOutputStream to write an array of bytes?

The Object Output Stream itself writes these bytes..
just use the FileOutputStream ... it has exactly the method you need..
void write(byte[] b)
Roedy Green - 21 Oct 2007 08:08 GMT
>The Object Output Stream itself writes these bytes..
>just use the FileOutputStream ... it has exactly the method you need..
>void write(byte[] b)

The whole point of base64 is to make sure all bytes are "printable".
If you write them out with an ObjectOutputStream you will sandwich
them in some unprintable binary housekeeping.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 21 Oct 2007 08:07 GMT
>I'm using an ObjectOutputStream to write to a FileOutputStream. The
>problem is there is extra data at the begining of the file.

I suspect you don't know what ObjectOutputStream is.  It is
serialisation format . See
http://mindprod.com/jgloss/serialization.html

Anything for base64 would more likely go through

DataOutPutStream to a  ByteArrayStream, converted to Base64 encoded
bytes then sent out with a plain OutputStream.

Since Base64 is not supported as one of the standard encodings, you
can't put it together into a unified outputstream.

see http://mindprod.com/jgloss/base64.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 19 Oct 2007 11:32 GMT
>I have a class that receives some data encoded as a base64binary
>string from a SOAP request, and stores it in a byte array. I need to
>write it to a binary file on disc. I thought that just writing the
>byte array with a FileOutputStream would do this, but this seems to
>result in writing the base64 encoding which was received.
>How do I convert this array into the binary data?

see http://mindprod.com/jgloss/base64.html

Normally you would decode it back to binary before writing it since
the binary is more compact.

See http://mindprod.com/applet/fileio.html
for how to use a DataOutputStream
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.