base64binary

L

lord.zoltar

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.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

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 ??
 
R

Roedy Green

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
 
L

lord.zoltar

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?
 
G

Gordon Beaton

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

--
 
C

Christian

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?
The Object Output Stream itself writes these bytes..
just use the FileOutputStream ... it has exactly the method you need..
void write(byte[] b)
 
R

Roedy Green

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
 
R

Roedy Green

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.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top