Direct Streaming Method CopyTo does not find the end

橙三吉。 提交于 2019-12-11 06:38:09

问题


I am reading a SSE by using this method

    Public Shared Sub ReadStreamForever(ByVal stream As Stream)
    Dim encoder = New UTF8Encoding()
    Dim buffer = New Byte(2047) {}
    Dim counter As Integer = 0
    While True
        If stream.CanRead Then
            Dim len As Integer = stream.Read(buffer, 0, 2048)
            counter = counter + 1
            If len > 0 Then
                Dim text = encoder.GetString(buffer, 0, len)
                SSEApplication.Push(text) 'Here I collect the text slices to a List(of string) object
            Else
                Exit While
            End If
        Else
            Exit While
        End If
    End While
    SSEApplication.writer() 'Here I write the content to a .txt file
End Sub

With my example data it takes about 2 seconds. I would prefer not to read the stream into memory though and tried this method

Public Shared Sub ReadStreamForever1(ByVal stream As Stream)

    Dim output As FileStream = File.OpenWrite("C:\Users\mini_dataset.txt")
    While True
        If stream.CanRead Then
            stream.CopyTo(output)
        Else
            Exit While
        End If
    End While
End Sub

But the process ends up in an endless loop (I guess) at least to me it looks like the end of the stream can not be found. I can break the process after a few seconds and all the data are in the .txt file. Any idea what I can do to get the direct stream to file method working?


回答1:


Stream.CanRead tells you whether a stream supports reading. Since it's apparently readable, While True will go on forever.
Let's verify whether the output Stream.CanWrite instead.

Public Shared Sub ReadStreamForever1(ByVal stream As Stream)
    Using output As FileStream = File.OpenWrite("[Output file path]")
        If output.CanWrite Then
            stream.CopyTo(output)
        End If
    End Using
End Sub

If the process takes some time and you need to report its progress, you could read the stream using a buffer (I didn't add any error checking but of course a try/catch block should be used):
(Here, with 100 parts division commonly used by a ProgressBar)

Public Sub ReadStreamForever1(ByVal stream As Stream)
    Dim BufferLength As Integer = 81920 'As the default stream buffer
    Dim Buffer(BufferLength) As Byte
    Dim BytesRead As Long = 0L

    Using output As FileStream = File.OpenWrite("[Output file path]")
        If output.CanWrite Then
            Dim Part As Long = stream.Length \ 100
            Dim PartCount As Integer = 0
            Dim read As Integer = 0
            Do
                read = stream.Read(Buffer, 0, BufferLength)
                If read = 0 Then Exit Do
                If (BytesRead / Part > PartCount) Then
                    PartCount += 1
                    'ReportWriteProgress(PartCount)
                End If
                output.Write(Buffer, 0, read)
                BytesRead += read
            Loop
        End If
    End Using
End Sub


来源:https://stackoverflow.com/questions/53740619/direct-streaming-method-copyto-does-not-find-the-end

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!