Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions snippets/fsharp/System.IO/BinaryReader/BaseStream/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="rwdouble.fs" />
</ItemGroup>
</Project>
32 changes: 32 additions & 0 deletions snippets/fsharp/System.IO/BinaryReader/BaseStream/rwdouble.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// <Snippet1>
open System
open System.IO

let arrayLength = 1000

// Create random data to write to the stream.
let randomGenerator = Random()
let dataArray =
Array.init arrayLength (fun _ -> 100.1 * randomGenerator.NextDouble())
do
use binWriter = new BinaryWriter(new MemoryStream())
// Write the data to the stream.
printfn $"Writing data to the stream."
for num in dataArray do
binWriter.Write num

// Create a reader using the stream from the writer.
use binReader = new BinaryReader(binWriter.BaseStream)
try
// Return to the beginning of the stream.
binReader.BaseStream.Position <- 0

// Read and verify the data.
printfn "Verifying the written data."
for num in dataArray do
if binReader.ReadDouble() <> num then
printfn "Error writing data."
printfn "The data was written and verified."
with :? EndOfStreamException as e ->
printfn $"Error writing data: {e.GetType().Name}."
// </Snippet1>
11 changes: 11 additions & 0 deletions snippets/fsharp/System.IO/BinaryReader/Overview/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="source.fs" />
<Compile Include="source5.fs" />
</ItemGroup>
</Project>
33 changes: 33 additions & 0 deletions snippets/fsharp/System.IO/BinaryReader/Overview/source.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module source

// <Snippet1>
open System.IO
open System.Text

let fileName = "AppSettings.dat"

let writeDefaultValues () =
use stream = File.Open(fileName, FileMode.Create)
use writer = new BinaryWriter(stream, Encoding.UTF8, false)
writer.Write 1.250F
writer.Write @"c:\Temp"
writer.Write 10
writer.Write true

let displayValues () =
if File.Exists fileName then
use stream = File.Open(fileName, FileMode.Open)
use reader = new BinaryReader(stream, Encoding.UTF8, false)
let aspectRatio = reader.ReadSingle()
let tempDirectory = reader.ReadString()
let autoSaveTime = reader.ReadInt32()
let showStatusBar = reader.ReadBoolean()

printfn $"Aspect ratio set to: {aspectRatio}"
printfn $"Temp directory is: {tempDirectory}"
printfn $"Auto save time set to: {autoSaveTime}"
printfn $"Show status bar: {showStatusBar}"

writeDefaultValues ()
displayValues ()
// </Snippet1>
50 changes: 50 additions & 0 deletions snippets/fsharp/System.IO/BinaryReader/Overview/source5.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module source5

//<snippet6>
open System
open System.IO
open System.Text

let CHUNK_SIZE = 1024

let dumpBytes (bdata: byte[]) len =
let mutable j = 0
// 3 * 16 chars for hex display, 16 chars for text and 8 chars
// for the 'gutter' int the middle.
let dumptext = StringBuilder(" ", 16 * 4 + 8)
for i = 0 to len - 1 do
dumptext.Insert(j * 3, $"{int bdata[i]:X2} ") |> ignore
let dchar = char bdata[i]
//' replace 'non-printable' chars with a '.'.
let dchar =
if Char.IsWhiteSpace dchar || Char.IsControl dchar then
'.'
else
dchar
dumptext.Append dchar |> ignore
j <- j + 1
if j = 16 then
printfn $"{dumptext}"
dumptext.Length <- 0
dumptext.Append " " |> ignore
j <- 0
// display the remaining line
if j > 0 then
for i = j to 15 do
dumptext.Insert(j * 3, " ") |> ignore
printfn $"{dumptext}"

[<EntryPoint>]
let main args =
if args.Length = 0 || File.Exists args[0] |> not then
printfn "Please provide an existing file name."
else
use fs = new FileStream(args[0], FileMode.Open, FileAccess.Read)
use br = new BinaryReader(fs, ASCIIEncoding())

let mutable chunk = br.ReadBytes CHUNK_SIZE
while chunk.Length > 0 do
dumpBytes chunk chunk.Length
chunk <- br.ReadBytes CHUNK_SIZE
0
//</snippet6>
12 changes: 12 additions & 0 deletions snippets/fsharp/System.IO/BinaryReader/Read/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="rwreadchar.fs" />
<Compile Include="rwreadbytes.fs" />
<Compile Include="rwreadchars.fs" />
</ItemGroup>
</Project>
29 changes: 29 additions & 0 deletions snippets/fsharp/System.IO/BinaryReader/Read/rwreadbytes.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module rwreadbytes

// <Snippet1>
open System
open System.IO

let arrayLength = 1000
let dataArray = Array.zeroCreate<byte> arrayLength
let verifyArray = Array.zeroCreate<byte> arrayLength

Random().NextBytes dataArray

do
use binWriter = new BinaryWriter(new MemoryStream())
printfn "Writing the data."
binWriter.Write(dataArray, 0, arrayLength)

use binReader = new BinaryReader(binWriter.BaseStream)
binReader.BaseStream.Position <- 0

if binReader.Read(verifyArray, 0, arrayLength) <> arrayLength then
printfn "Error writing the data."
else
for i = 0 to arrayLength - 1 do
if verifyArray[i] <> dataArray[i] then
printfn "Error writing the data."
else
printfn "The data was written and verified."
// </Snippet1>
29 changes: 29 additions & 0 deletions snippets/fsharp/System.IO/BinaryReader/Read/rwreadchar.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module rwreadchar

