开发者

Encoding data in URL for efficient transport

开发者 https://www.devze.com 2023-01-15 06:37 出处:网络
I\'m searching for a library that can encode data in URL, because using plain \"&param=value is\" ineffici开发者_如何转开发ent.

I'm searching for a library that can encode data in URL, because using plain "&param=value is" ineffici开发者_如何转开发ent.

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.


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").


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?


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.writeType. 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!

0

精彩评论

暂无评论...
验证码 换一张
取 消