ReadBase64() public method

public ReadBase64 ( byte array, int offset, int len ) : int
array byte
offset int
len int
return int
 [MonoTODO] // FIXME: Check how expanded entity is handled here.
 public int ReadBase64(byte [] buffer, int offset, int length)
 {
     if (entity != null)
     {
         return(entity.ReadBase64(buffer, offset, length));
     }
     else
     {
         return(source.ReadBase64(buffer, offset, length));
     }
 }
Esempio n. 2
0
 [MonoTODO]         // FIXME: Check how expanded entity is handled here.
 public int ReadBase64(byte [] array, int offset, int len)
 {
     if (entity != null)
     {
         return(entity.ReadBase64(array, offset, len));
     }
     else
     {
         return(source.ReadBase64(array, offset, len));
     }
 }
Esempio n. 3
0
        public static void Load(string filePath, object options)
        {
            Byte[] buffer = new Byte[80];
            MemoryStream ms;
            BinaryFormatter bf = new BinaryFormatter();

            System.Xml.XmlTextReader reader = new XmlTextReader(filePath);

            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:

                        if (reader.HasAttributes)
                        {
                            string name = reader.Name;
                            string val = reader.GetAttribute("Value");

                            ms = new MemoryStream();

                            int count = 0;
                            do
                            {
                                count = reader.ReadBase64(buffer, 0 , buffer.Length);
                                ms.Write(buffer, 0,count);
                            }
                            while (count == buffer.Length);

                            ms.Position = 0;

                            if (val != "null")
                            {
                                try
                                {
                                    object da = bf.Deserialize(ms);

                                    Console.Write("Applying {0} : ", name);
                                    options.GetType().GetProperty(name).SetValue(options, da, null);
                                    Console.WriteLine("OK");
                                }
                                catch (System.Runtime.Serialization.SerializationException e)
                                {
                                    Console.WriteLine("FAIL: {0}",e.Message);
                                }
                            }
                        }
                        break;
                }
            }
            reader.Close();
        }
		public void ReadBase64Test2 ()
		{
			string xml = "<root/>";
			XmlTextReader xtr = new XmlTextReader (new StringReader (xml));
			xtr.Read ();
			byte [] data = new byte [1];
			xtr.ReadBase64 (data, 0, 1);
			while (!xtr.EOF)
				xtr.Read ();

			xml = "<root></root>";
			xtr = new XmlTextReader (new StringReader (xml));
			xtr.Read ();
			xtr.ReadBase64 (data, 0, 1);
			while (!xtr.EOF)
				xtr.Read ();
		}
		public void ReadBase64 ()
		{
			byte [] bytes = new byte [] {4,14,54,114,134,184,254,255};
			
			string base64 = "<root><foo>BA42coa44</foo></root>";
			XmlTextReader xtr = new XmlTextReader (base64, XmlNodeType.Document, null);
			byte [] bytes2 = new byte [10];
			xtr.Read ();	// root
			xtr.Read ();	// foo
			this.AssertNodeValues (xtr, XmlNodeType.Element, 1, false, "foo", String.Empty,
				"foo", String.Empty, String.Empty, 0);
			AssertEquals (6, xtr.ReadBase64 (bytes2, 0, 10));
			this.AssertNodeValues (xtr, XmlNodeType.EndElement, 0, false, "root", String.Empty,
				"root", String.Empty, String.Empty, 0);
			Assert (!xtr.Read ());
			AssertEquals (4, bytes2 [0]);
			AssertEquals (14, bytes2 [1]);
			AssertEquals (54, bytes2 [2]);
			AssertEquals (114, bytes2 [3]);
			AssertEquals (134, bytes2 [4]);
			AssertEquals (184, bytes2 [5]);
			AssertEquals (0, bytes2 [6]);
			xtr.Close ();

			xtr = new XmlTextReader (base64, XmlNodeType.Document, null);
			bytes2 = new byte [10];
			xtr.Read ();	// root
			xtr.Read ();	// foo
			this.AssertNodeValues (xtr, XmlNodeType.Element, 1, false, "foo", String.Empty,
				"foo", String.Empty, String.Empty, 0);

			// Read less than 4 (i.e. one Base64 block)
			AssertEquals (1, xtr.ReadBase64 (bytes2, 0, 1));
			this.AssertNodeValues (xtr, XmlNodeType.Element, 1, false, "foo", String.Empty,
				"foo", String.Empty, String.Empty, 0);
			AssertEquals (4, bytes2 [0]);

			AssertEquals (5, xtr.ReadBase64 (bytes2, 0, 10));
			this.AssertNodeValues (xtr, XmlNodeType.EndElement, 0, false, "root", String.Empty,
				"root", String.Empty, String.Empty, 0);
			Assert (!xtr.Read ());
			AssertEquals (14, bytes2 [0]);
			AssertEquals (54, bytes2 [1]);
			AssertEquals (114, bytes2 [2]);
			AssertEquals (134, bytes2 [3]);
			AssertEquals (184, bytes2 [4]);
			AssertEquals (0, bytes2 [5]);
			while (!xtr.EOF)
				xtr.Read ();
			xtr.Close ();
		}
Esempio n. 6
0
        public static bool ExportItemPost(string ServerVersion, string sItemId, string sFile)
        {
            bool bSuccess = false;
            string sResponseText = string.Empty;
            System.Net.HttpWebRequest oHttpWebRequest = null;
            EwsProxyFactory.CreateHttpWebRequest(ref oHttpWebRequest);

            // Build request body...
            string EwsRequest = TemplateEwsRequests.ExportItems;
            EwsRequest = EwsRequest.Replace("##RequestServerVersion##", ServerVersion);
            EwsRequest = EwsRequest.Replace("##ItemId##", sItemId);

            try
            {

                // Use request to do POST to EWS so we get back the data for the item to export.
                byte[] bytes = Encoding.UTF8.GetBytes(EwsRequest);
                oHttpWebRequest.ContentLength = bytes.Length;
                using (Stream requestStream = oHttpWebRequest.GetRequestStream())
                {
                    requestStream.Write(bytes, 0, bytes.Length);
                    requestStream.Flush();
                    requestStream.Close();
                }

                // Get response
                HttpWebResponse oHttpWebResponse = (HttpWebResponse)oHttpWebRequest.GetResponse();

                StreamReader oStreadReader = new StreamReader(oHttpWebResponse.GetResponseStream());
                sResponseText = oStreadReader.ReadToEnd();

                // OK?
                if (oHttpWebResponse.StatusCode == HttpStatusCode.OK)
                {
                    int BUFFER_SIZE = 1024;
                    int iReadBytes = 0;

                    XmlDocument oDoc = new XmlDocument();
                    XmlNamespaceManager namespaces = new XmlNamespaceManager(oDoc.NameTable);
                    namespaces.AddNamespace("m", "http://schemas.microsoft.com/exchange/services/2006/messages");
                    oDoc.LoadXml(sResponseText);
                    XmlNode oData = oDoc.SelectSingleNode("//m:Data", namespaces);

                     // Write base 64 encoded text Data XML string into a binary base 64 text/XML file
                    BinaryWriter oBinaryWriter = new BinaryWriter(File.Open(sFile, FileMode.Create));
                    StringReader oStringReader = new StringReader(oData.OuterXml);
                    XmlTextReader oXmlTextReader = new XmlTextReader(oStringReader);
                    oXmlTextReader.MoveToContent();
                    byte[] buffer = new byte[BUFFER_SIZE];
                    do
                    {
                        iReadBytes = oXmlTextReader.ReadBase64(buffer, 0, BUFFER_SIZE);
                        oBinaryWriter.Write(buffer, 0, iReadBytes);
                    }
                    while (iReadBytes >= BUFFER_SIZE);

                    oXmlTextReader.Close();

                    oBinaryWriter.Flush();
                    oBinaryWriter.Close();

                    bSuccess = true;
                }

            }
            finally
            {

            }

            return bSuccess;
        }
Esempio n. 7
0
		private NuGenPropertyInfoCollection DeserializeObject(XmlTextReader xmlTextReader)
		{
			Debug.Assert(xmlTextReader != null, "xmlTextReader != null");

			NuGenPropertyInfoCollection propertyInfoCollection = new NuGenPropertyInfoCollection();
			xmlTextReader.Read();

			while (!xmlTextReader.EOF)
			{
				if (xmlTextReader.IsStartElement())
				{
					this.OnDeserializing(EventArgs.Empty);

					string propertyString = xmlTextReader.Name;

					bool isKey = xmlTextReader.GetAttribute(Resources.XmlAttribute_IsKey) == Resources.XmlValue_True;
					bool isRef = xmlTextReader.MoveToAttribute(Resources.XmlAttribute_IsRef);
					bool isList = xmlTextReader.MoveToAttribute(Resources.XmlAttribute_IsList);

					NuGenPropertyInfo propertyInfo = new NuGenPropertyInfo(
						XmlConvert.DecodeName(NuGenSerializer.GetPropertyFromString(propertyString)),
						null,
						isKey,
						isRef,
						isList
						);

					if (xmlTextReader.GetAttribute(Resources.XmlAttribute_IsNull) == Resources.XmlValue_True)
					{
						propertyInfo.Value = null;
					}
					else if (xmlTextReader.GetAttribute(Resources.XmlAttribute_IsSer) != null)
					{
						string refAttrValue = xmlTextReader.GetAttribute(Resources.XmlAttribute_Ref);

						if (refAttrValue != null)
						{
							propertyInfo.ReferenceCode = int.Parse(refAttrValue, CultureInfo.InvariantCulture);
						}

						string typeAttrValue = xmlTextReader.GetAttribute(Resources.XmlAttribute_Type);

						Type typeFromAttr = NuGenSerializer.StringToType[typeAttrValue] as Type;

						if (typeFromAttr == null)
						{
							propertyInfo.Value = NuGenActivator.CreateObject(typeAttrValue);
						}
						else
						{
							propertyInfo.Value = NuGenActivator.CreateObject(typeFromAttr);
						}

						propertyInfo.IsSerializable = true;
						INuGenSerializable serializable = (INuGenSerializable)propertyInfo.Value;
						serializable.Deserialize(_converter, xmlTextReader);

						_graph.Add(serializable, propertyInfo.ReferenceCode);
					}
					else if (propertyInfo.IsReference)
					{
						propertyInfo.ReferenceCode = int.Parse(xmlTextReader.GetAttribute(Resources.XmlAttribute_IsRef), CultureInfo.InvariantCulture);
					}
					else if (propertyInfo.IsList)
					{
						propertyInfo.Count = int.Parse(xmlTextReader.GetAttribute(Resources.XmlAttribute_Count), CultureInfo.InvariantCulture);

						if ((propertyInfo.Count > 0) && !xmlTextReader.IsEmptyElement)
						{
							propertyInfo.Properties = this.DeserializeObject(xmlTextReader);
						}
					}
					else if (!propertyInfo.IsKey)
					{
						if (xmlTextReader.GetAttribute(Resources.XmlAttribute_IsImage) == Resources.XmlValue_True)
						{
							int bufferLength = int.Parse(xmlTextReader.GetAttribute(Resources.XmlAttribute_Length), CultureInfo.InvariantCulture);
							byte[] buffer = new byte[bufferLength];
							xmlTextReader.ReadBase64(buffer, 0, bufferLength);
							propertyInfo.Value = NuGenImageConverter.BytesToImage(buffer);
						}
						else if (xmlTextReader.GetAttribute(Resources.XmlAttribute_IsEmfImage) == Resources.XmlValue_True)
						{
							int bufferLength = int.Parse(xmlTextReader.GetAttribute(Resources.XmlAttribute_Length), CultureInfo.InvariantCulture);
							byte[] buffer = new byte[bufferLength];
							xmlTextReader.ReadBase64(buffer, 0, bufferLength);
							propertyInfo.Value = NuGenMetafileConverter.BytesToMetafile(buffer);
						}
						else
						{
							propertyInfo.Value = xmlTextReader.ReadString();
						}
					}
					else if (propertyInfo.IsKey)
					{
						string attrRef = xmlTextReader.GetAttribute(Resources.XmlAttribute_Ref);

						if (attrRef != null)
						{
							propertyInfo.ReferenceCode = int.Parse(attrRef, CultureInfo.InvariantCulture);
						}

						string typeAttr = xmlTextReader.GetAttribute(Resources.XmlAttribute_Type);
						Type typeFromAttr = NuGenSerializer.StringToType[typeAttr] as Type;

						if (typeFromAttr == null)
						{
							propertyInfo.Value = NuGenActivator.CreateObject(typeAttr);
						}
						else
						{
							propertyInfo.Value = NuGenActivator.CreateObject(typeFromAttr);
						}

						if (!xmlTextReader.IsEmptyElement)
						{
							propertyInfo.Properties = this.DeserializeObject(xmlTextReader);
						}
					}

					propertyInfoCollection.Add(propertyInfo);
				}
				else if (xmlTextReader.NodeType == XmlNodeType.EndElement)
				{
					return propertyInfoCollection;
				}

				xmlTextReader.Read();
			}

			return propertyInfoCollection;
		}
