0

私が作成しているゲームのために、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 形式ではありません

4

2 に答える 2

1

JSON に割り当てる帯域幅がなく、コードベースがあまり変更されていない場合は、DataInput/OutputStreamを使用して独自のバイナリ エンコーディングを使用することをお勧めします。

于 2013-11-13T06:14:44.860 に答える
1

JSON オブジェクトがトランザクションごとに類似している場合、前のオブジェクトを次のオブジェクトの圧縮用の辞書として使用することを検討できます。これらのオブジェクトは、圧縮解除用の辞書として使用するために、反対側でも保持されます。setDictionaryメソッドを参照してください。最大 32K の辞書を 32 個のオブジェクトで埋めることができます (数バイトを除く)。

于 2013-11-13T20:54:26.503 に答える