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 Windows.Storage.Streams;
6 
7 namespace System.IO
8 {
9     public class TestStreamProvider
10     {
11         private const string TempFileNameBase = @"WinRT.Testing.IO.Streams.NetFxStreamProvider.DataFile";
12         private const string TempFileNameExt = @".tmp";
13         private const string TempFileName = TempFileNameBase + TempFileNameExt;
14 
15         private static byte[] s_modelStreamContents = null;
16         private static int s_modelStreamLength = 0x100000;
17 
18         private static readonly string s_tempFileFolder;
19         private static readonly string s_tempFilePath;
20 
TestStreamProvider()21         static TestStreamProvider()
22         {
23             s_tempFileFolder = ".";
24             s_tempFilePath = Path.Combine(s_tempFileFolder, TempFileName);
25         }
26 
27         public static byte[] ModelStreamContents
28         {
29             get
30             {
31                 if (s_modelStreamContents != null)
32                     return s_modelStreamContents;
33 
34                 const int randomSeed = 20090918;
35 
36                 Random rnd = new Random(randomSeed);
37                 s_modelStreamContents = new byte[ModelStreamLength];
38                 rnd.NextBytes(s_modelStreamContents);
39 
40                 return s_modelStreamContents;
41             }
42         }
43 
44         public static int ModelStreamLength
45         {
46             get
47             {
48                 return s_modelStreamLength;
49             }
50             set
51             {
52                 s_modelStreamContents = null;
53                 s_modelStreamLength = value;
54             }
55         }
56 
CheckContent(byte[] values, int offsInModelContents, int count)57         public static bool CheckContent(byte[] values, int offsInModelContents, int count)
58         {
59             for (int i = 0; i < count; i++)
60             {
61 
62                 if (!CheckContent(values[i], i + offsInModelContents))
63                 {
64 
65                     Console.WriteLine("Fail on {0}, {1}, {2}, {3}", i, i + offsInModelContents, values[i], ModelStreamContents[i + offsInModelContents]);
66 
67                     return false;
68                 }
69             }
70 
71             return true;
72         }
73 
CheckContent(byte value, int index)74         public static bool CheckContent(byte value, int index)
75         {
76             return value == ModelStreamContents[index];
77         }
78 
CreateMemoryStream()79         public static MemoryStream CreateMemoryStream()
80         {
81             byte[] data = new byte[ModelStreamLength];
82             Array.Copy(ModelStreamContents, data, data.Length);
83 
84             MemoryStream stream = new MemoryStream(data, 0, data.Length, true);
85             return stream;
86         }
87 
CreateReadOnlyStream()88         public static Stream CreateReadOnlyStream()
89         {
90             byte[] data = new byte[ModelStreamLength];
91             Array.Copy(ModelStreamContents, data, data.Length);
92 
93             MemoryStream stream = new MemoryStream(data, 0, data.Length, false);
94             return stream;
95         }
96 
CreateWriteOnlyStream()97         public static Stream CreateWriteOnlyStream()
98         {
99             byte[] data = new byte[ModelStreamLength];
100             Array.Copy(ModelStreamContents, data, data.Length);
101 
102             MemoryStream stream = new WriteOnlyStream(data);
103             return stream;
104         }
105 
CreateMemoryStreamAsInputStream()106         public static IInputStream CreateMemoryStreamAsInputStream()
107         {
108             MemoryStream memStream = CreateMemoryStream();
109             return memStream.AsInputStream();
110         }
111     }
112 }
113