내 앱에서 API 끝점에 대한 맞춤 스트림을 만드는 중입니다. 스트림에는 들어 가지 않으려는 사용자 지정 논리가 있어야하지만 기본 제공 스트림 클래스를 사용할 수 없다고 말하는 것으로 충분합니다.System.IO.StreamReader가 내 사용자 지정 스트림에서 읽을 수없는 이유는 무엇입니까?
나는 읽기 전용 스트림 (System.IO.Stream에서 상속)를 구현하기 위해 필요한 최소한의를했고, 나는 System.IO.BinaryReader 클래스 내 스트림에서 읽을 수 있다는 것을 확인했습니다
Dim reader As New System.IO.BinaryReader(GenerateStream(business, logic))
Dim enc As New System.Text.ASCIIEncoding
Dim contents As String = enc.GetString(reader.ReadBytes(CType(reader.BaseStream.Length, Int32)))
문자열 "내용"에는 전체 스트림에 대한 올바른 문자열이 들어 있습니다.
그러나, 나는 System.IO.StreamReader 클래스의 사용을 허용 할 수 싶습니다 :
Dim reader As New System.IO.StreamReader(GenerateStream(business, logic), System.Text.Encoding.ASCII)
Dim contents As String = reader.ReadToEnd
하지만 어떤 이유로 ReadToEnd는 항상 빈 문자열을 반환합니다.
아이디어가 있으십니까?
여기 스트림의 :
Public Overrides ReadOnly Property CanRead() As Boolean
Get
Return True
End Get
End Property
Public Overrides ReadOnly Property CanSeek() As Boolean
Get
Return False
End Get
End Property
Public Overrides ReadOnly Property CanWrite() As Boolean
Get
Return False
End Get
End Property
Public Overrides Sub Flush()
'this method intentionally left blank'
End Sub
Public Overrides ReadOnly Property Length() As Long
Get
Return 'some business logic'
End Get
End Property
Public Overrides Property Position() As Long
Get
Return bytePosition
End Get
Set(ByVal value As Long)
Throw New System.NotSupportedException
End Set
End Property
Public Overrides Function Read(ByVal buffer() As Byte, ByVal offset As Integer, ByVal count As Integer) As Integer
'I return 0 on an end of stream, otherwise the # of bytes successfully read.'
End Function
Public Overrides Function Seek(ByVal offset As Long, ByVal origin As System.IO.SeekOrigin) As Long
Throw New System.NotSupportedException
End Function
Public Overrides Sub SetLength(ByVal value As Long)
Throw New System.NotSupportedException()
End Sub
Public Overrides Sub Write(ByVal buffer() As Byte, ByVal offset As Integer, ByVal count As Integer)
Throw New System.NotSupportedException()
End Sub
데이터는 문자입니다 (이 경우 ascii로 인코딩 됨). BinaryReader를 사용하여 스트림의 끝을 지나서 읽지 않았지만 BinaryReader를 사용하여/to/end를 읽을 수 있고 올바른 출력을 얻을 수 있는지 확인했습니다. 아마도 문제를 찾는 것으로 구현하는 경우 문제가 사라질 수 있습니다. 나는 "ReadToEnd"에 대한 설명을 놓쳤다. 문제가 해결되면 문제가 해결됩니다. ReadToEnd의 BinaryReader 구현이 질문에 표시됩니다. –
문제는 내 읽기 방법에있었습니다./current/read가 스트림의 끝에 도달하면 내 read 메소드는 0을 리턴합니다. 빈 스트림을 읽는 경우에만 0을 반환합니다. –
아니요, 찾는 중 또는하지 않는 것이 문제가되지 않습니다. 알아 낸 것처럼 인터페이스 구현이 정확해야합니다. 모든 관련 코드를 보지 않고 이러한 오류를 찾는 것을 돕기는 어렵습니다 ...;) – Abel