[473] | 1 | /*
|
---|
| 2 | @file Include/Classes/System/IO/StreamWriter.ab
|
---|
| 3 | @brief StreamWriterの実装。
|
---|
| 4 | */
|
---|
| 5 |
|
---|
[271] | 6 | Namespace System
|
---|
| 7 | Namespace IO
|
---|
[473] | 8 |
|
---|
| 9 | /*
|
---|
[476] | 10 | @brief ストリームへの書き込みを行うTextWriterの派生。
|
---|
[473] | 11 | @date 2008/03/09
|
---|
| 12 | @auther Egtra
|
---|
| 13 | */
|
---|
[105] | 14 | Class StreamWriter
|
---|
[468] | 15 | Inherits TextWriter
|
---|
| 16 | Public
|
---|
| 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()
|
---|
[653] | 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
|
---|
[473] | 59 | End Sub
|
---|
| 60 |
|
---|
[468] | 61 | Protected
|
---|
| 62 | Override Sub Dispose(disposing As Boolean)
|
---|
| 63 | If disposing Then
|
---|
[653] | 64 | Flush()
|
---|
| 65 | flushLast()
|
---|
[468] | 66 | s.Dispose()
|
---|
| 67 | End If
|
---|
| 68 | End Sub
|
---|
| 69 |
|
---|
| 70 | Private
|
---|
| 71 | Sub init(stream As Stream)
|
---|
| 72 | s = stream
|
---|
[653] | 73 | buf = New Text.StringBuilder(4096)
|
---|
| 74 | '暫定。正式版ではUTF-8を標準とする。
|
---|
| 75 | encoding = New Text.Detail.WindowsCodePageEncoding(CP_ACP)
|
---|
| 76 | encoder = encoding.GetEncoder()
|
---|
[468] | 77 | End Sub
|
---|
| 78 |
|
---|
[653] | 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
|
---|
[468] | 92 | s As System.IO.Stream
|
---|
[271] | 93 | End Class
|
---|
| 94 |
|
---|
| 95 | End Namespace
|
---|
| 96 | End Namespace
|
---|