我目前正忙于使用 MonoTouch 开发 iOS 应用程序。
当连接到外部附件并建立 EASession 时,我需要将 NSInputStream 和 NSOutputStream 传递给另一个方法,为输入和输出流扩展 System.IO.Stream。
我不确定如何继续此操作,因为我使用的是一些独立于平台编写的 C# 库,因此我无法更改该方法以期望 NSInputStream/NSOutputStream。
将这些流转换为 System.IO.Stream 的最佳方法是什么?
谢谢
最佳答案
目前没有内置方法可以将 NSInputStream/NSOutputStream 转换为 System.IO.Stream,但您可以轻松编写自己的 System.IO.Stream 包装器,如下所示:
class MyInputStream : System.IO.Stream
{
NSInputStream input_stream;
public MyInputStream (NSInputStream str)
{
input_stream = str;
}
public override void Flush ()
{
throw new NotSupportedException ();
}
public override int Read (byte[] buffer, int offset, int count)
{
if (offset != 0)
throw new NotSupportedException ();
return input_stream.Read (buffer, count);
}
public override long Seek (long offset, System.IO.SeekOrigin origin)
{
throw new NotSupportedException ();
}
public override void SetLength (long value)
{
throw new NotSupportedException ();
}
public override void Write (byte[] buffer, int offset, int count)
{
throw new NotSupportedException ();
}
public override bool CanRead {
get {
return true;
}
}
public override bool CanSeek {
get {
return false;
}
}
public override bool CanWrite {
get {
return false;
}
}
public override long Length {
get {
throw new NotSupportedException ();
}
}
public override long Position {
get {
throw new NotSupportedException ();
}
set {
throw new NotSupportedException ();
}
}
}
关于c# - 将 InputStream 和 OutputStream 转换为 System.IO.Stream - Xamarin.iOS (MonoTouch),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16187329/