当前位置: 首页>>代码示例>>C#>>正文


C# IInputStream.ReadAsync方法代码示例

本文整理汇总了C#中IInputStream.ReadAsync方法的典型用法代码示例。如果您正苦于以下问题:C# IInputStream.ReadAsync方法的具体用法?C# IInputStream.ReadAsync怎么用?C# IInputStream.ReadAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IInputStream的用法示例。


在下文中一共展示了IInputStream.ReadAsync方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ComputeMd5

        public static async Task<string> ComputeMd5(IInputStream stream, BinaryStringEncoding encoding = BinaryStringEncoding.Utf8)
        {
            var hashAlgorithmProvider = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
            var hash = hashAlgorithmProvider.CreateHash();
            uint size = 1024*64;
            var buffer = new Buffer(size);
            while (true)
            {
                var x = await stream.ReadAsync(buffer, size, InputStreamOptions.Partial);
                if (x.Length < 1)
                {
                    break;
                }
                hash.Append(x);
            }

            var result = hash.GetValueAndReset();
            var hex = CryptographicBuffer.EncodeToHexString(result);
            return hex;
        }
开发者ID:gitter-badger,项目名称:InvalidPlayer,代码行数:20,代码来源:SecurityKit.cs

示例2: ProcessStream

        private async Task<HTTPRequest> ProcessStream(IInputStream stream)
        {
            Dictionary<string, string> _httpHeaders = null;
            Dictionary<string, string> _urlParameters = null;

            byte[] data = new byte[BufferSize];
            StringBuilder requestString = new StringBuilder();
            uint dataRead = BufferSize;

            IBuffer buffer = data.AsBuffer();

            string hValue = "";
            string hKey = "";

            try
            {
                // binary data buffer index
                uint bfndx = 0;

                // Incoming message may be larger than the buffer size.
                while (dataRead == BufferSize)
                {
                    await stream.ReadAsync(buffer, BufferSize, InputStreamOptions.Partial);
                    requestString.Append(Encoding.UTF8.GetString(data, 0, data.Length));
                    dataRead = buffer.Length;

                    // read buffer index
                    uint ndx = 0;
                    do
                    {
                        switch (ParserState)
                        {
                            case HttpParserState.METHOD:
                                if (data[ndx] != ' ')
                                    HTTPRequest.Method += (char)buffer.GetByte(ndx++);
                                else
                                {
                                    ndx++;
                                    ParserState = HttpParserState.URL;
                                }
                                break;
                            case HttpParserState.URL:
                                if (data[ndx] == '?')
                                {
                                    ndx++;
                                    hKey = "";
                                    HTTPRequest.Execute = true;
                                    _urlParameters = new Dictionary<string, string>();
                                    ParserState = HttpParserState.URLPARM;
                                }
                                else if (data[ndx] != ' ')
                                    HTTPRequest.URL += (char)buffer.GetByte(ndx++);
                                else
                                {
                                    ndx++;
                                    HTTPRequest.URL = WebUtility.UrlDecode(HTTPRequest.URL);
                                    ParserState = HttpParserState.VERSION;
                                }
                                break;
                            case HttpParserState.URLPARM:
                                if (data[ndx] == '=')
                                {
                                    ndx++;
                                    hValue = "";
                                    ParserState = HttpParserState.URLVALUE;
                                }
                                else if (data[ndx] == ' ')
                                {
                                    ndx++;

                                    HTTPRequest.URL = WebUtility.UrlDecode(HTTPRequest.URL);
                                    ParserState = HttpParserState.VERSION;
                                }
                                else
                                {
                                    hKey += (char)buffer.GetByte(ndx++);
                                }
                                break;
                            case HttpParserState.URLVALUE:
                                if (data[ndx] == '&')
                                {
                                    ndx++;
                                    hKey = WebUtility.UrlDecode(hKey);
                                    hValue = WebUtility.UrlDecode(hValue);
                                    _urlParameters[hKey] = _urlParameters.ContainsKey(hKey) ? _urlParameters[hKey] + ", " + hValue : hValue;
                                    hKey = "";
                                    ParserState = HttpParserState.URLPARM;
                                }
                                else if (data[ndx] == ' ')
                                {
                                    ndx++;
                                    hKey = WebUtility.UrlDecode(hKey);
                                    hValue = WebUtility.UrlDecode(hValue);
                                    _urlParameters[hKey] = _urlParameters.ContainsKey(hKey) ? _urlParameters[hKey] + ", " + hValue : hValue;
                                    HTTPRequest.URL = WebUtility.UrlDecode(HTTPRequest.URL);
                                    ParserState = HttpParserState.VERSION;
                                }
                                else
                                {
                                    hValue += (char)buffer.GetByte(ndx++);
//.........这里部分代码省略.........
开发者ID:apatlolla,项目名称:IoT,代码行数:101,代码来源:HttpRequestParser.cs

示例3: Read

        /// <summary>
        /// Reads the specified hashed block stream into a memory stream.
        /// </summary>
        /// <param name="input">The hashed block stream.</param>
        /// <returns>The de-hashed stream.</returns>
        public static async Task<Stream> Read(IInputStream input)
        {
            if (input == null)
                throw new ArgumentNullException("input");

            var blockIndex = 0;
            var result = new MemoryStream();
            var hash = WindowsRuntimeBuffer.Create(32);
            
            var reader = new DataReader(input)
            {
                ByteOrder = ByteOrder.LittleEndian,
            };
            var sha = HashAlgorithmProvider
                .OpenAlgorithm(HashAlgorithmNames.Sha256);

            try
            {
                while (true)
                {
                    // Detect end of file
                    var read = await reader.LoadAsync(4);
                    if (read == 0)
                        break;

                    // Verify block index
                    var index = reader.ReadInt32();
                    if (index != blockIndex)
                    {
                        throw new InvalidDataException(string.Format(
                            "Wrong block ID detected, expected: {0}, actual: {1}",
                            blockIndex, index));
                    }
                    blockIndex++;

                    // Block hash
                    hash = await input.ReadAsync(hash, 32);
                    if (hash.Length != 32)
                    {
                        throw new InvalidDataException(
                            "Data corruption detected (truncated data)");
                    }

                    read = await reader.LoadAsync(4);
                    if (read != 4)
                    {
                        throw new InvalidDataException(
                            "Data corruption detected (truncated data)");
                    }

                    // Validate block size (< 10MB)
                    var blockSize = reader.ReadInt32();
                    if (blockSize == 0)
                    {
                        // Terminator block
                        var isTerminator = hash
                            .ToArray()
                            .All(x => x == 0);

                        if (!isTerminator)
                        {
                            throw new InvalidDataException(
                                "Data corruption detected (invalid hash for terminator block)");
                        }

                        break;
                    }

                    if (0 > blockSize || blockSize > 10485760)
                    {
                        throw new InvalidDataException(
                            "Data corruption detected (truncated data)");
                    }

                    // Check data truncate
                    var loaded = await reader.LoadAsync((uint)blockSize);
                    if (loaded < blockSize)
                    {
                        throw new InvalidDataException(
                            "Data corruption detected (truncated data)");
                    }

                    var buffer = reader.ReadBuffer((uint)blockSize);

                    // Verify block integrity
                    var actual = sha.HashData(buffer);
                    if (!CryptographicBuffer.Compare(hash, actual))
                    {
                        throw new InvalidDataException(
                            "Data corruption detected (content corrupted)");
                    }

                    await result.WriteAsync(buffer.ToArray(),
                        0, (int)buffer.Length);
                }
//.........这里部分代码省略.........
开发者ID:Confuset,项目名称:7Pass-Remake,代码行数:101,代码来源:HashedBlockFileFormat.cs

示例4: GetFileHash

        /// <summary>
        /// Gets the SHA 256 hash of the specified file.
        /// </summary>
        /// <param name="input">The keyfile stream.</param>
        /// <param name="buffer">The buffer.</param>
        /// <returns>The hash of the keyfile.</returns>
        private static async Task<IBuffer> GetFileHash(
            IInputStream input, IBuffer buffer)
        {
            var sha = HashAlgorithmProvider
                .OpenAlgorithm(HashAlgorithmNames.Sha256)
                .CreateHash();

            while (true)
            {
                buffer = await input.ReadAsync(
                    buffer, buffer.Capacity);

                if (buffer.Length == 0)
                    break;

                sha.Append(buffer);
            }

            return sha.GetValueAndReset();
        }
开发者ID:Confuset,项目名称:7Pass-Remake,代码行数:26,代码来源:PasswordData.cs

示例5: GetHeaders

        /// <summary>
        /// Parse the headers fields.
        /// </summary>
        /// <param name="input">The input stream.</param>
        /// <param name="buffer">The header bytes reader.</param>
        /// <returns>The file headers.</returns>
        private static async Task<FileHeaders> GetHeaders(
            IInputStream input, IBuffer buffer)
        {
            var result = new FileHeaders();

            while (true)
            {
                buffer = await input.ReadAsync(buffer, 3);
                var field = (HeaderFields)buffer.GetByte(0);
                var size = BitConverter.ToUInt16(buffer.ToArray(1, 2), 0);

                if (size > 0)
                    buffer = await input.ReadAsync(buffer, size);

                switch (field)
                {
                    case HeaderFields.EndOfHeader:
                        return result;

                    case HeaderFields.CompressionFlags:
                        result.UseGZip = buffer.GetByte(0) == 1;
                        break;

                    case HeaderFields.EncryptionIV:
                        result.EncryptionIV = buffer
                            .ToArray().AsBuffer();
                        break;

                    case HeaderFields.MasterSeed:
                        result.MasterSeed = buffer
                            .ToArray().AsBuffer();
                        break;

                    case HeaderFields.StreamStartBytes:
                        result.StartBytes = buffer
                            .ToArray().AsBuffer();
                        break;

                    case HeaderFields.TransformSeed:
                        result.TransformSeed = buffer
                            .ToArray().AsBuffer();
                        break;

                    case HeaderFields.TransformRounds:
                        result.TransformRounds = BitConverter.ToUInt64(
                            buffer.ToArray(), 0);
                        break;

                    case HeaderFields.ProtectedStreamKey:
                        result.ProtectedStreamKey = buffer
                            .ToArray().AsBuffer();
                        break;

                    case HeaderFields.InnerRandomStreamID:
                        result.RandomAlgorithm = (CrsAlgorithm)
                            BitConverter.ToUInt32(buffer.ToArray(), 0);
                        break;
                }
            }
        }
开发者ID:Confuset,项目名称:7Pass-Remake,代码行数:66,代码来源:FileFormat.cs


注:本文中的IInputStream.ReadAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。