Exemplo n.º 1
2
        public async Task<BlobFingerprint> GetFingerprintAsync(Stream stream, CancellationToken cancellationToken)
        {
            using (var sha3 = new Keccak())
            using (var sha256 = SHA256.Create())
            using (var md5 = MD5.Create())
            {
                var buffers = new[] { AllocateBuffer(), AllocateBuffer() };
                var index = 0;

                var totalLength = 0L;

                var readTask = stream.ReadAsync(buffers[index], 0, buffers[index].Length, cancellationToken);

                for (;;)
                {
                    var length = await readTask.ConfigureAwait(false);

                    if (length < 1)
                        break;

                    var buffer = buffers[index];

                    if (++index >= buffers.Length)
                        index = 0;

                    readTask = stream.ReadAsync(buffers[index], 0, buffers[index].Length, cancellationToken);

                    totalLength += length;

                    sha3.TransformBlock(buffer, 0, length, null, 0);

                    sha256.TransformBlock(buffer, 0, length, null, 0);

                    md5.TransformBlock(buffer, 0, length, null, 0);
                }

                sha3.TransformFinalBlock(buffers[0], 0, 0);

                sha256.TransformFinalBlock(buffers[0], 0, 0);

                md5.TransformFinalBlock(buffers[0], 0, 0);

                foreach (var buffer in buffers)
                    FreeBuffer(buffer);

                var fingerprint = new BlobFingerprint(totalLength, sha3.Hash, sha256.Hash, md5.Hash);

                return fingerprint;
            }
        }
Exemplo n.º 2
1
        public static async Task<long> CopyStreamsAsyncParallel(Stream src, Stream dst)
        {
            long numCopied = 0;
            byte[][] buffer = new byte[2][];
            buffer[0] = new byte[0x1000];
            buffer[1] = new byte[0x1000];

            var index = 0;
            int numRead = await src.ReadAsync(buffer[index], 0, buffer[index].Length);

            while (numRead > 0)
            {
                var writeAsync = dst.WriteAsync(buffer[index], 0, numRead);
                index = index ^= 1;
                var readAsync = src.ReadAsync(buffer[index], 0, buffer[index].Length);

                Task.WaitAll(writeAsync, readAsync);
                numRead = readAsync.Result;

                numCopied += numRead;
            }
            await dst.FlushAsync();
            src.Dispose();
            dst.Dispose();

            return numCopied;

        }
Exemplo n.º 3
0
		public async Task LoadAsync(Stream input, CancellationToken cancellationToken)
		{
			if (input == null) throw new ArgumentNullException(nameof(input));

			var headerSizeBuffer = BitConverter.GetBytes(default(int));
			await input.ReadAsync(headerSizeBuffer, 0, headerSizeBuffer.Length, cancellationToken);

			var headerSize = BitConverter.ToInt32(headerSizeBuffer, 0);
			var headerBuffer = new byte[headerSize];
			await input.ReadAsync(headerBuffer, 0, headerBuffer.Length, cancellationToken);

			// Clear the files
			this.Files.Clear();

			using (var sr = new StringReader(Encoding.Unicode.GetString(headerBuffer, 0, headerBuffer.Length)))
			{
				var separator = new[] { '|' };

				string line;
				while ((line = await sr.ReadLineAsync()) != null)
				{
					var values = line.Split(separator);
					this.Add(values[0], int.Parse(values[1]));
				}
			}
		}
Exemplo n.º 4
0
        public override async Task FromStreamAsync(Stream stream, uint tagLength, TagVersion version)
        {
            long streamStart = stream.Position;
            int encoding = stream.ReadByte();
            switch (version)
            {
                case TagVersion.V24:
                case TagVersion.V23:
                    if (encoding == 0x00)
                    {
                        MimeType = await stream.ReadAnsiString(streamStart + tagLength).ConfigureAwait(false);
                        PictureType = (AttachedPictureType)stream.ReadByte();

                        Description = await stream.ReadAnsiString(streamStart + tagLength).ConfigureAwait(false);
                        Data = new byte[tagLength - (stream.Position - streamStart)];
                        await stream.ReadAsync(Data, 0, Data.Length).ConfigureAwait(false);
                    }
                    else if (encoding == 0x01)
                    {
                        MimeType = await stream.ReadAnsiString(streamStart + tagLength).ConfigureAwait(false);
                        PictureType = (AttachedPictureType)stream.ReadByte();
                        Description = await stream.ReadUnicodeStringAsync(streamStart + tagLength).ConfigureAwait(false);
                        Data = new byte[tagLength - (stream.Position - streamStart)];
                        await stream.ReadAsync(Data, 0, Data.Length).ConfigureAwait(false);
                    }
                    break;
                default:
                    Debug.WriteLine("APIC: Version not implemented - " + version.ToString());
                    break;
            }
        }
Exemplo n.º 5
0
        internal static async Task<TagHeader> ReadTagHeader(Stream stream)
        {
            var header = new TagHeader();
            byte[] headerBytes = ArrayPool<byte>.Shared.Rent(7);
            await stream.ReadAsync(headerBytes, 0, 7).ConfigureAwait(false);

            switch (Convert.ToInt32(headerBytes[0]))
            {
                case 2:
                    header.Version = TagVersion.V22;
                    break;
                case 3:
                    header.Version = TagVersion.V23;
                    break;
                case 4:
                    header.Version = TagVersion.V24;
                    break;
                default:
                    header.Version = TagVersion.Unknown;
                    break;
            }

            header.Unsynchronization = ((headerBytes[2] & 0x80) == 0x80);
            header.ExtendedHeader = ((headerBytes[2] & 0x40) == 0x40);
            header.Experimental = ((headerBytes[2] & 0x20) == 0x20);
            header.HasFooter = ((headerBytes[2] & 0x10) == 0x10);
            header.TagLength = headerBytes[3] * 128 * 128 * 128 + headerBytes[4] * 128 * 128 + headerBytes[5] * 128 + headerBytes[6];
            ArrayPool<byte>.Shared.Return(headerBytes);
            return header;
        }
Exemplo n.º 6
0
        public async Task <IChunkValidation> VerifyChunkAsync(System.IO.Stream fileStream, long from, long to, string expectedMd5, IStatus status)
        {
            if (!fileStream.CanSeek)
            {
                throw new Exception("Unable to seek in the file stream");
            }

            var chunkValidation = new ChunkValidation
            {
                From         = from,
                To           = to,
                ExpectedHash = expectedMd5
            };

            fileStream.Seek(from, SeekOrigin.Begin);

            var length = (int)(to - from + 1);

            byte[] buffer = new byte[length];
            await fileStream.ReadAsync(buffer, 0, length);

            chunkValidation.ActualHash = await getBytesHashAsyncDelegate.GetHashAsync(buffer, status);

            return(chunkValidation);
        }
