Imports System
Imports System.IO
Namespace CGS
'''
''' Wraps another stream and provides reporting for when bytes are read or written to the stream.
'''
Public Class ProgressStream
Inherits Stream
#Region "Private Data Members"
Private innerStream As Stream
#End Region
#Region "Constructor"
'''
''' Creates a new ProgressStream supplying the stream for it to report on.
'''
''' The underlying stream that will be reported on when bytes are read or written.
Public Sub New(ByVal streamToReportOn As Stream)
If streamToReportOn IsNot Nothing Then
Me.innerStream = streamToReportOn
Else
Throw New ArgumentNullException("streamToReportOn")
End If
End Sub
#End Region
#Region "Events"
'''
''' Raised when bytes are read from the stream.
'''
Public Event BytesRead As ProgressStreamReportDelegate
'''
''' Raised when bytes are written to the stream.
'''
Public Event BytesWritten As ProgressStreamReportDelegate
'''
''' Raised when bytes are either read or written to the stream.
'''
Public Event BytesMoved As ProgressStreamReportDelegate
Protected Overridable Sub OnBytesRead(ByVal bytesMoved As Integer)
Dim args = New ProgressStreamReportEventArgs(bytesMoved, innerStream.Length, innerStream.Position, True)
RaiseEvent BytesRead(Me, args)
End Sub
Protected Overridable Sub OnBytesWritten(ByVal bytesMoved As Integer)
Dim args = New ProgressStreamReportEventArgs(bytesMoved, innerStream.Length, innerStream.Position, False)
RaiseEvent BytesRead(Me, args)
End Sub
Protected Overridable Sub OnBytesMoved(ByVal bytesMoved__1 As Integer, ByVal isRead As Boolean)
Dim args = New ProgressStreamReportEventArgs(bytesMoved__1, innerStream.Length, innerStream.Position, isRead)
RaiseEvent BytesRead(Me, args)
End Sub
#End Region
#Region "Stream Members"
Public Overloads Overrides ReadOnly Property CanRead() As Boolean
Get
Return innerStream.CanRead
End Get
End Property
Public Overloads Overrides ReadOnly Property CanSeek() As Boolean
Get
Return innerStream.CanSeek
End Get
End Property
Public Overloads Overrides ReadOnly Property CanWrite() As Boolean
Get
Return innerStream.CanWrite
End Get
End Property
Public Overloads Overrides Sub Flush()
innerStream.Flush()
End Sub
Public Overloads Overrides ReadOnly Property Length() As Long
Get
Return innerStream.Length
End Get
End Property
Public Overloads Overrides Property Position() As Long
Get
Return innerStream.Position
End Get
Set(ByVal value As Long)
innerStream.Position = value
End Set
End Property
Public Overloads Overrides Function Read(ByVal buffer As Byte(), ByVal offset As Integer, ByVal count As Integer) As Integer
Dim bytesRead = innerStream.Read(buffer, offset, count)
OnBytesRead(bytesRead)
OnBytesMoved(bytesRead, True)
Return bytesRead
End Function
Public Overloads Overrides Function Seek(ByVal offset As Long, ByVal origin As SeekOrigin) As Long
Return innerStream.Seek(offset, origin)
End Function
Public Overloads Overrides Sub SetLength(ByVal value As Long)
innerStream.SetLength(value)
End Sub
Public Overloads Overrides Sub Write(ByVal buffer As Byte(), ByVal offset As Integer, ByVal count As Integer)
innerStream.Write(buffer, offset, count)
OnBytesWritten(count)
OnBytesMoved(count, False)
End Sub
Public Overloads Overrides Sub Close()
innerStream.Close()
MyBase.Close()
End Sub
#End Region
End Class
'''
''' Contains the pertinent data for a ProgressStream Report event.
'''
Public Class ProgressStreamReportEventArgs
Inherits EventArgs
'''
''' The number of bytes that were read/written to/from the stream.
'''
Private _BytesMoved As Integer
Public Property BytesMoved() As Integer
Get
Return _BytesMoved
End Get
Private Set(ByVal value As Integer)
_BytesMoved = value
End Set
End Property
'''
''' The total length of the stream in bytes.
'''
Private _StreamLength As Long
Public Property StreamLength() As Long
Get
Return _StreamLength
End Get
Private Set(ByVal value As Long)
_StreamLength = value
End Set
End Property
'''
''' The current position in the stream.
'''
Private _StreamPosition As Long
Public Property StreamPosition() As Long
Get
Return _StreamPosition
End Get
Private Set(ByVal value As Long)
_StreamPosition = value
End Set
End Property
'''
''' True if the bytes were read from the stream, false if they were written.
'''
Private _WasRead As Boolean
Public Property WasRead() As Boolean
Get
Return _WasRead
End Get
Private Set(ByVal value As Boolean)
_WasRead = value
End Set
End Property
'''
''' Default constructor for ProgressStreamReportEventArgs.
'''
Public Sub New()
MyBase.New()
End Sub
'''
''' Creates a new ProgressStreamReportEventArgs initializing its members.
'''
''' The number of bytes that were read/written to/from the stream.
''' The total length of the stream in bytes.
''' The current position in the stream.
''' True if the bytes were read from the stream, false if they were written.
Public Sub New(ByVal bytesMoved As Integer, ByVal streamLength As Long, ByVal streamPosition As Long, ByVal wasRead__1 As Boolean)
Me.New()
Me.BytesMoved = bytesMoved
Me.StreamLength = streamLength
Me.StreamPosition = streamPosition
Me.WasRead = WasRead
End Sub
End Class
'''
''' The delegate for handling a ProgressStream Report event.
'''
''' The object that raised the event, should be a ProgressStream.
''' The arguments raised with the event.
Public Delegate Sub ProgressStreamReportDelegate(ByVal sender As Object, ByVal args As ProgressStreamReportEventArgs)
End Namespace