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
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)
22 init(New FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
23 End Sub
24
25 /*
[473]26 @date 2008/03/09
[468]27 @auther Egtra
28 */
29 Sub StreamWriter(stream As Stream)
30 init(stream)
31 End Sub
32
[622]33 /*
34 @brief 基になるストリームを取得する
35 @date 2008/09/02
36 @auther NoWest
37 */
[627]38 Function BaseStream () As System.IO.Stream
[622]39 Return s
40 End Function
41
[473]42 Override Sub Flush()
[676]43 Flush(False)
[473]44 End Sub
45
[676]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
[468]76Protected
77 Override Sub Dispose(disposing As Boolean)
78 If disposing Then
[676]79 Flush(True)
[468]80 s.Dispose()
81 End If
82 End Sub
83
84Private
85 Sub init(stream As Stream)
86 s = stream
[653]87 buf = New Text.StringBuilder(4096)
88 '暫定。正式版ではUTF-8を標準とする。
[676]89 encoder = New Text.Detail.WindowsCodePageEncoder(CP_ACP)
[468]90 End Sub
91
[653]92 encoder As Text.Encoder
[468]93 s As System.IO.Stream
[271]94End Class
95
96End Namespace
97End Namespace
Note: See TracBrowser for help on using the repository browser.