-
Notifications
You must be signed in to change notification settings - Fork 10
/
DataStreamFactory.cs
200 lines (178 loc) · 8.71 KB
/
DataStreamFactory.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright (c) 2019 SceneGate
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
namespace Yarhl.IO
{
using System;
using System.IO;
using Yarhl.IO.StreamFormat;
/// <summary>
/// Factory of DataStream.
/// </summary>
public static class DataStreamFactory
{
/// <summary>
/// Creates a new <see cref="DataStream"/> from a <see cref="Stream"/>.
/// </summary>
/// <param name="stream">The stream to use as a base.</param>
/// <remarks>
/// <p>The dispose ownership is transferred to the new DataStream.</p>
/// </remarks>
/// <returns>A new <see cref="DataStream"/>.</returns>
public static DataStream FromStream(Stream stream)
{
if (stream == null)
throw new ArgumentNullException(nameof(stream));
return new DataStream(stream);
}
/// <summary>
/// Creates a new <see cref="DataStream"/> from a section of a
/// <see cref="Stream"/>.
/// </summary>
/// <remarks>
/// <p>The life-management of the stream is transferred to the
/// <see cref="DataStream"/>. This means that disposing the new
/// <see cref="DataStream"/> will potentially dispose the underlying
/// stream.</p>
/// <p>Check <see cref="FromStreamKeepingOwnership" /> if you don't
/// want this behavior.</p>
/// </remarks>
/// <param name="stream">The stream to use as a base.</param>
/// <param name="offset">Offset of the base stream.</param>
/// <param name="length">Length of the new substream.</param>
/// <returns>A new <see cref="DataStream"/>.</returns>
public static DataStream FromStream(Stream stream, long offset, long length)
{
if (stream == null)
throw new ArgumentNullException(nameof(stream));
if (offset < 0 || offset > stream.Length)
throw new ArgumentOutOfRangeException(nameof(offset));
if (length < 0 || offset + length > stream.Length)
throw new ArgumentOutOfRangeException(nameof(length));
return new DataStream(stream, offset, length, true);
}
/// <summary>
/// Creates a new <see cref="DataStream"/> from a section of a
/// <see cref="Stream"/>.
/// </summary>
/// <remarks>
/// <p>The dispose ownership is not transferred to the new
/// <see cref="DataStream" />. Instead, the caller is still responsible
/// to dispose according the stream argument.</p>
/// </remarks>
/// <param name="stream">The stream to use as a base.</param>
/// <param name="offset">Offset of the base stream.</param>
/// <param name="length">Length of the new substream.</param>
/// <returns>A new <see cref="DataStream"/>.</returns>
public static DataStream FromStreamKeepingOwnership(Stream stream, long offset, long length)
{
if (stream == null)
throw new ArgumentNullException(nameof(stream));
if (offset < 0 || offset > stream.Length)
throw new ArgumentOutOfRangeException(nameof(offset));
if (length < 0 || offset + length > stream.Length)
throw new ArgumentOutOfRangeException(nameof(length));
return new DataStream(stream, offset, length, false);
}
/// <summary>
/// Creates a new <see cref="DataStream"/> in memory.
/// </summary>
/// <returns>A new <see cref="DataStream"/>.</returns>
public static DataStream FromMemory()
{
var baseStream = new RecyclableMemoryStream();
return new DataStream(baseStream);
}
/// <summary>
/// Creates a new <see cref="DataStream"/> from an array of data.
/// </summary>
/// <param name="data">The array of data to use in the stream.</param>
/// <returns>A new <see cref="DataStream"/>.</returns>
public static DataStream FromArray(byte[] data)
{
if (data is null)
throw new ArgumentNullException(nameof(data));
return FromArray(data, 0, data.Length);
}
/// <summary>
/// Creates a new <see cref="DataStream"/> from an array of data.
/// </summary>
/// <param name="data">The array of data to use in the stream.</param>
/// <param name="offset">Offset in the array of data.</param>
/// <param name="length">Length of the new stream.</param>
/// <returns>A new <see cref="DataStream"/>.</returns>
public static DataStream FromArray(byte[] data, int offset, int length)
{
if (data == null)
throw new ArgumentNullException(nameof(data));
if (offset < 0 || offset > data.Length)
throw new ArgumentOutOfRangeException(nameof(offset));
if (length < 0 || offset + length > data.Length)
throw new ArgumentOutOfRangeException(nameof(length));
var baseStream = new MemoryStream(data, 0, data.Length);
return new DataStream(baseStream, offset, length, true);
}
/// <summary>
/// Creates a new <see cref="DataStream"/> from a file.
/// </summary>
/// <param name="path">The path of the file.</param>
/// <param name="mode">The mode to open the file.</param>
/// <returns>A new <see cref="DataStream"/>.</returns>
public static DataStream FromFile(string path, FileOpenMode mode)
{
if (string.IsNullOrEmpty(path))
throw new ArgumentNullException(nameof(path));
if (mode == FileOpenMode.Read && !File.Exists(path)) {
throw new FileNotFoundException("File to read does not exist", path);
}
var baseStream = new LazyFileStream(path, mode);
return new DataStream(baseStream);
}
/// <summary>
/// Creates a new <see cref="DataStream"/> from a section of a file.
/// </summary>
/// <param name="path">The path of the file.</param>
/// <param name="mode">The mode to open the file.</param>
/// <param name="offset">Offset from the start of the file.</param>
/// <param name="length">Length of the new stream.</param>
/// <returns>A new <see cref="DataStream"/>.</returns>
public static DataStream FromFile(string path, FileOpenMode mode, long offset, long length)
{
if (string.IsNullOrEmpty(path))
throw new ArgumentNullException(nameof(path));
if (mode == FileOpenMode.Read && !File.Exists(path)) {
throw new FileNotFoundException("File to read does not exist: " + path, path);
}
var info = new FileInfo(path);
long fileSize;
// If it's a windows symlink, get the length by opening the stream.
// Otherwise we would need P/Invoke calls.
// .NET does the redirection of the symlink in FileStream automatically.
if (info.Exists && info.Attributes.HasFlag(FileAttributes.ReparsePoint)) {
using var tempStream = new FileStream(path, FileMode.Open);
fileSize = tempStream.Length;
} else {
fileSize = info.Exists ? info.Length : 0;
}
if (offset < 0 || offset > fileSize)
throw new ArgumentOutOfRangeException(nameof(offset));
if (length < 0 || offset + length > fileSize)
throw new ArgumentOutOfRangeException(nameof(length));
var baseStream = new LazyFileStream(path, mode);
return new DataStream(baseStream, offset, length, true);
}
}
}