Index: trunk/Include/Classes/System/IO/FileStream.ab
===================================================================
--- trunk/Include/Classes/System/IO/FileStream.ab	(revision 300)
+++ trunk/Include/Classes/System/IO/FileStream.ab	(revision 336)
@@ -28,7 +28,6 @@
 	fileShare As DWord
 	fileOptions As DWord
-
-	/* 現在は未使用 */
-	/*Overlapped As OVERLAPPED*/
+	fileReadOverlapped As OVERLAPPED
+	fileWriteOverlapped As OVERLAPPED
 
 Public
@@ -100,18 +99,19 @@
 		'Throw System.IO.FileNotFoundException 
 			This.handle=0
+			Beep(220,500)
 			Exit Sub
 		End If
 
-		filePath = path
-		fileMode = mo
-		fileAccess = ac
-		fileShare = sh
-		fileOptions = op
+		This.filePath = path
+		This.fileMode = mo
+		This.fileAccess = ac
+		This.fileShare = sh
+		This.fileOptions = op
 	End Sub
 	Sub FileStream(path As String, mode As FileMode, access As FileAccess, share As FileShare)
-		FileStream(path,mode,access,share,FileOptions.None)
+		This.FileStream(path,mode,access,share,FileOptions.None)
 	End Sub
 	Sub FileStream(path As String, mode As FileMode, access As FileAccess)
-		FileStream(path,mode,access,FileShare.None,FileOptions.None)
+		This.FileStream(path,mode,access,FileShare.None,FileOptions.None)
 	End Sub
 	Sub FileStream(path As String, mode As FileMode)
@@ -131,5 +131,5 @@
 				access=FileAccess.Write
 		End Select
-		FileStream(path,mode,access,FileShare.None,FileOptions.None)
+		This.FileStream(path,mode,access,FileShare.None,FileOptions.None)
 	End Sub
 
@@ -142,5 +142,5 @@
 	Override Function CanRead() As Boolean
 		/* ファイルが読み込みに対応しているかを返す */
-		If fileAccess=GENERIC_READ/*FileAccess.Read*/ or fileAccess=GENERIC_READ or GENERIC_WRITE/*FileAccess.ReadWrite*/ Then
+		If This.fileAccess And GENERIC_READ Then
 			Return True
 		Else
@@ -151,15 +151,19 @@
 	Override Function CanSeek() As Boolean
 		/* ファイルがシークに対応しているかを返す */
-		Return True
-	End Function
-
-	Override Function CanTimeout() As Boolean
-		/* ファイルがタイムアウトに対応しているかを返す */
-		Return False /*今のところ対応していないのでFalse*/
-	End Function
+		If GetFileType(This.handle)=FILE_TYPE_DISK Then
+			Return True
+		Else
+			Return False
+		End If
+	End Function
+
+'	Override Function CanTimeout() As Boolean
+'		/* ファイルがタイムアウトに対応しているかを返す */
+'		Return False /*今のところ対応していないのでFalse*/
+'	End Function*/
 
 	Override Function CanWrite() As Boolean
 		/* ファイルが書き込みに対応しているかを返す */
-		If fileAccess=GENERIC_WRITE/*FileAccess.Write*/ or fileAccess=GENERIC_READ or GENERIC_WRITE/*FileAccess.ReadWrite*/ Then
+		If This.fileAccess And GENERIC_WRITE Then
 			Return True
 		Else
@@ -172,5 +176,5 @@
 	Function IsAsync() As Boolean
 		/* ファイルが非同期操作に対応しているかを返す */
-		If fileOptions=FILE_FLAG_OVERLAPPED/*FileOptions.Asynchronous*/ Then
+		If This.fileOptions=FILE_FLAG_OVERLAPPED/*FileOptions.Asynchronous*/ Then
 			Return True
 		Else
@@ -180,40 +184,49 @@
 
 	Override Function Length() As Int64
