'Classes/System/IO/TextWriter.ab

Namespace System
Namespace IO

/*
@brief テキスト書き込みの抽象基底クラス
@date 2007/03/05
@auther Egtra
*/
Class TextWriter
	Implements System.IDisposable
Public
	Virtual Sub ~TextWriter()
		Dispose(False)
	End Sub

'	Static Null = StreamWriter.Null As StreamWriter

Public
	Sub Close()
		Dispose(True)
	End Sub

	Sub Dispose()
		Dispose(True)
	End Sub

	Virtual Sub Flush()
	End Sub

	'これ以外全てのWrite/WriteLineは、最終的にこのWriteを呼ぶ。
	Sub Write(x As String)
		buf.Append(x)
		If buf.Length >= 4096 Then
			Flush()
		End If
	End Sub
	
	Sub Write(x As Boolean)
		buf.Append(x)
	End Sub
	
	Sub Write(x As SByte)
		buf.Append(x)
	End Sub

	Sub Write(x As Byte)
		buf.Append(x)
	End Sub

	Sub Write(x As Word)
		buf.Append(x)
	End Sub

	Sub Write(x As Integer)
		buf.Append(x)
	End Sub

	Sub Write(x As DWord)
		buf.Append(x)
	End Sub

	Sub Write(x As Long)
		buf.Append(x)
	End Sub

	Sub Write(x As QWord)
		buf.Append(x)
	End Sub

	Sub Write(x As Int64)
		buf.Append(x)
	End Sub

	Sub Write(x As Single)
		buf.Append(x)
	End Sub

	Sub Write(x As Double)
		buf.Append(x)
	End Sub

	Sub Write(x As Object)
		buf.Append(x)
	End Sub

	Sub WriteLine()
		Write(Environment.NewLine)
	End Sub

	Sub WriteLine(x As String)
		Write(x)
		WriteLine()
	End Sub

	Sub WriteLine(x As Boolean)
		Write(x)
		WriteLine()
	End Sub

	Sub WriteLine(x As Byte)
		Write(x)
		WriteLine()
	End Sub
	
	Sub WriteLine(x As SByte)
		Write(x)
		WriteLine()
	End Sub

	Sub WriteLine(x As Word)
		Write(x)
		WriteLine()
	End Sub

	Sub WriteLine(x As Integer)
		Write(x)
		WriteLine()
	End Sub

	Sub WriteLine(x As DWord)
		Write(x)
		WriteLine()
	End Sub

	Sub WriteLine(x As Long)
		Write(x)
		WriteLine()
	End Sub

	Sub WriteLine(x As QWord)
		Write(x)
		WriteLine()
	End Sub

	Sub WriteLine(x As Int64)
		Write(x)
		WriteLine()
	End Sub

	Sub WriteLine(x As Single)
		Write(x)
		WriteLine()
	End Sub

	Sub WriteLine(x As Double)
		Write(x)
		WriteLine()
	End Sub

	Sub WriteLine(x As Object)
		Write(x)
		WriteLine()
	End Sub

	Static Function Synchronized(writer As TextWriter) As TextWriter
		' TODO: 実装。とりあえずそのまま返却
		Return writer
	End Function

Protected
	Sub TextWriter()
		buf = New Text.StringBuilder
	End Sub

	Sub TextWriter(buffer As Text.StringBuilder)
		If ActiveBasic.IsNothing(buffer) Then
			Throw New ArgumentNullException("buffer")
		End If
		buf = buffer
	End Sub

	/*!
	@brief 内部バッファを返す
	@date 2008/12/27
	@auther Egtra
	@return 内部バッファ
	*/
	Function Buffer() As Text.StringBuilder
		Buffer = buf
	End Function

	Virtual Sub Dispose(disposing As Boolean)
		If disposing Then
			Flush()
			buf = Nothing
		End If
	End Sub

Private
	buf As Text.StringBuilder
End Class


End Namespace
End Namespace
