1 | Namespace System
|
---|
2 | Namespace IO
|
---|
3 |
|
---|
4 |
|
---|
5 | /* ほんとはmiscに入れるかかファイルを分けたほうがいいかもしれないが一先ず実装 */
|
---|
6 | Enum FileOptions
|
---|
7 | Asynchronous
|
---|
8 | DeleteOnClose
|
---|
9 | Encrypted
|
---|
10 | None
|
---|
11 | RandomAccess
|
---|
12 | SequentialScan
|
---|
13 | WriteThrough
|
---|
14 | End Enum
|
---|
15 |
|
---|
16 | Class FileStream
|
---|
17 | Inherits Stream
|
---|
18 |
|
---|
19 | handle As HANDLE
|
---|
20 |
|
---|
21 | /*
|
---|
22 | ファイルハンドルからこれらを取得できれば、これらは入らないが
|
---|
23 | 今のところは不明なので自前で実装するしかない
|
---|
24 | */
|
---|
25 | filePath As String
|
---|
26 | fileMode As DWord
|
---|
27 | fileAccess As DWord
|
---|
28 | fileShare As DWord
|
---|
29 | fileOptions As DWord
|
---|
30 |
|
---|
31 | /* 現在は未使用 */
|
---|
32 | /*Overlapped As OVERLAPPED*/
|
---|
33 |
|
---|
34 | Public
|
---|
35 | /* コンストラクタ.NETと同じように実装は難しい、一先ず動くものを実装したが変更が必要だと思う */
|
---|
36 | Sub FileStream(path As String, mode As FileMode, access As FileAccess, share As FileShare, options As FileOptions)
|
---|
37 | Dim ac As DWord
|
---|
38 | Dim mo As DWord
|
---|
39 | Dim sh As DWord
|
---|
40 | Dim op As DWord
|
---|
41 |
|
---|
42 | Select Case access
|
---|
43 | Case FileAccess.Read
|
---|
44 | ac=GENERIC_READ
|
---|
45 | Case FileAccess.ReadWrite
|
---|
46 | ac=GENERIC_READ or GENERIC_WRITE
|
---|
47 | Case FileAccess.Write
|
---|
48 | ac=GENERIC_WRITE
|
---|
49 | End Select
|
---|
50 |
|
---|
51 | Select Case share
|
---|
52 | Case FileShare.DeleteFile
|
---|
53 | sh=FILE_SHARE_DELETE
|
---|
54 | Case FileShare.None
|
---|
55 | sh=0
|
---|
56 | Case FileShare.Read
|
---|
57 | sh=FILE_SHARE_READ
|
---|
58 | Case FileShare.ReadWrite
|
---|
59 | sh=FILE_SHARE_READ or FILE_SHARE_WRITE
|
---|
60 | Case FileShare.Write
|
---|
61 | sh=FILE_SHARE_WRITE
|
---|
62 | End Select
|
---|
63 |
|
---|
64 | Select Case mode
|
---|
65 | Case FileMode.Append
|
---|
66 | mo=OPEN_ALWAYS
|
---|
67 | Case FileMode.Create
|
---|
68 | mo=CREATE_ALWAYS
|
---|
69 | Case FileMode.CreateNew
|
---|
70 | mo=CREATE_NEW
|
---|
71 | Case FileMode.Open
|
---|
72 | mo=OPEN_EXISTING
|
---|
73 | Case FileMode.OpenOrCreate
|
---|
74 | mo=OPEN_ALWAYS
|
---|
75 | Case FileMode.Truncate
|
---|
76 | mo=TRUNCATE_EXISTING
|
---|
77 | End Select
|
---|
78 |
|
---|
79 | Select Case options
|
---|
80 | Case FileOptions.Asynchronous
|
---|
81 | op=FILE_FLAG_OVERLAPPED
|
---|
82 | Case FileOptions.DeleteOnClose
|
---|
83 | op=FILE_FLAG_DELETE_ON_CLOSE
|
---|
84 | Case FileOptions.Encrypted
|
---|
85 | Case FileOptions.None
|
---|
86 | op=0
|
---|
87 | Case FileOptions.RandomAccess
|
---|
88 | op=FILE_FLAG_RANDOM_ACCESS
|
---|
89 | Case FileOptions.SequentialScan
|
---|
90 | op=FILE_FLAG_SEQUENTIAL_SCAN
|
---|
91 | Case FileOptions.WriteThrough
|
---|
92 | op=FILE_FLAG_WRITE_THROUGH
|
---|
93 | End Select
|
---|
94 |
|
---|
95 | This.handle=CreateFile(path As PSTR,ac,sh,ByVal NULL,mo,op,0)
|
---|
96 | If This.handle=INVALID_HANDLE_VALUE Then
|
---|
97 | 'エラー処理
|
---|
98 | 'Throw ArgumentException
|
---|
99 | 'Throw IOException
|
---|
100 | 'Throw System.IO.FileNotFoundException
|
---|
101 | This.handle=0
|
---|
102 | Exit Sub
|
---|
103 | End If
|
---|
104 |
|
---|
105 | filePath = path
|
---|
106 | fileMode = mo
|
---|
107 | fileAccess = ac
|
---|
108 | fileShare = sh
|
---|
109 | fileOptions = op
|
---|
110 | End Sub
|
---|
111 | Sub FileStream(path As String, mode As FileMode, access As FileAccess, share As FileShare)
|
---|
112 | FileStream(path,mode,access,share,FileOptions.None)
|
---|
113 | End Sub
|
---|
114 | Sub FileStream(path As String, mode As FileMode, access As FileAccess)
|
---|
115 | FileStream(path,mode,access,FileShare.None,FileOptions.None)
|
---|
116 | End Sub
|
---|
117 | Sub FileStream(path As String, mode As FileMode)
|
---|
118 | Dim access As FileAccess
|
---|
119 | Select Case mode
|
---|
120 | Case FileMode.Append
|
---|
121 | access=FileAccess.Write
|
---|
122 | Case FileMode.Create
|
---|
123 | access=FileAccess.ReadWrite
|
---|
124 | Case FileMode.CreateNew
|
---|
125 | access=FileAccess.ReadWrite
|
---|
126 | Case FileMode.Open
|
---|
127 | access=FileAccess.ReadWrite
|
---|
128 | Case FileMode.OpenOrCreate
|
---|
129 | access=FileAccess.ReadWrite
|
---|
130 | Case FileMode.Truncate
|
---|
131 | access=FileAccess.Write
|
---|
132 | End Select
|
---|
133 | FileStream(path,mode,access,FileShare.None,FileOptions.None)
|
---|
134 | End Sub
|
---|
135 |
|
---|
136 | Sub ~FileStream()
|
---|
137 | This.Flush()
|
---|
138 | This.Close()
|
---|
139 | End Sub
|
---|
140 |
|
---|
141 | Public
|
---|
142 | Override Function CanRead() As Boolean
|
---|
143 | /* ファイルが読み込みに対応しているかを返す */
|
---|
144 | If fileAccess=GENERIC_READ/*FileAccess.Read*/ or fileAccess=GENERIC_READ or GENERIC_WRITE/*FileAccess.ReadWrite*/ Then
|
---|
145 | Return True
|
---|
146 | Else
|
---|
147 | Return False
|
---|
148 | End If
|
---|
149 | End Function
|
---|
150 |
|
---|
151 | Override Function CanSeek() As Boolean
|
---|
152 | /* ファイルがシークに対応しているかを返す */
|
---|
153 | Return True
|
---|
154 | End Function
|
---|
155 |
|
---|
156 | Override Function CanTimeout() As Boolean
|
---|
157 | /* ファイルがタイムアウトに対応しているかを返す */
|
---|
158 | Return False /*今のところ対応していないのでFalse*/
|
---|
159 | End Function
|
---|
160 |
|
---|
161 | Override Function CanWrite() As Boolean
|
---|
162 | /* ファイルが書き込みに対応しているかを返す */
|
---|
163 | If fileAccess=GENERIC_WRITE/*FileAccess.Write*/ or fileAccess=GENERIC_READ or GENERIC_WRITE/*FileAccess.ReadWrite*/ Then
|
---|
164 | Return True
|
---|
165 | Else
|
---|
166 | Return False
|
---|
167 | End If
|
---|
168 | End Function
|
---|
169 |
|
---|
170 | /*Handle*/
|
---|
171 |
|
---|
172 | Function IsAsync() As Boolean
|
---|
173 | /* ファイルが非同期操作に対応しているかを返す */
|
---|
174 | If fileOptions=FILE_FLAG_OVERLAPPED/*FileOptions.Asynchronous*/ Then
|
---|
175 | Return True
|
---|
176 | Else
|
---|
177 | Return False
|
---|
178 | End If
|
---|
179 | End Function
|
---|
180 |
|
---|
181 | 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 | End Function
|
---|
187 |
|
---|
188 | Function Name() As String
|
---|
189 | /* 多分文字列をコピーして返す方がいいのと思うが。。。*/
|
---|
190 | Return filePath
|
---|
191 | End Function
|
---|
192 |
|
---|
193 | 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)
|
---|
202 | End Sub
|
---|
203 | 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 | 'TODO
|
---|
214 | End Sub
|
---|
215 | Override Function ReadTimeout() As Long
|
---|
216 | 'TODO
|
---|
217 | End Function
|
---|
218 |
|
---|
219 | /* Safe~Handle系の実装は要相談!! */
|
---|
220 | /* Function SafeFileHandle() As SafeFileHandle
|
---|
221 | End Function*/
|
---|
222 |
|
---|
223 | Override Sub WriteTimeout(value As Long)
|
---|
224 | 'TODO
|
---|
225 | End Sub
|
---|
226 | Override Function WriteTimeout() As Long
|
---|
227 | 'TODO
|
---|
228 | End Function
|
---|
229 |
|
---|
230 |
|
---|
231 | Public
|
---|
232 | Override Function BeginRead(ByRef buffer[] As Byte, offset As Long, count As Long, callback As AsyncCallback, state As Object) As System.IAsyncResult
|
---|
233 | 'TODO
|
---|
234 | End Function
|
---|
235 |
|
---|
236 | Override Function BeginWrite(ByRef buffer[] As Byte, offset As Long, count As Long, callback As AsyncCallback, state As Object) As System.IAsyncResult
|
---|
237 | 'TODO
|
---|
238 | End Function
|
---|
239 |
|
---|
240 | Override Sub Close()
|
---|
241 | CloseHandle(This.handle)
|
---|
242 | End Sub
|
---|
243 |
|
---|
244 | /* CreateObjRef*/
|
---|
245 | /* Dispose*/
|
---|
246 |
|
---|
247 | Override Sub EndRead(ByRef asyncResult As System.IAsyncResult)
|
---|
248 | 'TODO
|
---|
249 | End Sub
|
---|
250 |
|
---|
251 | Override Sub EndWrite(ByRef asyncResult As System.IAsyncResult)
|
---|
252 | 'TODO
|
---|
253 | End Sub
|
---|
254 |
|
---|
255 | /* Equals*/
|
---|
256 |
|
---|
257 | Override Sub Flush()
|
---|
258 | FlushFileBuffers(This.handle)
|
---|
259 | End Sub
|
---|
260 |
|
---|
261 | /* Function GetAccessControl() As FileSecurity
|
---|
262 | FileSecurityの実装がまだできてない。
|
---|
263 | End Function*/
|
---|
264 |
|
---|
265 | Override Function GetHashCode() As Long
|
---|
266 | Return ObjPtr(This) As Long
|
---|
267 | End Function
|
---|
268 |
|
---|
269 | /* GetLifetimeService*/
|
---|
270 |
|
---|
271 | /* Override Function GetType() As TypeInfo
|
---|
272 | Return Super.GetType()
|
---|
273 | End Function*/
|
---|
274 |
|
---|
275 | /* InitializeLifetimeService*/
|
---|
276 |
|
---|
277 | Sub Lock(position As Int64, length As Int64)
|
---|
278 | Dim p As LARGE_INTEGER
|
---|
279 | Dim l As LARGE_INTEGER
|
---|
280 | 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 | End Sub
|
---|
284 |
|
---|
285 | 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)
|
---|
290 | End Function
|
---|
291 |
|
---|
292 | Override Function ReadByte() As Long
|
---|
293 | Dim b As Byte
|
---|
294 | Dim ret = Read(b, 0, 1)
|
---|
295 | If ret <> 0 Then
|
---|
296 | Return b As Long
|
---|
297 | Else
|
---|
298 | Return -1
|
---|
299 | End If
|
---|
300 | End Function
|
---|
301 | /* ReferenceEquals*/
|
---|
302 |
|
---|
303 | 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
|
---|
317 | End Function
|
---|
318 |
|
---|
319 | /* Sub SetAccessControl(fileSecurity As FileSecurity)
|
---|
320 | FileSecurityの実装がまだできてない。
|
---|
321 | End Sub*/
|
---|
322 |
|
---|
323 | 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)
|
---|
332 | End If
|
---|
333 | End Sub
|
---|
334 |
|
---|
335 | /* Synchronized*/
|
---|
336 |
|
---|
337 | Override Function ToString() As String
|
---|
338 | Return Name()
|
---|
339 | End Function
|
---|
340 |
|
---|
341 | Sub Unlock(position As Int64, length As Int64)
|
---|
342 | Dim p As LARGE_INTEGER
|
---|
343 | Dim l As LARGE_INTEGER
|
---|
344 | 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 | End Sub
|
---|
348 |
|
---|
349 | 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)
|
---|
355 | End Sub
|
---|
356 |
|
---|
357 | Override Sub WriteByte(b As Byte)
|
---|
358 | Write(b, 0, 1)
|
---|
359 | End Sub
|
---|
360 |
|
---|
361 | Protected
|
---|
362 | /*非同期ファイル操作に必須だがどうやって実装しようか検討中*/
|
---|
363 | Override Function CreateWaitHandle() As System.Threading.WaitHandle
|
---|
364 | ' Return Nothing
|
---|
365 | End Function
|
---|
366 |
|
---|
367 | /* Dispose
|
---|
368 | Finalize
|
---|
369 | MemberwiseClone*/
|
---|
370 | End Class
|
---|
371 |
|
---|
372 |
|
---|
373 | End Namespace
|
---|
374 | End Namespace
|
---|