/
FileStreamOptions.cs
151 lines (136 loc) · 6.03 KB
/
FileStreamOptions.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Runtime.Versioning;
namespace System.IO
{
public sealed class FileStreamOptions
{
private FileMode _mode = FileMode.Open;
private FileAccess _access = FileAccess.Read;
private FileShare _share = FileStream.DefaultShare;
private FileOptions _options;
private long _preallocationSize;
private int _bufferSize = FileStream.DefaultBufferSize;
private UnixFileMode? _unixCreateMode;
/// <summary>
/// One of the enumeration values that determines how to open or create the file.
/// </summary>
/// <exception cref="T:System.ArgumentOutOfRangeException">When <paramref name="value" /> contains an invalid value.</exception>
public FileMode Mode
{
get => _mode;
set
{
if (value < FileMode.CreateNew || value > FileMode.Append)
{
ThrowHelper.ArgumentOutOfRangeException_Enum_Value();
}
_mode = value;
}
}
/// <summary>
/// A bitwise combination of the enumeration values that determines how the file can be accessed by the <see cref="FileStream" /> object. This also determines the values returned by the <see cref="FileStream.CanRead" /> and <see cref="FileStream.CanWrite" /> properties of the <see cref="FileStream" /> object.
/// </summary>
/// <exception cref="T:System.ArgumentOutOfRangeException">When <paramref name="value" /> contains an invalid value.</exception>
public FileAccess Access
{
get => _access;
set
{
if (value < FileAccess.Read || value > FileAccess.ReadWrite)
{
ThrowHelper.ArgumentOutOfRangeException_Enum_Value();
}
_access = value;
}
}
/// <summary>
/// A bitwise combination of the enumeration values that determines how the file will be shared by processes. The default value is <see cref="FileShare.Read" />.
/// </summary>
/// <exception cref="T:System.ArgumentOutOfRangeException">When <paramref name="value" /> contains an invalid value.</exception>
public FileShare Share
{
get => _share;
set
{
// don't include inheritable in our bounds check for share
FileShare tempshare = value & ~FileShare.Inheritable;
if (tempshare < FileShare.None || tempshare > (FileShare.ReadWrite | FileShare.Delete))
{
ThrowHelper.ArgumentOutOfRangeException_Enum_Value();
}
_share = value;
}
}
/// <summary>
/// A bitwise combination of the enumeration values that specifies additional file options. The default value is <see cref="FileOptions.None" />, which indicates synchronous IO.
/// </summary>
/// <exception cref="T:System.ArgumentOutOfRangeException">When <paramref name="value" /> contains an invalid value.</exception>
public FileOptions Options
{
get => _options;
set
{
// NOTE: any change to FileOptions enum needs to be matched here in the error validation
if (value != FileOptions.None && (value & ~(FileOptions.WriteThrough | FileOptions.Asynchronous | FileOptions.RandomAccess | FileOptions.DeleteOnClose | FileOptions.SequentialScan | FileOptions.Encrypted | (FileOptions)0x20000000 /* NoBuffering */)) != 0)
{
ThrowHelper.ArgumentOutOfRangeException_Enum_Value();
}
_options = value;
}
}
/// <summary>
/// The initial allocation size in bytes for the file. A positive value is effective only when a regular file is being created, overwritten, or replaced.
/// Negative values are not allowed.
/// In other cases (including the default 0 value), it's ignored.
/// </summary>
/// <exception cref="T:System.ArgumentOutOfRangeException">When <paramref name="value" /> is negative.</exception>
public long PreallocationSize
{
get => _preallocationSize;
set
{
ArgumentOutOfRangeException.ThrowIfNegative(value);
_preallocationSize = value;
}
}
/// <summary>
/// The size of the buffer used by <see cref="FileStream" /> for buffering. The default buffer size is 4096.
/// 0 or 1 means that buffering should be disabled. Negative values are not allowed.
/// </summary>
/// <exception cref="T:System.ArgumentOutOfRangeException">When <paramref name="value" /> is negative.</exception>
public int BufferSize
{
get => _bufferSize;
set
{
ArgumentOutOfRangeException.ThrowIfNegative(value);
_bufferSize = value;
}
}
/// <summary>
/// Unix file mode used when a new file is created.
/// </summary>
/// <exception cref="T:System.ArgumentException">When <paramref name="value" /> is an invalid file mode.</exception>
public UnixFileMode? UnixCreateMode
{
get
{
return _unixCreateMode;
}
[UnsupportedOSPlatform("windows")]
set
{
if (OperatingSystem.IsWindows())
{
throw new PlatformNotSupportedException(SR.PlatformNotSupported_UnixFileMode);
}
if (value.HasValue && ((value & ~FileSystem.ValidUnixFileModes) != 0))
{
throw new ArgumentException(SR.Arg_InvalidUnixFileMode, nameof(UnixCreateMode));
}
_unixCreateMode = value;
}
}
}
}