Index: trunk/ab5.0/ablib/src/Classes/System/IO/TextReader.ab
===================================================================
--- trunk/ab5.0/ablib/src/Classes/System/IO/TextReader.ab	(revision 655)
+++ trunk/ab5.0/ablib/src/Classes/System/IO/TextReader.ab	(revision 665)
@@ -20,11 +20,23 @@
 	End Sub
 
-	Abstract Function Peek() As Long
-	Abstract Function Read() As Long
+	Virtual Function Peek() As Long
+		If pos >= buf.Length Then
+			If Not Underflow() Then
+				Peek = -1
+				Exit Function
+			End If
+		End If
+		Peek = buf[pos]
+	End Function
+
+	Virtual Function Read() As Long
+		Read = Peek()
+		pos++
+	End Function
 	/*
 	@date 2008/02/26
 	@auther Egtra
 	*/
-	Function Read(buffer As *WCHAR, index As Long, count As Long) As Long
+	Virtual Function Read(buffer As *Char, index As Long, count As Long) As Long
 		If buffer = 0 Then
 			Throw New ArgumentNullException("buffer")
@@ -47,58 +59,65 @@
 	@retval 有効なStringインスタンス 読み取った1行
 	*/
-	Function ReadLine() As String
+	Virtual Function ReadLine() As String
 		If Peek() = -1 Then
 			Exit Function
 		End If
-		Dim sb = New Collections.Generic.List<WCHAR>
+		Dim sb = New Text.StringBuilder
+		Dim isLead = FALSE
 		Do
 			Dim ch = Read()
-			If ch = &h0D Then
-				If Peek() = &h0A Then
-					Read() 'CR LFの場合
+			If ch = -1 Then Exit Do
+			If isLead = FALSE Then
+				If ch = &h0D Then
+					If Peek() = &h0A Then
+						Read() 'CR LFの場合
+					End If
+					Exit Do
 				End If
-				Exit Do
-			End If
-			Select Case ch
-				Case -1 'EOF
-					Exit Do
-				Case &h0A 'LF
-					Exit Do
-				Case &h0B 'VT
-					Exit Do
-				Case &h0C 'FF
-					Exit Do
-				Case &h0D 'CR
-					Exit Do
-				Case &h85 'NEL
-					Exit Do
-				Case &h2028 'LS
-					Exit Do
-				Case &h2029 'PS
-					Exit Do
-			End Select
-			sb.Add(ch As WCHAR)
-		Loop
-		ReadLine = New String(sb As *WCHAR, sb.Count)
-	End Function
-	/*
-	@brief 現在位置からストリームの終わりまで読み込む。
-	@date 2008/02/26
-	@auther Egtra
-	*/
-	Virtual Function ReadToEnd() As String
-		Dim sb = New Text.StringBuilder(8192)
-		Do
-			Dim ch = Read()
-			If ch = -1 Then
-				ReadToEnd = sb.ToString
-				Exit Function
+				Select Case ch
+					Case -1 'EOF
+						Exit Do
+					Case &h0A 'LF
+						Exit Do
+					Case &h0B 'VT
+						Exit Do
+					Case &h0C 'FF
+						Exit Do
+					Case &h0D 'CR
+						Exit Do
+#ifdef UNICODE
+					Case &h85 'NEL
+						Exit Do
+					Case &h2028 'LS
+						Exit Do
+					Case &h2029 'PS
+						Exit Do
+#endif
+				End Select
 			End If
 			sb.Append(ch As Char)
+#ifndef UNICODE
+			isLead = IsDBCSLeadByte(ch As Byte)
+#endif
 		Loop
+		ReadLine = sb.ToString()
+	End Function
+
+	/*!
+	@brief 現在位置からストリームの終わりまで読み込む。
+	@date 2008/02/26
+	@auther Egtra
+	*/
+	Virtual Function ReadToEnd() As String
+		buf.Remove(0, pos)
+		While Underflow()
+		Wend
+		ReadToEnd = buf.ToString()
 	End Function
 
 Protected
 	Sub TextReader()
+		buf = New Text.StringBuilder
+		pos = 0
 	End Sub
 
@@ -106,21 +125,42 @@
 	End Sub
 
-	/*
-	@date 2008/02/26
-	@auther Egtra
-	*/
-	Virtual Function ReadImpl(buffer As *WCHAR, count As Long) As Long
-		Dim i As Long
-		For i = 0 To ELM(count)
-			Dim c = Read()
-			If c = -1 Then
-				ReadImpl = i
-				Exit Function
-			Else
-				buffer[i] = c As Char
-			End If
-		Next
-		ReadImpl = i
-	End Function
+	/*!
+	@brief バッファが足りなくなったときに呼ばれる。
+	@date 2008/12/27
+	@auther Egtra
+	@retval True まだEOFに達していない場合
+	@retval False EOFに達した場合
+	*/
+	Virtual Function Underflow() As Boolean
+		Underflow = False
+	End Function
+
+	/*!
+	@brief 内部バッファを返す
+	@date 2008/12/27
+	@auther Egtra
+	@return 内部バッファ
+	*/
+	Function Buffer() As Text.StringBuilder
+		Buffer = buf
+	End Function
+
+	/*!
+	@date 2008/02/26
+	@auther Egtra
+	*/
+	Function ReadImpl(buffer As *Char, count As Long) As Long
+		Dim p = StrPtr(buf)
+		While buf.Length - pos < count
+			If Underflow() = False Then Exit While
+		Wend
+		ReadImpl = Math.Min(buf.Length - pos, count)
+		ActiveBasic.Strings.ChrCopy(buffer, VarPtr(p[buf]), ReadImpl As SIZE_T)
+		pos += ReadImpl
+	End Function
+
+Private
+	buf As Text.StringBuilder
+	pos As Long
 End Class
 
@@ -136,12 +176,24 @@
 
 	Override Function Peek() As Long
+'		Using lock = cs.Lock()
+			Peek = base.Peek()
+'		End Using
+	End Function
+
+	Override Function Read() As Long
+'		Using lock = cs.Lock()
+			Read = base.Read()
+'		End Using
+	End Function
+
+	Override Function Read(buffer As *Char, index As Long, count As Long) As Long
+'		Using lock = cs.Lock()
+			Read = base.Read(buffer, index, count)
+'		End Using
+	End Function
+
+	Override Function ReadToEnd() As String
 '		Using lock = cs.Lock
-			Peek = base.Peek
-'		End Using
-	End Function
-
-	Override Function Read() As Long
-'		Using lock = cs.Lock
-			Read = base.Read
+			ReadToEnd = base.ReadToEnd
 '		End Using
 	End Function
@@ -159,9 +211,4 @@
 	End Sub
 
-	Override Function ReadImpl(buffer As *WCHAR, count As Long) As Long
-'		Using lock = cs.Lock
-			ReadImpl = base.ReadImpl(buffer, count)
-'		End Using
-	End Function
 Private
 	cs As ActiveBasic.Windows.CriticalSection
