String base64 decodificada onu-gziped de little-endian 4-byte, int em java int
-
16-09-2020 - |
Pergunta
Eu estou tentando implementar TMX arquivos no Android e eu estava esperando que alguém poderia ajudar.Com base no TMX guia, a fim de obter o GID do que eu tenho para
primeiro base64 decodificar a seqüência de caracteres e, em seguida, gunzip os dados obtidos, se o atributo de compressão é definida como "gzip", como no exemplo acima.Finalmente, você pode ler 4 bytes por vez para cada GID desde o início do fluxo de dados até o final.
Eu acho que eu descobri a decodificação base64 e "gunzip", mas o resultado do código abaixo é 27,0,0,0 de repetição.Eu acho que a saída é suposto ser
(0,0) (1,0) (2,0) (3,0) (0,1) (1,1) (2,1) (3,1) (0,2) (1,2) (2,2) (3,2)
Obrigado!
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();
}
}
Solução
Não use Reader
s para nada, mas o caráter de dados!
Usar um DataInput
para ler os números inteiros.Decore o seu GZIPInputStream
com um DataInputStream
e usar readInt
.
Se os ints são little-endian, você vai precisar para inverter a ordem dos bytes para o tipo.Java usa network byte order (big-endian).Para números inteiros, isto pode ser feito com Inteiro.reverseBytes.
Você pode imprimir os valores hexadecimais usando:
System.out.format("%08x", (int) n);
Como ler todos os int
valores a partir de uma sequência de comprimento arbitrário:
Um mecanismo seria usar o available()
método que estimativas o 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);
}
No caso geral, available()
não é um mecanismo confiável.Mas você pode aumentar seu fluxo com um buffer para verificar a disponibilidade de dados:
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();
}
}
Outras dicas
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();
}
}