Exemplo n.º 7
0
 private async void ReadAsync(Stream stream)
 {
     byte[] buffer = new byte[100];
     while (true)
     {
         try
         {
             int length = await stream.ReadAsync(buffer, 0, buffer.Length);
             Invoke(new MethodInvoker(delegate
             {
                 outputText.Append(buffer, length);
             }));
         }
         catch
         {
             if (this.Visible) { // we're not being closed
                 Invoke(new MethodInvoker(delegate
                 {
                     Stop(false);
                 }));
             }
             break;
         }
     }
 }
Exemplo n.º 8
0
        static async Task <int> Reader(System.IO.Stream stream)
        {
            Console.WriteLine("Reader started");
            int r = 0;

            for (; ;)
            {
                byte[] buffer = new byte[1024];
                try
                {
                    int read = await stream.ReadAsync(buffer, 0, 1024);

                    if (read == 0)
                    {
                        Console.WriteLine("read == 0");
                        break;
                    }

                    r += read;
                }
                catch (Exception e)
                {
                    break;
                }
            }
            return(r);
        }
Exemplo n.º 9
0
 // Reads a single FIX messages and returns it. It shall contain
 // all fields, including header and trailer. No validation is
 // performed.
 //
 // This method is NOT thread-safe.
 //
 // Throws MessageTooLargeException if incoming message exceeds
 // MaxMessageSize. After that every call to ReadMessage() will throw
 // MessageTooLargeException.
 //
 // Throws EmptyStreamException if nothing can be read from the
 // underlying stream.
 public async Task<ArraySegment<byte>> ReadMessage(Stream strm)
 {
     Assert.True(strm != null);
     var trailerMatcher = new MessageTrailerMatcher();
     int messageEnd = trailerMatcher.FindTrailer(_buf, _startPos, _endPos);
     // Keep reading from the underlying stream until we find trailer.
     while (messageEnd == 0)
     {
         EnsureBufferSpace();
         Assert.True(_endPos < _buf.Length, "_endPos = {0}, _buf.Length = {1}", _endPos, _buf.Length);
         int read = await strm.ReadAsync(_buf, _endPos, _buf.Length - _endPos);
         if (read <= 0)
         {
             var partial = new ArraySegment<byte>(_buf, _startPos, _endPos - _startPos);
             throw new EmptyStreamException("Read so far: " + partial.AsAscii());
         }
         messageEnd = trailerMatcher.FindTrailer(_buf, _endPos, _endPos + read);
         _endPos += read;
     }
     var res = new ArraySegment<byte>(_buf, _startPos, messageEnd - _startPos);
     _startPos = messageEnd;
     if (_log.IsDebugEnabled) _log.Debug("IN: {0}", res.AsAscii());
     else _log.Info("IN: {0}", Truncate(res.AsAscii(), 1024));
     return res;
 }
Exemplo n.º 10
0
        // this thing is roughly equivalent to:
        //public async Task<string> ReadStreamToEnd(Stream stream)
        //{
        //    ...
        //    var bytesRead = await steam.ReadAsync(buffer, 0, buffer.Length);
        //    ...
        //}
        public static IEnumerable<object> ReadStreamToEnd(Stream stream)
        {
            StringBuilder sb = new StringBuilder();
            var buffer = new byte[1024];
            var bytesRead = 0;

            do
            {
                // synchronous example--would work! probably gum up your throughput though
                //bytesRead = stream.Read(buffer, 0, buffer.Length);

                //var read = stream.ReadAsync(buffer, 0, buffer.Length);
                //yield return read;
                //bytesRead = read.GetResult<int>();

                var read = stream.ReadAsync(buffer, 0, buffer.Length);
                yield return read;
                Console.WriteLine("read " + read.Result);
                bytesRead = read.Result;

                // impossible.
                //Exception ex = null;
                //yield return stream.ReadAsyncC(buffer, 0, buffer.Length)(n => bytesRead = n, e => ex = e);

                //var read = stream.ReadAsyncTask(buffer, 0, buffer.Length);
                //yield return read;
                //bytesRead = read.Result;

                sb.Append(Encoding.UTF8.GetString(buffer, 0, bytesRead));
            }
            while (bytesRead > 0);

            yield return sb.ToString();
        }
Exemplo n.º 11
0
		public async Task UnpackAsync(Stream input, DirectoryInfo folder, bool append)
		{
			if (input == null) throw new ArgumentNullException("input");
			if (folder == null) throw new ArgumentNullException("folder");

			var mode = FileMode.Create;
			if (append)
			{
				mode = FileMode.Append;
			}
			foreach (var fileHeader in NetworkHelper.ReadString(input, _buffer).Split(PackageHelper.FileSeparator))
			{
				var name = fileHeader.Substring(0, fileHeader.IndexOf(PackageHelper.SizeSeparator));

				var filePath = Path.Combine(folder.FullName, name);
				var folderPath = Path.GetDirectoryName(filePath);
				if (!Directory.Exists(folderPath))
				{
					Directory.CreateDirectory(folderPath);
				}

				using (var output = new FileStream(filePath, mode))
				{
					int readBytes;
					while ((readBytes = await input.ReadAsync(_buffer, 0, _buffer.Length)) != 0)
					{
						await output.WriteAsync(_buffer, 0, readBytes);
					}
				}
			}
		}
Exemplo n.º 12
0
 private async Task CopyStream(Stream source, Stream destination)
 {
     var buffer = new byte[1024];
     var totalRead = 0;
     var token = cancellationTokenSource.Token;
     try
     {
         while (true)
         {
             var read = await source.ReadAsync(buffer, 0, 1024, token);
             if (read == 0)
             {
                 break;
             }
             Interlocked.Add(ref totalRead, read);
             if (shouldThrow(totalRead, buffer, 0, read))
             {
                 throw new Exception("Connection closed");
             }
             await destination.WriteAsync(buffer, 0, read, token);
             destination.Flush();
         }
     }
     finally
     {
         source.Close();
         destination.Close();
     }
 }
Exemplo n.º 13
0
        public async Task Download(Storage storage, Action<int> downloadProgress, Action<int> writeProgressAction, CancellationToken ct, Stream responseStream)
        {
            using (responseStream)
            {
                try
                {
                    for (;;)
                    {
                        if (!EnoughSpaceInWriteBuffer())
                        {
                            FlushBuffer(storage, writeProgressAction);
                        }

                        var bytesRead = await responseStream.ReadAsync(_writeBuffer, _writeBufferPosition, MaxPortionSize, ct);

                        if (bytesRead == 0)
                        {
                            FlushBuffer(storage, writeProgressAction);
                            break;
                        }

                        MoveReadBufferPosition(bytesRead);
                        downloadProgress(bytesRead);
                    }
                }
                finally 
                {
                    FlushBuffer(storage, writeProgressAction);
                }
            }
        }
