'Classes/System/IO/StreamReader.ab Namespace System Namespace IO /* @brief ストリームから読み取りを行うTextReaderの実装。 @date 2008/02/25 @auther Egtra */ Class StreamReader Inherits TextReader Public /* @date 2008/02/25 @auther Egtra */ Sub StreamReader(path As String) init(New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)) End Sub /* @date 2008/02/25 @auther Egtra */ Sub StreamReader(stream As Stream) init(stream) End Sub Public /* @brief 基になるストリームを取得する @date 2008/09/02 @auther NoWest */ Function BaseStream () As Stream Return s End Function Protected /* @date 2008/02/25 @auther Egtra */ Override Sub Dispose(disposing As Boolean) If disposing Then If Not ActiveBasic.IsNothing(s) Then s.Dispose(True) End If End If s = Nothing End Sub /* @date 2008/12/27 @auther Egtra */ Override Function Underflow() As Boolean Dim nextRead As *Byte Dim readSize = s.Read(mbBuf + bufSize, 0, bufCapacity - bufSize) If readSize = 0 Then If bufSize <> 0 Then #ifdef UNCODE Buffer.Append(&hfffd As WCHAR) #else Buffer.Append("?") #endif End If Underflow = False Exit Function End If Dim mbLen = bufSize + readSize '文字全体が揃っている部分を抽出する。 Dim pNext = mbBuf Do Dim q = e.NextChar(pNext, ((mbBuf + mbLen) - pNext) As Long) If q = pNext Then Exit Do End If pNext = q Loop '文字全体が揃っている部分を変換しTextReaderのバッファに送る。 Dim wcBufCapacity = e.GetMaxCharCount(mbLen) Dim wcBuf = GC_malloc_atomic(wcBufCapacity * SizeOf (WCHAR)) As *WCHAR Dim wcLen = e.GetChars(mbBuf, (pNext - mbBuf) As Long, wcBuf, wcBufCapacity) Dim appendStr As PCTSTR Dim appendLen = GetStr(wcBuf, wcLen As SIZE_T, appendStr) Dim readerBuffer = Buffer readerBuffer.Append(appendStr, 0, appendLen) 'マルチバイトの揃っていない部分を手前に連れてくる。 bufSize = ((mbBuf + mbLen) - pNext) As Long If bufSize <> 0 Then memmove(mbBuf, mbBuf + mbLen, bufSize) End If Underflow = True End Function Private /* @date 2008/02/25 @auther Egtra */ Sub init(str As Stream) s = str e = New Text.Detail.WindowsCodePageEncoding(CP_ACP) '暫定。 bufCapacity = 8192 mbBuf = GC_malloc_atomic(bufCapacity) As *Byte bufSize = 0 End Sub s As Stream e As Text.Encoding mbBuf As *Byte bufSize As Long bufCapacity As Long End Class End Namespace End Namespace