2019-07-29 02:05:05 +08:00
|
|
|
|
#Region "Microsoft.VisualBasic::1d29d822507b18e368473917fe944414, Microsoft.VisualBasic.Core\CommandLine\Stream.vb"
|
2019-06-04 20:43:55 +08:00
|
|
|
|
|
2019-07-29 02:05:05 +08:00
|
|
|
|
' Author:
|
|
|
|
|
|
'
|
|
|
|
|
|
' asuka (amethyst.asuka@gcmodeller.org)
|
|
|
|
|
|
' xie (genetics@smrucc.org)
|
|
|
|
|
|
' xieguigang (xie.guigang@live.com)
|
|
|
|
|
|
'
|
|
|
|
|
|
' Copyright (c) 2018 GPL3 Licensed
|
|
|
|
|
|
'
|
|
|
|
|
|
'
|
|
|
|
|
|
' GNU GENERAL PUBLIC LICENSE (GPL3)
|
|
|
|
|
|
'
|
|
|
|
|
|
'
|
|
|
|
|
|
' This program is free software: you can redistribute it and/or modify
|
|
|
|
|
|
' it under the terms of the GNU General Public License as published by
|
|
|
|
|
|
' the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
|
' (at your option) any later version.
|
|
|
|
|
|
'
|
|
|
|
|
|
' This program is distributed in the hope that it will be useful,
|
|
|
|
|
|
' but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
|
' GNU General Public License for more details.
|
|
|
|
|
|
'
|
|
|
|
|
|
' You should have received a copy of the GNU General Public License
|
|
|
|
|
|
' along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2019-07-24 20:26:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-07-29 02:05:05 +08:00
|
|
|
|
' /********************************************************************************/
|
2019-07-24 20:26:48 +08:00
|
|
|
|
|
2019-07-29 02:05:05 +08:00
|
|
|
|
' Summaries:
|
2019-07-24 20:26:48 +08:00
|
|
|
|
|
2019-07-29 02:05:05 +08:00
|
|
|
|
' Enum FileTypes
|
|
|
|
|
|
'
|
|
|
|
|
|
' DiskFile, MemoryFile, PipelineFile
|
|
|
|
|
|
'
|
|
|
|
|
|
'
|
|
|
|
|
|
'
|
|
|
|
|
|
'
|
|
|
|
|
|
'
|
|
|
|
|
|
' Module StreamExtensions
|
|
|
|
|
|
'
|
|
|
|
|
|
' Function: FileType, OpenForRead, OpenForWrite
|
|
|
|
|
|
'
|
|
|
|
|
|
'
|
|
|
|
|
|
' /********************************************************************************/
|
2019-06-04 20:43:55 +08:00
|
|
|
|
|
|
|
|
|
|
#End Region
|
|
|
|
|
|
|
|
|
|
|
|
Imports System.IO
|
2019-05-26 12:53:37 +08:00
|
|
|
|
Imports System.IO.MemoryMappedFiles
|
|
|
|
|
|
|
|
|
|
|
|
Namespace CommandLine
|
|
|
|
|
|
|
2019-05-26 13:09:51 +08:00
|
|
|
|
Public Enum FileTypes
|
|
|
|
|
|
DiskFile
|
|
|
|
|
|
PipelineFile
|
|
|
|
|
|
MemoryFile
|
|
|
|
|
|
End Enum
|
|
|
|
|
|
|
2019-05-26 12:53:37 +08:00
|
|
|
|
''' <summary>
|
|
|
|
|
|
''' + file path: C://path/to/file
|
|
|
|
|
|
''' + standard input: std_in://
|
|
|
|
|
|
''' + standard output: std_out://
|
|
|
|
|
|
''' + memory mapping file: memory://file/uri
|
|
|
|
|
|
''' </summary>
|
2019-07-24 20:26:48 +08:00
|
|
|
|
<HideModuleName> Public Module StreamExtensions
|
2019-05-26 12:53:37 +08:00
|
|
|
|
|
2019-05-26 13:09:51 +08:00
|
|
|
|
Public Function FileType(reference As String) As FileTypes
|
|
|
|
|
|
If reference.TextEquals("std_in://") OrElse reference.TextEquals("std_out://") Then
|
|
|
|
|
|
Return FileTypes.PipelineFile
|
|
|
|
|
|
ElseIf reference.ToLower.StartsWith("memory://") Then
|
|
|
|
|
|
Return FileTypes.MemoryFile
|
|
|
|
|
|
Else
|
|
|
|
|
|
Return FileTypes.DiskFile
|
|
|
|
|
|
End If
|
|
|
|
|
|
End Function
|
|
|
|
|
|
|
2019-07-24 20:26:48 +08:00
|
|
|
|
''' <summary>
|
|
|
|
|
|
''' 一个用于通用化的打开数据流读取对象的函数
|
|
|
|
|
|
''' </summary>
|
|
|
|
|
|
''' <param name="reference"></param>
|
|
|
|
|
|
''' <returns></returns>
|
2019-05-26 12:53:37 +08:00
|
|
|
|
Public Function OpenForRead(reference As String) As Stream
|
|
|
|
|
|
If reference.TextEquals("std_in://") Then
|
|
|
|
|
|
Return Console.OpenStandardInput
|
|
|
|
|
|
ElseIf reference.TextEquals("std_out://") Then
|
|
|
|
|
|
Throw New InvalidProgramException()
|
|
|
|
|
|
ElseIf reference.ToLower.StartsWith("memory://") Then
|
2019-07-24 20:26:48 +08:00
|
|
|
|
Dim view As Stream
|
|
|
|
|
|
|
2019-05-26 12:53:37 +08:00
|
|
|
|
reference = reference.GetTagValue(":/").Value
|
2019-07-24 20:26:48 +08:00
|
|
|
|
view = MemoryMappedFile.OpenExisting(reference).CreateViewStream
|
|
|
|
|
|
view.Seek(Scan0, SeekOrigin.Begin)
|
|
|
|
|
|
|
|
|
|
|
|
Return view
|
2019-05-26 12:53:37 +08:00
|
|
|
|
Else
|
|
|
|
|
|
Return New FileStream(reference, FileMode.Open, access:=FileAccess.Read, share:=FileShare.ReadWrite)
|
|
|
|
|
|
End If
|
|
|
|
|
|
End Function
|
|
|
|
|
|
|
2019-07-24 20:26:48 +08:00
|
|
|
|
''' <summary>
|
|
|
|
|
|
''' 请注意,这个函数在创建内存映射文件的时候,默认是0.5GB大小的
|
|
|
|
|
|
''' </summary>
|
|
|
|
|
|
''' <param name="reference"></param>
|
|
|
|
|
|
''' <param name="size"></param>
|
|
|
|
|
|
''' <returns></returns>
|
|
|
|
|
|
Public Function OpenForWrite(reference As String, Optional size& = 512 * 1024 * 1024) As Stream
|
2019-05-26 12:53:37 +08:00
|
|
|
|
If reference.TextEquals("std_in://") Then
|
|
|
|
|
|
Throw New InvalidProgramException
|
|
|
|
|
|
ElseIf reference.TextEquals("std_out://") Then
|
|
|
|
|
|
Return Console.OpenStandardOutput
|
|
|
|
|
|
ElseIf reference.ToLower.StartsWith("memory://") Then
|
2019-07-24 20:26:48 +08:00
|
|
|
|
Dim view As Stream
|
|
|
|
|
|
'Dim security As New MemoryMappedFileSecurity()
|
|
|
|
|
|
'Dim userName$ = "everyone"
|
|
|
|
|
|
|
|
|
|
|
|
'If Not App.IsMicrosoftPlatform Then
|
|
|
|
|
|
' ' 20190724 linux平台上很有可能不存在"everyone"这个账户角色
|
|
|
|
|
|
' userName = Environment.UserName
|
|
|
|
|
|
'End If
|
|
|
|
|
|
|
|
|
|
|
|
' Security.AddAccessRule(New AccessRule(Of MemoryMappedFileRights)(userName, MemoryMappedFileRights.FullControl, AccessControlType.Allow))
|
2019-05-26 12:53:37 +08:00
|
|
|
|
reference = reference.GetTagValue(":/").Value
|
2019-07-24 20:26:48 +08:00
|
|
|
|
view = MemoryMappedFile.CreateOrOpen(reference, size, MemoryMappedFileAccess.ReadWrite).CreateViewStream
|
|
|
|
|
|
'view = MemoryMappedFile.CreateOrOpen(
|
|
|
|
|
|
' reference, size,
|
|
|
|
|
|
' access:=MemoryMappedFileAccess.ReadWrite,
|
|
|
|
|
|
' options:=MemoryMappedFileOptions.DelayAllocatePages,
|
|
|
|
|
|
' memoryMappedFileSecurity:=security,
|
|
|
|
|
|
' inheritability:=HandleInheritability.Inheritable
|
|
|
|
|
|
').CreateViewStream
|
|
|
|
|
|
|
|
|
|
|
|
Call view.Seek(Scan0, SeekOrigin.Begin)
|
|
|
|
|
|
|
|
|
|
|
|
Return view
|
2019-05-26 12:53:37 +08:00
|
|
|
|
Else
|
|
|
|
|
|
Return New FileStream(reference, FileMode.OpenOrCreate, access:=FileAccess.Write, share:=FileShare.Read)
|
|
|
|
|
|
End If
|
|
|
|
|
|
End Function
|
|
|
|
|
|
End Module
|
2019-06-04 20:43:55 +08:00
|
|
|
|
End Namespace
|