Index: trunk/ab5.0/ablib/src/Classes/System/Exception.ab
===================================================================
--- trunk/ab5.0/ablib/src/Classes/System/Exception.ab	(revision 678)
+++ trunk/ab5.0/ablib/src/Classes/System/Exception.ab	(revision 682)
@@ -14,4 +14,6 @@
 Const AB_E_PLATFORMNOTSUPPORTED = &h80041539 '80131539
 Const AB_E_KEYNOTFOUND = &h80041577 '80131577
+Const AB_E_ARITHMETIC = &h80040216 '80070216
+Const AB_E_OVERFLOW = &h80041516 '80131516
 
 End Namespace
@@ -38,5 +40,4 @@
 MulticastNotSupportedException
 NullReferenceException
-OverflowException
 RankException
 StackOverflowException
@@ -712,4 +713,71 @@
 End Class
 
+/*!
+@brief	算術演算例外
+@author	Egtra
+@date	2009/02/16
+*/
+Class ArithmeticException
+	Inherits SystemException
+Public
+	/*!
+	@biref	コンストラクタ
+	*/
+	Sub ArithmeticException()
+		SystemException("ArithmeticException", Nothing)
+		HResult = ActiveBasic.AB_E_ARITHMETIC
+	End Sub
+	/*!
+	@biref	コンストラクタ
+	@param[in] message	エラーメッセージ
+	*/
+	Sub ArithmeticException(message As String)
+		SystemException(message, Nothing)
+		HResult = ActiveBasic.AB_E_ARITHMETIC
+	End Sub
+	/*!
+	@biref	コンストラクタ
+	@param[in] message	エラーメッセージ
+	@param[in] innerException	内部例外
+	*/
+	Sub ArithmeticException(message As String, innerException As Exception)
+		SystemException(message, innerException)
+		HResult = ActiveBasic.AB_E_ARITHMETIC
+	End Sub
+End Class
+
+/*!
+@brief	オーバーフロー例外
+@author	Egtra
+@date	2009/02/16
+*/
+Class OverflowException
+	Inherits ArithmeticException
+Public
+	/*!
+	@biref	コンストラクタ
+	*/
+	Sub OverflowException()
+		ArithmeticException("OverflowException", Nothing)
+		HResult = ActiveBasic.AB_E_OVERFLOW
+	End Sub
+	/*!
+	@biref	コンストラクタ
+	@param[in] message	エラーメッセージ
+	*/
+	Sub OverflowException(message As String)
+		ArithmeticException(message, Nothing)
+		HResult = ActiveBasic.AB_E_OVERFLOW
+	End Sub
+	/*!
+	@biref	コンストラクタ
+	@param[in] message	エラーメッセージ
+	@param[in] innerException	内部例外
+	*/
+	Sub OverflowException(message As String, innerException As Exception)
+		ArithmeticException(message, innerException)
+		HResult = ActiveBasic.AB_E_OVERFLOW
+	End Sub
+End Class
 
 End Namespace
Index: trunk/ab5.0/ablib/src/Classes/System/IO/MemoryStream.ab
===================================================================
--- trunk/ab5.0/ablib/src/Classes/System/IO/MemoryStream.ab	(revision 678)
+++ trunk/ab5.0/ablib/src/Classes/System/IO/MemoryStream.ab	(revision 682)
@@ -339,4 +339,8 @@
 	End Function*/
 
+	Function ToPointer() As VoidPtr
+		ToPointer = pointer
+	End Function
+
 	/*!
 	@brief	ストリームの内容全体をまるごと別のストリームに書き込みます。
Index: trunk/ab5.0/ablib/src/Classes/System/IO/Stream.ab
===================================================================
--- trunk/ab5.0/ablib/src/Classes/System/IO/Stream.ab	(revision 678)
+++ trunk/ab5.0/ablib/src/Classes/System/IO/Stream.ab	(revision 682)
@@ -155,4 +155,39 @@
 End Class
 
+/*!
+@brief 書き込みバイト数を記憶するヌルストリーム
+Encoding関連で使用
+@date 2009/02/16
+@auther Egtra
+*/
+Class WriteCountingNullStream
+	Inherits NullStream
+Public
+	Sub WriteCountingNullStream()
+		writeCount = 0
+	End Sub
+
+	Override Sub Write(buffer As *Byte, offset As Long, count As Long)
+		If count < 0 Then
+			Throw New ArgumentOutOfRangeException("count")
+		End If
+		writeCount += count
+	End Sub
+
+	Override Sub WriteByte(b As Byte)
+		writeCount++
+	End Sub
+
+	/*!
+	@brief 書き込まれたバイト数を返す。
+	*/
+	Function WriteCount() As Long
+		WriteCount = writeCount
+	End Function
+
+Private
+	writeCount As Long
+End Class
+
 End Namespace
 
Index: trunk/ab5.0/ablib/src/Classes/System/IO/StreamWriter.ab
===================================================================
--- trunk/ab5.0/ablib/src/Classes/System/IO/StreamWriter.ab	(revision 678)
+++ trunk/ab5.0/ablib/src/Classes/System/IO/StreamWriter.ab	(revision 682)
@@ -20,5 +20,5 @@
 	*/
 	Sub StreamWriter(path As String)
-		init(New FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
+		init(New FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None), Nothing)
 	End Sub
 
