Ignore:
Timestamp:
Nov 3, 2008, 11:42:22 PM (15 years ago)
Author:
イグトランス (egtra)
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ab5.0/ablib/src/Classes/System/IO/StreamWriter.ab

    r627 r653  
    4141
    4242    Override Sub Flush()
    43         Dim len = buf.Length
    44         If len > 0 Then
    45             s.Write(StrPtr(buf) As *Byte, 0, len)
    46             buf.Length = 0
    47         End If
    48     End Sub
    49 
    50     Override Sub Write(str As String)
    51         buf.Append(str)
    52         Dim len = buf.Length
    53         If len >= 2048 Then
    54             s.Write(StrPtr(buf) As *Byte, 0, len)
    55             buf.Length = 0
    56         End If
    57     End Sub
    58 
    59     Override Sub Write(x As Boolean)
    60         buf.Append(x)
    61     End Sub
    62    
    63     Override Sub Write(x As Char)
    64         buf.Append(x)
    65     End Sub
    66 
    67     Override Sub Write(x As Byte)
    68         buf.Append(x)
    69     End Sub
    70 #ifdef UNICODE
    71     Override Sub Write(x As SByte)
    72         buf.Append(x)
    73     End Sub
    74 #else
    75     Override Sub Write(x As Word)
    76         buf.Append(x)
    77     End Sub
    78 #endif
    79     Override Sub Write(x As Integer)
    80         buf.Append(x)
    81     End Sub
    82 
    83     Override Sub Write(x As DWord)
    84         buf.Append(x)
    85     End Sub
    86 
    87     Override Sub Write(x As Long)
    88         buf.Append(x)
    89     End Sub
    90 
    91     Override Sub Write(x As QWord)
    92         buf.Append(x)
    93     End Sub
    94 
    95     Override Sub Write(x As Int64)
    96         buf.Append(x)
    97     End Sub
    98 
    99     Override Sub Write(x As Single)
    100         buf.Append(x)
    101     End Sub
    102 
    103     Override Sub Write(x As Double)
    104         buf.Append(x)
    105     End Sub
    106 
    107     Override Sub Write(x As Object)
    108         Write(x.ToString)
     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
    10959    End Sub
    11060
     
    11262    Override Sub Dispose(disposing As Boolean)
    11363        If disposing Then
    114             Dim len = buf.Length
    115             If len > 0 Then
    116                 s.Write(StrPtr(buf) As *Byte, 0, len)
    117             End If
     64            Flush()
     65            flushLast()
    11866            s.Dispose()
    11967        End If
     
    12371    Sub init(stream As Stream)
    12472        s = stream
    125         buf = New System.Text.StringBuilder(4096)
     73        buf = New Text.StringBuilder(4096)
     74        '暫定。正式版ではUTF-8を標準とする。
     75        encoding = New Text.Detail.WindowsCodePageEncoding(CP_ACP)
     76        encoder = encoding.GetEncoder()
    12677    End Sub
    12778
    128     buf As Text.StringBuilder
     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
    12992    s As System.IO.Stream
    13093End Class
Note: See TracChangeset for help on using the changeset viewer.