Exemplo n.º 14
0
        public async void TakePhoto()
        {
            if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
            {
                await DisplayAlert("No Camera", "No camera available.", "OK");

                return;
            }

            var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
            {
                PhotoSize          = PhotoSize.Medium,
                CompressionQuality = 95
            });

            if (file == null)
            {
                return;
            }

            imageStream = file.GetStream();
            byteStream  = new byte[imageStream.Length];
            await imageStream.ReadAsync(byteStream, 0, (int)imageStream.Length);

            // Store as string
            imageStreamBase64 = Convert.ToBase64String(byteStream);
            PostImage.Source  = ImageSource.FromStream(() => new MemoryStream(byteStream));
        }
 /// <summary>Standard read/write loop using ReadAsync on the source and WriteAsync on the destination.</summary>
 private static async Task ArrayPoolCopyToAsyncCore(Stream source, Stream destination, int bufferSize, CancellationToken cancellationToken)
 {
     byte[] buffer = ArrayPool<byte>.Shared.Rent(bufferSize);
     bufferSize = 0; // reuse same field for high water mark to avoid needing another field in the state machine
     try
     {
         while (true)
         {
             int bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false);
             if (bytesRead == 0)
             {
                 break;
             }
             if (bytesRead > bufferSize)
             {
                 bufferSize = bytesRead;
             }
             await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
         }
     }
     finally
     {
         Array.Clear(buffer, 0, bufferSize); // clear only the most we used
         ArrayPool<byte>.Shared.Return(buffer, clearArray: false);
     }
 }
Exemplo n.º 16
0
        public async void PickPhoto()
        {
            if (!CrossMedia.Current.IsPickPhotoSupported)
            {
                await DisplayAlert("Photos Not Supported", "Permission not granted to photos.", "OK");

                return;
            }

            var file = await CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions
            {
                PhotoSize          = PhotoSize.Medium,
                CompressionQuality = 95
            });

            if (file == null)
            {
                return;
            }

            imageStream = file.GetStream();
            byteStream  = new byte[imageStream.Length];
            await imageStream.ReadAsync(byteStream, 0, (int)imageStream.Length);

            // Store as string
            imageStreamBase64 = Convert.ToBase64String(byteStream);
            PostImage.Source  = ImageSource.FromStream(() => new MemoryStream(byteStream));
        }
        public async Task<BitmapSource> LoadImageAsync(Stream imageStream, Uri uri)
        {
            if (imageStream == null)
            {
                return null;
            }

            var stream = new InMemoryRandomAccessStream();
            imageStream.CopyTo(stream.AsStreamForWrite());
            stream.Seek(0);

            BitmapImage bitmap = new BitmapImage();
            await bitmap.SetSourceAsync(stream);

            // convert to a writable bitmap so we can get the PixelBuffer back out later...
            // in case we need to edit and/or re-encode the image.
            WriteableBitmap bmp = new WriteableBitmap(bitmap.PixelHeight, bitmap.PixelWidth);
            stream.Seek(0);
            bmp.SetSource(stream);

            List<Byte> allBytes = new List<byte>();
            byte[] buffer = new byte[4000];
            int bytesRead = 0;
            while ((bytesRead = await imageStream.ReadAsync(buffer, 0, 4000)) > 0)
            {
                allBytes.AddRange(buffer.Take(bytesRead));
            }

            DataContainerHelper.Instance.WriteableBitmapToStorageFile(bmp, uri);

            return bmp;
        }
Exemplo n.º 18
0
 public async Task<string> ToBase64String(Stream stream)
 {
     var buffer = new byte[stream.Length];
     await stream.ReadAsync(buffer, 0, buffer.Length);
     var base64String = ToBase64String(buffer);
     return base64String;
 }
Exemplo n.º 19
0
        public static async Task ReadExactly(int length, System.IO.Stream stream, ArraySegment <byte> buffer, CancellationToken cancellationToken)
        {
            if (length == 0)
            {
                return;
            }

            if (buffer.Count < length)
            {
                // This will happen if the calling function supplied a buffer that was too small to fit the payload of the websocket frame.
                // Note that this can happen on the close handshake where the message size can be larger than the regular payload
                throw new InternalBufferOverflowException($"Unable to read {length} bytes into buffer (offset: {buffer.Offset} size: {buffer.Count}). Use a larger read buffer");
            }

            int offset = 0;

            do
            {
                int bytesRead = await stream.ReadAsync(buffer.Array, buffer.Offset + offset, length - offset, cancellationToken);

                if (bytesRead == 0)
                {
                    throw new EndOfStreamException(string.Format("Unexpected end of stream encountered whilst attempting to read {0:#,##0} bytes", length));
                }

                offset += bytesRead;
            } while (offset < length);

            return;
        }
Exemplo n.º 20
0
        public async Task<long> ReadSegments(
            IObserver<RecordSegment> observer,
            Stream stream,
            CancellationToken cancellationToken) {
            var lastOffset = stream.Position;

            var buffer = new byte[this.BufferSize];
            var kmpStateStart = 0;
            var kmpStateEnd = 0;
            var lastStartOffesets = new long[0];

            while (true) {
                var baseOffset = stream.Position;

                var count = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken);

                if (count == 0)
                    return lastOffset;

                var startOffsets = KmpUtil.GetOccurences(
                    EventStart,
                    new ArraySegment<byte>(buffer, 0, count),
                    cancellationToken,
                    ref kmpStateStart);

                var endOffsets = KmpUtil.GetOccurences(
                    EventEnd,
                    new ArraySegment<byte>(buffer, 0, count),
                    cancellationToken,
                    ref kmpStateEnd);

                if (endOffsets.Count == 0 || (startOffsets.Count + lastStartOffesets.Length) == 0)
                    return lastOffset;

                var segments = endOffsets.Zip(
                    lastStartOffesets
                        .Concat(startOffsets.Select(offset => baseOffset + offset))
                        .Take(endOffsets.Count),
                    (end, start) =>
                    new RecordSegment(start, (int) (baseOffset + end + EventEnd.Length - start)));

                var empty = true;
                foreach (var segment in segments) {
                    empty = false;
                    observer.OnNext(segment);
                    lastOffset = segment.End;
                }

                if (empty)
                    return lastOffset;

                lastStartOffesets = lastStartOffesets
                    .Concat(startOffsets.Select(x => baseOffset + x))
                    .Skip(endOffsets.Count)
                    .ToArray();
            }
        }
        public async Task<FastCGIPacket> ReadFromAsync(Stream ClientStream)
        {
            var Header = new byte[8];
            await ClientStream.ReadAsync(Header, 0, Header.Length);
            var Version = Header[0];
            Type = (FastCGI.PacketType)Header[1];
            RequestId = (ushort)((Header[2] << 8) | (Header[3] << 0));
            var ContentLength = (ushort)((Header[4] << 8) | (Header[5] << 0));
            var PaddingLength = Header[6];
            Content = new ArraySegment<byte>(new byte[ContentLength]);
            if (ContentLength + PaddingLength > 0)
            {
                await ClientStream.ReadAsync(Content.Array, 0, ContentLength);
                await ClientStream.ReadAsync(PaddingRead, 0, PaddingLength);
            }

            return this;
        }
