Tp.Integration.Messages.ServiceBus.Serialization.BinaryContainerTypeConverter.ConvertStreamToByteArray C# (CSharp) Method

ConvertStreamToByteArray() public method

Converts a Stream into byte[].
public ConvertStreamToByteArray ( Stream s ) : byte[]
s Stream
return byte[]
		public byte[] ConvertStreamToByteArray(Stream s)
		{
			if (s == null)
				return null;

			byte[] bytes = new byte[s.Length];
			int numBytesToRead = (int) s.Length;
			int numBytesRead = 0;
			while (numBytesToRead > 0)
			{
				// Read may return anything from 0 to numBytesToRead.
				int n = s.Read(bytes, numBytesRead, numBytesToRead);
				// The end of the Stream is reached.
				if (n == 0)
					break;
				numBytesRead += n;
				numBytesToRead -= n;
			}
			s.Close();

			return bytes;
		}

Usage Example

Ejemplo n.º 1
0
        /// <summary>
        /// Serializes binary data to a XmlNode.
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="ctorParamType"></param>
        /// <param name="parent"></param>
        protected void SerializeBinaryObject(Object obj, Type ctorParamType, XmlNode parent)
        {
            XmlElement proplist = null;
            String     val      = null;

            try
            {
                // If the objact is a Stream or can be converted to a byte[]...
                TypeConverter tc = TypeDescriptor.GetConverter(obj.GetType());
                if (tc.CanConvertTo(typeof(byte[])) || typeof(Stream).IsAssignableFrom(obj.GetType()))
                {
                    byte[] barr = null;

                    // Convert to byte[]
                    if (typeof(Stream).IsAssignableFrom(obj.GetType()))
                    {
                        // Convert a Stream to byte[]
                        var bctc = new BinaryContainerTypeConverter();
                        barr = bctc.ConvertStreamToByteArray((Stream)obj);
                    }
                    else
                    {
                        // Convert the object to a byte[]
                        barr = (byte[])tc.ConvertTo(obj, typeof(byte[]));
                    }

                    // Create a constructor node
                    proplist = parent.OwnerDocument.CreateElement(taglib.CONSTRUCTOR_TAG);
                    parent.AppendChild(proplist);

                    // Set info about the constructor type as attributes
                    SetObjectInfoAttributes("0", ctorParamType, proplist);

                    // Create a node for the binary data
                    XmlNode bindata = proplist.OwnerDocument.CreateElement(taglib.BINARY_DATA_TAG);
                    proplist.AppendChild(bindata);

                    // Set info about the binary data type as attributes (currently it's always byte[])
                    SetObjectInfoAttributes("0", typeof(byte[]), bindata);

                    // Convert the byte array to a string so it's easy to store it in XML
                    val = Convert.ToBase64String(barr, 0, barr.Length);

                    bindata.InnerText = val;
                }
            }
            catch (Exception exc)
            {
                if (!IgnoreSerialisationErrors)
                {
                    throw exc;
                }
            }
        }
All Usage Examples Of Tp.Integration.Messages.ServiceBus.Serialization.BinaryContainerTypeConverter::ConvertStreamToByteArray