Direct Streaming 方法 CopyTo 找不到结尾 [英] Direct Streaming Method CopyTo does not find the end

查看:68
本文介绍了Direct Streaming 方法 CopyTo 找不到结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这种方法读取 SSE

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

使用我的示例数据大约需要 2 秒.我不想将流读入内存并尝试了这种方法

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

但是这个过程最终会陷入无限循环(我猜)至少对我来说它看起来像是找不到流的结尾.我可以在几秒钟后中断这个过程,所有数据都在 .txt 文件中.知道我可以做些什么来使直接流到文件方法工作吗?

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?

推荐答案

Stream.CanRead 告诉你一个流是否支持读取.由于它显然是可读的,While True 将永远持续下去.
让我们验证输出是否 Stream.CanWrite相反.

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

如果该过程需要一些时间并且您需要报告其进度,您可以使用缓冲区读取流(我没有添加任何错误检查,但当然应该使用 try/catch 块):
(这里是 ProgressBar 常用的 100 部分划分)

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

这篇关于Direct Streaming 方法 CopyTo 找不到结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