Exemplo n.º 22
0
 public async Task AddFileAsync(string outputPath, Stream sourceStream)
 {
     // There could be some problems in this implementation if the
     // length is too much. Hopefully for the tests, that's not the case
     // and it's mostly ok.
     byte[] contents = new byte[sourceStream.Length];
     await sourceStream.ReadAsync(contents, 0, contents.Length);
     _files[outputPath] = Encoding.UTF8.GetString(contents);
 }
Exemplo n.º 23
0
	public async void StreamCopyTo(Stream destination, int bufferSize)
	{
		byte[] array = new byte[bufferSize];
		int count;
		while ((count = await destination.ReadAsync(array, 0, array.Length)) != 0)
		{
			await destination.WriteAsync(array, 0, count);
		}
	}
Exemplo n.º 24
0
        private async Task AssertHeaderAndOpen(Stream stream)
        {
            var buffer = new byte[2000];
            int bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);
            var head = Encoding.UTF8.GetString(buffer, 0, bytesRead);
            Assert.True(head.StartsWith(string.Format(s_head, "callback")));
            Assert.True(bytesRead > 1024);

            if (head.EndsWith(s_open))
            {
                Assert.True(head.EndsWith(s_open));
            }
            else
            {
                bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);
                var open = Encoding.UTF8.GetString(buffer, 0, bytesRead);
                Assert.Equal(s_open, open);
            }
        }
Exemplo n.º 25
0
 public static async Task CopyDataBlocksToStreamAsync(Stream sourceStream, Stream targetStream, CancellationToken cancellationToken = default(CancellationToken))
 {
     int len;
     while ((len = await sourceStream.ReadByteAsync()) > 0)
     {
         byte[] bytes = new byte[len];
         await sourceStream.ReadAsync(bytes, 0, len, cancellationToken).ConfigureAwait(false);
         await targetStream.WriteAsync(bytes, 0, len, cancellationToken);
     }
 }
Exemplo n.º 26
0
        public async Task<HttpResponseMessage> Save(Stream dataStream, User user, string photoDescription, double? rating, string fileExtension, bool isAvatar)
        {

            var bufferOriginal = new byte[dataStream.Length];
            var maxSize = user.Account.MaxSize;
            var photos = user.Photos;
            var currentSize = (photos == null ? 0 : photos.Sum(p => p.Size));
            // counting in MB
            if (maxSize != null &&  currentSize + (double) bufferOriginal.Length / 1024 / 1024 > maxSize)
            {
                var response = new HttpResponseMessage(HttpStatusCode.MethodNotAllowed);
                response.Content = new StringContent("The file doesn't meet size limit requirements", Encoding.UTF8, "text/plain");
                return response;
            };
            await dataStream.ReadAsync(bufferOriginal, 0, (int)dataStream.Length);
            //var prefix = System.Configuration. .ConfigurationManager.AppSettings["pathSave"] ?? "~/images/";
            var prefix = "~/images/";
            var uploadFolderOriginal = prefix + user.Login + "/original/";
            var uploadFolderPreview = prefix + user.Login + "/preview/";
            var rootOriginal = HttpContext.Current.Server.MapPath(uploadFolderOriginal);
            var rootPreview = HttpContext.Current.Server.MapPath(uploadFolderPreview);
            Directory.CreateDirectory(rootOriginal);
            Directory.CreateDirectory(rootPreview);

            var bufferPreview = PhotoConverter.Resize(bufferOriginal, 100, 100);

            var suffix = Guid.NewGuid() + "." + fileExtension;
            rootOriginal +=  suffix;
            uploadFolderOriginal += suffix;
            rootPreview += suffix;
            uploadFolderPreview += suffix;
            using (var stream = new FileStream(rootOriginal, FileMode.OpenOrCreate))
            {
                await stream.WriteAsync(bufferOriginal, 0, bufferOriginal.Length);
            }
            using (var stream = new FileStream(rootPreview, FileMode.OpenOrCreate))
            {
                await stream.WriteAsync(bufferPreview, 0, bufferPreview.Length);
            }

            var photo = new Photo
            {
                Description = photoDescription,
                User = user,
                Size = (double)dataStream.Length / 1024 / 1024,
                Rating = rating,
                SrcOriginal = uploadFolderOriginal,
                SrcPreview = uploadFolderPreview,
            };
            photosDb.Add(photo);
            photosDb.Save();
            var result = new HttpResponseMessage(HttpStatusCode.OK);
            result.Content = new StringContent("Successful upload", Encoding.UTF8, "text/plain");
            return result;
        }
Exemplo n.º 27
0
        public override async Task <int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
        {
            await _handshaker.EnsureHandshakeCompleteAsync(MultistreamHandshaker.HandshakeDirection.Incoming, cancellationToken).ConfigureAwait(false);

            if (count == 0)
            {
                return(0);
            }

            return(await _stream.ReadAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false));
        }
Exemplo n.º 28
0
 private static int EndRead(Stream stream, IAsyncResult ar, byte[] buffer)
 {
     try
     {
         return stream.EndRead(ar);
     }
     catch (Exception)
     {
         return stream.ReadAsync(buffer).Result;
     }
 }
Exemplo n.º 29
0
 private async Task<byte[]> ReadBytesAsync(Stream stream, int count, byte[] buffer = null, CancellationToken cancellationToken = default(CancellationToken)) {
    buffer = buffer ?? new byte[count];
    int bytesRemaining = count;
    int totalBytesRead = 0;
    while (bytesRemaining > 0) {
       int bytesRead = await stream.ReadAsync(buffer, totalBytesRead, bytesRemaining, cancellationToken).ConfigureAwait(false);
       bytesRemaining -= bytesRead;
       totalBytesRead += bytesRead;
    }
    return buffer;
 }
Exemplo n.º 30
0
 public static async Task<Message> DeserializeFromStreamAsync(Stream stream)
 {
     byte[] buffer = new byte[sizeof(uint) + 12 + sizeof(uint) + sizeof(uint)];
     await stream.ReadAsync(buffer, 0, buffer.Length);
     Message message = new Message();
     using (MemoryStream ms = new MemoryStream(buffer, false))
     using (BinaryReader reader = new BinaryReader(ms))
     {
         if (reader.ReadUInt32() != Magic)
             throw new FormatException();
         message.Command = reader.ReadFixedString(12);
         uint length = reader.ReadUInt32();
         if (length > 0x02000000)
             throw new FormatException();
         message.Checksum = reader.ReadUInt32();
         message.Payload = new byte[length];
     }
     await stream.ReadAsync(message.Payload, 0, message.Payload.Length);
     return message;
 }
