Pregunta

Estoy intentando implementar archivos TMX en Android y esperaba que alguien pudiera ayudarme.Basado en el guía tmx, para obtener los GID tengo que

primero base64 decodifica la cadena, luego comprime los datos resultantes si el atributo de compresión está configurado en "gzip" como en el ejemplo anterior.Finalmente, puede leer 4 bytes a la vez para cada GID desde el principio del flujo de datos hasta el final.

Creo que descubrí la decodificación base64 y 'gunzip' pero el resultado del siguiente código es 27,0,0,0 repetido.Creo que se supone que la salida es

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

¡Gracias!

 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();
 }
 }
¿Fue útil?

Solución

no usar Reader¡s para cualquier cosa menos datos de personajes!

Usar una DataInput para leer los números enteros.Decora tu GZIPInputStream con un DataInputStream y use readInt.

Si los enteros son little-endian, necesitarás invertir el orden de los bytes para el tipo.Java utiliza el orden de bytes de la red (big-endian).Para números enteros, esto se puede hacer con Entero.reverseBytes.

Puede imprimir los valores hexadecimales usando:

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

como leer todo int valores de una secuencia de longitud arbitraria:

Un mecanismo sería utilizar el available() método que estimados el número de bytes restantes:

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);
}

En el caso general, available() no es un mecanismo confiable.Pero puedes aumentar tu transmisión con un búfer para verificar la disponibilidad de datos:

  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();
    }
  }

Otros consejos

    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();
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top