This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathZInputStream.cs
95 lines (77 loc) · 2.2 KB
/
ZInputStream.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.Runtime.InteropServices;
using CoreFoundation;
using Foundation;
namespace InputStreamTest {
[Preserve (AllMembers = true)]
public class ZInputStream : NSInputStream {
NSStreamStatus status;
long read_length;
long total_length;
public ZInputStream (long total_length) : base ()
{
status = NSStreamStatus.NotOpen;
this.total_length = total_length;
}
public override NSStreamStatus Status {
get {
return status;
}
}
public override void Open ()
{
status = NSStreamStatus.Open;
Notify (CFStreamEventType.OpenCompleted);
}
public override void Close ()
{
status = NSStreamStatus.Closed;
}
public override bool HasBytesAvailable ()
{
return total_length > read_length;
}
public override nint Read (IntPtr buffer, nuint len)
{
int actual = Math.Min ((int) len, (int) (total_length - read_length));
byte [] bytes = new byte [actual];
for (int i = 0; i < actual; i++)
bytes [i] = (byte) 'z';
read_length += actual;
Marshal.Copy (bytes, 0, buffer, actual);
if (actual == 0)
Notify (CFStreamEventType.EndEncountered);
return actual;
}
protected override bool GetBuffer (out IntPtr buffer, out nuint len)
{
// Just call the base implemention (which will return false)
return base.GetBuffer (out buffer, out len);
}
/* protected override bool GetBuffer(out IntPtr buffer, out uint len)
{
// Just call the base implemention (which will return false)
return base.GetBuffer (out buffer, out len);
}*/
protected override bool SetCFClientFlags (CFStreamEventType inFlags, IntPtr inCallback, IntPtr inContextPtr)
{
// Just call the base implementation, which knows how to handle everything.
return base.SetCFClientFlags (inFlags, inCallback, inContextPtr);
}
bool notifying = false;
[Export ("_scheduleInCFRunLoop:forMode:")]
public void ScheduleInCFRunLoop (CFRunLoop runloop, NSString mode)
{
if (notifying)
return;
notifying = true;
Notify (CFStreamEventType.HasBytesAvailable);
notifying = false;
}
[Export ("_unscheduleFromCFRunLoop:forMode:")]
public void UnscheduleInCFRunLoop (CFRunLoop runloop, NSString mode)
{
// Nothing to do here
}
}
}