[445] | 1 |
|
---|
| 2 | NameSpace System
|
---|
| 3 | NameSpace IO
|
---|
| 4 |
|
---|
| 5 | Class MemoryStream
|
---|
| 6 | Inherits Stream
|
---|
| 7 |
|
---|
| 8 | Public
|
---|
| 9 | /*
|
---|
| 10 | 0に初期化される拡張可能な容量を使用して初期化
|
---|
| 11 | */
|
---|
| 12 | Sub MemoryStream()
|
---|
| 13 | This.writable = True
|
---|
| 14 | This.Resizable = True
|
---|
| 15 | This.visible = False
|
---|
| 16 | This.handle = HeapCreate(0,0,0)
|
---|
| 17 | This.p = HeapAlloc(This.handle,HEAP_ZERO_MEMORY,0)
|
---|
| 18 | This.StreamLength = 0
|
---|
| 19 | This.CurrentPosition = 0
|
---|
| 20 | End Sub
|
---|
| 21 | Sub MemoryStream(capacity As Long)
|
---|
| 22 | This.writable = True
|
---|
| 23 | This.Resizable = True
|
---|
| 24 | This.visible = False
|
---|
| 25 | This.handle = HeapCreate(0,0,0)
|
---|
| 26 | This.p = HeapAlloc(This.handle,HEAP_ZERO_MEMORY,capacity)
|
---|
| 27 | This.StreamLength = capacity
|
---|
| 28 | This.CurrentPosition = 0
|
---|
| 29 | End Sub
|
---|
| 30 | Sub MemoryStream(buffer As *Byte, length As Long)
|
---|
| 31 | This.writable = True
|
---|
| 32 | This.Resizable = False
|
---|
| 33 | This.visible = False
|
---|
| 34 | This.handle = HeapCreate(0,0,0)
|
---|
| 35 | This.p = HeapAlloc(This.handle,HEAP_ZERO_MEMORY,length)
|
---|
| 36 | MoveMemory(This.p,buffer,length)
|
---|
| 37 | This.StreamLength = length
|
---|
| 38 | This.CurrentPosition = 0
|
---|
| 39 | End Sub
|
---|
| 40 | Sub MemoryStream(buffer As *Byte, length As Long, writable As Boolean)
|
---|
| 41 | This.writable = writable
|
---|
| 42 | This.Resizable = False
|
---|
| 43 | This.visible = False
|
---|
| 44 | This.handle = HeapCreate(0,0,0)
|
---|
| 45 | This.p = HeapAlloc(This.handle,HEAP_ZERO_MEMORY,length)
|
---|
| 46 | MoveMemory(This.p,buffer,length)
|
---|
| 47 | This.StreamLength = length
|
---|
| 48 | This.CurrentPosition = 0
|
---|
| 49 | End Sub
|
---|
| 50 | Sub MemoryStream(buffer As *Byte, index As Long, count As Long)
|
---|
| 51 | This.writable = True
|
---|
| 52 | This.Resizable = False
|
---|
| 53 | This.visible = False
|
---|
| 54 | This.handle = HeapCreate(0,0,0)
|
---|
| 55 | This.p = HeapAlloc(This.handle,HEAP_ZERO_MEMORY,count)
|
---|
| 56 | MoveMemory(This.p,VarPtr(buffer[index]),count)
|
---|
| 57 | This.StreamLength = count
|
---|
| 58 | This.CurrentPosition = 0
|
---|
| 59 | End Sub
|
---|
| 60 | Sub MemoryStream(buffer As *Byte, index As Long, count As Long, writable As Boolean)
|
---|
| 61 | This.writable = writable
|
---|
| 62 | This.Resizable = False
|
---|
| 63 | This.visible = False
|
---|
| 64 | This.handle = HeapCreate(0,0,0)
|
---|
| 65 | This.p = HeapAlloc(This.handle,HEAP_ZERO_MEMORY,count)
|
---|
| 66 | MoveMemory(This.p,VarPtr(buffer[index]),count)
|
---|
| 67 | This.StreamLength = count
|
---|
| 68 | This.CurrentPosition = 0
|
---|
| 69 | End Sub
|
---|
| 70 | Sub MemoryStream(buffer As *Byte, index As Long, count As Long, writable As Boolean, visible As Boolean)
|
---|
| 71 | This.writable = writable
|
---|
| 72 | This.Resizable = False
|
---|
| 73 | This.visible = visible
|
---|
| 74 | This.handle = HeapCreate(0,0,0)
|
---|
| 75 | This.p = HeapAlloc(This.handle,HEAP_ZERO_MEMORY,count)
|
---|
| 76 | MoveMemory(This.p,VarPtr(buffer[index]),count)
|
---|
| 77 | This.StreamLength = count
|
---|
| 78 | This.CurrentPosition = 0
|
---|
| 79 | End Sub
|
---|
| 80 | Sub ~MemoryStream()
|
---|
| 81 | This.Close()
|
---|
| 82 | End Sub
|
---|
| 83 |
|
---|
| 84 | Public
|
---|
| 85 | Override Function CanRead() As Boolean
|
---|
| 86 | If This.p Then
|
---|
| 87 | Return True
|
---|
| 88 | Else
|
---|
| 89 | Return False
|
---|
| 90 | End If
|
---|
| 91 | End Function
|
---|
| 92 |
|
---|
| 93 | Override Function CanSeek() As Boolean
|
---|
| 94 | If This.p Then
|
---|
| 95 | Return True
|
---|
| 96 | Else
|
---|
| 97 | Return False
|
---|
| 98 | End If
|
---|
| 99 | End Function
|
---|
| 100 |
|
---|
| 101 | Override Function CanWrite() As Boolean
|
---|
| 102 | If This.p Then
|
---|
| 103 | Return This.writable
|
---|
| 104 | Else
|
---|
| 105 | Return False
|
---|
| 106 | End If
|
---|
| 107 | End Function
|
---|
| 108 |
|
---|
| 109 | /*
|
---|
| 110 | メモリの容量であってストリームの長さではない
|
---|
| 111 | メモリはヒープからブロック単位で確保されるので
|
---|
| 112 | 指定したストリーム長より大きくなることがある
|
---|
| 113 | また、配列として扱う場合の配列長となる
|
---|
| 114 | */
|
---|
| 115 | Virtual Function Capacity() As Long
|
---|
| 116 | If This.p = 0 Then Debug'Throw New ObjectDisposedException
|
---|
| 117 | Return HeapSize(This.handle,0,This.p)
|
---|
| 118 | End Function
|
---|
| 119 |
|
---|
| 120 | Virtual Sub Capacity(value As Long)
|
---|
| 121 | If This.p = 0 Then Debug'Throw New ObjectDisposedException
|
---|
| 122 | If value < 0 Then Debug'Throw New ArgumentOutOfRangeException
|
---|
| 123 | If value < This.StreamLength Then Debug'Throw New ArgumentOutOfRangeException
|
---|
| 124 | This.p = HeapReAlloc(This.handle,HEAP_ZERO_MEMORY,This.p,value)
|
---|
| 125 | End Sub
|
---|
| 126 |
|
---|
| 127 | /*
|
---|
| 128 | こちらは容量とは別のストリーム長
|
---|
| 129 | 実際のデータの長さはこちら
|
---|
| 130 | */
|
---|
| 131 | Override Function Length() As Int64
|
---|
| 132 | If This.p = 0 Then Debug'Throw New ObjectDisposedException
|
---|
| 133 | Return This.StreamLength
|
---|
| 134 | End Function
|
---|
| 135 |
|
---|
| 136 | /*
|
---|
| 137 | ストリームの現在位置を取得または設定
|
---|
| 138 | */
|
---|
| 139 | Override Sub Position(value As Int64)
|
---|
| 140 | If This.p = 0 Then Debug'Throw New ObjectDisposedException
|
---|
| 141 | If value < 0 Then Debug'Throw New ArgumentOutOfRangeException
|
---|
| 142 | If value > Int32.MaxValue() Then Debug'Throw New ArgumentOutOfRangeException
|
---|
| 143 | This.CurrentPosition = value As Long
|
---|
| 144 | End Sub
|
---|
| 145 |
|
---|
| 146 | Override Function Position() As Int64
|
---|
| 147 | If This.p = 0 Then Debug'Throw New ObjectDisposedException
|
---|
| 148 | Return This.CurrentPosition As Int64
|
---|
| 149 | End Function
|
---|
| 150 |
|
---|
| 151 | Override Sub Flush()
|
---|
| 152 | End Sub
|
---|
| 153 |
|
---|
| 154 | /* Virtual Function GetBuffer() As Array<Byte>
|
---|
| 155 | If visible = False Then Return NULL
|
---|
| 156 | Return p
|
---|
| 157 | End Function*/
|
---|
| 158 |
|
---|
| 159 | Override Function Read(buffer As *Byte, offset As Long, count As Long) As Long
|
---|
| 160 | If This.p = 0 Then Debug'Throw New ObjectDisposedException
|
---|
| 161 | If buffer = 0 Then Debug 'Throw New ArgumentNullException
|
---|
| 162 | If offset < 0 Then Debug'Throw New ArgumentOutOfRangeException
|
---|
| 163 | If count < 0 Then Debug'Throw New ArgumentOutOfRangeException
|
---|
| 164 | If (This.StreamLength - (This.CurrentPosition + count)) < 0 Then
|
---|
| 165 | count + = (This.StreamLength - (This.CurrentPosition + count))
|
---|
| 166 | ElseIf count < 1 Then
|
---|
| 167 | Return 0
|
---|
| 168 | End If
|
---|
| 169 | MoveMemory(VarPtr(buffer[offset]),VarPtr(This.p[This.CurrentPosition]),count)
|
---|
| 170 | This.CurrentPosition + = count
|
---|
| 171 | Return count
|
---|
| 172 | End Function
|
---|
| 173 |
|
---|
| 174 | Override Function ReadByte() As Long
|
---|
| 175 | Dim b As Byte
|
---|
| 176 | Dim ret = Read(VarPtr(b), 0, 1)
|
---|
| 177 | If ret <> 0 Then
|
---|
| 178 | Return b
|
---|
| 179 | Else
|
---|
| 180 | Return -1
|
---|
| 181 | End If
|
---|
| 182 | End Function
|
---|
| 183 |
|
---|
| 184 | Override Function Seek(offset As Int64, origin As SeekOrigin) As Int64
|
---|
| 185 | If This.p = 0 Then Debug'Throw New ObjectDisposedException
|
---|
| 186 | If offset > Int32.MaxValue() Then Debug'Throw New ArgumentOutOfRangeException
|
---|
| 187 | Select Case origin
|
---|
| 188 | Case SeekOrigin.Begin
|
---|
| 189 | If offset < 0 Then Debug 'Throw New IOException
|
---|
| 190 | This.CurrentPosition = offset As Long
|
---|
| 191 | Case SeekOrigin.Current
|
---|
| 192 | Beep(440,10)
|
---|
| 193 | If (This.CurrentPosition + offset) < 0 Then Debug 'Throw New IOException
|
---|
| 194 | This.CurrentPosition += offset As Long
|
---|
| 195 | Case SeekOrigin.End
|
---|
| 196 | If (This.StreamLength + offset) < 0 Then Debug 'Throw New IOException
|
---|
| 197 | This.CurrentPosition = (This.StreamLength + offset) As Long
|
---|
| 198 | Case Else
|
---|
| 199 | Debug 'Throw New ArgumentException
|
---|
| 200 | End Select
|
---|
| 201 | Return This.CurrentPosition As Int64
|
---|
| 202 | End Function
|
---|
| 203 |
|
---|
| 204 | Override Sub SetLength(value As Int64)
|
---|
| 205 | If This.p = 0 Then Debug'Throw New ObjectDisposedException
|
---|
| 206 | If This.writable = False Then Debug 'Throw New NotSupportedException
|
---|
| 207 | If This.Resizable = False Then Debug 'Throw New NotSupportedException
|
---|
| 208 | If value < 0 Then Debug'Throw New ArgumentOutOfRangeException
|
---|
| 209 | If value > Int32.MaxValue() Then Debug'Throw New ArgumentOutOfRangeException
|
---|
| 210 | This.p = HeapReAlloc(This.handle,HEAP_ZERO_MEMORY,This.p,value As Long)
|
---|
| 211 | This.StreamLength = value As Long
|
---|
| 212 | End Sub
|
---|
| 213 |
|
---|
| 214 | Override Sub Write(buffer As *Byte, offset As Long, count As Long)
|
---|
| 215 | If This.p = 0 Then Debug'Throw New ObjectDisposedException
|
---|
| 216 | If This.writable = False Then Debug'Throw New NotSupportedException
|
---|
| 217 | If buffer = 0 Then Debug 'Throw New ArgumentNullException
|
---|
| 218 | If offset < 0 Then Debug'Throw New ArgumentOutOfRangeException
|
---|
| 219 | If count < 0 Then Debug'Throw New ArgumentOutOfRangeException
|
---|
| 220 | If count > (This.StreamLength - This.CurrentPosition) Then
|
---|
| 221 | If This.Resizable = False Then
|
---|
| 222 | Debug 'Throw New NotSupportedException
|
---|
| 223 | Else
|
---|
| 224 | If count > (This.Capacity() - This.CurrentPosition) Then
|
---|
| 225 | This.p = HeapReAlloc(This.handle,HEAP_ZERO_MEMORY,This.p,This.CurrentPosition+count)
|
---|
| 226 | End If
|
---|
| 227 | This.StreamLength = This.CurrentPosition+count
|
---|
| 228 | End If
|
---|
| 229 | End If
|
---|
| 230 | MoveMemory(VarPtr(This.p[This.CurrentPosition]),VarPtr(buffer[offset]),count)
|
---|
| 231 | This.CurrentPosition + = count
|
---|
| 232 | End Sub
|
---|
| 233 |
|
---|
| 234 | Override Sub WriteByte(b As Byte)
|
---|
| 235 | Write(VarPtr(b), 0, 1)
|
---|
| 236 | End Sub
|
---|
| 237 |
|
---|
| 238 | /* Virtual Function ToArray() As List<Byte>
|
---|
| 239 | TODO:
|
---|
| 240 | End Function*/
|
---|
| 241 |
|
---|
| 242 | Virtual Sub WriteTo(stream As Stream)
|
---|
| 243 | If This.p = 0 Then Debug'Throw New ObjectDisposedException
|
---|
| 244 | If ActiveBasic.IsNothing(stream) Then Debug 'Throw New ArgumentNullException("path")
|
---|
| 245 | stream.Write(This.p,0,This.Capacity())
|
---|
| 246 | End Sub
|
---|
| 247 |
|
---|
| 248 | Protected
|
---|
| 249 | Override Sub Dispose(disposing As Boolean)
|
---|
| 250 | HeapFree(This.handle,0,InterlockedExchangePointer(ByVal VarPtr(This.p) As VoidPtr,NULL) As VoidPtr)
|
---|
| 251 | HeapDestroy(InterlockedExchangePointer(ByVal VarPtr(This.handle) As VoidPtr,NULL) As HANDLE)
|
---|
| 252 | End Sub
|
---|
| 253 |
|
---|
| 254 | /* status */
|
---|
| 255 | writable As Boolean
|
---|
| 256 | Resizable As Boolean
|
---|
| 257 | visible As Boolean
|
---|
| 258 |
|
---|
| 259 | StreamLength As Long
|
---|
| 260 | CurrentPosition As Long
|
---|
| 261 |
|
---|
| 262 | Private
|
---|
| 263 | handle As HANDLE
|
---|
| 264 | p As *Byte
|
---|
| 265 | End Class
|
---|
| 266 |
|
---|
| 267 | End NameSpace 'IO
|
---|
| 268 | End NameSpace 'System
|
---|