Android Question HttpServer OutputStream

XbNnX_507

Active Member
Licensed User
Longtime User
Hi, i'm using this library https://www.b4x.com/android/forum/t...er-in-your-android-application.25984/#content.

Whenever i try to write bytes to the outpustream with this code:
B4X:
        Dim r As Reflector
        r.Target = Response
        Dim jo As JavaObject = r.GetField("res")
        Dim ser As B4XSerializator
            
        Dim out As OutputStream = jo.RunMethod("getOutputStream", Null)
        Dim data() As Byte = ser.ConvertObjectToBytes( Object )
        out.WriteBytes( data, 0, data.Length )

i get java.io.EOFException: Unexpected end of ZLIB input stream at client side.
B4X:
Sub JobDone ( j as httjob )
  if j.success then
          dim data() as byte = bit.inpustreamtobytes( j.getinpustream) '< '-- Error
 end if
End Sub

Is there something wrong ?
Thanks.
 

XbNnX_507

Active Member
Licensed User
Longtime User
Are you closing the outputstream after you wrote to it?
Yes i did try that.
Can you post the full error message with the stack trace?
Here.
B4X:
Waiting for debugger to connect...
Program started.
java.io.EOFException: Unexpected end of ZLIB input stream
    at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:240)
    at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
    at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:122)
    at java.io.DataInputStream.readByte(DataInputStream.java:265)
    at anywheresoftware.b4a.randomaccessfile.B4XSerializator.readByte(B4XSerializator.java:133)
    at anywheresoftware.b4a.randomaccessfile.B4XSerializator.readObject(B4XSerializator.java:301)
    at anywheresoftware.b4a.randomaccessfile.B4XSerializator.ReadObject(B4XSerializator.java:112)
    at anywheresoftware.b4a.randomaccessfile.B4XSerializator.ConvertBytesToObject(B4XSerializator.java:82)
    at anywheresoftware.b4a.randomaccessfile.B4XSerializator$2.call(B4XSerializator.java:93)
    at anywheresoftware.b4a.randomaccessfile.B4XSerializator$2.call(B4XSerializator.java:1)
    at anywheresoftware.b4a.BA$4.run(BA.java:272)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Error reading response: (EOFException) java.io.EOFException: Unexpected end of ZLIB input stream
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Log the size of you data array after serialization to make sure you’re sending anything
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Do you accidentally write anything else to the stream before/after?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
What type of Java object does gefield(“res”) return?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0

XbNnX_507

Active Member
Licensed User
Longtime User

You misunderstood my question.
it has nothing to do with b4serializator. I'm converting a map ( CreateMap("test": "test") to bytes and then trying to send it over to the outpustream.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
You misunderstood my question.
Actually, I've overlooked the forum (B4A). I was thinking in terms of B4J. And yes, I also misunderstood what you are sending. So sort of ignore everything I've said so far.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Back on track. Instead of
B4X:
dim data() as byte = bit.inpustreamtobytes( j.getinpustream) '< '-- Error
do a
B4X:
Log(j.GetString)
Just to see if all you are getting is some binary data.
 
Upvote 0

XbNnX_507

Active Member
Licensed User
Longtime User
It does not return anything. Suppose is the normal behavior given that i'm not using Response.SendString().
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
That should not matter (that you did not use sendstring)
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Try this:
B4X:
Log(Job.Response.ContentLength)
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Try this on the server side:
B4X:
out.WriteBytes( data, 0, data.Length )
out.Flush ' <- my guess is that it is not flushing the write. I'm guessing though
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
The flush may be the wrong thing to do. A hint from this post (https://stackoverflow.com/a/5045073) indicates that ContentLength should be greater than 0 in order for the underlying servlet to recognize that it is done and flush the output stream. So you may need:
B4X:
'
        Dim r As Reflector
        r.Target = Response
        Dim jo As JavaObject = r.GetField("res")
        Dim ser As B4XSerializator

        Dim out As OutputStream = jo.RunMethod("getOutputStream", Null)
        Dim data() As Byte = ser.ConvertObjectToBytes( Object )
        jo.RunMethod("setContentLength", Array(data.Length)) '<--- This may do the trick
        out.WriteBytes( data, 0, data.Length )
 
Upvote 0

XbNnX_507

Active Member
Licensed User
Longtime User
@OliverA
Well i have tried everything that you have posted with no success.
jo.RunMethod("setContentLength", Array(data.Length)) '<--- This may do the trick
Sadly it does not.

I even try to set the content type with Response.SetContentType("Application/octet-stream")
No success either.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
What did
B4X:
Log(Job.Response.ContentLength)
produce on the client side?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…