示例#1
0
        /// <summary>
        /// Reads all data from the source <b>stream</b> and writes it to stream. Period handling and period terminator is added as required.
        /// </summary>
        /// <param name="stream">Source stream which data to write to stream.</param>
        /// <returns>Returns number of bytes written to source stream.</returns>
        /// <exception cref="ObjectDisposedException">Is raised when this object is disposed and this method is accessed.</exception>
        /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> is null.</exception>
        /// <exception cref="LineSizeExceededException">Is raised when <b>stream</b> has too big line.</exception>        
        public long WritePeriodTerminated(Stream stream)
        {
            if (m_IsDisposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            // We need to read lines, do period handling and write them to stream.
            long totalWritten = 0;
            byte[] buffer = new byte[m_BufferSize];
            ReadLineAsyncOP readLineOP = new ReadLineAsyncOP(buffer, SizeExceededAction.ThrowException);
            SmartStream reader = new SmartStream(stream, false);
            while (true)
            {
                reader.ReadLine(readLineOP, false);
                if (readLineOP.Error != null)
                {
                    throw readLineOP.Error;
                }
                // We reached end of stream, no more data.
                if (readLineOP.BytesInBuffer == 0)
                {
                    break;
                }

                // Period handling. If line starts with period(.), additional period is added.
                if (readLineOP.LineBytesInBuffer > 0 && buffer[0] == '.')
                {
                    // Add additional period.
                    Write(new[] {(byte) '.'}, 0, 1);
                    totalWritten++;
                }

                // Write line to source stream.
                Write(buffer, 0, readLineOP.BytesInBuffer);
                totalWritten += readLineOP.BytesInBuffer;
            }

            // Write period terminator.
            WriteLine(".");

            Flush();

            return totalWritten;
        }