• Post Reply Bookmark Topic Watch Topic
  • New Topic

Parsing and Decoding Base64 Data  RSS feed

 
Michael Scott
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm modifying some Java code so that it can run an existing web service. The web service reportedly will send a response message in a Base64 format. The following needs to be done this Base64 response.

1.) Parse it to extract an 'attachment' component (probably a DOM object).

2.) Decode this Base64 'attachment' component to the type ByteArrayOutputStream.

I might be mistaken in that the decoding might need to occur before the parsing.

I'd appreciate any suggestions or references to documentation on how this might be done. Thanks.
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can use commons-codec to encode/decode data

more: http://jakarta.apache.org/commons/codec/userguide.html
 
Michael Scott
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Are there any recommendations on where to find a Java API that parses Base64 data?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Base 64 support is actually built into the core Java classes. It is just not well documented, you need to google for "sun.misc.BASE64Encoder" or "sun.misc.BASE64Decoder" to get some code examples. Furthermore, even though these are sun packaged classes, they are also available in other JVMs -- like the IBM JVM -- mainly due to the classes being in somewhat common usage.

Henry
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean apart from the link that Ajay Singh already provided?
 
Michael Scott
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The link that has been provided appears to be limited to encoding and decoding, but on how to parse Base64 data. It's my understanding that the Base64 data which is returned to my Java program first requires that a component be extracted. The extracted component would then be decoded. I've looked in the Java API and in Google searches, but have not come upon a means of parsing Base64 data.

This might be a simple matter, but I'm not proficient in Java. I posted this under the Java 'advanced' forum because as I mentioned in my original post, the data is being returned from a web service which employs 'wsdl' files. These files identify the schema in the Base64 response data - this includes the component that will be need to be extracted. I was therefore wondering whether there might be some unique means of identifying this component in the Base64 response. In any case, I still expected that the Base64 data would first need to be parsed to identify this component and it's still not apparent how that might be done.

Thanks.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand what you mean by "parsing" Base64 data. All you do with Base64 is encode data into it and decode data from it. You might have to parse something else to get data that has been encoded in Base64 but that's a separate issue.

But you already phrased it that way in your first post. You had it right there. That's exactly what you need to do. But you're using phrases like "reportedly" and "It's my understanding". So I think you are getting ahead of yourself here, if you haven't actually seen the specs yet.

My guess at the requirements: you are going to get an XML document. It will contain a text node somewhere that contains Base64-encoded data. Your task will be to parse the XML and find that text node, then decode it into a byte array. If that's correct (and I think it is) then you need an XML parser and a Base64 decoder. Java already contains the former and you've been pointed at the latter.
 
Michael Scott
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul ... you described the requirement very accurately. However, in looking through the Java API, I haven't located Java's XML parser. Is there some particular class or method that you can refer me to?

Thanks.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was assuming you were using Java 1.4 or later. The XML parsers are in the package javax.xml.parsers if you are. But I don't think you can learn about handling XML in Java just by reading the API documentation about that package. I could suggest this online book as a good starting point.
 
Michael Scott
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul. The documentation you referred to should be very helpful.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:
Base 64 support is actually built into the core Java classes. It is just not well documented, you need to google for "sun.misc.BASE64Encoder" or "sun.misc.BASE64Decoder" to get some code examples. Furthermore, even though these are sun packaged classes, they are also available in other JVMs -- like the IBM JVM -- mainly due to the classes being in somewhat common usage.

Henry


Right. See: Why Developers Should Not Write Programs That Call 'sun' Packages.

It is safer to use a library like Apache Commons Codec than to use an undocumented API of which you don't know if it's still going to exist in future versions of Java, or in Java versions of other vendors than Sun.

So I would not use the sun.misc.BASE64Encoder etc. if you need this for any kind of "serious" piece of software.

The Apache library can encode as well as decode Base64.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I recall from reading the spec long ago, there is a bit of flexibility in implementing Base64 encoding. If you see any hint of that, you may want to use the very same encode/decode package as the folks who send you the XML. If you don't see any such caveats ... I likely imagined it.
 
D Rog
Ranch Hand
Posts: 472
Linux Objective C Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you use any 3rd party implementation you should check license requirements first unless you are not doing commercial product. For base64 you can find a lot of good public domain implementations so you do not need to deal with Apache license.
 
D Rog
Ranch Hand
Posts: 472
Linux Objective C Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by safer ? you mean Sun's implementation can have bugs, but Apache's not? or you mean Sun can die, but Apache not? or you mean Sun will rewrite tons of own code to avoid usage of own implementation to switch to Apache's one?
 
Michael Scott
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In responding my post, Paul made the following comment "I was assuming you were using Java 1.4 or later. The XML parsers are in the package javax.xml.parsers if you are."

My Java code actually runs under Domino which uses its own JDK Ver. 1.3. I'd appreciate any recommendation for an XML parser that is compatable with JDK 1.3.

Thanks.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by D Rog:
What do you mean by safer ? you mean Sun's implementation can have bugs, but Apache's not? or you mean Sun can die, but Apache not? or you mean Sun will rewrite tons of own code to avoid usage of own implementation to switch to Apache's one?

No, I am not talking about bugs ofcourse.

I mean that Sun's API is undocumented and that Sun can decide in any new version of Java to change the implementation, rename the classes or packages, or even remove the implementation. They are free to do any of those things and that's what Sun's document warns you about. It is a non-public API, and Sun can do with it whatever they please for whatever reason. Sun has no obligation to keep a private API exactly as it is in future versions of Java.

So if you use any of those Sun classes, there's a chance that your software might stop working on a future version of Java.

Also, if you use a different JRE than Sun's JRE (for example by IBM, HP or BEA) then there is a big chance that the Sun-specific classes will not exist, and your program will not work. By using Sun-specific classes, you're tying your program to a specific version of the JRE from a specific vendor. You don't want to do that.

Apache's library has a public API, and because of that at least you have some guarantee that the developers of the library will make an effort to keep the API the same in newer versions, because they know that backward compatibility is important for their customers. Also, you can use Apache's library on any JRE by any vendor.
[ February 09, 2007: Message edited by: Jesper Young ]
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use Sun's documented API to do Base64 encoding/decoding. See this post from the Sun forums.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!