Reputation: 507
I am trying to send a Json object (as a String) across a socket connection in java for a game I am making. This data is sent back and fourth very quickly (~30milliseconds per transfer) I don't have a whole lot of bandwidth, so I am trying to optimize my data transfer as best as I can.
here is what I have so far to take input and compress it (it also decompresses) this is my SSCCE I think thats the abbreviation?
public static void main(String[] args)
{
String compressedString = compress("{"playerX":"64","playerY":"224","playerTotalHealth":"100","playerCurrentHealth":"100","playerTotalMana":"50","playerCurrentMana":"50","playerExp":"0","playerExpTNL":"20","playerLevel":"1","points":"0","strength":"1","dexterity":"1","constitution":"1","intelligence":"1","wisdom":"1","items":["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24"],"currentMapX":"0","currentMapY":"0","playerBody":"1","playerHair":"6","playerClothes":"7"}");
System.out.println(compressedString);
System.out.println(compressedString.length());
System.out.println();
String decompressString = decompress(compressedString);
System.out.println(decompressString);
System.out.println(decompressString.length());
}
public static String compress(String str) {
String outStr = "";
try
{
if (str == null || str.length() == 0)
{
return str;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes());
gzip.close();
outStr = out.toString("ISO-8859-1");
}
catch(IOException e1)
{
e1.printStackTrace();
}
return outStr;
}
public static String decompress(String str) {
String outStr = "";
try{
if (str == null || str.length() == 0)
{
return str;
}
GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(str.getBytes("ISO-8859-1")));
BufferedReader bf = new BufferedReader(new InputStreamReader(gis, "ISO-8859-1"));
String line;
while ((line=bf.readLine())!=null)
{
outStr += line;
}
}
catch(IOException e2)
{
e2.printStackTrace();
}
return outStr;
}
The String I am trying to compress is 404 characters roughly, and it gets compressed to 256. I was wondering if I could create my own charset of the alphabet and a few special characters, and use that instead of all ISO-8859-1
How would I use that newly created charset to compress the String I inputted in the main method speed isn't nescessarily an issue since I am only compressing very small String objects, as long as it doesn't take >200 milliseconds. bear with me this compression stuff is all very new to me, I am sorry if I sound ignorant.
If I try to just use ASCII instead of ISO-8859-1, the string will compress but it will not decompress and throws ZipException - not in GZIP format
Upvotes: 0
Views: 2878
Reputation: 112394
If your JSON objects are similar from transaction to transaction, you could consider using previous objects as a dictionary for compression for the next object, where those objects are also retained at the other end to use as the dictionary for decompression. See the setDictionary method. You can fill up to 32K of dictionary with 32 of your objects (sans a few bytes).
Upvotes: 2
Reputation: 3344
If you don't have the bandwidth to spare for JSON, and your codebase isn't changing to much, you may want to use your own binary encoding using DataInput/OutputStream
Upvotes: 1