2

I have an array of bytes, The Hex starts (it's big) with this:

78 9C 9D 5B 4B 6F A4 38 10 FE 2F 9C 19 89 F2 83 47 1F 77 46 7B DA D3 CE 6A 2F A3 08 91 B4 93 B4 96 40 0B E8 64 A2 28 FF 7D A1 B1 3B 2E C0 ED 72 4F 94 E9 04 EA 61 57 7D F5 32 E4 23 7A AB 5E 55 D9 9C 5E A2 5D 01 10 47 55 B3 EF DA C3 BE 7C A8 0F AA 19 A2 DD D0 9D

It starts with 0x789c which is the lzma headers.

I am running it through the following method:

public byte[] decompress(byte[] bytesToDecompress)
    {
        byte[] returnValues = null;

        Inflater inflater = new Inflater();

        int numberOfBytesToDecompress = bytesToDecompress.length;

        inflater.setInput
        (
            bytesToDecompress,
            0,
            numberOfBytesToDecompress
        );

        int bufferSizeInBytes = numberOfBytesToDecompress;

        int numberOfBytesDecompressedSoFar = 0;
        List<Byte> bytesDecompressedSoFar = new ArrayList<Byte>();

        try
        {
            while (inflater.needsInput() == false)
            {
                byte[] bytesDecompressedBuffer = new byte[bufferSizeInBytes];

                int numberOfBytesDecompressedThisTime = inflater.inflate
                (
                    bytesDecompressedBuffer
                );

                numberOfBytesDecompressedSoFar += numberOfBytesDecompressedThisTime;

                for (int b = 0; b < numberOfBytesDecompressedThisTime; b++)
                {
                    bytesDecompressedSoFar.add(bytesDecompressedBuffer[b]);
                }
            }

            returnValues = new byte[bytesDecompressedSoFar.size()];
            for (int b = 0; b < returnValues.length; b++) 
            {
                returnValues[b] = (byte)(bytesDecompressedSoFar.get(b));
            }

        }
        catch (DataFormatException dfe)
        {
            dfe.printStackTrace();
        }

        inflater.end();

        return returnValues;
    }

    public String decompressToString(byte[] bytesToDecompress)
    {   
        byte[] bytesDecompressed = this.decompress
        (
            bytesToDecompress
        );

        String returnValue = null;

        try
        {
            returnValue = new String
            (
                bytesDecompressed,
                0,
                bytesDecompressed.length,
                "UTF-8"
            );  
        }
        catch (UnsupportedEncodingException uee)
        {
            uee.printStackTrace();
        }

        return returnValue;
    }

Like this: System.out.println(decompressToString(jsonDecompressed));

However I am getting nothing printed out.. There is also no stacktrace...

How come this is happening?

1 Answers1

1

Your code is already correct you just have to convert the hex string to a byte array using the method from the following question:

Convert a string representation of a hex dump to a byte array using Java?

public static byte[] hexStringToByteArray(String s) {
    int len = s.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                             + Character.digit(s.charAt(i+1), 16));
    }
    return data;
}

You can then use the previous method with the hex string (without spaces)

 public static void main(String[] args) {
    String strData = "789C9D5B4B6FA43810FE2F9C1989F283471F77467BDAD3CE6A2FA30891B493B496400BE864A228FF7DA1B13B2EC0ED724F94E904EA61577DF532E4237AAB5E55D99C5EA25D01104755B3EFDAC3BE7CA80FAA19A2DDD09D";
    byte[] jsonDecompressed = hexStringToByteArray(strData);
    System.out.println(decompressToString(jsonDecompressed));
}

and you obtain the correct decompressed value:

{"wave_num":911,"android_client":tr
Community
  • 1
  • 1
Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240