Index: trunk/Include/Classes/System/IO/MemoryStream.ab
===================================================================
--- trunk/Include/Classes/System/IO/MemoryStream.ab	(revision 471)
+++ trunk/Include/Classes/System/IO/MemoryStream.ab	(revision 472)
@@ -7,75 +7,119 @@
 
 Public
-	/*
-	0に初期化される拡張可能な容量を使用して初期化
+	/*!
+	@brief	MemoryStreamクラスの新しいインスタンスを初期化します。
+	@author	NoWest
+	@date	2008/3/12
 	*/
 	Sub MemoryStream()
 		This.writable = True
-		This.Resizable = True
-		This.visible = False
-		This.handle = HeapCreate(0,0,0)
-		This.p = HeapAlloc(This.handle,HEAP_ZERO_MEMORY,0)
-		This.StreamLength = 0
-		This.CurrentPosition = 0
-	End Sub
+		This.resizable = True
+		This.visible = False
+		This.create()
+		This.pointer = This.allocate(0)
+		This.streamLength = 0
+		This.currentPosition = 0
+	End Sub
+
+	/*!
+	@brief	MemoryStreamクラスの新しいインスタンスを初期容量を指定して初期化します。
+	@author	NoWest
+	@date	2008/3/12
+	*/
 	Sub MemoryStream(capacity As Long)
 		This.writable = True
-		This.Resizable = True
-		This.visible = False
-		This.handle = HeapCreate(0,0,0)
-		This.p = HeapAlloc(This.handle,HEAP_ZERO_MEMORY,capacity)
-		This.StreamLength = capacity
-		This.CurrentPosition = 0
-	End Sub
+		This.resizable = True
+		This.visible = False
+		This.create()
+		This.pointer = This.allocate(capacity)
+		This.streamLength = capacity
+		This.currentPosition = 0
+	End Sub
+
+	/*!
+	@brief	MemoryStreamクラスの新しいインスタンスを指定したバッファに基づいて初期化します。容量を変更することはできません。
+	@author	NoWest
+	@date	2008/3/12
+	*/
 	Sub MemoryStream(buffer As *Byte, length As Long)
 		This.writable = True
-		This.Resizable = False
-		This.visible = False
-		This.handle = HeapCreate(0,0,0)
-		This.p = HeapAlloc(This.handle,HEAP_ZERO_MEMORY,length)
-		MoveMemory(This.p,buffer,length)
-		This.StreamLength = length
-		This.CurrentPosition = 0
-	End Sub
+		This.resizable = False
+		This.visible = False
+		This.create()
+		This.pointer = This.allocate(length)
+		MoveMemory(This.pointer,buffer,length)
+		This.streamLength = length
+		This.currentPosition = 0
+	End Sub
+
+	/*!
+	@brief	MemoryStreamクラスの新しいインスタンスを指定したバッファに基づいて初期化します。容量を変更することはできません。また、書き込みをサポートするかどうかも指定できます。
+	@author	NoWest
+	@date	2008/3/12
+	*/
 	Sub MemoryStream(buffer As *Byte, length As Long, writable As Boolean)
 		This.writable = writable
-		This.Resizable = False
-		This.visible = False
-		This.handle = HeapCreate(0,0,0)
-		This.p = HeapAlloc(This.handle,HEAP_ZERO_MEMORY,length)
-		MoveMemory(This.p,buffer,length)
-		This.StreamLength = length
-		This.CurrentPosition = 0
-	End Sub
+		This.resizable = False
+		This.visible = False
+		This.create()
+		This.pointer = This.allocate(length)
+		MoveMemory(This.pointer,buffer,length)
+		This.streamLength = length
+		This.currentPosition = 0
+	End Sub
+
+	/*!
+	@brief	MemoryStreamクラスの新しいインスタンスを指定したバッファの指定された領域に基づいて初期化します。容量を変更することはできません。
+	@author	NoWest
+	@date	2008/3/12
+	*/
 	Sub MemoryStream(buffer As *Byte, index As Long, count As Long)
 		This.writable = True