Exemplo n.º 31
0
        public static async Task ReadLoop(Stream stream)
        {
            var buffer = new byte[4096];
            var count = -1;
            while (count != 0)
            {
                count = await stream.ReadAsync(buffer, 0, buffer.Length);

                //Console.WriteLine(Encoding.UTF8.GetString(buffer, 0, count) +"->Responsed");
                _packetReceived++;
            }
        }
		public async Task<Uri> UploadMessageAsync(Stream encryptedMessageContent, DateTime expiration, string contentType, string contentEncoding, CancellationToken cancellationToken) {
			Assert.That(encryptedMessageContent.Length, Is.GreaterThan(0));
			Assert.That(encryptedMessageContent.Position, Is.EqualTo(0));

			var buffer = new byte[encryptedMessageContent.Length - encryptedMessageContent.Position];
			await encryptedMessageContent.ReadAsync(buffer, 0, buffer.Length);
			lock (this.blobs) {
				var contentUri = new Uri(BaseUploadUri + (this.blobs.Count + 1));
				this.blobs[contentUri] = buffer;
				return contentUri;
			}
		}
Exemplo n.º 33
0
		public async Task<FastcgiPacket> ReadFromAsync(Stream ClientStream)
		{
			var Header = new byte[8];
			await ClientStream.ReadAsync(Header, 0, Header.Length);
			var Version = Header[0];
			Type = (Fastcgi.PacketType)Header[1];
			//BitConverter.ToUInt16()
			RequestId = (ushort)((Header[2] << 8) | (Header[3] << 0));
			var ContentLength = (ushort)((Header[4] << 8) | (Header[5] << 0));
			var PaddingLength = Header[6];
			Content = new ArraySegment<byte>(new byte[ContentLength]);
			if (ContentLength + PaddingLength > 0)
			{
				await ClientStream.ReadAsync(Content.Array, 0, ContentLength);
				await ClientStream.ReadAsync(PaddingRead, 0, PaddingLength);
			}

			//await Console.Out.WriteLineAsync(String.Format("READ: Packet({0}): {1}({2}:{3})", RequestId, Type, ContentLength, PaddingLength));

			return this;
		}
Exemplo n.º 34
0
        protected override async Task<int> ReadStreamAsync(Stream stream, byte[] buffer, string key)
        {
            int read = await stream.ReadAsync(buffer, 0, buffer.Length);

            // Close stream after download is finished and allow caching of the file
            if (read == 0)
            {
                stream.Dispose();
            }

            return read;
        }
        private static async Task ReadResponseStreamAsync(Stream responseStream)
        {
            byte[] readBuffer = new byte[512];

            int bytesRead;
            while ((bytesRead = await responseStream.ReadAsync(readBuffer, 0, readBuffer.Length)) != 0)
            {
                string content = Encoding.UTF8.GetString(readBuffer, 0, bytesRead);

                Console.WriteLine("Received: {0}", content);
            }
        }
Exemplo n.º 36
0
        public override int Read(byte[] buffer, int offset, int count)
        {
#if NETFX_CORE
            var task = stream.ReadAsync(buffer, offset, count);
            task.Wait();
            if (task.IsFaulted)
            {
                throw task.Exception;
            }
            return(task.Result);
#else
            return(stream.Read(buffer, offset, count));
#endif
        }
        private async Task copyStreamAsync(System.IO.Stream input, System.IO.Stream output, int bufferSize)
        {
            byte[] buffer;
            int    len;

            buffer = new byte[bufferSize];

            while ((len = await input.ReadAsync(buffer, 0, bufferSize)) > 0)
            {
                await output.WriteAsync(buffer, 0, len);
            }

            await output.FlushAsync();
        }
Exemplo n.º 38
0
        /// <summary>
        /// Copies potentially large stream, raising event as percent changes
        /// </summary>
        /// <param name="inStream">still open</param>
        /// <param name="outStream">flushed, but still open</param>
        /// <param name="inLength"></param>
        public static async Task StreamCopyWithProgress(System.IO.Stream inStream, System.IO.Stream outStream, long inLength)
        {
            byte[] zBuffer   = new byte[16384];
            int    zLen      = 0;
            int    pct       = 0;
            int    lastPct   = 0;
            double totalToDo = inLength;

            if (totalToDo == 0)
            {
                totalToDo = 1024 * 1024 * 1024;
            }
            long    doneSoFar = 0;
            Boolean Copying   = true;

            while (Copying)
            {
                zLen = await inStream.ReadAsync(zBuffer, 0, zBuffer.Length);

                if (zLen == 0)
                {
                    Copying = false;
                }
                else
                {
                    await outStream.WriteAsync(zBuffer, 0, zLen);

                    doneSoFar += zLen;
                    if (totalToDo < (doneSoFar + zBuffer.Length))
                    {
                        totalToDo *= 2;
                    }
                    pct = Convert.ToInt32((doneSoFar / totalToDo) * 100);
                    if (pct > lastPct)
                    {
                        if (Progress != null)
                        {
                            Progress(inStream, pct);
                        }
                        lastPct = pct;
                    }
                }
            }
            if (Progress != null)
            {
                Progress(inStream, 100);
            }
            outStream.Flush();
        }
Exemplo n.º 39
0
        public static async Task <string> ReadNextTokenAsync(System.IO.Stream stream, CancellationToken cancellationToken)
        {
            var length = await Binary.Varint.ReadUInt64Async(stream).ConfigureAwait(false);

            if (length == 0)
            {
                return(string.Empty);
            }

            if (length > 64 * 1024)
            {
                await DelimWriteBufferedAsync(stream, BytesMessageTooLarge, cancellationToken).ConfigureAwait(false);

                throw ErrMessageTooLarge;
            }

            var buffer = new byte[length];
            int res    = 0;
            int total  = 0;

            while ((res = await stream.ReadAsync(buffer, total, buffer.Length - total, cancellationToken).ConfigureAwait(false)) > 0)
            {
                total += res;
                if (total == (int)length)
                {
                    break;
                }

                await Task.Delay(1, cancellationToken).ConfigureAwait(false);
            }
            if (res <= 0)
            {
                return(string.Empty);
            }

            if (total != buffer.Length)
            {
                throw new Exception("could not read token");
            }

            if ((buffer.Length == 0) || (buffer[length - 1] != Delimiter))
            {
                throw ErrMessageMissingNewline;
            }

            return(Encoding.UTF8.GetString(buffer, 0, buffer.Length - 1));
        }
Exemplo n.º 40
0
        /// <summary>
        /// Writes a stream to another stream asynchronously
        /// </summary>
        /// <param name="readStream">the stream you need to read.</param>
        /// <param name="writeStream">the stream you want to write to.</param>
        /// <param name="onPercentageUpdate">Action to take on write percentage update.</param>
        public static async Task ReadWriteStreamAsync(Stream readStream, Stream writeStream, Action <int> onPercentageUpdate)
        {
            onPercentageUpdate?.Invoke(0);

            int Length = 65536;

            byte[] buffer = new byte[Length];

            long totalBytes    = readStream.Length;
            long receivedBytes = 0;

            int i = 0;
            int updateFrequency = 2;

            for (;;)
            {
                int bytesRead;
                if (i % updateFrequency == 0)
                {
                    bytesRead = await readStream.ReadAsync(buffer, 0, buffer.Length);
                }
                else
                {
                    bytesRead = readStream.Read(buffer, 0, buffer.Length);
                }

                if (bytesRead == 0)
                {
                    await Task.Yield();

                    break;
                }

                writeStream.Write(buffer, 0, bytesRead);
                receivedBytes += bytesRead;

                if (i++ % updateFrequency == 0)
                {
                    onPercentageUpdate?.Invoke((int)(((float)receivedBytes / totalBytes) * 100));
                }
            }

            onPercentageUpdate?.Invoke(100);

            readStream.Dispose();
            writeStream.Dispose();
        }
