| 1 | /*
|
|---|
| 2 | @file Include/Classes/System/IO/StreamWriter.ab
|
|---|
| 3 | @brief StreamWriterの実装。
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | Namespace System
|
|---|
| 7 | Namespace IO
|
|---|
| 8 |
|
|---|
| 9 | /*
|
|---|
| 10 | @brief ストリームへの書き込みを行うTextWriterの派生。
|
|---|
| 11 | @date 2008/03/09
|
|---|
| 12 | @auther Egtra
|
|---|
| 13 | */
|
|---|
| 14 | Class StreamWriter
|
|---|
| 15 | Inherits TextWriter
|
|---|
| 16 | Public
|
|---|
| 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), Nothing)
|
|---|
| 23 | End Sub
|
|---|
| 24 |
|
|---|
| 25 | /*
|
|---|
| 26 | @date 2008/03/09
|
|---|
| 27 | @auther Egtra
|
|---|
| 28 | */
|
|---|
| 29 | Sub StreamWriter(stream As Stream)
|
|---|
| 30 | init(stream, Nothing)
|
|---|
| 31 | End Sub
|
|---|
| 32 |
|
|---|
| 33 | /*
|
|---|
| 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 | /*
|
|---|
| 42 | @brief 基になるストリームを取得する
|
|---|
| 43 | @date 2008/09/02
|
|---|
| 44 | @auther NoWest
|
|---|
| 45 | */
|
|---|
| 46 | Function BaseStream () As System.IO.Stream
|
|---|
| 47 | Return s
|
|---|
| 48 | End Function
|
|---|
| 49 |
|
|---|
| 50 | Override Sub Flush()
|
|---|
| 51 | Flush(False)
|
|---|
| 52 | End Sub
|
|---|
| 53 |
|
|---|
| 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)
|
|---|
| 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 |
|
|---|
| 84 | Protected
|
|---|
| 85 | Override Sub Dispose(disposing As Boolean)
|
|---|
| 86 | If disposing Then
|
|---|
| 87 | Flush(True)
|
|---|
| 88 | s.Dispose()
|
|---|
| 89 | End If
|
|---|
| 90 | End Sub
|
|---|
| 91 |
|
|---|
| 92 | Private
|
|---|
| 93 | Sub init(stream As Stream, e As Text.Encoding)
|
|---|
| 94 | s = stream
|
|---|
| 95 | buf = New Text.StringBuilder(4096)
|
|---|
| 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
|
|---|
| 102 | End Sub
|
|---|
| 103 |
|
|---|
| 104 | encoder As Text.Encoder
|
|---|
| 105 | s As System.IO.Stream
|
|---|
| 106 | End Class
|
|---|
| 107 |
|
|---|
| 108 | End Namespace
|
|---|
| 109 | End Namespace
|
|---|