Reputation: 211
I am currently busy developing an iOS app using MonoTouch.
When connecting to an External Accessory and establishing a EASession, I need to pass the NSInputStream and NSOutputStream to another method expexting System.IO.Stream for both input and output streams.
I am not sure how to proceed with this as I am using some C# libraries that are written to be platform independent and therefore I cannot change the method to expect NSInputStream/NSOutputStream.
What is the best way to convert these streams to System.IO.Stream?
Thanks
Upvotes: 2
Views: 1378
Reputation: 211
This is how I got it working properly. If someone has any suggestions please let me know.
The InputStream is pretty straight forward but the OutputStream is a little different. The code below will allow you to call both Write() end WriteByte() methods. I've left in the Console.WriteLine() for people to see how this works.
InputStream:
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 ();
}
}
}
OutputStream:
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 ();
}
}
}
Upvotes: 1
Reputation: 19335
Currently there is no built-in way to convert NSInputStream/NSOutputStream to a System.IO.Stream, but you can easily write your own System.IO.Stream wrapper, something like this:
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 ();
}
}
}
Upvotes: 2