Exemplo n.º 41
0
        public static async Task <int> WriteFileAsync(System.IO.Stream stream, string path)
        {
            const int FILE_WRITE_SIZE = 84975;//写出缓冲区大小
            int       writeCount      = 0;

            using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Write, FILE_WRITE_SIZE, true)) {
                byte[] byteArr   = new byte[FILE_WRITE_SIZE];
                int    readCount = 0;
                while ((readCount = await stream.ReadAsync(byteArr, 0, byteArr.Length)) > 0)
                {
                    await fileStream.WriteAsync(byteArr, 0, readCount);

                    writeCount += readCount;
                }
            }
            return(writeCount);
        }
Exemplo n.º 42
0
        private static async Task <int> WriteFileAsync(System.IO.Stream stream, string path)
        {
            int writeCount = 0;

            using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Write, UploadConstants.BigFileWriteSize, true))
            {
                byte[] byteArr   = new byte[UploadConstants.BigFileWriteSize];
                int    readCount = 0;
                while ((readCount = await stream.ReadAsync(byteArr, 0, byteArr.Length)) > 0)
                {
                    await fileStream.WriteAsync(byteArr, 0, readCount);

                    writeCount += readCount;
                }
            }
            return(writeCount);
        }
Exemplo n.º 43
0
    async void OnMessage
        (Windows.Networking.Sockets.DatagramSocket sender,
        Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args)
    {
        using (System.IO.Stream stream = args.GetDataStream().AsStreamForRead())
        {
            // 受信データを取得
            byte[] receiveBytes = new byte[MAX_BUFFER_SIZE];
            await stream.ReadAsync(receiveBytes, 0, MAX_BUFFER_SIZE);

            lock (p_LockObject)
            {
                // 受信データを処理に引き渡す
                UDPReceiveEvent(receiveBytes);
            }
        }
    }
Exemplo n.º 44
0
        /// <summary>
        /// Creates an enumerable to be used with TaskFactoryExtensions.Iterate that copies data from one
        /// stream to another.
        /// </summary>
        /// <param name="input">The input stream.</param>
        /// <param name="output">The output stream.</param>
        /// <returns>An enumerable containing yielded tasks from the copy operation.</returns>
        private static IEnumerable <Task> CopyStreamIteratorUnicodeToUtf8Encoding(System.IO.Stream input, System.IO.Stream output)
        {
            // Create two buffers.  One will be used for the current read operation and one for the current
            // write operation.  We'll continually swap back and forth between them.
            byte[][] buffers         = new byte[2][] { new byte[BUFFER_SIZE], new byte[BUFFER_SIZE] };
            int      filledBufferNum = 0;
            Task     writeTask       = null;

            // Until there's no more data to be read
            while (true)
            {
                // Read from the input asynchronously
                var readTask = input.ReadAsync(buffers[filledBufferNum], 0, buffers[filledBufferNum].Length);

                // If we have no pending write operations, just yield until the read operation has
                // completed.  If we have both a pending read and a pending write, yield until both the read
                // and the write have completed.
                if (writeTask == null)
                {
                    yield return(readTask);

                    readTask.Wait(); // propagate any exception that may have occurred
                }
                else
                {
                    var tasks = new[] { readTask, writeTask };
                    yield return(Task.Factory.WhenAll(tasks));

                    Task.WaitAll(tasks); // propagate any exceptions that may have occurred
                }

                // If no data was read, nothing more to do.
                if (readTask.Result <= 0)
                {
                    break;
                }

                // Otherwise, write the written data out to the file
                writeTask = output.WriteAsyncUnicodeToUtf8Encoding(buffers[filledBufferNum], 0, readTask.Result);

                // Swap buffers
                filledBufferNum ^= 1;
            }
        }
Exemplo n.º 45
0
        /// <summary>
        /// Creates an enumerable to be used with TaskFactoryExtensions.Iterate that reads data
        /// from an input stream and passes it to a user-provided delegate.
        /// </summary>
        /// <param name="input">The source stream.</param>
        /// <param name="bufferSize">The size of the buffers to be used.</param>
        /// <param name="bufferAvailable">
        /// A delegate to be invoked when a buffer is available (provided the
        /// buffer and the number of bytes in the buffer starting at offset 0.
        /// </param>
        /// <returns>An enumerable containing yielded tasks from the operation.</returns>
        private static IEnumerable <Task> ReadIterator(System.IO.Stream input, int bufferSize, Action <byte[], int> bufferAvailable)
        {
            // Create a buffer that will be used over and over
            var buffer = new byte[bufferSize];

            // Until there's no more data
            while (true)
            {
                // Asynchronously read a buffer and yield until the operation completes
                var readTask = input.ReadAsync(buffer, 0, buffer.Length);
                yield return(readTask);

                // If there's no more data in the stream, we're done.
                if (readTask.Result <= 0)
                {
                    break;
                }

                // Otherwise, hand the data off to the delegate
                bufferAvailable(buffer, readTask.Result);
            }
        }
Exemplo n.º 46
0
        private static async Task VerifyPipeAsync(System.IO.Stream a, System.IO.Stream b)
        {
            var mes = new byte[1024];

            new Random().NextBytes(mes);
            mes[0] = 0x01;

            var aw = a.WriteAsync(mes, 0, mes.Length);
            var bw = b.WriteAsync(mes, 0, mes.Length);

            await Task.WhenAll(aw, bw).ConfigureAwait(false);

            var buf = new byte[mes.Length];
            var n   = await a.ReadAsync(buf, 0, buf.Length).ConfigureAwait(false);

            Assert.Equal(n, buf.Length);
            Assert.Equal(buf, mes);

            n = await b.ReadAsync(buf, 0, buf.Length).ConfigureAwait(false);

            Assert.Equal(n, buf.Length);
            Assert.Equal(buf, mes);
        }
Exemplo n.º 47
0
 public static Task <int> ReadAsync(this Stream stream, byte[] buffer)
 {
     return(stream.ReadAsync(buffer, 0, buffer.Length));
 }
Exemplo n.º 48
0
 public static async Task <int> ReadAsync(this Stream stream, JasilyBuffer buffer)
 => await stream.ReadAsync(buffer.Buffer, buffer.Offset, buffer.Count);
Exemplo n.º 49
0
 public static IAsyncResult BeginRead(this Stream s, byte[] buffer, int offset, int count, AsyncCallback callback, object state)
 {
     return(s.ReadAsync(buffer, offset, count).ToApm <int>(callback, state));
 }
