c# - 将 InputStream 和 OutputStream 转换为 System.IO.Stream - Xamarin.iOS (MonoTouch)

标签 c# ios xamarin.ios stream type-conversion

我目前正忙于使用 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/

相关文章:

ios - Base64Encoding 已弃用 : first deprecated in iOS 7. 0

ios - 我可以使用 Braintree iOS SDK 显示本地化的 Drop In UI

xamarin.ios - 对 Monotouch 中的事件、代表等感到困惑

c# - Linq 到 SQL : delete record with generic type method

c# - 指数进度报告

c# - child 不跟随 parent

ios - PFQuery 匹配查询不工作 Parse.com

c# - 如何在c#中使用for循环获取对象列表的值

c# - Json.Net 和 Monotouch 编译

c# - iPhone 应用程序 InputAccessoryView 未出现在 iPad 上