aspose file tools*
The moose likes Servlets and the fly likes Storing Byte Arrays in Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Storing Byte Arrays in "Web-Strings"?" Watch "Storing Byte Arrays in "Web-Strings"?" New topic
Author

Storing Byte Arrays in "Web-Strings"?

Andreas Schildbach
Ranch Hand

Joined: Jan 22, 2003
Posts: 34
Hello everyone,
is there a facility in the J2SE to efficiently store Byte Arrays in Strings that can be put into URL Parameters, HTTP Cookies, etc.
I'm thinking of something like Base64 or UUEncode, but without that Line-Splitting and with the end "==" marker.
Regards,
Andreas
Byron Estes
Ranch Hand

Joined: Feb 21, 2002
Posts: 313
Maybe if you told us why you need this we could point you to an alternate solution.
In general, since the cookies and parameters are text, you'll need to create delimited Strings that you can parse into arrays. Just be careful what you use for a delimiter since some characters have special meaning in HTTP. Also be sure to run encode and decode on the String at appropriate times to avoid embedded control characters that with also cause HTTP to fail. When I've done this I have a class that handles this for me that I reuse as needed. I even did this one time to di encrypted, multiple parameters in cookies.
If you've never used it, the StringTokenizer is great for the purpose of parsing a String into substrings. Lot's easier than finding a instance, getting it's index and calling the substring with the right indexes. Only thing to watch out for and in your case probably wouldn't even be an issue, but if you ever try to use a multiple character delimiter with StringTokenizer you'll think it's not working correctly because you'll get what you THINK are odd results. Actually with a little research you'll realize it's working fine, the issue is your understanding of the class. It only works with single String character delimiters. If you pass it a string with multiple characters, it will take it and not blink. It will, however, assume that you mean that each character in that String is a delimiter, not the sequence of characters. So...it will break whenever a single character in the delimiter String appears in the String value to be parsed.


Byron Estes<br />Sun Certified Enterprise Architect<br />Senior Consulant<br />Blackwell Consulting Services<br />Chicago, IL<br /><a href="http://www.bcsinc.com" target="_blank" rel="nofollow">www.bcsinc.com</a>
Andreas Schildbach
Ranch Hand

Joined: Jan 22, 2003
Posts: 34
All I want to do is store arbitrary byte arrays in Cookies and URLs that are embedded in mails (the byte arrays are the result of encrypted, serialized data objects).
For example, a link in a mail could look like this:
http://www.mydomain.tld/action.html?object=F66G7hgfu56UGO55
Since URLs in Mails should not exceed 70-80 chars, I want to store the byte array as efficiently as possible.
My question: Is there any facility in J2SE 1.4/J2EE 1.4 to support this idea?
Likewise, is there any facility to compress small Strings (~40 chars) that mostly contain alphanumeric characters? I tried java.util.zip.GZIPOutputStream, but such small data chunks tend to get longer instead of shorter.
Regards,
Andreas
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
The Jakarta Commons project has a toolkit with Base64 - the "Codec" download.
For your compressing small strings problem, I suggest you return to first principles and look for redundancy - choice of a compression technique depends highly on the actual data - zip is optimized for large files and builds big sequence tables.
If you have a decent sample of text, do counts of character and digraph frequency to start.
Bill
Byron Estes
Ranch Hand

Joined: Feb 21, 2002
Posts: 313
Let me get this right, you want to serialize a java object and store it in a cookie or as part of a URL?
If so, I'd go back to my original question which was looking more for the business problem you're trying to solve, not the technical solution you're trying to make work.
If you explain that more clearly, I think you'll get better responses.
If you do by chance end up managing to serialize objects and attaching them as you've indicated, I think you're going to be disappointed with the performance. Also, watch out serialized object "gotcha". If you change the class and recompile (...unless you make a few provisions for it), you're application won't recognize old versions of your serialized objects.
Good Luck.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Storing Byte Arrays in "Web-Strings"?