Buffered InputStream
namespace SRTSolutions.Elevate.IO
{
using global::System;
using global::System.Linq;
using global::System.IO;
/// <summary>
/// A Stream implementation that buffers an input stream.
/// Like System.IO.BufferedStream, except that it works
/// more like you'd expect.
/// </summary>
public class BufferedInputStream : Stream
{
private int bufferSize;
private Stream inputStream;
private byte[] buffer;
private long bufferStartPosition;
private long bufferStopPosition;
private long length;
/// <summary>
/// Initializes a new instance of the StreamWithBuffering class.
/// </summary>
/// <param name="inputStream">The input stream.</param>
/// <param name="bufferSize">Size of the buffer.</param>
public BufferedInputStream(Stream inputStream, int bufferSize)
{
if (!inputStream.CanRead)
throw new ArgumentException("inputStream cannot be read only.", "inputStream");
this.bufferSize = bufferSize;
this.inputStream = inputStream;
this.length = inputStream.Length;
this.buffer = new byte[bufferSize];
this.BufferAt(0);
}
private void BufferAt(long position)
{
this.bufferStartPosition = position;
this.bufferStopPosition = Math.Min(this.bufferStartPosition + this.bufferSize, this.length);
if (this.inputStream.Position != position)
{
this.inputStream.Position = position;
}
this.inputStream.Read(buffer, 0, this.bufferSize);
}
/// <summary>
/// When overridden in a derived class, gets a value indicating whether the current stream supports reading.
/// </summary>
/// <value></value>
/// <returns>true if the stream supports reading; otherwise, false.
/// </returns>
public override bool CanRead
{
get { return this.inputStream.CanRead; }
}
/// <summary>
/// When overridden in a derived class, gets a value indicating whether the current stream supports seeking.
/// </summary>
/// <value></value>
/// <returns>true if the stream supports seeking; otherwise, false.
/// </returns>
public override bool CanSeek
{
get { return this.inputStream.CanSeek; }
}
/// <summary>
/// When overridden in a derived class, gets a value indicating whether the current stream supports writing.
/// </summary>
/// <value></value>
/// <returns>true if the stream supports writing; otherwise, false.
/// </returns>
public override bool CanWrite
{
get { return false; }
}
/// <summary>
/// When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device.
/// </summary>
/// <exception cref="T:System.IO.IOException">
/// An I/O error occurs.
/// </exception>
public override void Flush()
{
throw new NotImplementedException();
}
/// <summary>
/// When overridden in a derived class, gets the length in bytes of the stream.
/// </summary>
/// <value></value>
/// <returns>
/// A long value representing the length of the stream in bytes.
/// </returns>
/// <exception cref="T:System.NotSupportedException">
/// A class derived from Stream does not support seeking.
/// </exception>
/// <exception cref="T:System.ObjectDisposedException">
/// Methods were called after the stream was closed.
/// </exception>
public override long Length
{
get { return this.length; }
}
private long position = 0;
/// <summary>
/// When overridden in a derived class, gets or sets the position within the current stream.
/// </summary>
/// <value></value>
/// <returns>
/// The current position within the stream.
/// </returns>
/// <exception cref="T:System.IO.IOException">
/// An I/O error occurs.
/// </exception>
/// <exception cref="T:System.NotSupportedException">
/// The stream does not support seeking.
/// </exception>
/// <exception cref="T:System.ObjectDisposedException">
/// Methods were called after the stream was closed.
/// </exception>
public override long Position
{
get { return position; }
set
{
if ((value >= this.bufferStopPosition || value < this.bufferStartPosition) && value != this.length)
{
this.BufferAt(value);
}
position = value;
}
}
/// <summary>
/// When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
/// </summary>
/// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between <paramref name="offset"/> and (<paramref name="offset"/> + <paramref name="count"/> - 1) replaced by the bytes read from the current source.</param>
/// <param name="offset">The zero-based byte offset in <paramref name="buffer"/> at which to begin storing the data read from the current stream.</param>
/// <param name="count">The maximum number of bytes to be read from the current stream.</param>
/// <returns>
/// The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.
/// </returns>
/// <exception cref="T:System.ArgumentException">
/// The sum of <paramref name="offset"/> and <paramref name="count"/> is larger than the buffer length.
/// </exception>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="buffer"/> is null.
/// </exception>
/// <exception cref="T:System.ArgumentOutOfRangeException">
/// <paramref name="offset"/> or <paramref name="count"/> is negative.
/// </exception>
/// <exception cref="T:System.IO.IOException">
/// An I/O error occurs.
/// </exception>
/// <exception cref="T:System.NotSupportedException">
/// The stream does not support reading.
/// </exception>
/// <exception cref="T:System.ObjectDisposedException">
/// Methods were called after the stream was closed.
/// </exception>
public override int Read(byte[] buffer, int offset, int count)
{
int bytesLeft = count;
while (this.position < this.length && bytesLeft > 0)
{
var copyCount = (int)Math.Min(bufferStopPosition - this.position, bytesLeft);
var bufferIndex = (int)(this.position - this.bufferStartPosition);
Buffer.BlockCopy(this.buffer, bufferIndex, buffer, offset + (count - bytesLeft), copyCount);
this.Position += copyCount;
bytesLeft -= copyCount;
}
return count - bytesLeft;
}
/// <summary>
/// When overridden in a derived class, sets the position within the current stream.
/// </summary>
/// <param name="offset">A byte offset relative to the <paramref name="origin"/> parameter.</param>
/// <param name="origin">A value of type <see cref="T:System.IO.SeekOrigin"/> indicating the reference point used to obtain the new position.</param>
/// <returns>
/// The new position within the current stream.
/// </returns>
/// <exception cref="T:System.IO.IOException">
/// An I/O error occurs.
/// </exception>
/// <exception cref="T:System.NotSupportedException">
/// The stream does not support seeking, such as if the stream is constructed from a pipe or console output.
/// </exception>
/// <exception cref="T:System.ObjectDisposedException">
/// Methods were called after the stream was closed.
/// </exception>
public override long Seek(long offset, SeekOrigin origin)
{
switch (origin)
{
case SeekOrigin.Begin:
this.Position = offset;
break;
case SeekOrigin.Current:
this.Position += offset;
break;
case SeekOrigin.End:
this.Position = this.length + offset;
break;
}
return this.Position;
}
/// <summary>
/// When overridden in a derived class, sets the length of the current stream.
/// </summary>
/// <param name="value">The desired length of the current stream in bytes.</param>
/// <exception cref="T:System.IO.IOException">
/// An I/O error occurs.
/// </exception>
/// <exception cref="T:System.NotSupportedException">
/// The stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output.
/// </exception>
/// <exception cref="T:System.ObjectDisposedException">
/// Methods were called after the stream was closed.
/// </exception>
public override void SetLength(long value)
{
throw new NotSupportedException();
}
/// <summary>
/// When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
/// </summary>
/// <param name="buffer">An array of bytes. This method copies <paramref name="count"/> bytes from <paramref name="buffer"/> to the current stream.</param>
/// <param name="offset">The zero-based byte offset in <paramref name="buffer"/> at which to begin copying bytes to the current stream.</param>
/// <param name="count">The number of bytes to be written to the current stream.</param>
/// <exception cref="T:System.ArgumentException">
/// The sum of <paramref name="offset"/> and <paramref name="count"/> is greater than the buffer length.
/// </exception>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="buffer"/> is null.
/// </exception>
/// <exception cref="T:System.ArgumentOutOfRangeException">
/// <paramref name="offset"/> or <paramref name="count"/> is negative.
/// </exception>
/// <exception cref="T:System.IO.IOException">
/// An I/O error occurs.
/// </exception>
/// <exception cref="T:System.NotSupportedException">
/// The stream does not support writing.
/// </exception>
/// <exception cref="T:System.ObjectDisposedException">
/// Methods were called after the stream was closed.
/// </exception>
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
}
}
Related examples in the same category