2

我目前正忙于使用 MonoTouch 开发 iOS 应用程序。

连接到外部附件并建立 EASession 时,我需要将 NSInputStream 和 NSOutputStream 传递给另一个方法,用于输入和输出流的 System.IO.Stream。

我不确定如何进行此操作,因为我正在使用一些编写为独立于平台的 C# 库,因此我无法将方法更改为期望 NSInputStream/NSOutputStream。

将这些流转换为 System.IO.Stream 的最佳方法是什么?

谢谢

4

2 回答 2

2

目前没有将 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 ();
        }
    }
}
于 2013-04-24T11:16:34.550 回答
1

这就是我让它正常工作的方式。如果有人有任何建议,请告诉我。

InputStream 非常简单,但 OutputStream 有点不同。下面的代码将允许您调用两个 Write() 结束 WriteByte() 方法。我已经离开 Console.WriteLine() 让人们看看它是如何工作的。

输入流:

class CustomInputStream : System.IO.Stream
{

    NSInputStream input_stream;

    public CustomInputStream (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, (uint)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 ();
        }
    }

}

输出流:

class CustomOutputStream : System.IO.Stream
{

    NSOutputStream output_stream;
    List<byte> buffer;

    public CustomOutputStream (NSOutputStream str)
    {
        output_stream = str;
        buffer = new List<byte>();
    }

    public override void Flush ()
    {
        Write(buffer.ToArray(),0,buffer.Count);
        buffer.Clear();
    }

    public override int Read (byte[] buffer, int offset, int count)
    {       
            throw new NotSupportedException ();
    }

    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)
    {
        if (offset != 0)
            throw new NotSupportedException ();

        int sent = 0;
        var stillToSend = new byte[buffer.Length-offset];
        buffer.CopyTo(stillToSend, offset);

        Console.WriteLine("out while begin (buffer: {0})",buffer.LongLength);   
        while (count > 0)
        {       
            if (output_stream.HasSpaceAvailable()) 
            {   
                Console.WriteLine("in while has space");
                var bytesWritten = output_stream.Write(buffer, (uint)count);
                if (bytesWritten == -1)
                {
                    Console.WriteLine(@"write error");
                    break;
                }
                else if (bytesWritten > 0)
                {
                    Console.WriteLine("Bytes written: "+ bytesWritten.ToString());
                    count -= bytesWritten;
                    if (0 == count)
                        break;

                    var temp = new List<byte>();
                    for (var i=bytesWritten; i<stillToSend.Length; i++)
                    {
                        temp.Add(stillToSend[i]);
                    }
                    stillToSend = temp.ToArray();
                }
            }
            else 
            {
                Console.WriteLine("in while has NO space");
            }
        }


    }
    public override void WriteByte (byte value)
    {

        buffer.Add(value);

    }

    public override bool CanRead {
        get {
            return false;

        }
    }


    public override bool CanSeek {
        get {
            return false;
        }
    }

    public override bool CanWrite {
        get {
            return true;
        }
    }

    public override long Length {
        get {
            throw new NotSupportedException ();
        }
    }

    public override long Position {
        get {
            throw new NotSupportedException ();
        }
        set {
            throw new NotSupportedException ();
        }
    }

}
于 2013-06-11T09:46:04.160 回答