-		/* ファイルのサイズの取得 */
-		Dim s As LARGE_INTEGER
-		s.LowPart=GetFileSize(This.handle,VarPtr(s.HighPart) As *DWord)
-		memcpy(VarPtr(Length),VarPtr(s),SizeOf(LARGE_INTEGER))
+		If This.CanSeek() Then
+			Dim length As LARGE_INTEGER
+			length.LowPart=GetFileSize(This.handle,VarPtr(length.HighPart) As *DWord)
+			Return MAKEQWORD(length.LowPart,length.HighPart)
+		End If
 	End Function
 
 	Function Name() As String
-		/* 多分文字列をコピーして返す方がいいのと思うが。。。*/
-		Return filePath
+		Return New String(This.filePath)
 	End Function
 	
 	Override Sub Position(value As Int64)
-		/* ファイルポインタの位置の設定 */
-		Dim o As LARGE_INTEGER
-		memcpy(VarPtr(o),VarPtr(value),SizeOf(LARGE_INTEGER))
-		If o.HighPart=0 And value<0 Then
-			o.LowPart=-Abs(o.LowPart) As Long
-		End If
-		Dim ret As LARGE_INTEGER
-		SetFilePointer(This.handle,o.LowPart,VarPtr(o.HighPart) As *DWord,FILE_CURRENT)
+		If This.CanSeek() Then
+			If This.IsAsync() Then
+				fileReadOverlapped.Offset=LODWORD(value)
+				fileReadOverlapped.OffsetHigh=HIDWORD(value)
+				fileWriteOverlapped.OffsetHigh=LODWORD(value)
+				fileWriteOverlapped.OffsetHigh=HIDWORD(value)
+			Else
+				Dim position As LARGE_INTEGER
+				position.LowPart=LODWORD(value)
+				position.HighPart=HIDWORD(value)
+				SetFilePointer(This.handle,position.LowPart,VarPtr(position.HighPart) As *DWord,FILE_BEGIN)
+			End If
+		End If
 	End Sub
 	Override Function Position() As Int64
-		/* ファイルポインタの位置の取得 */
-		Dim o As LARGE_INTEGER
-		o.HighPart=0
-		o.LowPart=0
-		o.LowPart=SetFilePointer(This.handle,0,VarPtr(o.HighPart) As *DWord,FILE_CURRENT)
-		memcpy(VarPtr(Position),VarPtr(o),SizeOf(LARGE_INTEGER))
-	End Function
-
-	Override Sub ReadTimeout(value As Long)
+		If This.CanSeek() Then
+			If This.IsAsync() Then
+				Return MAKEQWORD(fileReadOverlapped.Offset,fileReadOverlapped.OffsetHigh)
+			Else
+				Dim position As LARGE_INTEGER
+				ZeroMemory(VarPtr(position),SizeOf(LARGE_INTEGER))
+				position.LowPart=SetFilePointer(This.handle,position.LowPart,VarPtr(position.HighPart) As *DWord,FILE_CURRENT)
+				Return MAKEQWORD(position.LowPart,position.HighPart)
+			End If
+		End If
+	End Function
+
+/*	Override Sub ReadTimeout(value As Long)
 		'TODO
 	End Sub
 	Override Function ReadTimeout() As Long
 		'TODO
-	End Function
+	End Function*/
 
 	/* Safe～Handle系の実装は要相談！！ */
@@ -231,9 +244,15 @@
 Public
 	Override Function BeginRead(ByRef buffer[] As Byte, offset As Long, count As Long, callback As AsyncCallback, state As Object) As System.IAsyncResult
-		'TODO
+		If This.IsAsync() Then
+		Else
+			Read(buffer,offset,count)
+		End If
 	End Function
 
 	Override Function BeginWrite(ByRef buffer[] As Byte, offset As Long, count As Long, callback As AsyncCallback, state As Object) As System.IAsyncResult
-		'TODO
+		If This.IsAsync() Then
+		Else
+			Write(buffer,offset,count)
+		End If
 	End Function
 
