As part of my continuing work on a project accessing an API referenced in this post, I have encountered a problem in regard to zlib decompression.
The API documentation says:
To reduce transfer size, the content of some messages are sent in a compressed format. This compressed data is available in the compressedData field in the message. The format of this field is a base64 encoded zlib compressed JSON string.
Python example for decompressing import base64 import zlib
However my code:
throws the following error:
Loading libs...
BDSPlayerBoard V1 started at 1/5/2021 2:4:8
Java version: 11.0.1
main._mqttpersons (java line: 3287)
java.util.zip.ZipException: incorrect header check
at java.base/java.util.zip.InflaterInputStream.read(InflaterInputStream.java:165)
at java.base/java.io.FilterInputStream.read(FilterInputStream.java:107)
at anywheresoftware.b4a.objects.streams.File.Copy2(File.java:349)
at anywheresoftware.b4a.randomaccessfile.CompressedStreams.DecompressBytes(CompressedStreams.java:152)
at b4j.example.main._mqttpersons(main.java:3287)
at b4j.example.main._mqtt_messagearrived(main.java:3225)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
at anywheresoftware.b4a.BA$3.run(BA.java:247)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:834)
Pasting the compressedData string into an online zlib decompression tool returns a valid decompressed string.
Any suggestions on why my code is not returning a valid decompressed string, but throwing the error? What does "incorrect header check" indicate?
The API documentation says:
To reduce transfer size, the content of some messages are sent in a compressed format. This compressed data is available in the compressedData field in the message. The format of this field is a base64 encoded zlib compressed JSON string.
Python example for decompressing import base64 import zlib
Python:
data = "eJyl0b1uwyAQAOBXQcy1hLHjn65RorZppQ6VKnU74GKj2hABHqLI716ztK6aenA24Lj7TncXiibocH5U9J5QKZgSUPOEbzKRpCmKBJRME8ZzDjLljDFO7wg9ofPW+CnlQhWUWSWLfDEnfpRwCqDNdD5C53EqI7SIalbEmgZ6jLcXbJUme90M6CzEiA/gAropGNyA4/QSzZqhXG/mbG4+I5AP3ffoejD/k5WAYj2ZbubkrtPgyd6hke1fMc7Yeh20jYXoln63gGJ5O8uTLuctbHU3NWHIwV5r4WfOHG8Q67n4BGeFhjwghAWxOpZwg1jNxQN03dCTV3Cf6BdIwGo9+Wuxby1a8q6bRptr4Dh+AT9i+9U="
compressed_data = base64.b64decode(data.encode())
output = zlib.decompress(compressed_data).decode()
print(output)
However my code:
B4X:
Dim parser As JSONParser
parser.Initialize(JSON)
Dim root As Map = parser.NextObject
Dim compressedData As String = root.Get("compressedData")
Dim compData() As Byte = compressedData.GetBytes("UTF8")
Dim compress As CompressedStreams
Dim decompressed() As Byte
decompressed = compress.DecompressBytes(compData, "zlib")
Dim decompressedData As String
decompressedData = BytesToString(decompressed, 0, decompressed.Length, "UTF8")
Log(decompressedData)
throws the following error:
Loading libs...
BDSPlayerBoard V1 started at 1/5/2021 2:4:8
Java version: 11.0.1
main._mqttpersons (java line: 3287)
java.util.zip.ZipException: incorrect header check
at java.base/java.util.zip.InflaterInputStream.read(InflaterInputStream.java:165)
at java.base/java.io.FilterInputStream.read(FilterInputStream.java:107)
at anywheresoftware.b4a.objects.streams.File.Copy2(File.java:349)
at anywheresoftware.b4a.randomaccessfile.CompressedStreams.DecompressBytes(CompressedStreams.java:152)
at b4j.example.main._mqttpersons(main.java:3287)
at b4j.example.main._mqtt_messagearrived(main.java:3225)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
at anywheresoftware.b4a.BA$3.run(BA.java:247)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:834)
Pasting the compressedData string into an online zlib decompression tool returns a valid decompressed string.
Any suggestions on why my code is not returning a valid decompressed string, but throwing the error? What does "incorrect header check" indicate?