private Stream OpenInputStream_NoLock() { string fp = DataFileFullPath; if (mOpenOutputStream != null) { #if DEBUG Debugger.Break(); #endif throw new exception.OutputStreamOpenException( "Cannot open an input Stream while an output Stream is open: " + fp); } FileStream inputFS; CheckDataFile(); try { inputFS = new FileStream(fp, FileMode.Open, FileAccess.Read, FileShare.Read); } catch (Exception e) { #if DEBUG Debugger.Break(); #endif throw new exception.OperationNotValidException( String.Format("Could not open file {0}", fp), e); } CloseNotifyingStream res = new CloseNotifyingStream(inputFS); res.StreamClosed += InputStreamClosed_StreamClosed; mOpenInputStreams.Add(res); return(res); }
private void OutputStream_StreamClosed(object sender, EventArgs e) { if (ReferenceEquals(sender, mOpenOutputStream)) { lock (m_lock) { mOpenOutputStream.StreamClosed -= new EventHandler(OutputStream_StreamClosed); mOpenOutputStream = null; } } }
private void InputStreamClosed_StreamClosed(object sender, EventArgs e) { CloseNotifyingStream cnStm = sender as CloseNotifyingStream; if (cnStm != null) { lock (m_lock) { cnStm.StreamClosed -= InputStreamClosed_StreamClosed; if (mOpenInputStreams.Contains(cnStm)) { mOpenInputStreams.Remove(cnStm); } } } }
/// <summary> /// Gets an output <see cref="Stream"/> providing write access to the <see cref="FileDataProvider"/> /// </summary> /// <returns>The ouput stream</returns> /// <exception cref="exception.DataMissingException"> /// Thrown if the data stored in the <see cref="DataProvider"/> is missing from the underlying storage mechanism /// </exception> /// <exception cref="exception.InputStreamsOpenException"> /// Thrown if another output <see cref="Stream"/> from the data provider is already/still open /// </exception> /// <exception cref="exception.OutputStreamOpenException"> /// Thrown if another output <see cref="Stream"/> from the data provider is already/still open /// </exception> /// <exception cref="exception.OperationNotValidException"> /// Thrown if the underlying data file could not be opened in write-mode - see inner <see cref="Exception"/> for datails of cause /// </exception> public override Stream OpenOutputStream() { lock (m_lock) { string fp = DataFileFullPath; if (mOpenOutputStream != null) { #if DEBUG Debugger.Break(); #endif throw new exception.OutputStreamOpenException( "Cannot open an output Stream while another output Stream is already open: " + fp); } if (mOpenInputStreams.Count > 0) { #if DEBUG Debugger.Break(); #endif throw new exception.InputStreamsOpenException( "Cannot open an output Stream while one or more input Streams are open: " + fp); } CheckDataFile(); FileStream outputFS; try { outputFS = new FileStream(fp, FileMode.Open, FileAccess.Write, FileShare.None); } catch (Exception e) { throw new exception.OperationNotValidException( String.Format("Could not open file {0}", fp), e); } mOpenOutputStream = new CloseNotifyingStream(outputFS); mOpenOutputStream.StreamClosed += OutputStream_StreamClosed; return(mOpenOutputStream); } }