Changeset 432 for trunk/Include/Classes/System/IO/StreamReader.ab
- Timestamp:
- Feb 27, 2008, 1:04:59 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Include/Classes/System/IO/StreamReader.ab
r426 r432 1 'Classes/System/IO/StreamReader.ab 2 1 3 Namespace System 2 4 Namespace IO 3 5 6 /* 7 @brief ストリームから読み取りを行うTextReaderの実装。 8 @date 2008/02/25 9 @auther Egtra 10 */ 4 11 Class StreamReader 5 12 Inherits TextReader 6 13 Public 14 /* 15 @date 2008/02/25 16 @auther Egtra 17 */ 7 18 Sub StreamReader(path As String) 8 19 init(New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)) 9 20 End Sub 10 21 22 /* 23 @date 2008/02/25 24 @auther Egtra 25 */ 11 26 Sub StreamReader(stream As Stream) 12 27 init(stream) 13 28 End Sub 14 29 15 Override Sub Dispose(disposing As Boolean) 16 s.Dispose(disposing) 17 size = 0 18 cur = 0 19 last = 0 20 End Sub 21 30 /* 31 @date 2008/02/25 32 @auther Egtra 33 */ 22 34 Override Function Peek() As Long 23 35 If cur = last Then … … 32 44 End Function 33 45 46 /* 47 @date 2008/02/25 48 @auther Egtra 49 */ 34 50 Override Function Read() As Long 35 51 Read = Peek() … … 37 53 End Function 38 54 39 Override Function Read(buffer As *StrChar, index As Long, count As Long) As Long 40 If buffer = 0 Then 41 ElseIf index < 0 Then 42 ElseIf count < 0 Then 55 /* 56 @date 2008/02/26 57 @auther Egtra 58 */ 59 Override Function ReadToEnd() As String 60 Dim sb = New Text.StringBuilder(65536) 61 sb.Append(buf, cur, last - cur) 62 Do 63 Dim read = Read(buf, 0, size) 64 sb.Append(buf, 0, read) 65 If read < size Then 66 ReadToEnd = sb.ToString 67 Exit Function 68 End If 69 Loop 70 End Function 71 72 Protected 73 /* 74 @date 2008/02/25 75 @auther Egtra 76 */ 77 Override Sub Dispose(disposing As Boolean) 78 If disposing Then 79 If Not ActiveBasic.IsNothing(s) Then 80 s.Dispose(True) 81 End If 43 82 End If 83 s = Nothing 84 size = 0 85 cur = 0 86 last = 0 87 End Sub 44 88 89 /* 90 @date 2008/02/25 91 @auther Egtra 92 */ 93 Override Function ReadImpl(buffer As *StrChar, index As Long, count As Long) As Long 45 94 Dim n = last - cur 46 95 If count <= n Then 47 Read = ReadFromBuffer(buffer, index, count)96 ReadImpl = ReadFromBuffer(buffer, index, count) 48 97 Exit Function 49 98 End If 50 99 Dim p = VarPtr(buffer[index]) 51 Read = ReadFromBuffer(p, 0, n)52 If Read = count Then 'バッファの中身で足りた場合100 ReadImpl = ReadFromBuffer(p, 0, n) 101 If ReadImpl = count Then 'バッファの中身で足りた場合 53 102 Exit Function 54 103 End If … … 62 111 End If 63 112 p = VarPtr(p[n]) 64 Read += n113 ReadImpl += n 65 114 count -= n 66 115 End If … … 70 119 Exit Function 71 120 End If 72 Read += ReadFromBuffer(p, 0, Math.Min(last, count)) 73 End Function 74 75 Override Function ReadLine() As String 76 Dim sb = New Text.StringBuilder(256) 77 Do 78 Dim ch = Read() 79 If ch = &h0D Then 80 If Peek() = &h0A Then 81 Read() 'CR LFの場合 82 End If 83 Exit Do 84 End If 85 Select Case ch 86 Case -1 'EOF 87 Exit Do 88 Case &h0A 'LF 89 Exit Do 90 Case &h0B 'VT 91 Exit Do 92 Case &h0C 'FF 93 Exit Do 94 Case &h0D 'CR 95 Exit Do 96 ' Case &h85 'NEL 97 ' Exit Do 98 ' Case &h2028 'LS 99 ' Exit Do 100 ' Case &h2029 'PS 101 ' Exit Do 102 End Select 103 sb.Append(ch As StrChar) 'ToDo キャスト不要にすべきというチケットを書くこと 104 Loop 105 ReadLine = sb.ToString 106 End Function 107 108 Override Function ReadToEnd() As String 121 ReadImpl += ReadFromBuffer(p, 0, Math.Min(last, count)) 109 122 End Function 110 123 111 124 Private 125 /* 126 @date 2008/02/25 127 @auther Egtra 128 */ 112 129 Sub init(str As Stream) 113 130 s = str … … 120 137 /** 121 138 @brief バッファの中身から読み取る。 139 @date 2008/02/25 140 @auther Egtra 122 141 文字数が足りなくても、元のストリームまで読みには行かない。 123 142 */ … … 132 151 cur As Long 133 152 last As Long '中身の終わり 134 buf As * Byte '暫定153 buf As *SByte '暫定 135 154 End Class 136 155
Note:
See TracChangeset
for help on using the changeset viewer.