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

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

#231に関連して、エンコーディング周りを見直し、Encoder/Decoderをストリーム用に特化。
UTF8Encodingをコンパイル可能にし、ビルドに含めるようにした。ただし、実装が不完全なためテストは不可。
(#231)

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