Exemplo n.º 50
0
        public async Task LoadPreviewImage()
        {
            // Geting url for download
            var    httpClient   = new System.Net.Http.HttpClient();
            string fullPathFrom = AddBackslash(Path);

            fullPathFrom += Name;

            string url     = "https://graph.microsoft.com/v1.0/me" + fullPathFrom + ":/thumbnails/0/medium";
            var    request = new System.Net.Http.HttpRequestMessage(HttpMethod.Get, url);

            request.Version = Version.Parse("1.0");
            request.Headers.Authorization =
                new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", Account.Token);
            System.Net.Http.HttpResponseMessage response = await httpClient.SendAsync(request);

            string urlImage = null;

            using (System.IO.Stream stream = await response.Content.ReadAsStreamAsync())
            {
                DeserializedImage deserializedImage = new DeserializedImage();

                DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedImage.GetType());
                deserializedImage = ser.ReadObject(stream) as DeserializedImage;

                if (deserializedImage == null || String.IsNullOrEmpty(deserializedImage.Url))
                {
                    throw new NullReferenceException("Couldn't deserialized the data");
                }
                urlImage = deserializedImage.Url;
            }

            // Download image
            httpClient = new System.Net.Http.HttpClient();
            request    = new System.Net.Http.HttpRequestMessage(HttpMethod.Get, urlImage);
            response   = await httpClient.SendAsync(request);

            using (var streamCopy = new MemoryStream())
            {
                using (System.IO.Stream stream = await response.Content.ReadAsStreamAsync())
                {
                    Byte[] bytes  = new Byte[1024];
                    int    reader = 0;
                    do
                    {
                        reader = await stream.ReadAsync(bytes, 0, bytes.Length);

                        await streamCopy.WriteAsync(bytes, 0, reader);
                    } while (reader > 0);

                    await CoreApplication.MainView.Dispatcher.RunAsync(CoreDispatcherPriority.Low, async() =>
                    {
                        PreviewImage = new BitmapImage();
                        // ReSharper disable AccessToDisposedClosure
                        streamCopy.Seek(0, SeekOrigin.Begin);
                        await PreviewImage.SetSourceAsync(streamCopy.AsRandomAccessStream());
                        // ReSharper restore AccessToDisposedClosure
                    });
                }
            }
        }
Exemplo n.º 51
0
 public static Task <int> ReadAsync(this Stream stream, byte[] buffer, CancellationToken cancellationToken)
 {
     return(stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken));
 }
Exemplo n.º 52
0
        /// <summary>BufferedStream should be as thin a wrapper as possible. We want ReadAsync to delegate to
        /// ReadAsync of the underlying _stream rather than calling the base Stream which implements the one in terms of the other.
        /// This allows BufferedStream to affect the semantics of the stream it wraps as little as possible. </summary>
        /// <returns>-2 if _bufferSize was set to 0 while waiting on the semaphore; otherwise num of bytes read.</returns>
        private async Task <int> ReadFromUnderlyingStreamAsync(byte[] array, int offset, int count,
                                                               CancellationToken cancellationToken,
                                                               int bytesAlreadySatisfied,
                                                               Task semaphoreLockTask)
        {
            // Same conditions validated with exceptions in ReadAsync:
            // (These should be Debug.Requires(..) but that method had some issues in async methods; using Assert(..) for now.)
            Debug.Assert(array != null);
            Debug.Assert(offset >= 0);
            Debug.Assert(count >= 0);
            Debug.Assert(array.Length - offset >= count);
            Debug.Assert(_stream != null);
            Debug.Assert(_stream.CanRead);
            Debug.Assert(_bufferSize > 0);
            Debug.Assert(semaphoreLockTask != null);

            // Employ async waiting based on the same synchronization used in BeginRead of the abstract Stream.
            await semaphoreLockTask.ConfigureAwait(false);

            try
            {
                // The buffer might have been changed by another async task while we were waiting on the semaphore.
                // Check it now again.
                int bytesFromBuffer = ReadFromBuffer(array, offset, count);
                if (bytesFromBuffer == count)
                {
                    return(bytesAlreadySatisfied + bytesFromBuffer);
                }

                if (bytesFromBuffer > 0)
                {
                    count  -= bytesFromBuffer;
                    offset += bytesFromBuffer;
                    bytesAlreadySatisfied += bytesFromBuffer;
                }

                Debug.Assert(_readLen == _readPos);
                _readPos = _readLen = 0;

                // If there was anything in the write buffer, clear it.
                if (_writePos > 0)
                {
                    await FlushWriteAsync(cancellationToken).ConfigureAwait(false);  // no Begin-End read version for Flush. Use Async.
                }
                // If the requested read is larger than buffer size, avoid the buffer and still use a single read:
                if (count >= _bufferSize)
                {
                    return(bytesAlreadySatisfied + await _stream.ReadAsync(array, offset, count, cancellationToken).ConfigureAwait(false));
                }

                // Ok. We can fill the buffer:
                EnsureBufferAllocated();
                _readLen = await _stream.ReadAsync(_buffer, 0, _bufferSize, cancellationToken).ConfigureAwait(false);

                bytesFromBuffer = ReadFromBuffer(array, offset, count);
                return(bytesAlreadySatisfied + bytesFromBuffer);
            }
            finally
            {
                SemaphoreSlim sem = LazyEnsureAsyncActiveSemaphoreInitialized();
                sem.Release();
            }
        }
Exemplo n.º 53
0
 /// <summary>
 /// Read remaining buffer from offset
 /// </summary>
 /// <param name="stream"></param>
 /// <param name="buffer"></param>
 /// <param name="offset"></param>
 /// <param name="ct"></param>
 /// <returns></returns>
 public static Task <int> ReadAsync(this Stream stream, byte[] buffer, int offset,
                                    CancellationToken ct)
 {
     return(stream.ReadAsync(buffer, offset, buffer.Length - offset, ct));
 }