@@ -276,16 +295,23 @@
 
 	Sub Lock(position As Int64, length As Int64)
-		Dim p As LARGE_INTEGER
-		Dim l As LARGE_INTEGER
-		memcpy(VarPtr(p),VarPtr(position),SizeOf(LARGE_INTEGER))
-		memcpy(VarPtr(l),VarPtr(length),SizeOf(LARGE_INTEGER))
-		LockFile(This.handle,p.LowPart,p.HighPart,l.LowPart,l.HighPart)
 	End Sub
 
 	Override Function Read(ByRef buffer[] As Byte, offset As Long, count As Long) As Long
-		If offset Then
-			SetFilePointer(This.handle,offset,NULL,FILE_CURRENT)
-		End If
-		ReadFile(This.handle,buffer,count,VarPtr(Read) As *DWord,ByVal NULL)
+		If This.CanRead() Then
+			Dim ret As DWord
+			If This.IsAsync() Then
+				ReadFile(This.handle,VarPtr(buffer[offset]),count,VarPtr(ret),This.fileReadOverlapped)
+				While This.fileReadOverlapped.Internal=STATUS_PENDING
+				Wend
+				fileReadOverlapped.Offset+=LODWORD(ret)
+				fileReadOverlapped.OffsetHigh+=HIDWORD(ret)
+				fileWriteOverlapped.Offset+=LODWORD(ret)
+				fileWriteOverlapped.OffsetHigh+=HIDWORD(ret)
+				Return ret
+			Else
+				ReadFile(This.handle,VarPtr(buffer[offset]),count,VarPtr(ret),ByVal NULL)
+				Return ret
+			End If
+		End If
 	End Function
 
@@ -302,17 +328,38 @@
 
 	Override Function Seek(offset As Int64, origin As SeekOrigin) As Long
-		Dim o As LARGE_INTEGER
-		memcpy(VarPtr(o),VarPtr(offset),SizeOf(LARGE_INTEGER))
-		If o.HighPart=0 And offset<0 Then
-			o.LowPart=-o.LowPart
-		End If
-		Select Case origin
-			Case SeekOrigin.Begin
-				SetFilePointer(This.handle,o.LowPart,VarPtr(o.HighPart) As *DWord,FILE_BEGIN)
-			Case SeekOrigin.Current
-				SetFilePointer(This.handle,o.LowPart,VarPtr(o.HighPart) As *DWord,FILE_CURRENT)
-			Case SeekOrigin.End
-				SetFilePointer(This.handle,o.LowPart,VarPtr(o.HighPart) As *DWord,FILE_END)
-		End Select
+		If This.CanSeek() Then
+			If This.IsAsync() Then
+				Select Case origin
+					Case SeekOrigin.Begin
+						fileReadOverlapped.Offset=LODWORD(offset)
+						fileReadOverlapped.OffsetHigh=HIDWORD(offset)
+						fileWriteOverlapped.OffsetHigh=LODWORD(offset)
+						fileWriteOverlapped.OffsetHigh=HIDWORD(offset)
+					Case SeekOrigin.Current
+						fileReadOverlapped.Offset+=LODWORD(offset)
+						fileReadOverlapped.OffsetHigh+=HIDWORD(offset)
+						fileWriteOverlapped.Offset+=LODWORD(offset)
+						fileWriteOverlapped.OffsetHigh+=HIDWORD(offset)
+					Case SeekOrigin.End
+						fileReadOverlapped.Offset=LODWORD(This.Length()+offset)
+						fileReadOverlapped.OffsetHigh=HIDWORD(This.Length()+offset)
+						fileWriteOverlapped.Offset=LODWORD(This.Length()+offset)
+						fileWriteOverlapped.OffsetHigh=HIDWORD(This.Length()+offset)
+				End Select
+			Else
+				Dim seek As LARGE_INTEGER
+				seek.LowPart=LODWORD(offset)
+				seek.HighPart=HIDWORD(offset)
+				If seek.HighPart=0 And offset<0 Then seek.LowPart=-seek.LowPart
+				Select Case origin
+					Case SeekOrigin.Begin
+						Return SetFilePointer(This.handle,seek.LowPart,VarPtr(seek.HighPart) As *DWord,FILE_BEGIN)
+					Case SeekOrigin.Current
+						Return SetFilePointer(This.handle,seek.LowPart,VarPtr(seek.HighPart) As *DWord,FILE_CURRENT)
+					Case SeekOrigin.End
+						Return SetFilePointer(This.handle,seek.LowPart,VarPtr(seek.HighPart) As *DWord,FILE_CURRENT)
+				End Select
+			End If
+		End If
 	End Function
 