// <Snippet1>
open System
open System.IO

let invalidPathChars = Path.GetInvalidPathChars()
let memStream = new MemoryStream()
let binWriter = new BinaryWriter(memStream)

// Write to memory.
printf "Invalid file path characters are: "
for i = 0 to invalidPathChars.Length - 1 do
binWriter.Write invalidPathChars[i]

// Create the reader using the same MemoryStream
// as used with the writer.
let binReader = new BinaryReader(memStream)

// Set Position to the beginning of the stream.
memStream.Position <- 0

// Read the data from memory and write it to the console.
printf $"{binReader.ReadString()}"
let memoryData =
[| for _ = 0L to memStream.Length - memStream.Position - 1L do
Convert.ToChar(binReader.Read()) |]
printfn $"{memoryData}"
// </Snippet1>
25 changes: 25 additions & 0 deletions snippets/fsharp/System.IO/BinaryReader/Read/rwreadchars.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// <Snippet1>
open System.IO

let invalidPathChars = Path.GetInvalidPathChars()
let memStream = new MemoryStream()
let binWriter = new BinaryWriter(memStream)

// Write to memory.
binWriter.Write "Invalid file path characters are: "
binWriter.Write(invalidPathChars, 0, invalidPathChars.Length)

// Create the reader using the same MemoryStream
// as used with the writer.
let binReader = new BinaryReader(memStream)

// Set Position to the beginning of the stream.
memStream.Position <- 0

// Read the data from memory and write it to the console.
printf $"{binReader.ReadString()}"
let arraySize = memStream.Length - memStream.Position |> int
let memoryData = Array.zeroCreate<char> arraySize
binReader.Read(memoryData, 0, arraySize) |> ignore
printfn $"{memoryData}"
// </Snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System.IO/BinaryReader/ReadByte/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="rwbyte.fs" />
</ItemGroup>
</Project>
33 changes: 33 additions & 0 deletions snippets/fsharp/System.IO/BinaryReader/ReadByte/rwbyte.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// <Snippet1>
open System
open System.IO

// Create random data to write to the stream.
let writeArray = Array.zeroCreate<byte> 1000
Random().NextBytes writeArray

let binWriter = new BinaryWriter(new MemoryStream())
let binReader = new BinaryReader(binWriter.BaseStream)

try
// Write the data to the stream.
printfn "Writing the data."
for i = 0 to writeArray.Length - 1 do
binWriter.Write writeArray[i]

// Set the stream position to the beginning of the stream.
binReader.BaseStream.Position <- 0

let mutable failed = false
// Read and verify the data from the stream.
for i = 0 to writeArray.Length - 1 do
if binReader.ReadByte() <> writeArray[i] then
printfn "Error writing the data."
failed <- true
if not failed then
printfn "The data was written and verified."

// Catch the EndOfStreamException and write an error message.
with :? EndOfStreamException as e ->
printfn $"Error writing the data.\n{e.GetType().Name}"
// </Snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System.IO/BinaryReader/ReadBytes/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="rwbytes.fs" />
</ItemGroup>
</Project>
35 changes: 35 additions & 0 deletions snippets/fsharp/System.IO/BinaryReader/ReadBytes/rwbytes.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// <Snippet1>
open System
open System.IO

let arrayLength = 1000

// Create random data to write to the stream.
let dataArray = Array.zeroCreate<byte> arrayLength
Random().NextBytes dataArray

let binWriter = new BinaryWriter(new MemoryStream())

// Write the data to the stream.ch
printfn "Writing the data."
binWriter.Write dataArray

// Create the reader using the stream from the writer.
let binReader = new BinaryReader(binWriter.BaseStream)

// Set Position to the beginning of the stream.
binReader.BaseStream.Position <- 0

// Read and verify the data.
let verifyArray = binReader.ReadBytes arrayLength
if verifyArray.Length <> arrayLength then
printfn "Error writing the data."
else
let mutable failed = false
for i = 0 to arrayLength - 1 do
if verifyArray[i] <> dataArray[i] then
printfn "Error writing the data."
failed <- true
if not failed then
printfn "The data was written and verified."
// </Snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System.IO/BinaryReader/ReadChar/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="rwchar.fs" />
</ItemGroup>
</Project>
26 changes: 26 additions & 0 deletions snippets/fsharp/System.IO/BinaryReader/ReadChar/rwchar.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// <Snippet1>
open System.IO

let invalidPathChars = Path.GetInvalidPathChars()
let memStream = new MemoryStream()
let binWriter = new BinaryWriter(memStream)

// Write to memory.
binWriter.Write "Invalid file path characters are: "
for i = 0 to invalidPathChars.Length - 1 do
binWriter.Write invalidPathChars[i]

// Create the reader using the same MemoryStream
// as used with the writer.
let binReader = new BinaryReader(memStream)

// Set Position to the beginning of the stream.
memStream.Position <- 0

// Read the data from memory and write it to the console.
printf $"{binReader.ReadString()}"
let memoryData = Array.zeroCreate<char> (int (memStream.Length - memStream.Position))
for i = 0 to memoryData.Length - 1 do
memoryData[i] <- binReader.ReadChar()
printfn $"{memoryData}"
// </Snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System.IO/BinaryReader/ReadChars/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="rwchars.fs" />
</ItemGroup>
</Project>
Loading