Darwinism/ComputingServices/FileSystem/Protocols/NetTransfer.vb

91 lines
3.2 KiB
VB.net
Raw Normal View History

2018-04-14 03:26:02 +08:00
#Region "Microsoft.VisualBasic::13997d7f290f3481713b72ff12e68f45, ComputingServices\FileSystem\Protocols\NetTransfer.vb"
2016-06-29 07:48:25 +08:00
' Author:
'
' asuka (amethyst.asuka@gcmodeller.org)
2016-12-07 23:50:18 +08:00
' xie (genetics@smrucc.org)
2018-04-14 03:26:02 +08:00
' xieguigang (xie.guigang@live.com)
2016-06-29 07:48:25 +08:00
'
2017-12-24 22:32:04 +08:00
' Copyright (c) 2018 GPL3 Licensed
2016-06-29 07:48:25 +08:00
'
'
' GNU GENERAL PUBLIC LICENSE (GPL3)
'
2018-04-14 03:26:02 +08:00
'
2016-06-29 07:48:25 +08:00
' 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/>.
2018-04-14 03:26:02 +08:00
' /********************************************************************************/
' Summaries:
' Module NetTransfer
'
' Sub: Download, Transfer, Upload
'
'
' /********************************************************************************/
2016-06-29 07:48:25 +08:00
#End Region
Imports System.IO
Imports System.Text.RegularExpressions
Imports Microsoft.VisualBasic.Net
Imports Microsoft.VisualBasic.Net.Protocols
Imports Microsoft.VisualBasic.Net.Protocols.Reflection
Imports Microsoft.VisualBasic.Serialization
Namespace FileSystem.Protocols
Public Module NetTransfer
''' <summary>
''' 从本地上传文件
''' </summary>
''' <param name="local"></param>
''' <param name="destination"></param>
''' <param name="remote"></param>
Public Sub Upload(local As String, destination As String, remote As IPEndPoint)
2016-02-05 06:27:21 +08:00
Using file As New IO.RemoteFileStream(destination, FileMode.OpenOrCreate, remote)
Dim localFile As New FileStream(local, FileMode.Open)
Call Transfer(localFile, file, 1024 * 1024)
End Using
End Sub
Public Sub Transfer(source As Stream, target As Stream, bufLen As Integer)
Dim buffer As Byte() = New Byte(bufLen - 1) {}
Do While source.Position < source.Length
Dim d As Integer = (source.Length - source.Position) - buffer.Length
If d < 0 Then ' 注意d 是负值的
buffer = New Byte(-d - 1) {}
End If
Call source.Read(buffer, Scan0, buffer.Length)
Call target.Write(buffer, Scan0, buffer.Length)
Call target.Flush()
Loop
End Sub
Public Sub Download(destination As String, local As String, remote As IPEndPoint)
2016-02-05 06:27:21 +08:00
Using file As New IO.RemoteFileStream(destination, FileMode.OpenOrCreate, remote)
Dim localFile As New FileStream(local, FileMode.OpenOrCreate)
Call Transfer(file, localFile, 1024 * 1024)
End Using
End Sub
End Module
2016-06-29 07:48:25 +08:00
End Namespace