-		This.Resizable = False
-		This.visible = False
-		This.handle = HeapCreate(0,0,0)
-		This.p = HeapAlloc(This.handle,HEAP_ZERO_MEMORY,count)
-		MoveMemory(This.p,VarPtr(buffer[index]),count)
-		This.StreamLength = count
-		This.CurrentPosition = 0
-	End Sub
+		This.resizable = False
+		This.visible = False
+		This.create()
+		This.pointer = This.allocate(count)
+		MoveMemory(This.pointer,VarPtr(buffer[index]),count)
+		This.streamLength = count
+		This.currentPosition = 0
+	End Sub
+
+	/*!
+	@brief	MemoryStreamクラスの新しいインスタンスを指定したバッファの指定された領域に基づいて初期化します。容量を変更することはできません。また、書き込みをサポートするかどうかを指定できます。
+	@author	NoWest
+	@date	2008/3/12
+	*/
 	Sub MemoryStream(buffer As *Byte, index As Long, count As Long, writable As Boolean)
 		This.writable = writable
-		This.Resizable = False
-		This.visible = False
-		This.handle = HeapCreate(0,0,0)
-		This.p = HeapAlloc(This.handle,HEAP_ZERO_MEMORY,count)
-		MoveMemory(This.p,VarPtr(buffer[index]),count)
-		This.StreamLength = count
-		This.CurrentPosition = 0
-	End Sub
+		This.resizable = False
+		This.visible = False
+		This.create()
+		This.pointer = This.allocate(count)
+		MoveMemory(This.pointer,VarPtr(buffer[index]),count)
+		This.streamLength = count
+		This.currentPosition = 0
+	End Sub
+
+	/*!
+	@brief	MemoryStreamクラスの新しいインスタンスを指定したバッファの指定された領域に基づいて初期化します。容量を変更することはできません。また、書き込みをサポートするかどうかを指定できます。GetBufferメソッドをサポートするかどうかを指定できます。
+	@author	NoWest
+	@date	2008/3/12
+	*/
 	Sub MemoryStream(buffer As *Byte, index As Long, count As Long, writable As Boolean, visible As Boolean)
 		This.writable = writable
-		This.Resizable = False
+		This.resizable = False
 		This.visible = visible
-		This.handle = HeapCreate(0,0,0)
-		This.p = HeapAlloc(This.handle,HEAP_ZERO_MEMORY,count)
-		MoveMemory(This.p,VarPtr(buffer[index]),count)
-		This.StreamLength = count
-		This.CurrentPosition = 0
-	End Sub
+		This.create()
+		This.pointer = This.allocate(count)
+		MoveMemory(This.pointer,VarPtr(buffer[index]),count)
+		This.streamLength = count
+		This.currentPosition = 0
+	End Sub
+
+	/*!
+	@brief	MemoryStreamクラスのデストラクタです。
+	@author	NoWest
+	@date	2008/3/12
+	*/
 	Sub ~MemoryStream()
 		This.Close()
@@ -83,6 +127,11 @@
 
 Public
+	/*!
+	@brief	ストリームが読み取りをサポートしているかどうかを示す値を取得します。
+	@author	NoWest
+	@date	2008/3/12
+	*/
 	Override Function CanRead() As Boolean
-		If This.p Then
+		If This.pointer Then
 			Return True
 		Else
@@ -91,6 +140,11 @@
 	End Function
 
+	/*!
+	@brief	ストリームがシークをサポートしているかどうかを示す値を取得します。
+	@author	NoWest
+	@date	2008/3/12
+	*/
 	Override Function CanSeek() As Boolean
-		If This.p Then
+		If This.pointer Then
 			Return True
 		Else
@@ -99,6 +153,11 @@
 	End Function
 
+	/*!
+	@brief	ストリームが書き込みをサポートしているかどうかを示す値を取得します。
+	@author	NoWest
+	@date	2008/3/12
+	*/
 	Override Function CanWrite() As Boolean
-		If This.p Then
+		If This.pointer Then
 			Return This.writable
 		Else
@@ -107,44 +166,56 @@
 	End Function
 
