* DeflateStream.cs: MonoTouch reverse callbacks need to be static
authorJonathan Pryor <jpryor@novell.com>
Mon, 14 Sep 2009 18:25:28 +0000 (18:25 -0000)
committerJonathan Pryor <jpryor@novell.com>
Mon, 14 Sep 2009 18:25:28 +0000 (18:25 -0000)
  methods and be annotated with [MonoPInvokeCallback].  Get things
  working with these limitations.

svn path=/trunk/mcs/; revision=141905

mcs/class/System/System.IO.Compression/ChangeLog
mcs/class/System/System.IO.Compression/DeflateStream.cs

index 47f0064f7427d647ce057fafb46e71b5de53f0a2..0a63cf70c3732498f16da68db4041985392fead7 100644 (file)
@@ -1,3 +1,9 @@
+2009-09-14 Jonathan Pryor <jpryor@novell.com>
+
+       * DeflateStream.cs: MonoTouch reverse callbacks need to be static
+         methods and be annotated with [MonoPInvokeCallback].  Get things
+         working with these limitations.
+
 2009-07-20 Gonzalo Paniagua Javier <gonzalo@novell.com>
 
        * GzipStream.cs: fixed Dispose (bool).
index 825557923c7ed20634a4e778d74286a935455c42..2e40030a0e9a644a176a217ccc29df89c67809c0 100644 (file)
@@ -50,6 +50,8 @@ namespace System.IO.Compression {
                IntPtr z_stream;
                byte [] io_buffer;
 
+               GCHandle data;
+
                public DeflateStream (Stream compressedStream, CompressionMode mode) :
                        this (compressedStream, mode, false, false)
                {
@@ -68,10 +70,11 @@ namespace System.IO.Compression {
                        if (mode != CompressionMode.Compress && mode != CompressionMode.Decompress)
                                throw new ArgumentException ("mode");
 
+                       this.data = GCHandle.Alloc (this);
                        this.base_stream = compressedStream;
                        this.feeder = (mode == CompressionMode.Compress) ? new UnmanagedReadOrWrite (UnmanagedWrite) :
                                                                           new UnmanagedReadOrWrite (UnmanagedRead);
-                       this.z_stream = CreateZStream (mode, gzip, feeder);
+                       this.z_stream = CreateZStream (mode, gzip, feeder, GCHandle.ToIntPtr (data));
                        if (z_stream == IntPtr.Zero) {
                                this.base_stream = null;
                                this.feeder = null;
@@ -101,9 +104,26 @@ namespace System.IO.Compression {
                                CheckResult (res, "Dispose");
                        }
 
+                       if (data.IsAllocated) {
+                               data.Free ();
+                               data = new GCHandle ();
+                       }
+
                        base.Dispose (disposing);
                }
 
+#if MONOTOUCH
+               [MonoPInvokeCallback (typeof (UnmanagedReadOrWrite))]
+#endif
+               static int UnmanagedRead (IntPtr buffer, int length, IntPtr data)
+               {
+                       GCHandle s = GCHandle.FromIntPtr (data);
+                       var self = s.Target as DeflateStream;
+                       if (self == null)
+                               return -1;
+                       return self.UnmanagedRead (buffer, length);
+               }
+
                int UnmanagedRead (IntPtr buffer, int length)
                {
                        int total = 0;
@@ -126,6 +146,18 @@ namespace System.IO.Compression {
                        return total;
                }
 
+#if MONOTOUCH
+               [MonoPInvokeCallback (typeof (UnmanagedReadOrWrite))]
+#endif
+               static int UnmanagedWrite (IntPtr buffer, int length, IntPtr data)
+               {
+                       GCHandle s = GCHandle.FromIntPtr (data);
+                       var self = s.Target as DeflateStream;
+                       if (self == null)
+                               return -1;
+                       return self.UnmanagedWrite (buffer, length);
+               }
+
                int UnmanagedWrite (IntPtr buffer, int length)
                {
                        int total = 0;
@@ -380,7 +412,7 @@ namespace System.IO.Compression {
 #endif
 
                [DllImport (LIBNAME)]
-               static extern IntPtr CreateZStream (CompressionMode compress, bool gzip, UnmanagedReadOrWrite feeder);
+               static extern IntPtr CreateZStream (CompressionMode compress, bool gzip, UnmanagedReadOrWrite feeder, IntPtr data);
 
                [DllImport (LIBNAME)]
                static extern int CloseZStream (IntPtr stream);