文字列base64は、リトルエンディアン4バイトintからjava intにgzip圧縮されていません

StackOverflow https://stackoverflow.com/questions/3262348

質問

私はANDROIDでTMXファイルを実装しようとしていますが、誰かが助けてくれることを望んでいました。に基づく TMXガイド, 、GIDを取得するために私はする必要があります

最初にbase64で文字列をデコードし、上記の例のようにcompression属性が"gzip"に設定されている場合、結果のデータをgunzipします。最後に、データストリームの先頭から最後まで、各GIDごとに一度に4バイトを読み取ることができます。

私はbase64デコードと'gunzip'を理解したと思いますが、以下のコードの結果は27,0,0,0繰り返しです。出力は次のようになっていると思います

(0,0) (1,0) (2,0) (3,0) (0,1) (1,1) (2,1) (3,1) (0,2) (1,2) (2,2) (3,2)

ありがとう!

 public static void main( String[] args )
 {
 String myString = "H4sIAAAAAAAAAO3NoREAMAgEsLedAfafE4+s6l0jolNJiif18tt/Fj8AAMC9ARtYg28AEAAA";

 byte[] decode = Base64.decodeBase64(myString);

 ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(decode); 
 GZIPInputStream gzipInputStream;

 int read;
 try
 {
      gzipInputStream = new GZIPInputStream(byteArrayInputStream);

      InputStreamReader inputStreamReader = new InputStreamReader(gzipInputStream);
      BufferedReader bufferedReader = new BufferedReader(inputStreamReader, 4);

      while ( ( read = bufferedReader.read() ) != -1 )
      {
           System.out.println("read :" + read);
      }
 }
 catch (IOException e)
 {
      e.printStackTrace();
 }
 }
役に立ちましたか?

解決

使用しないでください Reader文字データ以外の何のためのs!

Aを使う DataInput 整数を読み取る。あなたを飾る GZIPInputStream を使って DataInputStream と使用 readInt.

Intがリトルエンディアンの場合は、その型のバイトの順序を逆にする必要があります。Javaはネットワークバイトオーダ(ビッグエンディアン)を使用します。整数の場合、これは次のように行うことができます 整数。リバースバイト.

次を使用して16進値を印刷できます:

System.out.format("%08x", (int) n);

すべてを読む方法 int 任意の長さのストリームからの値:

一つのメカニズムは、使用することであろう available() メソッドは、 見積もり 残りのバイト数:

byte[] ints = {0x00, 0x00, 0x00, (byte) 0xFF,
               (byte) 0xAA, (byte) 0xBB, (byte) 0xEE, (byte) 0xFF};
ByteArrayInputStream array = new ByteArrayInputStream(ints);
DataInputStream data = new DataInputStream(array);
while(data.available() > 0) {
  int reversed = Integer.reverseBytes(data.readInt());
  System.out.format("%08x%n", reversed);
}

一般的なケースでは, available() 信頼できるメカニズムではありません。ただし、バッファを使用してストリームを拡張して、データの可用性を確認することができます:

  public static void main(String[] args) throws IOException {
    byte[] ints = {0x00, 0x00, 0x00, (byte) 0xFF,
                    (byte) 0xAA, (byte) 0xBB, (byte) 0xEE, (byte) 0xFF};
    ByteArrayInputStream array = new ByteArrayInputStream(ints);
    BufferedInputStream buffer = new BufferedInputStream(array);
    DataInputStream data = new DataInputStream(buffer);
    while(hasData(data)) {
      int reversed = Integer.reverseBytes(data.readInt());
      System.out.format("%08x%n", reversed);
    }
  }

  public static boolean hasData(InputStream in) throws IOException {
    in.mark(1);
    try {
      return in.read() != -1;
    } finally {
      in.reset();
    }
  }

他のヒント

    public static void main( String[] args )
{
    String myString = "H4sIAAAAAAAAA+3X2QrCMBAF0OKbSwWrCC4vdV/+w///JKeQgWFIatJ0SfE+HJDGonfaTJI8y7IlORkLkotrZ3I0SjUux2zfCSX/h/7tX/T9/5jjQl7kalSfb4nk2JAJ2Y78eUzJjMwjcnAtQnHt2sixIgVZR+TgWtjca8a4dvy+viNyaE1ycC2kh+WaZqvdUDmeppYxQp5DV313KG3n2JE9OYw8R2m5rw+638WuHzw/mrzjMWS/81k/ZM6U5ofsdz7rh8yZ0vxw9VtX361yfkzOlOZHSC/Xa4NtDgw1PwAgDvdSre/eGot7aV1PHgPbWTW1/a5vDn1WTW1f4ptDn1Vd+5KUyf1IkdXvS1LmOqti7wEAAAAAAAAAAF37AlFWzCEQJwAA";

    byte[] decode = Base64.decodeBase64(myString);

    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(decode); 

    int read;
    try
    {
        GZIPInputStream gzipInputStream = new GZIPInputStream(byteArrayInputStream);

        DataInputStream dataInputStream = new DataInputStream(gzipInputStream);

        for ( int i = 0; i < decode.length; i++ )
        {
            read = dataInputStream.readInt();
            int rRead = Integer.reverseBytes(read);
            System.out.println("read :" + rRead);

        }
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}
.
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top