B4J Question java.io.IOException: Stream closed

JackKirk

Well-Known Member
Licensed User
Longtime User
I create an input stream via a http job:

Payload_inputstream = Job.GetInputStream

when I copy it to an outputstream via:

File.Copy2(wrk_AWS_S3_B4A.Payload_inputstream, wrk_out)

I get the following:
Error occurred on line: 4781 (Show_list)
java.io.IOException: Stream closed
at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:172)
at java.io.BufferedInputStream.read(BufferedInputStream.java:338)
at java.io.FilterInputStream.read(FilterInputStream.java:107)
at anywheresoftware.b4a.objects.streams.File.Copy2(File.java:362)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at anywheresoftware.b4a.shell.DebugResumableSub$RemoteResumableSub.resume(DebugResumableSub.java:22)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:137)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at anywheresoftware.b4a.keywords.Common$14.run(Common.java:1770)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7842)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
(Exception) java.lang.Exception: java.io.IOException: Stream closed
If I trap it with:

Try
File.Copy2(wrk_AWS_S3_B4A.Payload_inputstream, wrk_out)
Catch
Log(LastException)
End Try

the copy actually works.

Any suggestions as to what I could do to prevent the error would be appreciated.
 

JackKirk

Well-Known Member
Licensed User
Longtime User
Oops,

I just worked out that this is happening because I do an earlier:

File.Copy2(wrk_AWS_S3_B4A.Payload_inputstream, wrk_out)

which according to the File.Copy2 documentation automatically closes wrk_AWS_S3_B4A.Payload_inputstream.

So my question/s become:

1. how do I stop File.Copy2 automatically closing wrk_AWS_S3_B4A.Payload_inputstream?
2. alternatively, how can I open wrk_AWS_S3_B4A.Payload_inputstream again?
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
I really do wish I could delete a thread.

Turns out the second

Try
File.Copy2(wrk_AWS_S3_B4A.Payload_inputstream, wrk_out)
Catch
Log(LastException)
End Try

was not actually working - its tied up with trying to move a video to a photo gallery when SDK > 29 - and it wasn't actually doing it when I went and looked in the gallery.

I have got around the problem by:

1. copying the input stream to a temporary location

B4X:
                'Move video to temporary location
                wrk_out = File.OpenOutput(File.DirInternal, "temp.mp4", False)
                File.Copy2(wrk_AWS_S3_B4A.Payload_inputstream, wrk_out)
                wrk_out.Close

2. using it multiple times:

B4X:
                wrk_in = File.OpenInput(File.DirInternal, "temp.mp4")
                wrk_out = ...
                File.Copy2(wrk_in, wrk_out)
                wrk_out.Close

3. deleting it:
B4X:
                    File.Delete(File.DirInternal, "temp.mp4")
 
Upvote 0
Top