@@ -28,5 +28,13 @@
 	*/
 	Sub StreamWriter(stream As Stream)
-		init(stream)
+		init(stream, Nothing)
+	End Sub
+
+	/*
+	@date 2009/02/16
+	@auther Egtra
+	*/
+	Sub StreamWriter(stream As Stream, encoding As Text.Encoding)
+		init(stream, encoding)
 	End Sub
 
@@ -83,9 +91,13 @@
 
 Private
-	Sub init(stream As Stream)
+	Sub init(stream As Stream, e As Text.Encoding)
 		s = stream
 		buf = New Text.StringBuilder(4096)
-		'暫定。正式版ではUTF-8を標準とする。
-		encoder = New Text.Detail.WindowsCodePageEncoder(CP_ACP)
+		If ActiveBasic.IsNothing(e) Then
+			'暫定。正式版ではUTF-8を標準とする。
+			encoder = New Text.Detail.WindowsCodePageEncoder(CP_ACP)
+		Else
+			encoder = e.GetEncoder()
+		End If
 	End Sub
 
Index: trunk/ab5.0/ablib/src/Classes/System/Text/Encoding.ab
===================================================================
--- trunk/ab5.0/ablib/src/Classes/System/Text/Encoding.ab	(revision 678)
+++ trunk/ab5.0/ablib/src/Classes/System/Text/Encoding.ab	(revision 682)
@@ -74,5 +74,12 @@
 	@date	2007/12/08
 	*/
-	Abstract Function GetBytesCountCore(src As *WCHAR, srcCount As Long) As Long
+	Virtual Function GetBytesCountCore(src As *WCHAR, srcCount As Long) As Long
+		Dim s = New IO.Detail.WriteCountingNullStream
+		GetEncoder().Encode(src, srcCount, s, True)
+		GetBytesCountCore = s.WriteCount
+		If GetBytesCountCore < 0 Then
+			Throw New OverflowException
+		End If
+	End Function
 Public
 	/*!
@@ -110,5 +117,14 @@
 	@date	2007/12/08
 	*/
-	Abstract Function GetBytesCore(src As *WCHAR, srcCount As Long, dst As *Byte, dstCount As Long) As Long
+	Virtual Function GetBytesCore(src As *WCHAR, srcCount As Long, dst As *Byte, dstCount As Long) As Long
+		Dim s = New IO.MemoryStream
+		GetEncoder().Encode(src, srcCount, s, True)
+		Dim size = s.Position()
+		If size > (dstCount As QWord) Then
+			Throw New ArgumentException("dstCount")
+		End If
+		GetBytesCore = size As Long
+		memcpy(dst, s.ToPointer(), size As SIZE_T)
+	End Function
 Public
 	/*!
@@ -136,5 +152,10 @@
 	@date	2007/12/08
 	*/
-	Abstract Function GetCharsCountCore(src As *Byte, srcCount As Long) As Long
+	Virtual Function GetCharsCountCore(src As *Byte, srcCount As Long) As Long
+		Dim s = New IO.MemoryStream(src, srcCount)
+		Dim l = New Collections.Generic.List<WCHAR>
+		GetDecoder().Decode(l, s)
+		GetCharsCountCore = l.Count
+	End Function
 Public
 	/*!
@@ -172,5 +193,15 @@
 	@date	2007/12/08
 	*/
-	Abstract Function GetCharsCore(src As *Byte, srcCount As Long, dst As *WCHAR, dstCount As Long) As Long
+	Virtual Function GetCharsCore(src As *Byte, srcCount As Long, dst As *WCHAR, dstCount As Long) As Long
+		Dim s = New IO.MemoryStream(src, srcCount)
+		Dim l = New Collections.Generic.List<WCHAR>
+		GetDecoder().Decode(l, s)
+		If l.Count > dstCount Then
+			Throw New ArgumentException("dstCount")
+		End If
+		Dim p As *WCHAR
+		p = l
+		memcpy(dst, p, (l.Count As SIZE_T) * 2)
+	End Function
 Public
 #ifdef UNICODE
Index: trunk/ab5.0/ablib/src/Classes/System/Text/UTF8Encoding.ab
===================================================================
--- trunk/ab5.0/ablib/src/Classes/System/Text/UTF8Encoding.ab	(revision 678)
+++ trunk/ab5.0/ablib/src/Classes/System/Text/UTF8Encoding.ab	(revision 682)
@@ -111,5 +111,5 @@
 							dst.Add((&hDC00 Or (buffer And &h3FF)) As WCHAR)
 						Else
-							'最短形式でないもの、4バイト形式で10FFFFを超えるコードポイントのもの
+							'最短形式でないもの、または4バイト形式で10FFFFを超えるコードポイントのもの
 							dst.Add(&hfffd As WCHAR)
 						End If
@@ -196,16 +196,4 @@
 	End Function
 */
-Protected
-	Override Function GetBytesCountCore(src As *WCHAR, srcCount As Long) As Long
-	End Function
-
-	Override Function GetBytesCore(src As *WCHAR, srcCount As Long, dst As *Byte, dstCount As Long) As Long
-	End Function
-
-	Override Function GetCharsCountCore(src As *Byte, srcCount As Long) As Long
-	End Function
-
-	Override Function GetCharsCore(src As *Byte, srcCount As Long, dst As *WCHAR, dstCount As Long) As Long
-	End Function
 
 Private
