views:

33

answers:

3

Hello,

I'm searching for a library that can encode data in URL, because using plain "&param=value is" inefficient.

I'm looking for a library that will accept a defined set of parameters and will encode it into a format that is safe to sent over HTTP GET request. I will be using this to send tracking events from my player to the backend.

I remember reading at some blog post that Chrome is using a nice library to encode data when it send statistics back to google servers, that library was recently open sourced by Google. Alas, I can't find it, and I don't remember it's name :)

Thanks for helping, Maxim.

A: 

Do you mean URL encoding? Then you can use java.net.URLEncoder#encode(String,String) for URL encoding a String with a given encoding (like "UTF-8").

abhin4v
A: 

What do you mean by

using plain "&param=value is" inefficient.

?

It seems to me that any library that you use will, under the covers, be simply using string manipulation to build the URL, so Im struggling to see why you think using such a library will be more efficient.

What measurements have you taken to test your hypothesis? What performance levels do you need?

Visage
I mean that the "param" can be skipped as I know what value positions. Helios answer outlines the basics.
Maxim Veksler
A: 

If you want a quick and dirty solution I can propose this:

If:

  • your set of params is fixed (or you can indicate the set of params using some short id)

Do:

  1. Remove the param name overhead concatenate all in a String, byte[] or similar in a way you can decode it later :) I should avoid Java Serialization because it includes a lot of info. I'd try something simple like DataOutputStream.write*Type*. Simple to write and simple to read.
  2. Remove data redundancy Zip it. Use java.util.zip.GZipOutputStream. That's why I prefer the step 1 to generate a byte[] representation!
  3. Make it URL-friendly. Here you'll increase the size in bytes you need. So try to do it with the least overhead. I should try with Base64. It's zipped so it's not redundant (it uses the least space posible). So the only chance is to use a representation that don't use too much space. Hexa would double the size. But Base64 only adds 33%.

Theese steps are not complicated.

  1. Uses a custom class. Maybe the alternative serialization method so it's easier to invoke.
  2. Uses java.util.zip.GZipOutput/InputStream. It's straight-forward.
  3. Uses some Base64 encoding/decoding library. Straigh-forward too.

Hope it helps!

helios
One slight problem with Base-64 is its use of `/`. There are similar, more URL-friendly schemes.
MSalters
Good point.....
helios
Hi helios, what you describe is straight what I need, obviously I can implement that myself but the lib I'm talking about (that google released) already does all that. Regarding Base64. There is a version of Base64 that is url safe, for ex. see this implementation http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Base64.html#encodeBase64URLSafe(byte[])
Maxim Veksler