-	/*
-	メモリの容量であってストリームの長さではない
-	メモリはヒープからブロック単位で確保されるので
-	指定したストリーム長より大きくなることがある
-	また、配列として扱う場合の配列長となる
+	/*!
+	@brief	ストリームに割り当てるメモリ容量をバイト単位で設定します。
+	@author	NoWest
+	@date	2008/3/12
+	*/
+	Virtual Sub Capacity(value As Long)
+		If This.pointer = 0 Then Throw New ObjectDisposedException("MemoryStream: This stream has closed.")
+		If value < 0 Then Throw New ArgumentOutOfRangeException("MemoryStream.Capacity: A capacity is set to negative value.", New System.Int64(value),"Capacity")
+		If value < This.streamLength Then Throw New ArgumentOutOfRangeException("MemoryStream.Capacity: A capacity is less than the current length of the stream.",New System.Int64(value),"Capacity")
+		This.pointer = This.reallocate(This.pointer,value)
+	End Sub
+
+	/*!
+	@brief	ストリームに割り当てられたメモリ容量をバイト単位で取得します。
+	@author	NoWest
+	@date	2008/3/12
 	*/
 	Virtual Function Capacity() As Long
-		If This.p = 0 Then Debug'Throw New ObjectDisposedException
-		Return HeapSize(This.handle,0,This.p)
-	End Function
-
-	Virtual Sub Capacity(value As Long)
-		If This.p = 0 Then Debug'Throw New ObjectDisposedException
-		If value < 0 Then Debug'Throw New ArgumentOutOfRangeException
-		If value < This.StreamLength Then Debug'Throw New ArgumentOutOfRangeException
-		This.p = HeapReAlloc(This.handle,HEAP_ZERO_MEMORY,This.p,value)
-	End Sub
-
-	/*
-	こちらは容量とは別のストリーム長
-	実際のデータの長さはこちら
+		If This.pointer = 0 Then Throw New ObjectDisposedException("MemoryStream: This stream has closed.")
+		Return This.size(This.pointer)
+	End Function
+
+	/*!
+	@brief	ストリームの長さをバイト単位で取得します。Capacityプロパティの値より小さい場合があります。
+	@author	NoWest
+	@date	2008/3/12
 	*/
 	Override Function Length() As Int64
-		If This.p = 0 Then Debug'Throw New ObjectDisposedException
-		Return This.StreamLength
-	End Function
-
-	/*
-	ストリームの現在位置を取得または設定
+		If This.pointer = 0 Then Throw New ObjectDisposedException("MemoryStream: This stream has closed.")
+		Return This.streamLength
+	End Function
+
+	/*!
+	@brief	ストリームの現在位置を設定します。
+	@author	NoWest
+	@date	2008/3/12
 	*/
 	Override Sub Position(value As Int64)
-		If This.p = 0 Then Debug'Throw New ObjectDisposedException
-		If value < 0 Then Debug'Throw New ArgumentOutOfRangeException
-		If value > Int32.MaxValue() Then Debug'Throw New ArgumentOutOfRangeException
-		This.CurrentPosition = value As Long
-	End Sub
-
+		If This.pointer = 0 Then Throw New ObjectDisposedException("MemoryStream: This stream has closed.")
+		If value < 0 Then Throw New ArgumentOutOfRangeException("MemoryStream.Position: The position is set to a negative value.", New System.Int64(value), "Position")
+		If value > Int32.MaxValue() Then Throw New ArgumentOutOfRangeException("MemoryStream.Position: The position is a value greater than MaxValue.", New System.Int64(value), "Position")
+		This.currentPosition = value As Long
+	End Sub
+
+	/*!
+	@brief	ストリームの現在位置を取得します。
+	@author	NoWest
+	@date	2008/3/12
+	*/
 	Override Function Position() As Int64
-		If This.p = 0 Then Debug'Throw New ObjectDisposedException
-		Return This.CurrentPosition As Int64
+		If This.pointer = 0 Then Throw New ObjectDisposedException("MemoryStream: This stream has closed.")
+		Return This.currentPosition As Int64
 	End Function
 
