Changeset 336
- Timestamp:
- Sep 20, 2007, 3:15:53 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Include/Classes/System/IO/FileStream.ab
r289 r336 28 28 fileShare As DWord 29 29 fileOptions As DWord 30 31 /* 現在は未使用 */ 32 /*Overlapped As OVERLAPPED*/ 30 fileReadOverlapped As OVERLAPPED 31 fileWriteOverlapped As OVERLAPPED 33 32 34 33 Public … … 100 99 'Throw System.IO.FileNotFoundException 101 100 This.handle=0 101 Beep(220,500) 102 102 Exit Sub 103 103 End If 104 104 105 filePath = path106 fileMode = mo107 fileAccess = ac108 fileShare = sh109 fileOptions = op105 This.filePath = path 106 This.fileMode = mo 107 This.fileAccess = ac 108 This.fileShare = sh 109 This.fileOptions = op 110 110 End Sub 111 111 Sub FileStream(path As String, mode As FileMode, access As FileAccess, share As FileShare) 112 FileStream(path,mode,access,share,FileOptions.None)112 This.FileStream(path,mode,access,share,FileOptions.None) 113 113 End Sub 114 114 Sub FileStream(path As String, mode As FileMode, access As FileAccess) 115 FileStream(path,mode,access,FileShare.None,FileOptions.None)115 This.FileStream(path,mode,access,FileShare.None,FileOptions.None) 116 116 End Sub 117 117 Sub FileStream(path As String, mode As FileMode) … … 131 131 access=FileAccess.Write 132 132 End Select 133 FileStream(path,mode,access,FileShare.None,FileOptions.None)133 This.FileStream(path,mode,access,FileShare.None,FileOptions.None) 134 134 End Sub 135 135 … … 142 142 Override Function CanRead() As Boolean 143 143 /* ファイルが読み込みに対応しているかを返す */ 144 If fileAccess=GENERIC_READ/*FileAccess.Read*/ or fileAccess=GENERIC_READ or GENERIC_WRITE/*FileAccess.ReadWrite*/Then144 If This.fileAccess And GENERIC_READ Then 145 145 Return True 146 146 Else … … 151 151 Override Function CanSeek() As Boolean 152 152 /* ファイルがシークに対応しているかを返す */ 153 Return True 154 End Function 155 156 Override Function CanTimeout() As Boolean 157 /* ファイルがタイムアウトに対応しているかを返す */ 158 Return False /*今のところ対応していないのでFalse*/ 159 End Function 153 If GetFileType(This.handle)=FILE_TYPE_DISK Then 154 Return True 155 Else 156 Return False 157 End If 158 End Function 159 160 ' Override Function CanTimeout() As Boolean 161 ' /* ファイルがタイムアウトに対応しているかを返す */ 162 ' Return False /*今のところ対応していないのでFalse*/ 163 ' End Function*/ 160 164 161 165 Override Function CanWrite() As Boolean 162 166 /* ファイルが書き込みに対応しているかを返す */ 163 If fileAccess=GENERIC_WRITE/*FileAccess.Write*/ or fileAccess=GENERIC_READ or GENERIC_WRITE/*FileAccess.ReadWrite*/Then167 If This.fileAccess And GENERIC_WRITE Then 164 168 Return True 165 169 Else … … 172 176 Function IsAsync() As Boolean 173 177 /* ファイルが非同期操作に対応しているかを返す */ 174 If fileOptions=FILE_FLAG_OVERLAPPED/*FileOptions.Asynchronous*/ Then178 If This.fileOptions=FILE_FLAG_OVERLAPPED/*FileOptions.Asynchronous*/ Then 175 179 Return True 176 180 Else … … 180 184 181 185 Override Function Length() As Int64 182 /* ファイルのサイズの取得 */ 183 Dim s As LARGE_INTEGER 184 s.LowPart=GetFileSize(This.handle,VarPtr(s.HighPart) As *DWord) 185 memcpy(VarPtr(Length),VarPtr(s),SizeOf(LARGE_INTEGER)) 186 If This.CanSeek() Then 187 Dim length As LARGE_INTEGER 188 length.LowPart=GetFileSize(This.handle,VarPtr(length.HighPart) As *DWord) 189 Return MAKEQWORD(length.LowPart,length.HighPart) 190 End If 186 191 End Function 187 192 188 193 Function Name() As String 189 /* 多分文字列をコピーして返す方がいいのと思うが。。。*/ 190 Return filePath 194 Return New String(This.filePath) 191 195 End Function 192 196 193 197 Override Sub Position(value As Int64) 194 /* ファイルポインタの位置の設定 */ 195 Dim o As LARGE_INTEGER 196 memcpy(VarPtr(o),VarPtr(value),SizeOf(LARGE_INTEGER)) 197 If o.HighPart=0 And value<0 Then 198 o.LowPart=-Abs(o.LowPart) As Long 199 End If 200 Dim ret As LARGE_INTEGER 201 SetFilePointer(This.handle,o.LowPart,VarPtr(o.HighPart) As *DWord,FILE_CURRENT) 198 If This.CanSeek() Then 199 If This.IsAsync() Then 200 fileReadOverlapped.Offset=LODWORD(value) 201 fileReadOverlapped.OffsetHigh=HIDWORD(value) 202 fileWriteOverlapped.OffsetHigh=LODWORD(value) 203 fileWriteOverlapped.OffsetHigh=HIDWORD(value) 204 Else 205 Dim position As LARGE_INTEGER 206 position.LowPart=LODWORD(value) 207 position.HighPart=HIDWORD(value) 208 SetFilePointer(This.handle,position.LowPart,VarPtr(position.HighPart) As *DWord,FILE_BEGIN) 209 End If 210 End If 202 211 End Sub 203 212 Override Function Position() As Int64 204 /* ファイルポインタの位置の取得 */ 205 Dim o As LARGE_INTEGER 206 o.HighPart=0 207 o.LowPart=0 208 o.LowPart=SetFilePointer(This.handle,0,VarPtr(o.HighPart) As *DWord,FILE_CURRENT) 209 memcpy(VarPtr(Position),VarPtr(o),SizeOf(LARGE_INTEGER)) 210 End Function 211 212 Override Sub ReadTimeout(value As Long) 213 If This.CanSeek() Then 214 If This.IsAsync() Then 215 Return MAKEQWORD(fileReadOverlapped.Offset,fileReadOverlapped.OffsetHigh) 216 Else 217 Dim position As LARGE_INTEGER 218 ZeroMemory(VarPtr(position),SizeOf(LARGE_INTEGER)) 219 position.LowPart=SetFilePointer(This.handle,position.LowPart,VarPtr(position.HighPart) As *DWord,FILE_CURRENT) 220 Return MAKEQWORD(position.LowPart,position.HighPart) 221 End If 222 End If 223 End Function 224 225 /* Override Sub ReadTimeout(value As Long) 213 226 'TODO 214 227 End Sub 215 228 Override Function ReadTimeout() As Long 216 229 'TODO 217 End Function 230 End Function*/ 218 231 219 232 /* Safe~Handle系の実装は要相談!! */ … … 231 244 Public 232 245 Override Function BeginRead(ByRef buffer[] As Byte, offset As Long, count As Long, callback As AsyncCallback, state As Object) As System.IAsyncResult 233 'TODO 246 If This.IsAsync() Then 247 Else 248 Read(buffer,offset,count) 249 End If 234 250 End Function 235 251 236 252 Override Function BeginWrite(ByRef buffer[] As Byte, offset As Long, count As Long, callback As AsyncCallback, state As Object) As System.IAsyncResult 237 'TODO 253 If This.IsAsync() Then 254 Else 255 Write(buffer,offset,count) 256 End If 238 257 End Function 239 258 … … 276 295 277 296 Sub Lock(position As Int64, length As Int64) 278 Dim p As LARGE_INTEGER279 Dim l As LARGE_INTEGER280 memcpy(VarPtr(p),VarPtr(position),SizeOf(LARGE_INTEGER))281 memcpy(VarPtr(l),VarPtr(length),SizeOf(LARGE_INTEGER))282 LockFile(This.handle,p.LowPart,p.HighPart,l.LowPart,l.HighPart)283 297 End Sub 284 298 285 299 Override Function Read(ByRef buffer[] As Byte, offset As Long, count As Long) As Long 286 If offset Then 287 SetFilePointer(This.handle,offset,NULL,FILE_CURRENT) 288 End If 289 ReadFile(This.handle,buffer,count,VarPtr(Read) As *DWord,ByVal NULL) 300 If This.CanRead() Then 301 Dim ret As DWord 302 If This.IsAsync() Then 303 ReadFile(This.handle,VarPtr(buffer[offset]),count,VarPtr(ret),This.fileReadOverlapped) 304 While This.fileReadOverlapped.Internal=STATUS_PENDING 305 Wend 306 fileReadOverlapped.Offset+=LODWORD(ret) 307 fileReadOverlapped.OffsetHigh+=HIDWORD(ret) 308 fileWriteOverlapped.Offset+=LODWORD(ret) 309 fileWriteOverlapped.OffsetHigh+=HIDWORD(ret) 310 Return ret 311 Else 312 ReadFile(This.handle,VarPtr(buffer[offset]),count,VarPtr(ret),ByVal NULL) 313 Return ret 314 End If 315 End If 290 316 End Function 291 317 … … 302 328 303 329 Override Function Seek(offset As Int64, origin As SeekOrigin) As Long 304 Dim o As LARGE_INTEGER 305 memcpy(VarPtr(o),VarPtr(offset),SizeOf(LARGE_INTEGER)) 306 If o.HighPart=0 And offset<0 Then 307 o.LowPart=-o.LowPart 308 End If 309 Select Case origin 310 Case SeekOrigin.Begin 311 SetFilePointer(This.handle,o.LowPart,VarPtr(o.HighPart) As *DWord,FILE_BEGIN) 312 Case SeekOrigin.Current 313 SetFilePointer(This.handle,o.LowPart,VarPtr(o.HighPart) As *DWord,FILE_CURRENT) 314 Case SeekOrigin.End 315 SetFilePointer(This.handle,o.LowPart,VarPtr(o.HighPart) As *DWord,FILE_END) 316 End Select 330 If This.CanSeek() Then 331 If This.IsAsync() Then 332 Select Case origin 333 Case SeekOrigin.Begin 334 fileReadOverlapped.Offset=LODWORD(offset) 335 fileReadOverlapped.OffsetHigh=HIDWORD(offset) 336 fileWriteOverlapped.OffsetHigh=LODWORD(offset) 337 fileWriteOverlapped.OffsetHigh=HIDWORD(offset) 338 Case SeekOrigin.Current 339 fileReadOverlapped.Offset+=LODWORD(offset) 340 fileReadOverlapped.OffsetHigh+=HIDWORD(offset) 341 fileWriteOverlapped.Offset+=LODWORD(offset) 342 fileWriteOverlapped.OffsetHigh+=HIDWORD(offset) 343 Case SeekOrigin.End 344 fileReadOverlapped.Offset=LODWORD(This.Length()+offset) 345 fileReadOverlapped.OffsetHigh=HIDWORD(This.Length()+offset) 346 fileWriteOverlapped.Offset=LODWORD(This.Length()+offset) 347 fileWriteOverlapped.OffsetHigh=HIDWORD(This.Length()+offset) 348 End Select 349 Else 350 Dim seek As LARGE_INTEGER 351 seek.LowPart=LODWORD(offset) 352 seek.HighPart=HIDWORD(offset) 353 If seek.HighPart=0 And offset<0 Then seek.LowPart=-seek.LowPart 354 Select Case origin 355 Case SeekOrigin.Begin 356 Return SetFilePointer(This.handle,seek.LowPart,VarPtr(seek.HighPart) As *DWord,FILE_BEGIN) 357 Case SeekOrigin.Current 358 Return SetFilePointer(This.handle,seek.LowPart,VarPtr(seek.HighPart) As *DWord,FILE_CURRENT) 359 Case SeekOrigin.End 360 Return SetFilePointer(This.handle,seek.LowPart,VarPtr(seek.HighPart) As *DWord,FILE_CURRENT) 361 End Select 362 End If 363 End If 317 364 End Function 318 365 … … 322 369 323 370 Override Sub SetLength(value As Int64) 324 Dim o As LARGE_INTEGER 325 memcpy(VarPtr(o),VarPtr(value),SizeOf(LARGE_INTEGER)) 326 If o.HighPart=0 And value<0 Then 327 o.LowPart=-o.LowPart 328 End If 329 If Length()=>value Then 330 SetFilePointer(This.handle,o.LowPart,VarPtr(o.HighPart) As *DWord,FILE_BEGIN) 331 SetEndOfFile(This.handle) 371 If This.CanWrite() and This.CanSeek() Then 372 If This.IsAsync() Then 373 Else 374 Dim current = This.Position() 375 This.Position(value) 376 SetEndOfFile(This.handle) 377 End If 332 378 End If 333 379 End Sub … … 336 382 337 383 Override Function ToString() As String 338 Return Name()384 Return This.Name() 339 385 End Function 340 386 341 387 Sub Unlock(position As Int64, length As Int64) 342 Dim p As LARGE_INTEGER343 Dim l As LARGE_INTEGER344 memcpy(VarPtr(p),VarPtr(position),SizeOf(LARGE_INTEGER))345 memcpy(VarPtr(l),VarPtr(length),SizeOf(LARGE_INTEGER))346 UnlockFile(This.handle,p.LowPart,p.HighPart,l.LowPart,l.HighPart)347 388 End Sub 348 389 349 390 Override Sub Write(ByRef buffer[] As Byte, offset As Long, count As Long) 350 If offset Then 351 SetFilePointer(This.handle,offset,NULL,FILE_CURRENT) 352 End If 353 Dim ret As DWord 354 WriteFile(This.handle,buffer,count,VarPtr(ret),ByVal NULL) 391 If This.CanWrite() Then 392 Dim ret As DWord 393 If This.IsAsync() Then 394 WriteFile(This.handle,VarPtr(buffer[offset]),count,VarPtr(ret),This.fileWriteOverlapped) 395 While This.fileReadOverlapped.Internal=STATUS_PENDING 396 Wend 397 This.fileReadOverlapped.Offset+=LODWORD(ret) 398 This.fileReadOverlapped.OffsetHigh+=HIDWORD(ret) 399 This.fileWriteOverlapped.Offset+=LODWORD(ret) 400 This.fileWriteOverlapped.OffsetHigh+=HIDWORD(ret) 401 Else 402 WriteFile(This.handle,VarPtr(buffer[offset]),count,VarPtr(ret),ByVal NULL) 403 End If 404 End If 355 405 End Sub 356 406 … … 360 410 361 411 Protected 362 /*非同期ファイル操作に必須だがどうやって実装しようか検討中*/363 412 Override Function CreateWaitHandle() As System.Threading.WaitHandle 364 ' Return Nothing 413 '調査した限りでは、System.Threading.EventWaitHandleクラスをNewする模様。 414 '現状ではSystem.Threading.WaitHandleクラスをNewしてからHandleにて設定 415 Dim wh As System.Threading.WaitHandle 416 wh.Handle=CreateEvent(NULL,TRUE,FALSE,NULL) 417 Return wh 365 418 End Function 366 419 … … 368 421 Finalize 369 422 MemberwiseClone*/ 423 Private 370 424 End Class 371 425
Note:
See TracChangeset
for help on using the changeset viewer.