私が作成しているゲームのために、Java のソケット接続を介して Json オブジェクトを (文字列として) 送信しようとしています。このデータは非常に迅速に (転送ごとに約 30 ミリ秒) 送り返され、4 番目に送信されます。
これは、これまでに入力して圧縮するために持っているものです(解凍も行います)これは私のSSCCEです。それは略語だと思いますか?
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;
}
圧縮しようとしている文字列はおおよそ 404 文字で、256 文字に圧縮されます。アルファベットといくつかの特殊文字の独自の文字セットを作成し、すべての ISO-8859-1 の代わりにそれを使用できるかどうか疑問に思っていました
新しく作成された文字セットを使用して、メイン メソッドに入力した文字列を圧縮する方法は、200 ミリ秒を超えない限り、非常に小さな文字列オブジェクトのみを圧縮しているため、必ずしも問題ではありません。この圧縮は私にとってまったく新しいものです。無知に聞こえたら申し訳ありません。
ISO-8859-1 の代わりに ASCII を使用しようとすると、文字列は圧縮されますが、圧縮解除されず、ZipException がスローされます - GZIP 形式ではありません