1

我正在尝试在 Android 中实现 TMX 文件,我希望有人能提供帮助。基于TMX 指南,为了获得 GID,我必须

首先对字符串进行base64解码,然后如果压缩属性设置为“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();
 }
 }
4

2 回答 2

1

不要将Readers 用于除字符数据之外的任何内容!

使用 aDataInput读取整数。用 a装饰你GZIPInputStreamDataInputStream并使用readInt

如果整数是 little-endian,则需要反转该类型的字节顺序。Java 使用网络字节顺序(big-endian)。对于整数,可以使用Integer.reverseBytes来完成。

您可以使用以下方法打印十六进制值:

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();
    }
  }
于 2010-07-16T07:53:38.420 回答
0
    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();
    }
}
于 2010-07-16T09:25:53.033 回答