简体   繁体   English

与.AsInputStream()转换流一起使用时的C#DataReader问题

[英]C# DataReader issue when used with .AsInputStream() converted stream

I need to do various reads on a stream for a decryption process. 我需要对流进行各种读取以进行解密过程。 (for WinRT project) For one, I'd like to do a ReadByte() to get the first byte of the stream. (对于WinRT项目)首先,我想做一个ReadByte()来获取流的第一个字节。 There-after I'd like to read a few bytes into an array, and then read the rest of the bytes into a buffer. 在我想要将几个字节读入数组之后,然后将其余字节读入缓冲区。

I open up a DataReader on a passed IInputStream object. 我在传递的IInputStream对象上打开一个DataReader。 Which in turn was created from a System.IO.Stream object using the .AsInputStream() method. 而这又是使用.AsInputStream()方法从System.IO.Stream对象创建的。 When I look at the DataReader object during debugging I see that the UnconsumedBufferLength is 0, and I am unable to do ReadByte() or ReadBytes() lest I want to get a "The operation attempted to access data outside the valid range" exception. 当我在调试期间查看DataReader对象时,我看到UnconsumedBufferLength为0,并且我无法执行ReadByte()或ReadBytes(),以免我想获得“尝试访问有效范围之外的数据的操作”异常。

Why does the DataReader object seem empty? 为什么DataReader对象看起来是空的? I've had issues with the AsInputStream() method not returning an actual IInputStream before. 我之前遇到的问题是AsInputStream()方法没有返回实际的IInputStream。 How can I ultimately open a DataReader object on a System.IO.Stream object. 我怎样才能最终在System.IO.Stream对象上打开DataReader对象。

Code where the DataReader is assigned: 分配DataReader的代码:

private Stream DecryptStream(IInputStream streamToDecrypt, byte[] key)
    {
        try
        {
            var dataReader = new DataReader(streamToDecrypt);

            int ivLength = dataReader.ReadByte();  //Throws exception (UnconsumedBufferLength = 0 remember)

            byte[] iv = new byte[ivLength];
            dataReader.ReadBytes(iv);   //Throws exception (UnconsumedBufferLength = 0 remember)


            IBuffer toDecryptBuffer = new Windows.Storage.Streams.Buffer(dataReader.UnconsumedBufferLength);
            toDecryptBuffer = dataReader.ReadBuffer(dataReader.UnconsumedBufferLength);   //Works, but only because toDecryptBuffer is of length 0. which is still useless. 

Code that calls the above method: 调用上述方法的代码:

Stream plainStream = DecryptStream(streamToDecrypt.AsInputStream(),key);

Update: Code which created the stream This is where the stream is first created, from a System.IO.Compression.ZipArchive object. 更新:创建流的代码这是首先从System.IO.Compression.ZipArchive对象创建流的位置。 This is then passed to the interim Decryption funciton as "streamToDecrypt" 然后将其作为“streamToDecrypt”传递给临时解密功能

ZipArchiveEntry metaEntry = archive.Entries.Where(x => x.FullName == "myFullNameHere").FirstOrDefault();
Stream returnStream = metaEntry.Open();

You need to call datareader.LoadAsync(size) to load the file into the buffer before reading it. 在读取文件之前,需要调用datareader.LoadAsync(size)将文件加载到缓冲区中。

Change your decrypt method to something like this: 将解密方法更改为以下内容:

private async Task<Stream> DecryptStream(IInputStream streamToDecrypt, byte[] key, int uncompressedSize)
{
    try
    {
        var dataReader = new DataReader(streamToDecrypt);
        await datareader.LoadAsync(uncompressedSize);

        int ivLength = dataReader.ReadByte();  //Throws exception (UnconsumedBufferLength = 0 remember)

        byte[] iv = new byte[ivLength];
        dataReader.ReadBytes(iv);   //Throws exception (UnconsumedBufferLength = 0 remember)

Then when you call it, pass in the uncompressed size from the ZipArchiveEntry: 然后当你调用它时,从ZipArchiveEntry传入未压缩的大小:

await DecryptStream(streamToDecrypt.AsInputStream(),key, streamToDecrypt.Length);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM