예제 #1
0
        /// <summary>
        /// Writes the value of the current instance of the class to the output stream.
        /// </summary>
        /// <param name="output">The output stream.</param>
        /// <param name="bufferSize">Buffer size.</param>
        /// <param name="outputStatus">To parameter is passed information about the state of the writes to stream.</param>
        public void WriteToStream(Stream output, int bufferSize, StreamWriteEventArgs outputStatus)
        {
            if (this.Value == null)
            {
                return;
            }

            if (output == null)
            {
                throw new ArgumentNullException("stream");
            }

            if (bufferSize <= 0)
            {
                throw new ArgumentOutOfRangeException("The value of the bufferSize must be greater than zero.");
            }

            if (outputStatus == null)
            {
                outputStatus = new StreamWriteEventArgs();
            }

            if (this.Value.GetType() == typeof(byte[]))
            {
                output.Write((byte[])this.Value, 0, ((byte[])this.Value).Length);
                outputStatus.BytesWritten = ((byte[])this.Value).Length;
            }
            else if (this.Value.GetType() == typeof(Stream) || this.Value.GetType().IsSubclassOf(typeof(Stream)))
            {
                if (((Stream)this.Value).CanSeek)
                {
                    ((Stream)this.Value).Position = 0;
                }

                using (BinaryReader br = new BinaryReader((Stream)this.Value))
                {
                    int    bytesRead = 0;
                    byte[] buffer    = new byte[bufferSize];

                    while ((bytesRead = br.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        output.Write(buffer, 0, bytesRead);
                        outputStatus.BytesWritten = bytesRead;
                    }
                }
            }
            else
            {
                var buffer = Encoding.Default.GetBytes(this.Value.ToString());
                output.Write(buffer, 0, buffer.Length);
                outputStatus.BytesWritten = buffer.Length;
            }
        }
예제 #2
0
 /// <summary>
 /// Writes the value of the current instance of the class to the output stream.
 /// </summary>
 /// <param name="output">The output stream.</param>
 /// <param name="outputStatus">To parameter is passed information about the state of the writes to stream.</param>
 public void WriteToStream(Stream output, StreamWriteEventArgs outputStatus)
 {
     this.WriteToStream(output, 4096, outputStatus);
 }
예제 #3
0
 /// <summary>
 /// Writes the value of the current instance of the class to the output stream.
 /// </summary>
 /// <param name="output">The output stream.</param>
 /// <param name="bufferSize">Buffer size.</param>
 /// <param name="outputStatus">To parameter is passed information about the state of the writes to stream.</param>
 public void WriteToStream(Stream output, int bufferSize, StreamWriteEventArgs outputStatus)
 {
     this.WriteToStream(output, bufferSize, outputStatus, null, Encoding.Default);
 }
예제 #4
0
        /// <summary>
        /// Writes the value of the current instance of the class to the output stream.
        /// </summary>
        /// <param name="output">The output stream.</param>
        /// <param name="bufferSize">Buffer size.</param>
        /// <param name="outputStatus">To parameter is passed information about the state of the writes to stream.</param>
        /// <param name="contentType">The type of content.</param>
        /// <param name="codePage">The character encoding.</param>
        public void WriteToStream(Stream output, int bufferSize, StreamWriteEventArgs outputStatus, string contentType, Encoding codePage)
        {
            if (this.Value == null)
            {
                return;
            }

            if (output == null)
            {
                throw new ArgumentNullException("stream");
            }

            if (bufferSize <= 0)
            {
                throw new ArgumentOutOfRangeException("The value of the bufferSize must be greater than zero.");
            }

            if (outputStatus == null)
            {
                outputStatus = new StreamWriteEventArgs();
            }

            if (this.Value.GetType() == typeof(byte[]))
            {
                output.Write((byte[])this.Value, 0, ((byte[])this.Value).Length);
                outputStatus.BytesWritten = ((byte[])this.Value).Length;
            }
            else if (this.Value.GetType() == typeof(Stream) || this.Value.GetType().IsSubclassOf(typeof(Stream)))
            {
                if (((Stream)this.Value).CanSeek)
                {
                    ((Stream)this.Value).Position = 0;
                }

                using (BinaryReader br = new BinaryReader((Stream)this.Value))
                {
                    int    bytesRead = 0;
                    byte[] buffer    = new byte[bufferSize];

                    while ((bytesRead = br.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        output.Write(buffer, 0, bytesRead);
                        outputStatus.BytesWritten = bytesRead;
                    }
                }
            }

            /*else if (this.Value.GetType() == typeof(string) || this.Value.GetType() == typeof(char) || this.Value.GetType() == typeof(StringBuilder))
             * {
             * var buffer = codePage.GetBytes(this.Value.ToString());
             * output.Write(buffer, 0, buffer.Length);
             * outputStatus.BytesWritten = buffer.Length;
             * }*/
            else
            {
                string dataToWrite = "";

                if (contentType == null)
                {
                    contentType = "";
                }

                contentType = contentType.ToLower();

                if (contentType.Contains("json"))
                {
                    dataToWrite = new System.Web.Script.Serialization.JavaScriptSerializer()
                    {
                        MaxJsonLength  = Int32.MaxValue,
                        RecursionLimit = Int32.MaxValue
                    }.Serialize(this.Value);
                }
                else if (contentType.Contains("xml"))
                {
                    using (var m = new MemoryStream())
                    {
                        new System.Xml.Serialization.XmlSerializer(this.Value.GetType()).Serialize(m, this.Value);
                        dataToWrite = codePage.GetString(m.ToArray());
                    }
                }
                else
                {
                    dataToWrite = this.Value.ToString();
                }

                var buffer = codePage.GetBytes(dataToWrite);
                output.Write(buffer, 0, buffer.Length);
                outputStatus.BytesWritten = buffer.Length;
            }
        }
예제 #5
0
 /// <summary>
 /// Writes the value of the current instance of the class to the output stream.
 /// </summary>
 /// <param name="output">The output stream.</param>
 /// <param name="outputStatus">To parameter is passed information about the state of the writes to stream.</param>
 public void WriteToStream(Stream output, StreamWriteEventArgs outputStatus)
 {
     this.WriteToStream(output, 4096, outputStatus, null, Encoding.Default);
 }