@@ -157,19 +228,29 @@
 	End Function*/
 
+	/*!
+	@brief	ストリームから指定されたバイト数を読み取り、bufferに書き込みます。
+	@author	NoWest
+	@date	2008/3/12
+	*/
 	Override Function Read(buffer As *Byte, offset As Long, count As Long) As Long
-		If This.p = 0 Then Debug'Throw New ObjectDisposedException
-		If buffer = 0 Then Debug 'Throw New ArgumentNullException
-		If offset < 0 Then Debug'Throw New ArgumentOutOfRangeException
-		If count < 0 Then Debug'Throw New ArgumentOutOfRangeException
-		If (This.StreamLength - (This.CurrentPosition + count)) < 0 Then
-			count + = (This.StreamLength - (This.CurrentPosition + count))
+		If This.pointer = 0 Then Throw New ObjectDisposedException("MemoryStream: This stream has closed.")
+		If buffer = 0 Then Throw New ArgumentNullException("FileStream.Read: An argument is a null value.", "buffer")
+		If offset < 0 Then Throw New ArgumentOutOfRangeException("MemoryStream.Read: An argument is a negative value.", New System.Int64(offset), "offset")
+		If count < 0 Then Throw New ArgumentOutOfRangeException("MemoryStream.Read: An argument is a negative value.", New System.Int64(count), "count")
+		If (This.streamLength - (This.currentPosition + count)) < 0 Then
+			count + = (This.streamLength - (This.currentPosition + count))
 		ElseIf count < 1 Then
 			Return 0
 		End If
-		MoveMemory(VarPtr(buffer[offset]),VarPtr(This.p[This.CurrentPosition]),count)
-		This.CurrentPosition + = count
+		MoveMemory(VarPtr(buffer[offset]),VarPtr(This.pointer[This.currentPosition]),count)
+		This.currentPosition + = count
 		Return count
 	End Function
 
+	/*!
+	@brief	ストリームから指定された1バイト読み取ります。
+	@author	NoWest
+	@date	2008/3/12
+	*/
 	Override Function ReadByte() As Long
 		Dim b As Byte
@@ -182,85 +263,154 @@
 	End Function
 
+	/*!
+	@brief	ストリーム内の位置を指定した値に設定します。
+	@author	NoWest
+	@date	2008/3/12
+	*/
 	Override Function Seek(offset As Int64, origin As SeekOrigin) As Int64
-		If This.p = 0 Then Debug'Throw New ObjectDisposedException
-		If offset > Int32.MaxValue() Then Debug'Throw New ArgumentOutOfRangeException
+		If This.pointer = 0 Then Throw New ObjectDisposedException("MemoryStream: This stream has closed.")
+		If offset > Int32.MaxValue() Then Throw New ArgumentOutOfRangeException("MemoryStream.Seek: The offset is a value greater than MaxValue.", New System.Int64(offset), "offset")
 		Select Case origin
 			Case SeekOrigin.Begin
-				If offset < 0 Then Debug 'Throw New IOException
-				This.CurrentPosition = offset As Long
+				If offset < 0 Then Throw New IOException("MemoryStream.Seek: Seeking is attempted before the beginning of the stream.")
+				This.currentPosition = offset As Long
 			Case SeekOrigin.Current
 				Beep(440,10)
-				If (This.CurrentPosition + offset) < 0 Then Debug 'Throw New IOException
-				This.CurrentPosition += offset As Long
+				If (This.currentPosition + offset) < 0 Then Throw New IOException("MemoryStream.Seek: Seeking is attempted before the beginning of the stream.")
+				This.currentPosition += offset As Long
 			Case SeekOrigin.End
-				If (This.StreamLength + offset) < 0 Then Debug 'Throw New IOException
-				This.CurrentPosition = (This.StreamLength + offset) As Long
+				If (This.streamLength + offset) < 0 Then Throw New IOException("MemoryStream.Seek: Seeking is attempted before the beginning of the stream.")
+				This.currentPosition = (This.streamLength + offset) As Long
 			Case Else
-				Debug 'Throw New ArgumentException
+				Throw New ArgumentException("MemoryStream.Seek: An argument is an invalid SeekOrigin","origin")
 		End Select
