| 1 | #Region "Microsoft.VisualBasic::95b5d7881640710928a233afc2a56209, Microsoft.VisualBasic.Core\Extensions\Doc\PartitionedStream.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 | ' Delegate Function |
| 35 | ' |
| 36 | ' |
| 37 | ' Class PartitionedStream |
| 38 | ' |
| 39 | ' Properties: Current, EOF, Total |
| 40 | ' |
| 41 | ' Constructor: (+2 Overloads) Sub New |
| 42 | ' |
| 43 | ' Function: PartitionByLines, ReadPartition, ToString |
| 44 | ' |
| 45 | ' Sub: (+2 Overloads) Dispose |
| 46 | ' |
| 47 | ' |
| 48 | ' |
| 49 | ' /********************************************************************************/ |
| 50 | |
| 51 | #End Region |
| 52 | |
| 53 | Imports System.Text |
| 54 | |
| 55 | Public Delegate Function PartitioningMethod(block As String, ByRef Left As String) As String() |
| 56 | |
| 57 | Namespace Text |
| 58 | |
| 59 | ''' <summary> |
| 60 | ''' 只是针对文本文件的 |
| 61 | ''' </summary> |
| 62 | Public Class PartitionedStream : Implements System.IDisposable |
| 63 | |
| 64 | ReadOnly _readerStream As IO.FileStream |
| 65 | ReadOnly _blockSize As Integer |
| 66 | ReadOnly _partitions As PartitioningMethod |
| 67 | ReadOnly _encoding As System.Text.Encoding |
| 68 | |
| 69 | Sub New(path As String, blockSize As Integer, partitioning As PartitioningMethod, Optional encoding As Encoding = Nothing) |
| 70 | _readerStream = New IO.FileStream(path, IO.FileMode.Open) |
| 71 | _blockSize = blockSize |
| 72 | _partitions = partitioning |
| 73 | _Total = _readerStream.Length |
| 74 | |
| 75 | If _encoding Is Nothing Then |
| 76 | _encoding = System.Text.Encoding.Default |
| 77 | Else |
| 78 | _encoding = encoding |
| 79 | End If |
| 80 | End Sub |
| 81 | |
| 82 | ''' <summary> |
| 83 | ''' 依照换行符来进行分区 |
| 84 | ''' </summary> |
| 85 | ''' <param name="path"></param> |
| 86 | ''' <param name="blockSize"></param> |
| 87 | ''' <param name="encoding"></param> |
| 88 | Sub New(path As String, blockSize As Integer, Optional encoding As Encoding = Nothing) |
| 89 | Call Me.New(path, blockSize, AddressOf PartitionedStream.PartitionByLines, encoding) |
| 90 | End Sub |
| 91 | |
| 92 | Public ReadOnly Property EOF As Boolean |
| 93 | Get |
| 94 | Return _Current >= _Total |
| 95 | End Get |
| 96 | End Property |
| 97 | |
| 98 | Public ReadOnly Property Current As Long |
| 99 | Public ReadOnly Property Total As Long |
| 100 | |
| 101 | Dim previous As Byte() |
| 102 | |
| 103 | Public Function ReadPartition() As String() |
| 104 | If EOF Then Return Nothing |
| 105 | |
| 106 | Dim chunkBuffer As Byte() |
| 107 | |
| 108 | If _Current + _blockSize > _Total Then |
| 109 | chunkBuffer = New Byte(_blockSize - 1) {} |
| 110 | Else |
| 111 | chunkBuffer = New Byte(_Total - _Current - 1) {} |
| 112 | End If |
| 113 | |
| 114 | Call _readerStream.Read(chunkBuffer, _Current, chunkBuffer.Length) |
| 115 | Call previous.Add(chunkBuffer) |
| 116 | |
| 117 | Dim Text As String = _encoding.GetString(previous) |
| 118 | Dim rtvl = Me._partitions(Text, Text) |
| 119 | previous = _encoding.GetBytes(Text) |
| 120 | Return rtvl |
| 121 | End Function |
| 122 | |
| 123 | Public Overrides Function ToString() As String |
| 124 | Return _readerStream.Name |
| 125 | End Function |
| 126 | |
| 127 | #Region "IDisposable Support" |
| 128 | Private disposedValue As Boolean ' To detect redundant calls |
| 129 | |
| 130 | ' IDisposable |
| 131 | Protected Overridable Sub Dispose(disposing As Boolean) |
| 132 | If Not Me.disposedValue Then |
| 133 | If disposing Then |
| 134 | ' TODO: dispose managed state (managed objects). |
| 135 | End If |
| 136 | |
| 137 | ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below. |
| 138 | ' TODO: set large fields to null. |
| 139 | End If |
| 140 | Me.disposedValue = True |
| 141 | End Sub |
| 142 | |
| 143 | ' TODO: override Finalize() only if Dispose(disposing As Boolean) above has code to free unmanaged resources. |
| 144 | 'Protected Overrides Sub Finalize() |
| 145 | ' ' Do not change this code. Put cleanup code in Dispose(disposing As Boolean) above. |
| 146 | ' Dispose(False) |
| 147 | ' MyBase.Finalize() |
| 148 | 'End Sub |
| 149 | |
| 150 | ' This code added by Visual Basic to correctly implement the disposable pattern. |
| 151 | Public Sub Dispose() Implements IDisposable.Dispose |
| 152 | ' Do not change this code. Put cleanup code in Dispose(disposing As Boolean) above. |
| 153 | Dispose(True) |
| 154 | ' TODO: uncomment the following line if Finalize() is overridden above. |
| 155 | ' GC.SuppressFinalize(Me) |
| 156 | End Sub |
| 157 | #End Region |
| 158 | |
| 159 | Public Shared Function PartitionByLines(block As String, ByRef Left As String) As String() |
| 160 | Dim Tokens As String() = block.LineTokens |
| 161 | |
| 162 | Left = Tokens.LastOrDefault |
| 163 | Tokens = Tokens.Takes(Tokens.Length - 1).ToArray |
| 164 | Return Tokens |
| 165 | End Function |
| 166 | End Class |
| 167 | End Namespace |