source: branch/egtra-stream-without-en_dec/Classes/System/IO/StreamReader.ab@ 673

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

Encoder/Decoderを排除した実装(UTF8Encodingは修正していません)。

File size: 2.5 KB
Line 
1'Classes/System/IO/StreamReader.ab
2
3Namespace System
4Namespace IO
5
6/*
7@brief ストリームから読み取りを行うTextReaderの実装。
8@date 2008/02/25
9@auther Egtra
10*/
11Class StreamReader
12 Inherits TextReader
13Public
14 /*
15 @date 2008/02/25
16 @auther Egtra
17 */
18 Sub StreamReader(path As String)
19 init(New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
20 End Sub
21
22 /*
23 @date 2008/02/25
24 @auther Egtra
25 */
26 Sub StreamReader(stream As Stream)
27 init(stream)
28 End Sub
29
30Public
31 /*
32 @brief 基になるストリームを取得する
33 @date 2008/09/02
34 @auther NoWest
35 */
36 Function BaseStream () As Stream
37 Return s
38 End Function
39
40
41Protected
42 /*
43 @date 2008/02/25
44 @auther Egtra
45 */
46 Override Sub Dispose(disposing As Boolean)
47 If disposing Then
48 If Not ActiveBasic.IsNothing(s) Then
49 s.Dispose(True)
50 End If
51 End If
52 s = Nothing
53 End Sub
54
55 /*
56 @date 2008/12/27
57 @auther Egtra
58 */
59 Override Function Underflow() As Boolean
60 Dim nextRead As *Byte
61 Dim readSize = s.Read(mbBuf + bufSize, 0, bufCapacity - bufSize)
62 If readSize = 0 Then
63 If bufSize <> 0 Then
64#ifdef UNCODE
65 Buffer.Append(&hfffd As WCHAR)
66#else
67 Buffer.Append("?")
68#endif
69 End If
70 Underflow = False
71 Exit Function
72 End If
73 Dim mbLen = bufSize + readSize
74 '文字全体が揃っている部分を抽出する。
75 Dim pNext = mbBuf
76 Do
77 Dim q = e.NextChar(pNext, ((mbBuf + mbLen) - pNext) As Long)
78 If q = pNext Then
79 Exit Do
80 End If
81 pNext = q
82 Loop
83 '文字全体が揃っている部分を変換しTextReaderのバッファに送る。
84 Dim wcBufCapacity = e.GetMaxCharCount(mbLen)
85 Dim wcBuf = GC_malloc_atomic(wcBufCapacity * SizeOf (WCHAR)) As *WCHAR
86 Dim wcLen = e.GetChars(mbBuf, (pNext - mbBuf) As Long, wcBuf, wcBufCapacity)
87 Dim appendStr As PCTSTR
88 Dim appendLen = GetStr(wcBuf, wcLen As SIZE_T, appendStr)
89 Dim readerBuffer = Buffer
90 readerBuffer.Append(appendStr, 0, appendLen)
91 'マルチバイトの揃っていない部分を手前に連れてくる。
92 bufSize = ((mbBuf + mbLen) - pNext) As Long
93 If bufSize <> 0 Then
94 memmove(mbBuf, mbBuf + mbLen, bufSize)
95 End If
96 Underflow = True
97 End Function
98Private
99
100 /*
101 @date 2008/02/25
102 @auther Egtra
103 */
104 Sub init(str As Stream)
105 s = str
106 e = New Text.Detail.WindowsCodePageEncoding(CP_ACP) '暫定。
107 bufCapacity = 8192
108 mbBuf = GC_malloc_atomic(bufCapacity) As *Byte
109 bufSize = 0
110 End Sub
111
112 s As Stream
113 e As Text.Encoding
114 mbBuf As *Byte
115 bufSize As Long
116 bufCapacity As Long
117End Class
118
119End Namespace
120End Namespace
Note: See TracBrowser for help on using the repository browser.