了解克隆流的不同方法,以及在哪种情况下使用哪种方法

发布于 2024-12-07 16:05:47 字数 1238 浏览 0 评论 0原文

复制文件流时,我遇到了两个示例:

这个,

Using requestStream as IO.Stream = state.Request.EndGetRequestStream(ar)
    ' Copy the file contents to the request stream.
    Const bufferLength As Integer = 2048
    Dim buffer(bufferLength - 1) As Byte
    Dim count As Integer = 0
    Dim readBytes As Integer = 0
    Using stream As IO.FileStream = IO.File.OpenRead(state.FileName)
        While readBytes <> 0
            readBytes = stream.Read(buffer, 0, bufferLength)
            requestStream.Write(buffer, 0, readBytes)
            count += readBytes
        End While
    End Using
End Using

还有这个:

    'Copy the contents of the file to the request stream.
    Dim fileContents As Byte()
    Using fileStream As New IO.StreamReader(state.FileName)
        fileContents = Encoding.UTF8.GetBytes(fileStream.ReadToEnd())
    End Using

    Using requestStream As IO.Stream = state.Request.EndGetRequestStream(ar)
        requestStream.Write(fileContents, 0, fileContents.Length)
    End Using

我的理解是第一个将一个流直接复制到另一个流,第二个通过字节数组复制。两者都起作用并实现相同的目标。我相信第一个会更快,但第二个看起来更简单,更容易维护等。

不过,我看不到你在第一个中设置编码的位置。为什么您需要在其中一个中执行此操作,而不是在另一个中执行此操作?

此外,对每个片段的优点和缺点进行客观评论也会很有用。谢谢

When copying a file stream, I've come across two examples:

This one,

Using requestStream as IO.Stream = state.Request.EndGetRequestStream(ar)
    ' Copy the file contents to the request stream.
    Const bufferLength As Integer = 2048
    Dim buffer(bufferLength - 1) As Byte
    Dim count As Integer = 0
    Dim readBytes As Integer = 0
    Using stream As IO.FileStream = IO.File.OpenRead(state.FileName)
        While readBytes <> 0
            readBytes = stream.Read(buffer, 0, bufferLength)
            requestStream.Write(buffer, 0, readBytes)
            count += readBytes
        End While
    End Using
End Using

and this one:

    'Copy the contents of the file to the request stream.
    Dim fileContents As Byte()
    Using fileStream As New IO.StreamReader(state.FileName)
        fileContents = Encoding.UTF8.GetBytes(fileStream.ReadToEnd())
    End Using

    Using requestStream As IO.Stream = state.Request.EndGetRequestStream(ar)
        requestStream.Write(fileContents, 0, fileContents.Length)
    End Using

My understanding is that the first one copies one stream directly to another, the second copies via a byte array. Both work and achieve the same thing. I believe the first one would be quicker, but the second one looks a lot simpler and easier to maintain etc.

I can't see where you set the encoding in the first one though. Why would you need to do it in one, but not the other?

Also, objective comments on the pro's and con's of each snippet would be useful. Thx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺深海 2024-12-14 16:05:47

第二个不能处理任意二进制数据 - 它将数据视为 UTF-8 编码的文本数据,然后重新编码 - 这是一个非常糟糕的主意除非您确实知道它是 UTF-8 编码的文本。

第二种形式也使用字节数组,一次仅一个缓冲区。

但是,您在第一个版本中遇到了一个错误:当 bytesRead 不为零时,您将继续前进,但它开始为 0。

您可能想要:

Using stream As IO.FileStream = IO.File.OpenRead(state.FileName)
    readBytes = stream.Read(buffer, 0, bufferLength)
    While readBytes <> 0
        requestStream.Write(buffer, 0, readBytes)
        count += readBytes
        readBytes = stream.Read(buffer, 0, bufferLength)
    End While
End Using

在 C# 中我会使用:

int bytesRead;
while ((bytesRead = stream.Read(buffer, 0, bufferLength)) > 0)
{
    requestStream.Write(buffer, 0, readBytes);
    count += readBytes;
}

但我不知道是否可以像 VB 中那样进行复合赋值。

其他一些选项:

  • 您可以使用 File.ReadAllBytes 将整个文件作为字节数组读取,但显然如果它是一个大文件,这会浪费内存。这是第二个代码的安全版本。
  • 您可以使用 Stream.CopyTo(Stream)< /code>如果您使用的是 .NET 4。如果没有,您可能会自己编写相同的代码作为扩展方法,以便从其他项目中重用。

The second one will not work with arbitrary binary data - it treats the data as UTF-8-encoded text data, then re-encodes it - this is a very bad idea unless you actually know it's UTF-8-encoded text.

The second form uses a byte array as well, just one buffer at a time.

However, you've got a bug in the first version: you're going to keep going while bytesRead is non-zero, but it starts as 0.

You might want:

Using stream As IO.FileStream = IO.File.OpenRead(state.FileName)
    readBytes = stream.Read(buffer, 0, bufferLength)
    While readBytes <> 0
        requestStream.Write(buffer, 0, readBytes)
        count += readBytes
        readBytes = stream.Read(buffer, 0, bufferLength)
    End While
End Using

In C# I'd use:

int bytesRead;
while ((bytesRead = stream.Read(buffer, 0, bufferLength)) > 0)
{
    requestStream.Write(buffer, 0, readBytes);
    count += readBytes;
}

but I don't know whether you can do a compound assignment like that in VB.

Some other options:

  • You can use File.ReadAllBytes to read a whole file as a byte array, although obviously that will waste memory if it's a large file. This is a sort of safe version of your second code.
  • You can use Stream.CopyTo(Stream) if you're using .NET 4. If not, you could potentially write the same code yourself as an extension method to be reused from other projects.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文