-		Return This.CurrentPosition As Int64
-	End Function
-
+		Return This.currentPosition As Int64
+	End Function
+
+	/*!
+	@brief	ストリームの長さを設定します。メモリ容量を超えては設定できません。
+	@author	NoWest
+	@date	2008/3/12
+	*/
 	Override Sub SetLength(value As Int64)
-		If This.p = 0 Then Debug'Throw New ObjectDisposedException
-		If This.writable = False Then Debug 'Throw New NotSupportedException
-		If This.Resizable = False Then Debug 'Throw New NotSupportedException
-		If value < 0 Then Debug'Throw New ArgumentOutOfRangeException
-		If value > Int32.MaxValue() Then Debug'Throw New ArgumentOutOfRangeException
-		This.p = HeapReAlloc(This.handle,HEAP_ZERO_MEMORY,This.p,value As Long)
-		This.StreamLength = value As Long
-	End Sub
-
+		If This.pointer = 0 Then Throw New ObjectDisposedException("MemoryStream: This stream has closed.")
+		If This.writable = False Then Throw New NotSupportedException("MemoryStream: The current stream is not writable")
+		If This.resizable = False Then Throw New NotSupportedException("MemoryStream: The current stream is not resizable")
+		If value > This.Capacity() Then Throw New NotSupportedException("MemoryStream.SetLength: This stream length is larger than the current capacity.")
+		If value < 0 Then Throw New ArgumentOutOfRangeException("MemoryStream.Read: An argument is a negative value.", New System.Int64(value), "value")
+		If value > Int32.MaxValue() Then Throw New ArgumentOutOfRangeException("MemoryStream.SetLength: The length is a value greater than MaxValue.", New System.Int64(value), "value")
+		This.pointer = This.reallocate(This.pointer,value As Long)
+		This.streamLength = value As Long
+	End Sub
+
+	/*!
+	@brief	ストリームにbufferの内容を指定したバイト数書き込みます。
+	@author	NoWest
+	@date	2008/3/12
+	*/
 	Override Sub Write(buffer As *Byte, offset As Long, count As Long)
-		If This.p = 0 Then Debug'Throw New ObjectDisposedException
-		If This.writable = False Then Debug'Throw New NotSupportedException
-		If buffer = 0 Then Debug 'Throw New ArgumentNullException
-		If offset < 0 Then Debug'Throw New ArgumentOutOfRangeException
-		If count < 0 Then Debug'Throw New ArgumentOutOfRangeException
-		If count > (This.StreamLength - This.CurrentPosition) Then 
-			If This.Resizable = False Then
-				Debug 'Throw New NotSupportedException
-			Else
-				If count > (This.Capacity() - This.CurrentPosition) Then 
-					This.p = HeapReAlloc(This.handle,HEAP_ZERO_MEMORY,This.p,This.CurrentPosition+count)
-				End If
-				This.StreamLength = This.CurrentPosition+count
+		If This.pointer = 0 Then Throw New ObjectDisposedException("MemoryStream: This stream has closed.")
+		If This.writable = False Then Throw New NotSupportedException("MemoryStream: The current stream is not writable")
+		If buffer = 0 Then Throw New ArgumentNullException("MemoryStream.Write: An argument is a null value.", "buffer")
+		If offset < 0 Then Throw New ArgumentOutOfRangeException("MemoryStream.Write: An argument is a negative value.", New System.Int64(offset), "offset")
+		If count < 0 Then Throw New ArgumentOutOfRangeException("MemoryStream.Write: An argument is a negative value.", New System.Int64(count), "count")
+		If count > (This.streamLength - This.currentPosition) Then 
+			If This.resizable = False Then Throw New NotSupportedException("MemoryStream: The current stream is not resizable")
+			If (This.Capacity() - This.currentPosition) < count Then 
+				This.pointer = This.reallocate(This.pointer,This.currentPosition+count)
 			End If
