From 589e588aa02af8e1f007e63c98960f90671b0188 Mon Sep 17 00:00:00 2001 From: albert-du <52804499+albert-du@users.noreply.github.com> Date: Sat, 5 Mar 2022 13:10:53 -0800 Subject: [PATCH] NotSupportedException F# snippets --- .../Overview/BadState1.fs | 34 ++++++++++++ .../Overview/TestProp1.fs | 55 +++++++++++++++++++ .../Overview/TestProp2.fs | 48 ++++++++++++++++ .../NotSupportedException/Overview/fs.fsproj | 12 ++++ xml/System/NotSupportedException.xml | 3 + 5 files changed, 152 insertions(+) create mode 100644 snippets/fsharp/System/NotSupportedException/Overview/BadState1.fs create mode 100644 snippets/fsharp/System/NotSupportedException/Overview/TestProp1.fs create mode 100644 snippets/fsharp/System/NotSupportedException/Overview/TestProp2.fs create mode 100644 snippets/fsharp/System/NotSupportedException/Overview/fs.fsproj diff --git a/snippets/fsharp/System/NotSupportedException/Overview/BadState1.fs b/snippets/fsharp/System/NotSupportedException/Overview/BadState1.fs new file mode 100644 index 00000000000..448563e30e7 --- /dev/null +++ b/snippets/fsharp/System/NotSupportedException/Overview/BadState1.fs @@ -0,0 +1,34 @@ +module BadState + +// +open System.IO +open System.Text + +let main = task { + let enc = Encoding.Unicode + let value = "This is a string to persist." + let bytes = enc.GetBytes value + + let fs = new FileStream(@".\TestFile.dat", FileMode.Open, FileAccess.Read) + let t = fs.WriteAsync(enc.GetPreamble(), 0, enc.GetPreamble().Length) + let t2 = t.ContinueWith(fun a -> fs.WriteAsync(bytes, 0, bytes.Length)) + let! _ = t2 + fs.Close() +} +main.Wait() + +// The example displays the following output: +// Unhandled Exception: System.NotSupportedException: Stream does not support writing. +// at System.IO.Stream.BeginWriteInternal(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state +// , Boolean serializeAsynchronously) +// at System.IO.FileStream.BeginWrite(Byte[] array, Int32 offset, Int32 numBytes, AsyncCallback userCallback, Object sta +// teObject) +// at System.IO.Stream.<>c.b__53_0(Stream stream, ReadWriteParameters args, AsyncCallback callback, +// Object state) +// at System.Threading.Tasks.TaskFactory`1.FromAsyncTrim[TInstance,TArgs](TInstance thisRef, TArgs args, Func`5 beginMet +// hod, Func`3 endMethod) +// at System.IO.Stream.BeginEndWriteAsync(Byte[] buffer, Int32 offset, Int32 count) +// at System.IO.FileStream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken) +// at System.IO.Stream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count) +// at .main() +// \ No newline at end of file diff --git a/snippets/fsharp/System/NotSupportedException/Overview/TestProp1.fs b/snippets/fsharp/System/NotSupportedException/Overview/TestProp1.fs new file mode 100644 index 00000000000..67c754a2d28 --- /dev/null +++ b/snippets/fsharp/System/NotSupportedException/Overview/TestProp1.fs @@ -0,0 +1,55 @@ +module TestProp1 + +// +open System +open System.IO + +module FileUtilities = + type EncodingType = + | None = 0 + | Unknown = -1 + | Utf8 = 1 + | Utf16 = 2 + | Utf32 = 3 + + let getEncodingType (fs: FileStream) = + task { + let bytes = Array.zeroCreate 4 + let! bytesRead = fs.ReadAsync(bytes, 0, 4) + if bytesRead < 2 then + return EncodingType.None + + elif bytesRead >= 3 && bytes[0] = 0xEFuy && bytes[1] = 0xBBuy && bytes[2] = 0xBFuy then + return EncodingType.Utf8 + else + let value = BitConverter.ToUInt32(bytes, 0) + if bytesRead = 4 && (value = 0x0000FEFFu || value = 0xFEFF0000u) then + return EncodingType.Utf32 + else + let value16 = BitConverter.ToUInt16(bytes, 0) + if value16 = 0xFEFFus || value16 = 0xFFFEus then + return EncodingType.Utf16 + else + return EncodingType.Unknown + } + +let main _ = + task { + let name = @".\TestFile.dat" + let fs = new FileStream(name, FileMode.Create, FileAccess.Write) + let! et = FileUtilities.getEncodingType fs + printfn $"Filename: {name}, Encoding: {et}" + } + +// The example displays the following output: +// Unhandled Exception: System.NotSupportedException: Stream does not support reading. +// at System.IO.FileStream.BeginRead(Byte[] array, Int32 offset, Int32 numBytes, AsyncCallback callback, Object state) +// at System.IO.Stream.<>c.b__46_0(Stream stream, ReadWriteParameters args, AsyncCallback callback, Object state) +// at System.Threading.Tasks.TaskFactory`1.FromAsyncTrim[TInstance, TArgs](TInstance thisRef, TArgs args, Func`5 beginMethod, Func`3 endMethod) +// at System.IO.Stream.BeginEndReadAsync(Byte[] buffer, Int32 offset, Int32 count) +// at System.IO.FileStream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken) +// at System.IO.Stream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count) +// at FileUtilities.GetEncodingType(FileStream fs) +// at Example.Main() +// at Example.
() +// \ No newline at end of file diff --git a/snippets/fsharp/System/NotSupportedException/Overview/TestProp2.fs b/snippets/fsharp/System/NotSupportedException/Overview/TestProp2.fs new file mode 100644 index 00000000000..b5f68d73b68 --- /dev/null +++ b/snippets/fsharp/System/NotSupportedException/Overview/TestProp2.fs @@ -0,0 +1,48 @@ +module TestProp2 + +open System +open System.IO + +module FileUtilities = + type EncodingType = + | None = 0 + | Unknown = -1 + | Utf8 = 1 + | Utf16 = 2 + | Utf32 = 3 + + // + let getEncodingType (fs: FileStream) = + task { + if not fs.CanRead then + return EncodingType.Unknown + else + let bytes = Array.zeroCreate 4 + let! bytesRead = fs.ReadAsync(bytes, 0, 4) + if bytesRead < 2 then + return EncodingType.None + + elif bytesRead >= 3 && bytes[0] = 0xEFuy && bytes[1] = 0xBBuy && bytes[2] = 0xBFuy then + return EncodingType.Utf8 + else + let value = BitConverter.ToUInt32(bytes, 0) + if bytesRead = 4 && (value = 0x0000FEFFu || value = 0xFEFF0000u) then + return EncodingType.Utf32 + else + let value16 = BitConverter.ToUInt16(bytes, 0) + if value16 = 0xFEFFus || value16 = 0xFFFEus then + return EncodingType.Utf16 + else + return EncodingType.Unknown + } + // The example displays the following output: + // Filename: .\TestFile.dat, Encoding: Unknown + // + +let main _ = + task { + let name = @".\TestFile.dat" + let fs = new FileStream(name, FileMode.Create, FileAccess.Write) + let! et = FileUtilities.getEncodingType fs + printfn $"Filename: {name}, Encoding: {et}" + } diff --git a/snippets/fsharp/System/NotSupportedException/Overview/fs.fsproj b/snippets/fsharp/System/NotSupportedException/Overview/fs.fsproj new file mode 100644 index 00000000000..46b9a78591e --- /dev/null +++ b/snippets/fsharp/System/NotSupportedException/Overview/fs.fsproj @@ -0,0 +1,12 @@ + + + Exe + net6.0 + + + + + + + + \ No newline at end of file diff --git a/xml/System/NotSupportedException.xml b/xml/System/NotSupportedException.xml index f5b9ef8a686..0656dafb3a8 100644 --- a/xml/System/NotSupportedException.xml +++ b/xml/System/NotSupportedException.xml @@ -98,6 +98,7 @@ - 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 object and then attempts to write to it. :::code language="csharp" source="~/snippets/csharp/System/NotSupportedException/Overview/BadState1.cs" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/NotSupportedException/Overview/BadState1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/System.NotSupportedException/vb/BadState1.vb" id="Snippet1"::: 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 object by providing the correct arguments to the constructor. @@ -107,11 +108,13 @@ The following example defines a `DetectEncoding` method that throws a exception when it attempts to read from the beginning of a stream that does not support read access. :::code language="csharp" source="~/snippets/csharp/System/NotSupportedException/Overview/TestProp1.cs" id="Snippet3"::: + :::code language="fsharp" source="~/snippets/fsharp/System/NotSupportedException/Overview/TestProp1.fs" id="Snippet3"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/System.NotSupportedException/vb/TestProp1.vb" id="Snippet3"::: You can eliminate the exception by examining the value of the property and exiting the method if the stream is read-only. :::code language="csharp" source="~/snippets/csharp/System/NotSupportedException/Overview/TestProp2.cs" id="Snippet4"::: + :::code language="fsharp" source="~/snippets/fsharp/System/NotSupportedException/Overview/TestProp2.fs" id="Snippet4"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/System.NotSupportedException/vb/TestProp2.vb" id="Snippet4"::: ## Related exception types