public override void Write(BytePtr ptr, UInt32 length) { try { if (sync) { Monitor.Enter(this); } FlushHandler(buffer, next.DiffWithSmallerNumber(buffer)); next = buffer; FlushHandler(ptr, length); } finally { if (sync) { Monitor.Exit(this); } } }
protected BytePtr next; // Assumption: buffer <= next <= bufferLimit public BufferedSink(BytePtr buffer, BytePtr bufferLimit, Boolean flushAtWriteLine, Encoder defaultEncoder, FlushHandler flushHandler, Boolean sync) : base(flushAtWriteLine, defaultEncoder) { if (buffer >= bufferLimit) { throw new ArgumentException(String.Format("buffer {0} must be < bufferLimit {1}", buffer, bufferLimit)); } if (bufferLimit.DiffWithSmallerNumber(buffer) < MinimumBufferSize) { throw new ArgumentException(String.Format("buffer length {0} is too small (minimum is {1})", bufferLimit.DiffWithSmallerNumber(buffer), MinimumBufferSize)); } if (flushHandler == null) { throw new ArgumentNullException("flushHandler"); } this.buffer = buffer; this.bufferLimit = bufferLimit; this.sync = sync; this.FlushHandler = flushHandler; this.next = buffer; }
/// <summary> /// Note: if you would like to use this in a multithreaded application, you should /// lock the object while using the buffer if you want to prevent another thread from using it /// Example: /// lock(buffer) /// { /// BufferSice slice = buffer.GetAvailableBuffer(); /// // fill in buffer /// // ... need more buffer? /// buffer.Flush(); /// slice = buffer.GetAvailableBuffer(); /// // ... /// } /// </summary> /// <returns></returns> public override BufferSlice GetAvailableBuffer() { return(new BufferSlice(next, bufferLimit.DiffWithSmallerNumber(next))); }