| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Runtime.InteropServices; |
| | | 7 | | |
| | | 8 | | namespace System.Reflection.Internal |
| | | 9 | | { |
| | | 10 | | internal static partial class StreamExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Attempts to read all of the requested bytes from the stream into the buffer |
| | | 14 | | /// </summary> |
| | | 15 | | /// <returns> |
| | | 16 | | /// The number of bytes read. Less than <paramref name="count" /> will |
| | | 17 | | /// only be returned if the end of stream is reached before all bytes can be read. |
| | | 18 | | /// </returns> |
| | | 19 | | /// <remarks> |
| | | 20 | | /// Unlike <see cref="Stream.Read(byte[], int, int)"/> it is not guaranteed that |
| | | 21 | | /// the stream position or the output buffer will be unchanged if an exception is |
| | | 22 | | /// returned. |
| | | 23 | | /// </remarks> |
| | | 24 | | internal static int TryReadAll(this Stream stream, byte[] buffer, int offset, int count) |
| | 0 | 25 | | { |
| | | 26 | | // The implementations for many streams, e.g. FileStream, allows 0 bytes to be |
| | | 27 | | // read and returns 0, but the documentation for Stream.Read states that 0 is |
| | | 28 | | // only returned when the end of the stream has been reached. Rather than deal |
| | | 29 | | // with this contradiction, let's just never pass a count of 0 bytes |
| | 0 | 30 | | Debug.Assert(count > 0); |
| | | 31 | | |
| | | 32 | | int totalBytesRead; |
| | | 33 | | int bytesRead; |
| | 0 | 34 | | for (totalBytesRead = 0; totalBytesRead < count; totalBytesRead += bytesRead) |
| | 0 | 35 | | { |
| | | 36 | | // Note: Don't attempt to save state in-between calls to .Read as it would |
| | | 37 | | // require a possibly massive intermediate buffer array |
| | 0 | 38 | | bytesRead = stream.Read(buffer, |
| | 0 | 39 | | offset + totalBytesRead, |
| | 0 | 40 | | count - totalBytesRead); |
| | 0 | 41 | | if (bytesRead == 0) |
| | 0 | 42 | | { |
| | 0 | 43 | | break; |
| | | 44 | | } |
| | 0 | 45 | | } |
| | 0 | 46 | | return totalBytesRead; |
| | 0 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Resolve image size as either the given user-specified size or distance from current position to end-of-strea |
| | | 51 | | /// Also performs the relevant argument validation and publicly visible caller has same argument names. |
| | | 52 | | /// </summary> |
| | | 53 | | /// <exception cref="ArgumentException">size is 0 and distance from current position to end-of-stream can't fit |
| | | 54 | | /// <exception cref="ArgumentOutOfRangeException">Size is negative or extends past the end-of-stream from curren |
| | | 55 | | internal static int GetAndValidateSize(Stream stream, int size, string streamParameterName) |
| | 0 | 56 | | { |
| | 0 | 57 | | long maxSize = stream.Length - stream.Position; |
| | | 58 | | |
| | 0 | 59 | | if (size < 0 || size > maxSize) |
| | 0 | 60 | | { |
| | 0 | 61 | | throw new ArgumentOutOfRangeException(nameof(size)); |
| | | 62 | | } |
| | | 63 | | |
| | 0 | 64 | | if (size != 0) |
| | 0 | 65 | | { |
| | 0 | 66 | | return size; |
| | | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | if (maxSize > int.MaxValue) |
| | 0 | 70 | | { |
| | 0 | 71 | | throw new ArgumentException(SR.StreamTooLarge, streamParameterName); |
| | | 72 | | } |
| | | 73 | | |
| | 0 | 74 | | return (int)maxSize; |
| | 0 | 75 | | } |
| | | 76 | | } |
| | | 77 | | } |
| | | 78 | | |