/// <summary> /// Default constructor. /// </summary> /// <param name="owner">Owner stream.</param> /// <param name="terminator">Data terminator.</param> /// <param name="storeStream">Stream where to store readed header.</param> /// <param name="maxCount">Maximum number of bytes to read. Value 0 means not limited.</param> /// <param name="exceededAction">Specifies how this method behaves when maximum line size exceeded.</param> /// <param name="callback">The AsyncCallback delegate that is executed when asynchronous operation completes.</param> /// <param name="asyncState">User-defined object that qualifies or contains information about an asynchronous operation.</param> /// <exception cref="ArgumentNullException">Is raised when <b>owner</b>,<b>terminator</b> or <b>storeStream</b> is null reference.</exception> public ReadToTerminatorAsyncOperation(SmartStream owner, string terminator, Stream storeStream, long maxCount, SizeExceededAction exceededAction, AsyncCallback callback, object asyncState) { if (owner == null) { throw new ArgumentNullException("owner"); } if (terminator == null) { throw new ArgumentNullException("terminator"); } if (storeStream == null) { throw new ArgumentNullException("storeStream"); } if (maxCount < 0) { throw new ArgumentException("Argument 'maxCount' must be >= 0."); } m_pOwner = owner; m_Terminator = terminator; m_pTerminatorBytes = Encoding.ASCII.GetBytes(terminator); m_pStoreStream = storeStream; m_MaxCount = maxCount; m_SizeExceededAction = exceededAction; m_pAsyncCallback = callback; m_pAsyncState = asyncState; m_pAsyncWaitHandle = new AutoResetEvent(false); m_pLineBuffer = new byte[Workaround.Definitions.MaxStreamLineLength]; // Start reading data. m_pOwner.BeginReadLine(m_pLineBuffer, 0, m_pLineBuffer.Length - 2, m_SizeExceededAction, ReadLine_Completed, null); }
/// <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; }
/// <summary> /// Starts period-terminated data reading. /// </summary> /// <param name="stream">Owner SmartStream.</param> /// <returns>Returns true if read line completed synchronously, false if asynchronous operation pending.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> is null reference.</exception> internal bool Start(SmartStream stream) { if (stream == null) { throw new ArgumentNullException("stream"); } m_pOwner = stream; // Clear old data, if any. m_IsCompleted = false; m_BytesStored = 0; m_LinesStored = 0; m_pException = null; m_IsCompletedSync = DoRead(); return m_IsCompletedSync; }
/// <summary> /// Default constructor. /// </summary> /// <param name="owner">Owner stream.</param> /// <param name="storeStream">Stream where to store readed data.</param> /// <param name="count">Number of bytes to read from source stream.</param> /// <param name="callback">The AsyncCallback delegate that is executed when asynchronous operation completes.</param> /// <param name="asyncState">User-defined object that qualifies or contains information about an asynchronous operation.</param> /// <exception cref="ArgumentNullException">Is raised when <b>owner</b> or <b>storeStream</b> is null reference.</exception> /// <exception cref="ArgumentException">Is raised when any of the arguments has invalid value.</exception> public ReadToStreamAsyncOperation(SmartStream owner, Stream storeStream, long count, AsyncCallback callback, object asyncState) { if (owner == null) { throw new ArgumentNullException("owner"); } if (storeStream == null) { throw new ArgumentNullException("storeStream"); } if (count < 0) { throw new ArgumentException("Argument 'count' must be >= 0."); } m_pOwner = owner; m_pStoreStream = storeStream; m_Count = count; m_pAsyncCallback = callback; m_pAsyncState = asyncState; m_pAsyncWaitHandle = new AutoResetEvent(false); if (m_Count == 0) { Completed(); } else { DoDataReading(); } }
/// <summary> /// Default constructor. /// </summary> /// <param name="owner">Owner stream.</param> /// <param name="buffer">Buffer where to store data.</param> /// <param name="offset">The location in <b>buffer</b> to begin storing the data.</param> /// <param name="maxCount">Maximum number of bytes to read.</param> /// <param name="exceededAction">Specifies how this method behaves when maximum line size exceeded.</param> /// <param name="callback">The AsyncCallback delegate that is executed when asynchronous operation completes.</param> /// <param name="asyncState">User-defined object that qualifies or contains information about an asynchronous operation.</param> /// <exception cref="ArgumentNullException">Is raised when <b>owner</b>,<b>buffer</b> is null reference.</exception> /// <exception cref="ArgumentOutOfRangeException">Is raised when any of the arguments has out of valid range.</exception> public ReadLineAsyncOperation(SmartStream owner, byte[] buffer, int offset, int maxCount, SizeExceededAction exceededAction, AsyncCallback callback, object asyncState) { if (owner == null) { throw new ArgumentNullException("owner"); } if (buffer == null) { throw new ArgumentNullException("buffer"); } if (offset < 0) { throw new ArgumentOutOfRangeException("offset", "Argument 'offset' value must be >= 0."); } if (offset > buffer.Length) { throw new ArgumentOutOfRangeException("offset", "Argument 'offset' value must be < buffer.Length."); } if (maxCount < 0) { throw new ArgumentOutOfRangeException("maxCount", "Argument 'maxCount' value must be >= 0."); } if (offset + maxCount > buffer.Length) { throw new ArgumentOutOfRangeException("maxCount", "Argument 'maxCount' is bigger than than argument 'buffer' can store."); } m_pOwner = owner; m_pBuffer = buffer; m_OffsetInBuffer = offset; m_MaxCount = maxCount; m_SizeExceededAction = exceededAction; m_pAsyncCallback = callback; m_pAsyncState = asyncState; m_pAsyncWaitHandle = new AutoResetEvent(false); DoLineReading(); }
/// <summary> /// Cleans up any resources being used. /// </summary> public void Dispose() { if (m_IsDisposed) { return; } m_IsDisposed = true; m_pOwner = null; m_pStream = null; m_pReadLineOP.Dispose(); m_pReadLineOP = null; m_pException = null; Completed = null; }
/// <summary> /// Starts reading line. /// </summary> /// <param name="async">If true then this method can complete asynchronously. If false, this method completed always syncronously.</param> /// <param name="stream">Owner SmartStream.</param> /// <returns>Returns true if read line completed synchronously, false if asynchronous operation pending.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> is null reference.</exception> internal bool Start(bool async, SmartStream stream) { if (stream == null) { throw new ArgumentNullException("stream"); } m_pOwner = stream; // Clear old data, if any. m_IsCompleted = false; m_BytesInBuffer = 0; m_LastByte = -1; m_pException = null; m_IsCompletedSync = DoLineReading(async); return m_IsCompletedSync; }
/// <summary> /// Cleans up any resources being used. /// </summary> public void Dispose() { if (m_IsDisposed) { return; } m_IsDisposed = true; m_pOwner = null; m_pBuffer = null; m_pException = null; Completed = null; }
/// <summary> /// Default constructor. /// </summary> /// <param name="owner">Owner stream.</param> /// <exception cref="ArgumentNullException">Is raised when <b>owner</b> is null reference.</exception> public BufferReadAsyncOP(SmartStream owner) { if (owner == null) { throw new ArgumentNullException("owner"); } m_pOwner = owner; }
/// <summary> /// Reads header from source <b>stream</b> and writes it to stream. /// </summary> /// <param name="stream">Stream from where to read header.</param> /// <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> public void WriteHeader(Stream stream) { if (m_IsDisposed) { throw new ObjectDisposedException(GetType().Name); } if (stream == null) { throw new ArgumentNullException("stream"); } SmartStream reader = new SmartStream(stream, false); reader.ReadHeader(this, -1, SizeExceededAction.ThrowException); }
/// <summary> /// Default constructor. /// </summary> /// <param name="stream">Source stream.</param> /// <param name="access">Specifies stream access mode.</param> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> is null reference.</exception> public QuotedPrintableStream(SmartStream stream, FileAccess access) { if (stream == null) { throw new ArgumentNullException("stream"); } m_pStream = stream; m_AccessMode = access; m_pDecodedBuffer = new byte[Workaround.Definitions.MaxStreamLineLength]; m_pEncodedBuffer = new byte[78]; line_buf = new byte[Workaround.Definitions.MaxStreamLineLength]; }