[mapguide-commits] r7057 - in branches/maestro-4.0.x: . OSGeo.MapGuide.MaestroAPI.Local OSGeo.MapGuide.MaestroAPI.Native

svn_mapguide at osgeo.org svn_mapguide at osgeo.org
Mon Oct 1 06:37:48 PDT 2012


Author: jng
Date: 2012-10-01 06:37:48 -0700 (Mon, 01 Oct 2012)
New Revision: 7057

Modified:
   branches/maestro-4.0.x/
   branches/maestro-4.0.x/OSGeo.MapGuide.MaestroAPI.Local/LocalConnection.cs
   branches/maestro-4.0.x/OSGeo.MapGuide.MaestroAPI.Native/LocalNativeConnection.cs
Log:
Backport r7056 to 4.0.x


Property changes on: branches/maestro-4.0.x
___________________________________________________________________
Modified: svn:mergeinfo
   - /trunk/Tools/Maestro:6490-6494,6923-6924,6926,6928,7026,7034
   + /trunk/Tools/Maestro:6490-6494,6923-6924,6926,6928,7026,7034,7056

Modified: branches/maestro-4.0.x/OSGeo.MapGuide.MaestroAPI.Local/LocalConnection.cs
===================================================================
--- branches/maestro-4.0.x/OSGeo.MapGuide.MaestroAPI.Local/LocalConnection.cs	2012-10-01 13:12:08 UTC (rev 7056)
+++ branches/maestro-4.0.x/OSGeo.MapGuide.MaestroAPI.Local/LocalConnection.cs	2012-10-01 13:37:48 UTC (rev 7057)
@@ -307,18 +307,50 @@
             return result;
         }
 
+        const int MAX_INPUT_STREAM_SIZE_MB = 30;
+
         public override void SetResourceData(string resourceid, string dataname, ResourceDataType datatype, System.IO.Stream stream, Utility.StreamCopyProgressDelegate callback)
         {
-            byte[] data = Utility.StreamAsArray(stream);
-            if (callback != null)
-                callback(0, data.Length, data.Length);
             var res = GetResourceService();
-            MgByteSource source = new MgByteSource(data, data.Length);
-            MgByteReader reader = source.GetReader();
-            res.SetResourceData(new MgResourceIdentifier(resourceid), dataname, datatype.ToString(), reader);
-            LogMethodCall("MgResourceService::SetResourceData", true, resourceid, dataname, datatype.ToString(), "MgByteReader");
-            if (callback != null)
-                callback(data.Length, 0, data.Length);
+            MgByteReader reader = null;
+            string tmpPath = null;
+            //If stream is under our hard-coded limit (and it's seekable, which is how we're able to get that number), use the
+            //overload of MgByteSource that accepts a byte[]. Otherwise dump the stream to a temp file and use the
+            //file name overload (otherwise if our input stream happens to be several GBs, we run risk of
+            //System.OutOfMemoryExceptions being thrown back at us)
+            if (stream.CanSeek && stream.Length < (MAX_INPUT_STREAM_SIZE_MB * 1024 * 1024))
+            {
+                byte[] data = Utility.StreamAsArray(stream);
+                MgByteSource source = new MgByteSource(data, data.Length);
+                reader = source.GetReader();
+            }
+            else
+            {
+                tmpPath = Path.GetTempFileName();
+                using (FileStream fs = File.OpenWrite(tmpPath))
+                {
+                    MaestroAPI.Utility.CopyStream(stream, fs, false);
+                }
+                MgByteSource source = new MgByteSource(tmpPath);
+                reader = source.GetReader();
+            }
+            try
+            {
+                res.SetResourceData(new MgResourceIdentifier(resourceid), dataname, datatype.ToString(), reader);
+                LogMethodCall("MgResourceService::SetResourceData", true, resourceid, dataname, datatype.ToString(), "MgByteReader");
+            }
+            finally
+            {
+                if (!string.IsNullOrEmpty(tmpPath) && File.Exists(tmpPath))
+                {
+                    //Be a responsible citizen and clean up our temp files when done
+                    try
+                    {
+                        File.Delete(tmpPath);
+                    }
+                    catch { }
+                }
+            }
         }
 
         public override void UploadPackage(string filename, Utility.StreamCopyProgressDelegate callback)

Modified: branches/maestro-4.0.x/OSGeo.MapGuide.MaestroAPI.Native/LocalNativeConnection.cs
===================================================================
--- branches/maestro-4.0.x/OSGeo.MapGuide.MaestroAPI.Native/LocalNativeConnection.cs	2012-10-01 13:12:08 UTC (rev 7056)
+++ branches/maestro-4.0.x/OSGeo.MapGuide.MaestroAPI.Native/LocalNativeConnection.cs	2012-10-01 13:37:48 UTC (rev 7057)
@@ -944,17 +944,50 @@
             return this;
         }
 
+        const int MAX_INPUT_STREAM_SIZE_MB = 30;
+
         public override void SetResourceData(string resourceid, string dataname, ResourceDataType datatype, Stream stream, OSGeo.MapGuide.MaestroAPI.Utility.StreamCopyProgressDelegate callback)
         {
-            byte[] data = Utility.StreamAsArray(stream);
-            if (callback != null)
-                callback(0, data.Length, data.Length);
             MgResourceService res = this.Connection.CreateService(MgServiceType.ResourceService) as MgResourceService;
-            MgByteReader reader = new MgByteReader(data, data.Length, "binary/octet-stream");
-            res.SetResourceData(new MgResourceIdentifier(resourceid), dataname, datatype.ToString(), reader);
-            LogMethodCall("MgResourceService::SetResourceData", true, resourceid, dataname, datatype.ToString(), "MgByteReader");
-            if (callback != null)
-                callback(data.Length, 0, data.Length);
+            MgByteReader reader = null;
+            string tmpPath = null;
+            //If stream is under our hard-coded limit (and it's seekable, which is how we're able to get that number), use the
+            //overload of MgByteSource that accepts a byte[]. Otherwise dump the stream to a temp file and use the
+            //file name overload (otherwise if our input stream happens to be several GBs, we run risk of
+            //System.OutOfMemoryExceptions being thrown back at us)
+            if (stream.CanSeek && stream.Length < (MAX_INPUT_STREAM_SIZE_MB * 1024 * 1024))
+            {
+                byte[] data = Utility.StreamAsArray(stream);
+                MgByteSource source = new MgByteSource(data, data.Length);
+                reader = source.GetReader();
+            }
+            else
+            {
+                tmpPath = Path.GetTempFileName();
+                using (FileStream fs = File.OpenWrite(tmpPath))
+                {
+                    MaestroAPI.Utility.CopyStream(stream, fs, false);
+                }
+                MgByteSource source = new MgByteSource(tmpPath);
+                reader = source.GetReader();
+            }
+            try
+            {
+                res.SetResourceData(new MgResourceIdentifier(resourceid), dataname, datatype.ToString(), reader);
+                LogMethodCall("MgResourceService::SetResourceData", true, resourceid, dataname, datatype.ToString(), "MgByteReader");
+            }
+            finally
+            {
+                if (!string.IsNullOrEmpty(tmpPath) && File.Exists(tmpPath))
+                {
+                    //Be a responsible citizen and clean up our temp files when done
+                    try
+                    {
+                        File.Delete(tmpPath);
+                    }
+                    catch { }
+                }
+            }
         }
 
         public override void UploadPackage(string filename, OSGeo.MapGuide.MaestroAPI.Utility.StreamCopyProgressDelegate callback)



More information about the mapguide-commits mailing list