forked from dotnet/machinelearning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBytesStreamSource.cs
49 lines (41 loc) · 1.22 KB
/
BytesStreamSource.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.IO;
using System.Text;
using Microsoft.ML.Data;
using Microsoft.ML.Runtime;
namespace Microsoft.ML.TestFramework
{
/// <summary>
/// On demand open up new streams over a source of bytes.
/// </summary>
public sealed class BytesStreamSource : IMultiStreamSource
{
private readonly byte[] _data;
public int Count => 1;
public BytesStreamSource(byte[] data)
{
Contracts.AssertValue(data);
_data = data;
}
public BytesStreamSource(string data)
: this(Encoding.UTF8.GetBytes(data))
{
}
public string GetPathOrNull(int index)
{
Contracts.Check(index == 0);
return null;
}
public Stream Open(int index)
{
Contracts.Check(index == 0);
return new MemoryStream(_data, writable: false);
}
public TextReader OpenTextReader(int index)
{
return new StreamReader(Open(index));
}
}
}