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

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

TextWriter及びその派生クラスをEncodingを用いるように変更。UTF8EncodingをEncodingの変更に追従させていないので、それ関連を一時的にコメントアウト。

File size: 2.0 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 Dim buf = Buffer()
44 Dim pws As PCWSTR
45 Dim size = GetWCStr(buf.ToString(), pws)
46 Dim pwsEnd = VarPtr(pws[size])
47 Dim charConverted As Long
48 Dim byteBuf[4095] As Byte
49 Dim byteSize As Long
50 Dim completed As Boolean
51 Do
52 Dim converted As Long
53 encoder.Convert(pws, size, byteBuf, Len(byteBuf), False, charConverted, byteSize, completed)
54 s.Write(byteBuf, 0, byteSize)
55 pws = VarPtr(pws[charConverted])
56 size -= charConverted
57 Loop Until pws = pwsEnd
58 buf.Length = 0
59 End Sub
60
61Protected
62 Override Sub Dispose(disposing As Boolean)
63 If disposing Then
64 Flush()
65 flushLast()
66 s.Dispose()
67 End If
68 End Sub
69
70Private
71 Sub init(stream As Stream)
72 s = stream
73 buf = New Text.StringBuilder(4096)
74 '暫定。正式版ではUTF-8を標準とする。
75 encoding = New Text.Detail.WindowsCodePageEncoding(CP_ACP)
76 encoder = encoding.GetEncoder()
77 End Sub
78
79 Sub flushLast()
80 Dim charConverted As Long
81 Dim byteBuf[63] As Byte
82 Dim byteSize As Long
83 Dim completed As Boolean
84 Do
85 encoder.Convert(0, 0, byteBuf, Len(byteBuf), False, charConverted, byteSize, completed)
86 s.Write(byteBuf, 0, byteSize)
87 Loop Until completed
88 End Sub
89
90 encoding As Text.Encoding
91 encoder As Text.Encoder
92 s As System.IO.Stream
93End Class
94
95End Namespace
96End Namespace
Note: See TracBrowser for help on using the repository browser.