Exemplo n.º 54
0
 public static async Task <int> ReadAsync(this Stream stream, byte[] buffer, CancellationToken cancellationToken) =>
 await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken);
        }  // ReadAsync_MemoryStream

        internal static IAsyncOperationWithProgress <IBuffer, uint> ReadAsync_AbstractStream(Stream stream, IBuffer buffer, uint count,
                                                                                             InputStreamOptions options)
        {
            Debug.Assert(stream != null);
            Debug.Assert(stream.CanRead);
            Debug.Assert(buffer != null);
            Debug.Assert(buffer is IBufferByteAccess);
            Debug.Assert(0 <= count);
            Debug.Assert(count <= int.MaxValue);
            Debug.Assert(count <= buffer.Capacity);
            Debug.Assert(options == InputStreamOptions.None || options == InputStreamOptions.Partial || options == InputStreamOptions.ReadAhead);

            int bytesRequested = (int)count;

            // Check if the buffer is our implementation.
            // IF YES: In that case, we can read directly into its data array.
            // IF NO:  The buffer is of unknown implementation. It's not backed by a managed array, but the wrapped stream can only
            //         read into a managed array. If we used the user-supplied buffer we would need to copy data into it after every read.
            //         The spec allows to return a buffer instance that is not the same as passed by the user. So, we will create an own
            //         buffer instance, read data *directly* into the array backing it and then return it to the user.
            //         Note: the allocation costs we are paying for the new buffer are unavoidable anyway, as we we would need to create
            //         an array to read into either way.

            IBuffer dataBuffer = buffer as WindowsRuntimeBuffer;

            if (dataBuffer == null)
            {
                dataBuffer = WindowsRuntimeBuffer.Create((int)Math.Min((uint)int.MaxValue, buffer.Capacity));
            }

            // This operation delegate will we run inside of the returned IAsyncOperationWithProgress:
            Func <CancellationToken, IProgress <uint>, Task <IBuffer> > readOperation = async(cancelToken, progressListener) =>
            {
                // No bytes read yet:
                dataBuffer.Length = 0;

                // Get the buffer backing array:
                byte[] data;
                int    offset;
                bool   managedBufferAssert = dataBuffer.TryGetUnderlyingData(out data, out offset);
                Debug.Assert(managedBufferAssert);

                // Init tracking values:
                bool done           = cancelToken.IsCancellationRequested;
                int  bytesCompleted = 0;

                // Loop until EOS, cancelled or read enough data according to options:
                while (!done)
                {
                    int bytesRead = 0;

                    try
                    {
                        // Read asynchronously:
                        bytesRead = await stream.ReadAsync(data, offset + bytesCompleted, bytesRequested - bytesCompleted, cancelToken)
                                    .ConfigureAwait(continueOnCapturedContext: false);

                        // We will continue here on a different thread when read async completed:
                        bytesCompleted += bytesRead;
                        // We will handle a cancelation exception and re-throw all others:
                    }
                    catch (OperationCanceledException)
                    {
                        // We assume that cancelToken.IsCancellationRequested is has been set and simply proceed.
                        // (we check cancelToken.IsCancellationRequested later)
                        Debug.Assert(cancelToken.IsCancellationRequested);

                        // This is because if the cancellation came after we read some bytes we want to return the results we got instead
                        // of an empty cancelled task, so if we have not yet read anything at all, then we can throw cancellation:
                        if (bytesCompleted == 0 && bytesRead == 0)
                        {
                            throw;
                        }
                    }

                    // Update target buffer:
                    dataBuffer.Length = (uint)bytesCompleted;

                    Debug.Assert(bytesCompleted <= bytesRequested);

                    // Check if we are done:
                    done = options == InputStreamOptions.Partial || // If no complete read was requested, any amount of data is OK
                           bytesRead == 0 ||                          // this implies EndOfStream
                           bytesCompleted == bytesRequested ||        // read all requested bytes
                           cancelToken.IsCancellationRequested;       // operation was cancelled

                    // Call user Progress handler:
                    if (progressListener != null)
                    {
                        progressListener.Report(dataBuffer.Length);
                    }
                }  // while (!done)

                // If we got here, then no error was detected. Return the results buffer:
                return(dataBuffer);
            };  // readOperation

            return(AsyncInfo.Run <IBuffer, UInt32>(readOperation));
        }  // ReadAsync_AbstractStream
Exemplo n.º 56
0
 private Task <int> InternalReadAsync(byte[] buffer, int offset, int count)
 {
     return(stream.ReadAsync(buffer, offset, count));
 }
Exemplo n.º 57
0
        public async Task DownloadData()
        {
            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36");
                try
                {
                    const long TimeoutLimit = 10000;

                    CancellationTokenSource.Token.ThrowIfCancellationRequested();
                    HttpResponseMessage message = await client.GetAsync(_link, HttpCompletionOption.ResponseHeadersRead);

                    long?            totalLength = message.Content.Headers.ContentLength;
                    System.IO.Stream stream      = await message.Content.ReadAsStreamAsync();

                    _tempFile = System.IO.Path.GetTempFileName();

                    AddRollbackOperation(() =>
                                         System.IO.File.Delete(_tempFile));

                    System.IO.FileStream fsStream = new System.IO.FileStream(_tempFile, System.IO.FileMode.Create);

                    AddRollbackOperation(() =>
                    {
                        if (fsStream.CanRead || fsStream.CanWrite)
                        {
                            fsStream.Close();
                        }
                    });

                    byte[] buffer        = new byte[8192];
                    long   currentLength = 0;

                    int len = 0;

                    bool finished = false;

                    System.Threading.CancellationTokenSource cancSrc =
                        new System.Threading.CancellationTokenSource();

                    DateTime time = DateTime.Now;

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                    Task timeoutTask = Task.Run(async() =>
                    {
                        while (!finished)
                        {
                            if ((DateTime.Now - time).TotalMilliseconds > TimeoutLimit)
                            {
                                cancSrc.Cancel();
                                stream.Close();
                                break;
                            }
                            else
                            {
                                await Task.Delay(1);
                            }
                        }
                    });
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed

                    while ((len = await stream.ReadAsync(buffer, 0, buffer.Length, cancSrc.Token)) > 0)
                    {
                        if (timeoutTask.IsCompleted)
                        {
                            timeoutTask.Start();
                        }

                        time = DateTime.Now;

                        CancellationTokenSource.Token.ThrowIfCancellationRequested();
                        fsStream.Write(buffer, 0, len);
                        currentLength += len;
                        double percentage = (double)currentLength / (totalLength ?? -currentLength) * 5500;
                        Percentage = _previousPercentage + ((percentage > 0) ? (int)percentage : 0);
                    }

                    finished = true;

                    Percentage = 7500;

                    fsStream.Close();
                }
                catch (HttpRequestException)
                {
                    System.Windows.Forms.MessageBox.Show("Cannot download program data. Please, check your internet connection.", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                    await OperationsRollback();

                    Environment.Exit(-1);
                }
                catch (ObjectDisposedException)
                {
                    System.Windows.Forms.MessageBox.Show("Cannot download program data. Please, check your internet connection.", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                    await OperationsRollback();

                    Environment.Exit(-1);
                }
                catch (System.OperationCanceledException)
                {
                    await OperationsRollback();

                    Environment.Exit(0);
                }
                catch (Exception e)
                {
                    System.Windows.Forms.MessageBox.Show("Oops, that's so embarrassing... An error occurred: " + e.ToString(), "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                    CurrentDescription = "Operations rollback...";
                    await OperationsRollback();

                    Environment.Exit(e.HResult);
                }
            }
        }
Exemplo n.º 58
-1
 /// <summary>
 /// 获取Stream的Base64字符串
 /// </summary>
 /// <param name="stream"></param>
 /// <returns></returns>
 public static async Task<string> GetBase64StringAsync(Stream stream)
 {
     byte[] arr = new byte[stream.Length];
     stream.Position = 0;
     await stream.ReadAsync(arr, 0, (int)stream.Length);
     return Convert.ToBase64String(arr, Base64FormattingOptions.None);
 }