#Region "Microsoft.VisualBasic::0d8ef0f0ddb5f1296d8c296ce0b4bb84, ComputingServices\FileSystem\Protocols\FileOpen.vb"
' 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 .
' /********************************************************************************/
' Summaries:
' Class RenameArgs
'
' Properties: [New], old
'
' Class DeleteArgs
'
' Properties: [option], obj
'
' Class FileOpen
'
' Properties: Access, Mode
'
' Function: OpenHandle, ToString
'
' Class ReadBuffer
'
' Properties: length, offset
'
' Constructor: (+2 Overloads) Sub New
' Function: (+2 Overloads) CreateBuffer
'
' Interface IReadWriteBuffer
'
' Properties: length, offset
'
' Class WriteStream
'
' Properties: buffer, Handle, length, offset
'
' Constructor: (+2 Overloads) Sub New
' Function: CreateBuffer, Serialize
'
' Class FileStreamPosition
'
' Properties: Position
'
' Constructor: (+2 Overloads) Sub New
'
'
' /********************************************************************************/
#End Region
Imports System.IO
Imports System.Text
Imports Microsoft.VisualBasic.Language
Imports Microsoft.VisualBasic.Net.Protocols
Imports Microsoft.VisualBasic.Serialization.JSON
Namespace FileSystem.Protocols
Public Class RenameArgs
Public Property old As String
Public Property [New] As String
End Class
Public Class DeleteArgs
Public Property obj As String
Public Property [option] As Integer
End Class
'''
''' Initializes a new instance of the System.IO.FileStream class with the specified
''' path and creation mode.
'''
Public Class FileOpen : Inherits FileHandle
'''
''' Specifies how the operating system should open a file.
'''
'''
Public Property Mode As Integer
Public Property Access As Integer = FileAccess.Read
Public Overrides Function ToString() As String
Return $"[{DirectCast(Mode, FileMode).ToString }] " & FileName.ToFileURL
End Function
'''
''' Initializes a new instance of the System.IO.FileStream class with the specified
''' path and creation mode.
'''
Public Function OpenHandle() As FileStream
Dim mode As FileMode = DirectCast(Me.Mode, FileMode)
Dim access As FileAccess = DirectCast(Me.Access, FileAccess)
Return New FileStream(FileName, mode, access)
End Function
End Class
Public Class ReadBuffer : Inherits FileHandle
Implements IReadWriteBuffer
Public Property offset As Integer Implements IReadWriteBuffer.offset
'''
''' 输入的参数的长度
'''
'''
Public Property length As Integer Implements IReadWriteBuffer.length
Sub New()
End Sub
Sub New(handle As FileHandle)
Call MyBase.New(handle)
End Sub
Public Function CreateBuffer() As Byte()
Return CreateBuffer(Me)
End Function
Public Shared Function CreateBuffer(op As IReadWriteBuffer) As Byte()
Dim buffer As Byte() = New Byte(op.length + op.offset - 1) {}
Return buffer
End Function
End Class
Public Interface IReadWriteBuffer
Property offset As Integer
Property length As Integer
End Interface
Public Class WriteStream : Inherits RawStream
Implements IReadWriteBuffer
Public Property Handle As FileHandle
Public Property length As Integer Implements IReadWriteBuffer.length
Public Property offset As Integer Implements IReadWriteBuffer.offset
Public Property buffer As Byte()
Sub New()
End Sub
Sub New(raw As Byte())
Dim buf As Byte() = New Byte(INT32 - 1) {}
Dim p As int = Scan0
Dim handleLen As Integer
Dim bufferLen As Integer
Call Array.ConstrainedCopy(raw, p + INT32, buf, Scan0, INT32) : length = BitConverter.ToInt32(buf, Scan0)
Call Array.ConstrainedCopy(raw, p + INT32, buf, Scan0, INT32) : offset = BitConverter.ToInt32(buf, Scan0)
Call Array.ConstrainedCopy(raw, p + INT32, buf, Scan0, INT32) : handleLen = BitConverter.ToInt32(buf, Scan0)
Call Array.ConstrainedCopy(raw, p + INT32, buf, Scan0, INT32) : bufferLen = BitConverter.ToInt32(buf, Scan0)
buf = New Byte(handleLen - 1) {}
Call Array.ConstrainedCopy(raw, p + handleLen, buf, Scan0, handleLen)
Dim json As String = Encoding.UTF8.GetString(buf)
Handle = json.LoadObject(Of FileHandle)
buffer = New Byte(bufferLen - 1) {}
Call Array.ConstrainedCopy(raw, p, buffer, Scan0, bufferLen)
End Sub
Public Overrides Function Serialize() As Byte()
Dim handle As Byte() = System.Text.Encoding.UTF8.GetBytes(Me.Handle.GetJson)
Dim length As Byte() = BitConverter.GetBytes(Me.length)
Dim offset As Byte() = BitConverter.GetBytes(Me.offset)
Dim chunkBuffer As Byte() = New Byte(INT32 + ' length
INT32 + ' offset
INT32 + ' handle length
INT32 + ' buffer length
handle.Length +
buffer.Length - 1) {}
Dim p As int = Scan0
Dim handleLen As Byte() = BitConverter.GetBytes(handle.Length)
Dim bufferLen As Byte() = BitConverter.GetBytes(buffer.Length)
Call Array.ConstrainedCopy(length, Scan0, chunkBuffer, p + INT32, INT32)
Call Array.ConstrainedCopy(offset, Scan0, chunkBuffer, p + INT32, INT32)
Call Array.ConstrainedCopy(handleLen, Scan0, chunkBuffer, p + INT32, INT32)
Call Array.ConstrainedCopy(bufferLen, Scan0, chunkBuffer, p + INT32, INT32)
Call Array.ConstrainedCopy(handle, Scan0, chunkBuffer, p + handle.Length, handle.Length)
Call Array.ConstrainedCopy(buffer, Scan0, chunkBuffer, p, buffer.Length)
Return chunkBuffer
End Function
Public Function CreateBuffer() As Byte()
Return ReadBuffer.CreateBuffer(Me)
End Function
End Class
Public Class FileStreamPosition : Inherits FileHandle
Public Const [GET] As Long = -100
'''
''' -100表示获取
'''
'''
Public Property Position As Long = [GET]
Sub New(handle As FileHandle)
Call MyBase.New(handle)
End Sub
Sub New()
End Sub
End Class
End Namespace