private static bool CanApplyReadMemoryStreamOptimization(Stream stream) { MemoryStream memStream = stream as MemoryStream; if (memStream == null) { return(false); } ArraySegment <byte> arrSeg; return(memStream.TryGetBuffer(out arrSeg)); }
public static IBuffer GetWindowsRuntimeBuffer(this MemoryStream underlyingStream, Int32 positionInStream, Int32 length) { if (underlyingStream == null) { throw new ArgumentNullException(nameof(underlyingStream)); } if (positionInStream < 0) { throw new ArgumentOutOfRangeException(nameof(positionInStream)); } if (length < 0) { throw new ArgumentOutOfRangeException(nameof(length)); } if (underlyingStream.Capacity <= positionInStream) { throw new ArgumentException(SR.Argument_StreamPositionBeyondEOS); } Contract.Ensures(Contract.Result <IBuffer>() != null); Contract.Ensures(Contract.Result <IBuffer>().Length == length); Contract.Ensures(Contract.Result <IBuffer>().Capacity == length); Contract.EndContractBlock(); ArraySegment <byte> streamData; if (!underlyingStream.TryGetBuffer(out streamData)) { throw new UnauthorizedAccessException(SR.UnauthorizedAccess_InternalBuffer); } Int32 originInStream = streamData.Offset; Debug.Assert(underlyingStream.Length <= Int32.MaxValue); Int32 buffCapacity = Math.Min(length, underlyingStream.Capacity - positionInStream); Int32 buffLength = Math.Max(0, Math.Min(length, ((Int32)underlyingStream.Length) - positionInStream)); return(new WindowsRuntimeBuffer(streamData.Array, originInStream + positionInStream, buffLength, buffCapacity)); }
public static IBuffer GetWindowsRuntimeBuffer(this MemoryStream underlyingStream) { if (underlyingStream == null) { throw new ArgumentNullException(nameof(underlyingStream)); } Contract.Ensures(Contract.Result <IBuffer>() != null); Contract.Ensures(Contract.Result <IBuffer>().Length == underlyingStream.Length); Contract.Ensures(Contract.Result <IBuffer>().Capacity == underlyingStream.Capacity); Contract.EndContractBlock(); ArraySegment <byte> streamData; if (!underlyingStream.TryGetBuffer(out streamData)) { throw new UnauthorizedAccessException(SR.UnauthorizedAccess_InternalBuffer); } return(new WindowsRuntimeBuffer(streamData.Array, (Int32)streamData.Offset, (Int32)underlyingStream.Length, underlyingStream.Capacity)); }
internal static IAsyncOperationWithProgress <IBuffer, UInt32> ReadAsync_MemoryStream(Stream stream, IBuffer buffer, UInt32 count) { Debug.Assert(stream != null); Debug.Assert(stream is SREMemoryStream); Debug.Assert(stream.CanRead); Debug.Assert(stream.CanSeek); Debug.Assert(buffer != null); Debug.Assert(buffer is IBufferByteAccess); Debug.Assert(0 <= count); Debug.Assert(count <= Int32.MaxValue); Debug.Assert(count <= buffer.Capacity); Contract.EndContractBlock(); // We will return a different buffer to the user backed directly by the memory stream (avoids memory copy). // This is permitted by the WinRT stream contract. // The user specified buffer will not have any data put into it: buffer.Length = 0; SREMemoryStream memStream = stream as SREMemoryStream; Debug.Assert(memStream != null); try { IBuffer dataBuffer = memStream.GetWindowsRuntimeBuffer((Int32)memStream.Position, (Int32)count); if (dataBuffer.Length > 0) { memStream.Seek(dataBuffer.Length, SeekOrigin.Current); } return(AsyncInfo.CreateCompletedOperation <IBuffer, UInt32>(dataBuffer)); } catch (Exception ex) { return(AsyncInfo.CreateFaultedOperation <IBuffer, UInt32>(ex)); } } // ReadAsync_MemoryStream