/* @file Include/Classes/System/IO/StreamWriter.ab @brief StreamWriterの実装。 */ Namespace System Namespace IO /* @brief ストリームへの書き込みを行うTextWriterの派生。 @date 2008/03/09 @auther Egtra */ Class StreamWriter Inherits TextWriter Public /* @date 2008/03/09 @auther Egtra */ Sub StreamWriter(path As String) init(New FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None)) End Sub /* @date 2008/03/09 @auther Egtra */ Sub StreamWriter(stream As Stream) init(stream) End Sub /* @brief 基になるストリームを取得する @date 2008/09/02 @auther NoWest */ Function BaseStream () As System.IO.Stream Return s End Function Override Sub Flush() Dim buf = Buffer() Dim pws As PCWSTR Dim size = GetWCStr(buf.ToString(), pws) Dim pwsEnd = VarPtr(pws[size]) Dim charConverted As Long Dim byteBuf[4095] As Byte Dim byteSize As Long Dim completed As Boolean Do Dim converted As Long encoder.Convert(pws, size, byteBuf, Len(byteBuf), False, charConverted, byteSize, completed) s.Write(byteBuf, 0, byteSize) pws = VarPtr(pws[charConverted]) size -= charConverted Loop Until pws = pwsEnd buf.Length = 0 End Sub Protected Override Sub Dispose(disposing As Boolean) If disposing Then Flush() flushLast() s.Dispose() End If End Sub Private Sub init(stream As Stream) s = stream buf = New Text.StringBuilder(4096) '暫定。正式版ではUTF-8を標準とする。 encoding = New Text.Detail.WindowsCodePageEncoding(CP_ACP) encoder = encoding.GetEncoder() End Sub Sub flushLast() Dim charConverted As Long Dim byteBuf[63] As Byte Dim byteSize As Long Dim completed As Boolean Do encoder.Convert(0, 0, byteBuf, Len(byteBuf), False, charConverted, byteSize, completed) s.Write(byteBuf, 0, byteSize) Loop Until completed End Sub encoding As Text.Encoding encoder As Text.Encoder s As System.IO.Stream End Class End Namespace End Namespace