source: trunk/ab5.0/ablib/src/Classes/System/IO/StreamWriter.ab@ 682

Last change on this file since 682 was 682, checked in by イグトランス (egtra), 15 years ago

UTF8Encodingクラスをとりあえず使える状態に。ただし、BOM出力はまだ不可能。
(#231)

File size: 2.1 KB
RevLine 
[473]1/*
2@file Include/Classes/System/IO/StreamWriter.ab
3@brief StreamWriterの実装。
4*/
5
[271]6Namespace System
7Namespace IO
[473]8
9/*
[476]10@brief ストリームへの書き込みを行うTextWriterの派生。
[473]11@date 2008/03/09
12@auther Egtra
13*/
[105]14Class StreamWriter
[468]15 Inherits TextWriter
16Public
17 /*
[473]18 @date 2008/03/09
[468]19 @auther Egtra
20 */
21 Sub StreamWriter(path As String)
[682]22 init(New FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None), Nothing)
[468]23 End Sub
24
25 /*
[473]26 @date 2008/03/09
[468]27 @auther Egtra
28 */
29 Sub StreamWriter(stream As Stream)
[682]30 init(stream, Nothing)
[468]31 End Sub
32
[622]33 /*
[682]34 @date 2009/02/16
35 @auther Egtra
36 */
37 Sub StreamWriter(stream As Stream, encoding As Text.Encoding)
38 init(stream, encoding)
39 End Sub
40
41 /*
[622]42 @brief 基になるストリームを取得する
43 @date 2008/09/02
44 @auther NoWest
45 */
[627]46 Function BaseStream () As System.IO.Stream
[622]47 Return s
48 End Function
49
[473]50 Override Sub Flush()
[676]51 Flush(False)
[473]52 End Sub
53
[676]54 Sub Flush(last As Boolean)
55#ifdef UNICODE
56 encoder.Encode(StrPtr(buf), buf.Length As SIZE_T, s, last)
57 buf.Remove(0, buf.Length As SIZE_T)
58#else
59 Dim p = StrPtr(buf)
60 Dim i = 0 As SIZE_T
61 Dim mbLen = 0 As SIZE_T
62 While i <= buf.Length
63 mbLen = i
64 'ヌル文字で終わっていないので、CharNextは使えない
65 If IsDBCSLeadByte(p[i]) Then
66 i += 2
67 Else
68 i += 1
69 End If
70 Wend
71 Dim wcBuf As PCWSTR
72 Dim wcLen = GetStr(p, mbLen, wcBuf)
73 encoder.Encode(wcBuf, wcLen, s, last)
74 buf.Remove(0, mbLen)
75 If last Then
76 If buf.Length <> 0 Then
77 s.WriteByte(Asc("?"))
78 buf.Length = 0
79 End If
80 End If
81#endif
82 End Sub
83
[468]84Protected
85 Override Sub Dispose(disposing As Boolean)
86 If disposing Then
[676]87 Flush(True)
[468]88 s.Dispose()
89 End If
90 End Sub
91
92Private
[682]93 Sub init(stream As Stream, e As Text.Encoding)
[468]94 s = stream
[653]95 buf = New Text.StringBuilder(4096)
[682]96 If ActiveBasic.IsNothing(e) Then
97 '暫定。正式版ではUTF-8を標準とする。
98 encoder = New Text.Detail.WindowsCodePageEncoder(CP_ACP)
99 Else
100 encoder = e.GetEncoder()
101 End If
[468]102 End Sub
103
[653]104 encoder As Text.Encoder
[468]105 s As System.IO.Stream
[271]106End Class
107
108End Namespace
109End Namespace
Note: See TracBrowser for help on using the repository browser.