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 // See the LICENSE file in the project root for more information. 4 5 using System; 6 using System.IO; 7 using System.Runtime.InteropServices.WindowsRuntime; 8 using System.Text; 9 using System.Threading; 10 using System.Threading.Tasks; 11 using Windows.Foundation; 12 using Windows.Storage.Streams; 13 14 namespace System.Net.Http.Functional.Tests 15 { 16 public class MultiInterfaceNonRewindableReadOnlyStream : Stream, IInputStream 17 { 18 byte[] _bytes; 19 int _position; 20 MultiInterfaceNonRewindableReadOnlyStream(byte[] bytes)21 public MultiInterfaceNonRewindableReadOnlyStream(byte[] bytes) 22 { 23 _bytes = bytes; 24 _position = 0; 25 } 26 MultiInterfaceNonRewindableReadOnlyStream(string content)27 public MultiInterfaceNonRewindableReadOnlyStream(string content) 28 { 29 _bytes = Encoding.UTF8.GetBytes(content); 30 _position = 0; 31 } 32 33 public override bool CanRead 34 { 35 get { return true; } 36 } 37 38 public override bool CanSeek 39 { 40 get { return false; } 41 } 42 43 public override bool CanWrite 44 { 45 get { return false; } 46 } 47 Flush()48 public override void Flush() 49 { 50 } 51 52 public override long Length 53 { 54 get { return _bytes.Length; } 55 } 56 57 public override long Position 58 { 59 get 60 { 61 return _position; 62 } 63 set 64 { 65 throw new NotSupportedException(); 66 } 67 } 68 Read(byte[] buffer, int offset, int count)69 public override int Read(byte[] buffer, int offset, int count) 70 { 71 byte[] dataBytes = ReadInternal(count); 72 73 for (int i = 0; i < dataBytes.Length; i++) 74 { 75 buffer[offset + i] = dataBytes[i]; 76 } 77 78 return dataBytes.Length; 79 } 80 Seek(long offset, SeekOrigin origin)81 public override long Seek(long offset, SeekOrigin origin) 82 { 83 throw new NotSupportedException(); 84 } 85 SetLength(long value)86 public override void SetLength(long value) 87 { 88 throw new NotSupportedException(); 89 } 90 Write(byte[] buffer, int offset, int count)91 public override void Write(byte[] buffer, int offset, int count) 92 { 93 throw new NotSupportedException(); 94 } 95 IDisposable.Dispose()96 void IDisposable.Dispose() 97 { 98 } 99 ReadInternal(int count)100 byte[] ReadInternal(int count) 101 { 102 byte[] dataBytes; 103 104 if (_position == _bytes.Length) 105 { 106 dataBytes = new byte[0]; 107 } 108 else 109 { 110 int numBytes = Math.Min(_bytes.Length - _position, count); 111 var stream = new MemoryStream(_bytes, _position, numBytes); 112 _position += numBytes; 113 dataBytes = stream.ToArray(); 114 } 115 116 return dataBytes; 117 } 118 IInputStream.ReadAsync(IBuffer buffer, uint count, InputStreamOptions options)119 IAsyncOperationWithProgress<IBuffer, uint> IInputStream.ReadAsync(IBuffer buffer, uint count, InputStreamOptions options) 120 { 121 byte[] dataBytes = ReadInternal((int)count); 122 123 var ibuffer = dataBytes.AsBuffer(); 124 125 return AsyncInfo.Run( 126 delegate (CancellationToken cancellationToken, IProgress<uint> progress) 127 { 128 return Task.FromResult<IBuffer>(ibuffer); 129 }); 130 } 131 } 132 } 133