Stream Auto Closing?

robh

Member
Licensed User
Longtime User
I have the following code where 'result.resultstream' is a valid reponse from a web request.

I am attempting the to first check for a particular piece of text in the stream and if found then continue on to parse the stream elsewhere within my application(using sax parser). The problem I am having is that when performing the 1st check by converting the stream to text, this appears to auto close the stream? the second operation then fails with a "attempt to read a closed stream" error message (shown in red). Is there anyway to read utilise the stream more than once?

Dim t As TextReader
t.Initialize(result.ResultStream)


' have we any image in the response?
If t.ReadAll.IndexOf("GetProductImageFullSizeResult") = -1 Then
ToastMessageShow("There is no image available for this item.",True)
Return
End If



xmlParser.Initialize()
xmlParser.Parse(result.ResultStream,"GetProducIImageFullSize")
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
A stream is not an array. Usually you can only read it once.

Are you using HttpUtils? If yes the you can just call HttpUtils.GetInputStream again to get a new input stream.
If not then you will need to something like:
B4X:
Dim str As String
str = t.ReadAll
If str.IndexOf(...) ...

...
Dim In As InputStream
Dim b() As Byte
b = str.GetBytes("UTF8")
In.InitializeFromBytesArray(b, 0, b.Length)
XmlParser.Parse(in, ...)
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…