1 #Region "Microsoft.VisualBasic::a601e76aef37afbfcceef80194b0269d, Microsoft.VisualBasic.Core\Extensions\IO\Extensions\StreamHelper.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     Module StreamHelper
35     
36     '     Function: CopyStream
37     
38     '     Sub: Write, WriteLine
39     
40     ' /********************************************************************************/
41
42 #End Region
43
44 Imports System.IO
45 Imports System.Runtime.CompilerServices
46 Imports System.Text
47 Imports Microsoft.VisualBasic.CommandLine.Reflection
48 Imports Microsoft.VisualBasic.Language
49 Imports Microsoft.VisualBasic.Text
50
51 Public Module StreamHelper
52
53     ''' <summary>
54     ''' Download stream data from the http response.
55     ''' </summary>
56     ''' <param name="stream">
57     ''' Create from <see cref="WebServiceUtils.GetRequestRaw(String, BooleanString)"/>
58     ''' </param>
59     ''' <returns></returns>
60     <ExportAPI("Stream.Copy"Info:="Download stream data from the http response.")>
61     <Extension> Public Function CopyStream(stream As Stream, Optional target As Stream = NothingOptional bufferSize% = 64 * 1024) As Stream
62         If stream Is Nothing Then
63             Return If(target, New MemoryStream)
64         End If
65
66         Dim buffer As Byte() = New Byte(bufferSize - 1) {}
67         Dim i As int = Scan0
68
69         With target Or DirectCast(New MemoryStream(), Stream).AsDefault
70             Do While (i = stream.Read(buffer, 0, buffer.Length)) > 0
71                 Call .Write(buffer, 0, i)
72             Loop
73
74             Return .ByRef
75         End With
76     End Function
77
78     <Extension>
79     Public Sub Write(stream As Stream, value$, Optional encoding As Encoding = Nothing)
80         With (encoding Or UTF8).GetBytes(value)
81             Call stream.Write(.ByRef, Scan0, .Length)
82             Call stream.Flush()
83         End With
84     End Sub
85
86     <MethodImpl(MethodImplOptions.AggressiveInlining)>
87     <Extension>
88     Public Sub WriteLine(stream As Stream, Optional value$ = ""Optional encoding As Encoding = NothingOptional newLine$ = vbCrLf)
89         Call stream.Write(value & newLine, encoding)
90     End Sub
91 End Module