Esempio n. 8
0
        public static System.Drawing.Icon DeserializeIcon(XmlElement xml)
        {
            System.Drawing.Icon img = null;
            if (xml == null || xml.InnerText == "")
                return null;
            bool bDecodeBinHex = false;
            if (xml.HasAttribute("encoding") && xml.GetAttribute("encoding") == "binhex")
                bDecodeBinHex = true;
            System.IO.StringReader sr = new System.IO.StringReader(xml.OuterXml);
            System.Xml.XmlTextReader xr = new System.Xml.XmlTextReader(sr);
            System.IO.MemoryStream mem = new System.IO.MemoryStream(1024);
            // Skip <image> to data
            xr.Read();

            byte[] base64 = new byte[1024];
            int base64len = 0;
            if (bDecodeBinHex)
            {
                do
                {
                    base64len = xr.ReadBinHex(base64, 0, 1024);
                    if (base64len > 0)
                        mem.Write(base64, 0, base64len);

                } while (base64len != 0);
            }
            else
            {
                do
                {
                    base64len = xr.ReadBase64(base64, 0, 1024);
                    if (base64len > 0)
                        mem.Write(base64, 0, base64len);

                } while (base64len != 0);
            }
            mem.Position = 0;
            img = new System.Drawing.Icon(mem);

            return img;
        }
Esempio n. 9
0
        /// <summary>
        /// XML element is expected to be something like <image>Image data Base64 encoded</image>
        /// </summary>
        /// <param name="xml">Image data</param>
        /// <returns></returns>
        public static System.Drawing.Image DeserializeImage(XmlElement xml)
        {
            System.Drawing.Image img = null;
            if (xml == null || xml.InnerText == "")
                return null;

            System.IO.StringReader sr = new System.IO.StringReader(xml.OuterXml);
            System.Xml.XmlTextReader xr = new System.Xml.XmlTextReader(sr);
            System.IO.MemoryStream mem = new System.IO.MemoryStream(1024);
            // Skip <image> to data
            xr.Read();

            byte[] base64 = new byte[1024];
            int base64len = 0;
            do
            {
                base64len = xr.ReadBase64(base64, 0, 1024);
                if (base64len > 0)
                    mem.Write(base64, 0, base64len);

            } while (base64len != 0);

            img = System.Drawing.Image.FromStream(mem);

            return img;
        }