jj: Add Tests for Filesystem module, flush on file write in Filesystem

This commit is contained in:
Jonathan Jenne
2018-11-19 16:29:26 +01:00
parent 83a63ff25d
commit 0f105b6ebf
13 changed files with 621 additions and 17 deletions

View File

@@ -57,7 +57,6 @@ Public Class FileContainer
End Get
End Property
''' <summary>
''' Gibt eine Auflistung der Dateien in Container zurück.
''' </summary>
@@ -212,7 +211,7 @@ Public Class FileContainer
''' </summary>
''' <param name="FilePath">Der Pfad zur Datei, die im Container gespeichert werden soll</param>
''' <exception cref="FileNotFoundException" />
Public Sub AddFile(FilePath As String)
Public Function AddFile(FilePath As String) As String
Try
Dim oFileInfo As New FileInfo(FilePath)
@@ -220,14 +219,14 @@ Public Class FileContainer
Throw New FileNotFoundException($"{FilePath} is not a valid path.")
End If
AddFile(IO.File.ReadAllBytes(FilePath))
Return AddFileAsByteArray(IO.File.ReadAllBytes(FilePath))
Catch ex As Exception
_logger.Error(ex)
Throw ex
End Try
End Sub
End Function
Public Sub AddFile(FileContents As Byte())
Public Function AddFileAsByteArray(FileContents As Byte()) As String
Try
Dim oFileEntry As New FileEntry With {
.Contents = FileContents,
@@ -235,11 +234,13 @@ Public Class FileContainer
}
_inner.Files.Add(oFileEntry)
Return oFileEntry.FileId
Catch ex As Exception
_logger.Error(ex)
Throw ex
End Try
End Sub
End Function
Public Function GetFile(FileId As String) As FileEntry
Return _inner.Files.Where(Function(f) f.FileId = FileId).FirstOrDefault()
@@ -260,7 +261,10 @@ Public Class FileContainer
End Function
Private Sub BytesToFile(Data As Byte(), FilePath As String)
IO.File.WriteAllBytes(FilePath, Data)
Using oSourceStream As New FileStream(FilePath, FileMode.Append, FileAccess.Write, FileShare.None)
oSourceStream.Write(Data, 0, Data.Length)
oSourceStream.Flush()
End Using
End Sub
Private Function FileToBytes(FilePath As String) As Byte()
@@ -277,8 +281,9 @@ Public Class FileContainer
End Function
Private Async Sub BytesToFileAsync(Data As Byte(), FilePath As String)
Using oSourceStream As New FileStream(FilePath, FileMode.Append, FileAccess.Write, FileShare.None, bufferSize:=4096, useAsync:=True)
Using oSourceStream As New FileStream(FilePath, FileMode.Append, FileAccess.Write, FileShare.None, 4096, True)
Await oSourceStream.WriteAsync(Data, 0, Data.Length)
Await oSourceStream.FlushAsync()
End Using
End Sub
End Class