@@ -322,12 +369,11 @@
 
 	Override Sub SetLength(value As Int64)
-		Dim o As LARGE_INTEGER
-		memcpy(VarPtr(o),VarPtr(value),SizeOf(LARGE_INTEGER))
-		If o.HighPart=0 And value<0 Then
-			o.LowPart=-o.LowPart
-		End If
-		If Length()=>value Then
-			SetFilePointer(This.handle,o.LowPart,VarPtr(o.HighPart) As *DWord,FILE_BEGIN)
-			SetEndOfFile(This.handle)
+		If This.CanWrite() and This.CanSeek() Then
+			If This.IsAsync() Then
+			Else
+				Dim current = This.Position()
+				This.Position(value)
+				SetEndOfFile(This.handle)
+			End If
 		End If
 	End Sub
@@ -336,21 +382,25 @@
 
 	Override Function ToString() As String
-		Return Name()
+		Return This.Name()
 	End Function
 
 	Sub Unlock(position As Int64, length As Int64)
-		Dim p As LARGE_INTEGER
-		Dim l As LARGE_INTEGER
-		memcpy(VarPtr(p),VarPtr(position),SizeOf(LARGE_INTEGER))
-		memcpy(VarPtr(l),VarPtr(length),SizeOf(LARGE_INTEGER))
-		UnlockFile(This.handle,p.LowPart,p.HighPart,l.LowPart,l.HighPart)
 	End Sub
 
 	Override Sub Write(ByRef buffer[] As Byte, offset As Long, count As Long)
-		If offset Then
-			SetFilePointer(This.handle,offset,NULL,FILE_CURRENT)
-		End If
-		Dim ret As DWord
-		WriteFile(This.handle,buffer,count,VarPtr(ret),ByVal NULL)
+		If This.CanWrite() Then
+			Dim ret As DWord
+			If This.IsAsync() Then
+				WriteFile(This.handle,VarPtr(buffer[offset]),count,VarPtr(ret),This.fileWriteOverlapped)
+				While This.fileReadOverlapped.Internal=STATUS_PENDING
+				Wend
+				This.fileReadOverlapped.Offset+=LODWORD(ret)
+				This.fileReadOverlapped.OffsetHigh+=HIDWORD(ret)
+				This.fileWriteOverlapped.Offset+=LODWORD(ret)
+				This.fileWriteOverlapped.OffsetHigh+=HIDWORD(ret)
+			Else
+				WriteFile(This.handle,VarPtr(buffer[offset]),count,VarPtr(ret),ByVal NULL)
+			End If
+		End If
 	End Sub
 
@@ -360,7 +410,10 @@
 
 Protected
-	/*非同期ファイル操作に必須だがどうやって実装しようか検討中*/
 	Override Function CreateWaitHandle() As System.Threading.WaitHandle
-'		Return Nothing
+		'調査した限りでは、System.Threading.EventWaitHandleクラスをNewする模様。
+		'現状ではSystem.Threading.WaitHandleクラスをNewしてからHandleにて設定
+		Dim wh As System.Threading.WaitHandle
+		wh.Handle=CreateEvent(NULL,TRUE,FALSE,NULL)
+		Return wh
 	End Function
 
@@ -368,4 +421,5 @@
 	Finalize
 	MemberwiseClone*/
+Private
 End Class
 
