From 0bd2a98080a9224c985ee8b25b317cf8cd633dee Mon Sep 17 00:00:00 2001 From: xieguigang Date: Sat, 25 May 2019 13:10:01 +0800 Subject: [PATCH] unify method for union the file read and write? --- 47-dotnet_Microsoft.VisualBasic.vbproj | 3 +++ .../MMFProtocol/MapStream/MSReader.vb | 20 +++++++++---------- .../MMFProtocol/MapStream/MSWriter.vb | 14 +++++-------- CommandLine/CLI/Stream/Extensions.vb | 6 ++++++ CommandLine/CLI/Stream/StreamReader.vb | 6 ++++++ CommandLine/CLI/Stream/StreamWriter.vb | 6 ++++++ CommandLine/CommandLine.vb | 6 +++++- 7 files changed, 41 insertions(+), 20 deletions(-) create mode 100644 CommandLine/CLI/Stream/Extensions.vb create mode 100644 CommandLine/CLI/Stream/StreamReader.vb create mode 100644 CommandLine/CLI/Stream/StreamWriter.vb diff --git a/47-dotnet_Microsoft.VisualBasic.vbproj b/47-dotnet_Microsoft.VisualBasic.vbproj index c12f5e7b..afb78082 100644 --- a/47-dotnet_Microsoft.VisualBasic.vbproj +++ b/47-dotnet_Microsoft.VisualBasic.vbproj @@ -955,6 +955,9 @@ + + + diff --git a/ApplicationServices/Parallel/MMFProtocol/MapStream/MSReader.vb b/ApplicationServices/Parallel/MMFProtocol/MapStream/MSReader.vb index 8172441a..f08657ba 100644 --- a/ApplicationServices/Parallel/MMFProtocol/MapStream/MSReader.vb +++ b/ApplicationServices/Parallel/MMFProtocol/MapStream/MSReader.vb @@ -58,21 +58,21 @@ Imports System.IO.MemoryMappedFiles Namespace Parallel.MMFProtocol.MapStream - Public MustInherit Class IMapBase : Implements IDisposable + Public MustInherit Class MapStream : Implements IDisposable Public ReadOnly Property URI As String - Protected _chunkBuffer As Byte() - Protected _mmfileStream As MemoryMappedFile + Protected buffer As Byte() + Protected file As MemoryMappedFile - Sub New(uri As String, ChunkSize As Long) + Sub New(uri As String, chunkSize As Integer) Me._URI = uri - Me._chunkBuffer = New Byte(ChunkSize - 1) {} + Me.buffer = New Byte(chunkSize - 1) {} End Sub Public Function Read() As MMFStream - Call _mmfileStream.CreateViewStream.Read(_chunkBuffer, Scan0, _chunkBuffer.Length) - Return New MMFStream(_chunkBuffer) + Call file.CreateViewStream.Read(buffer, Scan0, buffer.Length) + Return New MMFStream(buffer) End Function Public Overrides Function ToString() As String @@ -112,7 +112,7 @@ Namespace Parallel.MMFProtocol.MapStream #End Region End Class - Public Class MSIOReader : Inherits IMapBase + Public Class MSIOReader : Inherits MapStream Implements IDisposable ''' @@ -132,7 +132,7 @@ Namespace Parallel.MMFProtocol.MapStream ''' 内存映射文件的数据块的预分配大小 Sub New(uri As String, callback As DataArrival, ChunkSize As Long) Call MyBase.New(uri, ChunkSize) - _mmfileStream = MemoryMappedFile.OpenExisting(uri) + file = MemoryMappedFile.OpenExisting(uri) _dataArrivals = callback Call Parallel.RunTask(AddressOf __threadElapsed) @@ -152,7 +152,7 @@ Namespace Parallel.MMFProtocol.MapStream ''' Public Function ReadBadge() As Long Dim buf As Byte() = New Byte(MMFStream.INT64 - 1) {} - Call _mmfileStream.CreateViewStream.Read(buf, Scan0, buf.Length) + Call file.CreateViewStream.Read(buf, Scan0, buf.Length) Dim n As Long = BitConverter.ToInt64(buf, Scan0) Return n End Function diff --git a/ApplicationServices/Parallel/MMFProtocol/MapStream/MSWriter.vb b/ApplicationServices/Parallel/MMFProtocol/MapStream/MSWriter.vb index 1750d287..6e521772 100644 --- a/ApplicationServices/Parallel/MMFProtocol/MapStream/MSWriter.vb +++ b/ApplicationServices/Parallel/MMFProtocol/MapStream/MSWriter.vb @@ -59,12 +59,12 @@ Namespace Parallel.MMFProtocol.MapStream #Region "Nested Type Definitions" ''' - ''' + ''' Memory stream writer ''' ''' ''' mmfServer的主要功能是创建并维护一个内存映射文件 ''' - Public Class MSWriter : Inherits IMapBase + Public Class MSWriter : Inherits MapStream ''' ''' @@ -75,20 +75,16 @@ Namespace Parallel.MMFProtocol.MapStream Call MyBase.New(uri, chunkSize) Try - _mmfileStream = - MemoryMappedFiles.MemoryMappedFile.CreateNew( - uri, chunkSize) + file = MemoryMappedFiles.MemoryMappedFile.CreateNew(uri, chunkSize) Catch ex As Exception - _mmfileStream = - MemoryMappedFiles.MemoryMappedFile.CreateOrOpen( - uri, chunkSize) + file = MemoryMappedFiles.MemoryMappedFile.CreateOrOpen(uri, chunkSize) Finally End Try End Sub Public Sub WriteStream(byteData As Byte()) If Not byteData.IsNullOrEmpty Then - Call _mmfileStream.CreateViewStream.Write( + Call file.CreateViewStream.Write( byteData, Scan0, byteData.Length) End If End Sub diff --git a/CommandLine/CLI/Stream/Extensions.vb b/CommandLine/CLI/Stream/Extensions.vb new file mode 100644 index 00000000..47ab8b24 --- /dev/null +++ b/CommandLine/CLI/Stream/Extensions.vb @@ -0,0 +1,6 @@ +Namespace CommandLine.Stream + + Public Module Extensions + + End Module +End Namespace \ No newline at end of file diff --git a/CommandLine/CLI/Stream/StreamReader.vb b/CommandLine/CLI/Stream/StreamReader.vb new file mode 100644 index 00000000..37126cb8 --- /dev/null +++ b/CommandLine/CLI/Stream/StreamReader.vb @@ -0,0 +1,6 @@ +Namespace CommandLine.Stream + + Public Class StreamReader + + End Class +End Namespace \ No newline at end of file diff --git a/CommandLine/CLI/Stream/StreamWriter.vb b/CommandLine/CLI/Stream/StreamWriter.vb new file mode 100644 index 00000000..545cdf47 --- /dev/null +++ b/CommandLine/CLI/Stream/StreamWriter.vb @@ -0,0 +1,6 @@ +Namespace CommandLine.Stream + + Public Class StreamWriter + + End Class +End Namespace \ No newline at end of file diff --git a/CommandLine/CommandLine.vb b/CommandLine/CommandLine.vb index b8c63768..cdd1a0db 100644 --- a/CommandLine/CommandLine.vb +++ b/CommandLine/CommandLine.vb @@ -64,7 +64,6 @@ Imports Microsoft.VisualBasic.ComponentModel.Collection.Generic Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel Imports Microsoft.VisualBasic.Language Imports Microsoft.VisualBasic.Language.Default -Imports Microsoft.VisualBasic.Language.UnixBash.FileSystem Imports Microsoft.VisualBasic.Linq Imports Microsoft.VisualBasic.Scripting Imports Microsoft.VisualBasic.Scripting.Expressions @@ -73,6 +72,11 @@ Imports Microsoft.VisualBasic.Text Namespace CommandLine + ' file path: C://path/to/file + ' standard input: std_in:// + ' standard output: std_out:// + ' memory mapping file: memory://file/uri + ''' ''' A command line object that parse from the user input commandline string. ''' (从用户所输入的命令行字符串之中解析出来的命令行对象,标准的命令行格式为: