Skip to content

Commit 834f2a4

Browse files
committed
minor changes to File async append extensions
1 parent 08ab1be commit 834f2a4

1 file changed

Lines changed: 12 additions & 12 deletions

File tree

src/FSharpx.Core/AsyncOperations.fs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ namespace Microsoft.FSharp.Control
9696
do! Async.SwitchToThreadPool ()
9797
return res }
9898

99+
100+
let private LinesToBytes ((lines:string array), encoder) =
101+
use memStrm = new System.IO.MemoryStream()
102+
use sWriter = new System.IO.StreamWriter(memStrm, encoder)
103+
lines |> Array.iter (fun line -> sWriter.WriteLine(line))
104+
do sWriter.Flush()
105+
memStrm.ToArray()
99106

100107
type System.IO.File with
101108
static member AsyncOpenText(path) = UnblockViaNewThread (fun () -> System.IO.File.OpenText(path))
@@ -135,6 +142,7 @@ namespace Microsoft.FSharp.Control
135142

136143
#if FX_NO_FILE_OPTIONS
137144
#else
145+
138146
// Aims to take advantage of IO completion ports using FileStream.AsyncWrite and FileOptions.Asynchronous, so no FX_NO_FILE_OPTIONS version
139147
static member AsyncWriteAllBytes(path, bytes) =
140148
let bufferSize = 4096 // as per File.WriteAllBytes
@@ -157,11 +165,7 @@ namespace Microsoft.FSharp.Control
157165
static member AsyncWriteAllLines(path, (lines:string array), ?encoder) =
158166
let enc = match encoder with Some e -> e | None -> System.Text.Encoding.Default
159167
async{
160-
use memStrm = new System.IO.MemoryStream()
161-
use sWriter = new System.IO.StreamWriter(memStrm, enc)
162-
lines |> Array.iter (fun line -> sWriter.WriteLine(line))
163-
do sWriter.Flush()
164-
let bs = memStrm.ToArray()
168+
let bs = LinesToBytes (lines, enc)
165169
return! System.IO.File.AsyncWriteAllBytes(path, bs)
166170
}
167171

@@ -181,17 +185,13 @@ namespace Microsoft.FSharp.Control
181185
return! System.IO.File.AsyncAppendAllBytes(path, bs)
182186
}
183187

184-
// Different memory profile to File.WriteAllLines, converts all lines to a byte[] before doing any writing, then a single write op.
185-
// Not good for writing huge files, but writing one line at a time, as per File.WriteAllLines has its own issues,
188+
// Different memory profile to File.AppendAllLines, converts all lines to a byte[] before doing any writing, then a single write op.
189+
// Not good for writing huge files, but writing one line at a time, as per File.AppendAllLines has its own issues,
186190
// e.g. sequential application of async IO (so lines are written in order)
187191
static member AsyncAppendAllLines(path, (lines:string array), ?encoder) =
188192
let enc = match encoder with Some e -> e | None -> System.Text.Encoding.Default
189193
async{
190-
use memStrm = new System.IO.MemoryStream()
191-
use sWriter = new System.IO.StreamWriter(memStrm, enc)
192-
lines |> Array.iter (fun line -> sWriter.WriteLine(line))
193-
do sWriter.Flush()
194-
let bs = memStrm.ToArray()
194+
let bs = LinesToBytes (lines, enc)
195195
return! System.IO.File.AsyncAppendAllBytes(path, bs)
196196
}
197197

0 commit comments

Comments
 (0)