-		End If
-		MoveMemory(VarPtr(This.p[This.CurrentPosition]),VarPtr(buffer[offset]),count)
-		This.CurrentPosition + = count
-	End Sub
-
+			This.streamLength = This.currentPosition+count
+		End If
+		MoveMemory(VarPtr(This.pointer[This.currentPosition]),VarPtr(buffer[offset]),count)
+		This.currentPosition + = count
+	End Sub
+
+	/*!
+	@brief	ストリームに1バイト書き込みます。
+	@author	NoWest
+	@date	2008/3/12
+	*/
 	Override Sub WriteByte(b As Byte)
 		Write(VarPtr(b), 0, 1)
 	End Sub
 
-/*	Virtual Function ToArray() As List<Byte>
+/*	Virtual Function ToArray() As Array<Byte>
 		TODO:
 	End Function*/
 
+	/*!
+	@brief	ストリームの内容全体をまるごと別のストリームに書き込みます。
+	@author	NoWest
+	@date	2008/3/12
+	*/
 	Virtual Sub WriteTo(stream As Stream)
-		If This.p = 0 Then Debug'Throw New ObjectDisposedException
-		If ActiveBasic.IsNothing(stream) Then Debug 'Throw New ArgumentNullException("path")
-		stream.Write(This.p,0,This.Capacity())
+		If This.pointer = 0 Then Throw New ObjectDisposedException("MemoryStream: This stream has closed.")
+		If ActiveBasic.IsNothing(stream) Then Throw New ArgumentNullException("MemoryStream.WriteTo: An argument is a null value.", "stream")
+		stream.Write(This.pointer,0,This.Capacity())
 	End Sub
 
 Protected
+
+	/*!
+	@brief	Streamクラスから継承
+	@author	NoWest
+	@date	2008/3/12
+	*/
 	Override Sub Dispose(disposing As Boolean)
-		HeapFree(This.handle,0,InterlockedExchangePointer(ByVal VarPtr(This.p) As VoidPtr,NULL) As VoidPtr)
+		This.destroy()
+	End Sub
+
+Private
+	/* Heap memory address */
+	pointer As *Byte
+	/* MemoryStream status */
+	writable As Boolean
+	resizable As Boolean
+	visible As Boolean
+	streamLength As Long
+	currentPosition As Long
+
+	/*
+	バッファ管理用のPrivate関数類
+	一応初期設定ではGC_mallocでメモリを確保しています。
+	*/
+	handle As HANDLE
+	Sub create()
+#ifdef NOT_USE_GC
+		This.handle = HeapCreate(0,0,0)
+#endif
+	End Sub
+
+	Sub destroy()
+#ifdef NOT_USE_GC
+		HeapFree(This.handle,0,InterlockedExchangePointer(ByVal VarPtr(This.pointer) As VoidPtr,NULL) As VoidPtr)
 		HeapDestroy(InterlockedExchangePointer(ByVal VarPtr(This.handle) As VoidPtr,NULL) As HANDLE)
-	End Sub
-
-	/* status */
-	writable As Boolean
-	Resizable As Boolean
-	visible As Boolean
-
-	StreamLength As Long
-	CurrentPosition As Long
-
-Private
-	handle As HANDLE
-	p As *Byte
+#endif
+	End Sub
+
+	Function allocate(length As SIZE_T) As VoidPtr
+#ifdef NOT_USE_GC
+		Return HeapAlloc(This.handle,HEAP_ZERO_MEMORY,length)
+#else
+		Return GC_malloc_atomic(length)
+#endif
+	End Function
+
+	Function reallocate(p As VoidPtr, length As SIZE_T) As VoidPtr
+#ifdef NOT_USE_GC
+		Return HeapReAlloc(This.handle,HEAP_ZERO_MEMORY,p,This.currentPosition+count)
+#else
+		Return _System_pGC->__realloc(p,length)
+#endif
+	End Function
+
+	Function size(p As VoidPtr) As Long
+#ifdef NOT_USE_GC
+		Return HeapSize(This.handle,0,p)
+#else
+		Dim pmemobj = _System_pGC->GetMemoryObjectPtr(p)
+		Return pmemobj->size
+#endif
+	End Function
 End Class
 
