Skip to content

Commit 6f42d4f

Browse files
authored
NotSupportedException F# snippets (#7801)
1 parent c2aa640 commit 6f42d4f

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module BadState
2+
3+
// <Snippet1>
4+
open System.IO
5+
open System.Text
6+
7+
let main = task {
8+
let enc = Encoding.Unicode
9+
let value = "This is a string to persist."
10+
let bytes = enc.GetBytes value
11+
12+
let fs = new FileStream(@".\TestFile.dat", FileMode.Open, FileAccess.Read)
13+
let t = fs.WriteAsync(enc.GetPreamble(), 0, enc.GetPreamble().Length)
14+
let t2 = t.ContinueWith(fun a -> fs.WriteAsync(bytes, 0, bytes.Length))
15+
let! _ = t2
16+
fs.Close()
17+
}
18+
main.Wait()
19+
20+
// The example displays the following output:
21+
// Unhandled Exception: System.NotSupportedException: Stream does not support writing.
22+
// at System.IO.Stream.BeginWriteInternal(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state
23+
// , Boolean serializeAsynchronously)
24+
// at System.IO.FileStream.BeginWrite(Byte[] array, Int32 offset, Int32 numBytes, AsyncCallback userCallback, Object sta
25+
// teObject)
26+
// at System.IO.Stream.<>c.<BeginEndWriteAsync>b__53_0(Stream stream, ReadWriteParameters args, AsyncCallback callback,
27+
// Object state)
28+
// at System.Threading.Tasks.TaskFactory`1.FromAsyncTrim[TInstance,TArgs](TInstance thisRef, TArgs args, Func`5 beginMet
29+
// hod, Func`3 endMethod)
30+
// at System.IO.Stream.BeginEndWriteAsync(Byte[] buffer, Int32 offset, Int32 count)
31+
// at System.IO.FileStream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
32+
// at System.IO.Stream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count)
33+
// at <StartupCode:fs>.main()
34+
// </Snippet1>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
module TestProp1
2+
3+
// <Snippet3>
4+
open System
5+
open System.IO
6+
7+
module FileUtilities =
8+
type EncodingType =
9+
| None = 0
10+
| Unknown = -1
11+
| Utf8 = 1
12+
| Utf16 = 2
13+
| Utf32 = 3
14+
15+
let getEncodingType (fs: FileStream) =
16+
task {
17+
let bytes = Array.zeroCreate<byte> 4
18+
let! bytesRead = fs.ReadAsync(bytes, 0, 4)
19+
if bytesRead < 2 then
20+
return EncodingType.None
21+
22+
elif bytesRead >= 3 && bytes[0] = 0xEFuy && bytes[1] = 0xBBuy && bytes[2] = 0xBFuy then
23+
return EncodingType.Utf8
24+
else
25+
let value = BitConverter.ToUInt32(bytes, 0)
26+
if bytesRead = 4 && (value = 0x0000FEFFu || value = 0xFEFF0000u) then
27+
return EncodingType.Utf32
28+
else
29+
let value16 = BitConverter.ToUInt16(bytes, 0)
30+
if value16 = 0xFEFFus || value16 = 0xFFFEus then
31+
return EncodingType.Utf16
32+
else
33+
return EncodingType.Unknown
34+
}
35+
36+
let main _ =
37+
task {
38+
let name = @".\TestFile.dat"
39+
let fs = new FileStream(name, FileMode.Create, FileAccess.Write)
40+
let! et = FileUtilities.getEncodingType fs
41+
printfn $"Filename: {name}, Encoding: {et}"
42+
}
43+
44+
// The example displays the following output:
45+
// Unhandled Exception: System.NotSupportedException: Stream does not support reading.
46+
// at System.IO.FileStream.BeginRead(Byte[] array, Int32 offset, Int32 numBytes, AsyncCallback callback, Object state)
47+
// at System.IO.Stream.<>c.<BeginEndReadAsync>b__46_0(Stream stream, ReadWriteParameters args, AsyncCallback callback, Object state)
48+
// at System.Threading.Tasks.TaskFactory`1.FromAsyncTrim[TInstance, TArgs](TInstance thisRef, TArgs args, Func`5 beginMethod, Func`3 endMethod)
49+
// at System.IO.Stream.BeginEndReadAsync(Byte[] buffer, Int32 offset, Int32 count)
50+
// at System.IO.FileStream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
51+
// at System.IO.Stream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count)
52+
// at FileUtilities.GetEncodingType(FileStream fs)
53+
// at Example.Main()
54+
// at Example.<Main>()
55+
// </Snippet3>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
module TestProp2
2+
3+
open System
4+
open System.IO
5+
6+
module FileUtilities =
7+
type EncodingType =
8+
| None = 0
9+
| Unknown = -1
10+
| Utf8 = 1
11+
| Utf16 = 2
12+
| Utf32 = 3
13+
14+
// <Snippet4>
15+
let getEncodingType (fs: FileStream) =
16+
task {
17+
if not fs.CanRead then
18+
return EncodingType.Unknown
19+
else
20+
let bytes = Array.zeroCreate<byte> 4
21+
let! bytesRead = fs.ReadAsync(bytes, 0, 4)
22+
if bytesRead < 2 then
23+
return EncodingType.None
24+
25+
elif bytesRead >= 3 && bytes[0] = 0xEFuy && bytes[1] = 0xBBuy && bytes[2] = 0xBFuy then
26+
return EncodingType.Utf8
27+
else
28+
let value = BitConverter.ToUInt32(bytes, 0)
29+
if bytesRead = 4 && (value = 0x0000FEFFu || value = 0xFEFF0000u) then
30+
return EncodingType.Utf32
31+
else
32+
let value16 = BitConverter.ToUInt16(bytes, 0)
33+
if value16 = 0xFEFFus || value16 = 0xFFFEus then
34+
return EncodingType.Utf16
35+
else
36+
return EncodingType.Unknown
37+
}
38+
// The example displays the following output:
39+
// Filename: .\TestFile.dat, Encoding: Unknown
40+
// </Snippet4>
41+
42+
let main _ =
43+
task {
44+
let name = @".\TestFile.dat"
45+
let fs = new FileStream(name, FileMode.Create, FileAccess.Write)
46+
let! et = FileUtilities.getEncodingType fs
47+
printfn $"Filename: {name}, Encoding: {et}"
48+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="BadState1.fs" />
9+
<Compile Include="TestProp1.fs" />
10+
<Compile Include="TestProp2.fs" />
11+
</ItemGroup>
12+
</Project>

xml/System/NotSupportedException.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
- You know the state of the object in advance (usually because your code has instantiated it), but the object is mis-configured. The following example illustrates this issue. It creates a read-only <xref:System.IO.FileStream> object and then attempts to write to it.
9999
100100
:::code language="csharp" source="~/snippets/csharp/System/NotSupportedException/Overview/BadState1.cs" id="Snippet1":::
101+
:::code language="fsharp" source="~/snippets/fsharp/System/NotSupportedException/Overview/BadState1.fs" id="Snippet1":::
101102
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/System.NotSupportedException/vb/BadState1.vb" id="Snippet1":::
102103
103104
You can eliminate the exception by ensuring that the instantiated object supports the functionality you intend. The following example addresses the problem of the read-only <xref:System.IO.FileStream> object by providing the correct arguments to the <xref:System.IO.FileStream.%23ctor%28System.String%2CSystem.IO.FileMode%2CSystem.IO.FileAccess%29?displayProperty=nameWithType> constructor.
@@ -107,11 +108,13 @@
107108
The following example defines a `DetectEncoding` method that throws a <xref:System.NotSupportedException> exception when it attempts to read from the beginning of a stream that does not support read access.
108109
109110
:::code language="csharp" source="~/snippets/csharp/System/NotSupportedException/Overview/TestProp1.cs" id="Snippet3":::
111+
:::code language="fsharp" source="~/snippets/fsharp/System/NotSupportedException/Overview/TestProp1.fs" id="Snippet3":::
110112
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/System.NotSupportedException/vb/TestProp1.vb" id="Snippet3":::
111113
112114
You can eliminate the exception by examining the value of the <xref:System.IO.FileStream.CanRead%2A?displayProperty=nameWithType> property and exiting the method if the stream is read-only.
113115
114116
:::code language="csharp" source="~/snippets/csharp/System/NotSupportedException/Overview/TestProp2.cs" id="Snippet4":::
117+
:::code language="fsharp" source="~/snippets/fsharp/System/NotSupportedException/Overview/TestProp2.fs" id="Snippet4":::
115118
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/System.NotSupportedException/vb/TestProp2.vb" id="Snippet4":::
116119
117120
## Related exception types

0 commit comments

Comments
 (0)