StreamConnector


The StreamConnector class comes with ADE in the examples folder. It wraps the IStream class used by COM, allowing convenient use of CAObject::PictureToStream and CATable::GraphToStream from a .NET program.

 ' StreamConnector.vb
 '
 ' (c) 2003-2015 Lumina Decision Systems, Inc.   All rights reserved.
 '
 ' The System.IO.Stream class in .NET is not compatible with the IStream
 ' class used by COM.  This class adds a wrapper around an existing IO.Stream
 ' to implement the IStream interface.  The resulting class is both an
 ' IStream and a Stream.  The underlying stream does not have to be 
 ' inheritable.
  
  
 Imports System.Collections.Generic
 Imports System.Runtime.InteropServices
 Imports System.Runtime.InteropServices.ComTypes
 Imports System.IO
  
 Public Class StreamConnector
    Inherits Stream
    Implements IStream
  
    Sub New(ByVal stream As Stream)
        m_stream = stream
    End Sub
  
    ' ==========================================================================================
    ' First we replicate the System.IO.Stream interface:
    Public Overrides Sub Close()
        m_stream.Close()
    End Sub
    Public Overloads Sub Dispose()
        m_stream.Dispose()
    End Sub
    'Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    '    m_stream.Dispose(disposing)
    'End Sub
    Public Overrides Sub EndWrite(ByVal asyncResult As System.IAsyncResult)
        m_stream.EndWrite(asyncResult)
    End Sub
    Public Overrides Sub Flush()
        m_stream.Flush()
    End Sub
    Public Overrides Sub SetLength(ByVal value As Long)
        m_stream.SetLength(value)
    End Sub
    Public Overrides Sub Write(ByVal buffer() As Byte, ByVal offset As Integer, ByVal count As Integer)
        m_stream.Write(buffer, offset, count)
    End Sub
    Public Overrides Sub WriteByte(ByVal value As Byte)
        m_stream.WriteByte(value)
    End Sub
    Public Overrides Function BeginRead(ByVal buffer() As Byte, ByVal offset As Integer, ByVal count As Integer, ByVal callback As System.AsyncCallback,  ByVal state As Object) As System.IAsyncResult
        BeginRead = m_stream.BeginRead(buffer, offset, count, callback, state)
    End Function
    Public Overrides Function BeginWrite(ByVal buffer() As Byte, ByVal offset As Integer, ByVal count As Integer, ByVal callback As System.AsyncCallback, ByVal state As Object) As System.IAsyncResult
        BeginWrite = m_stream.BeginWrite(buffer, offset, count, callback, state)
    End Function
    Public Overrides Function EndRead(ByVal asyncResult As System.IAsyncResult) As Integer
        EndRead = m_stream.EndRead(asyncResult)
    End Function
    Public Overrides Function Read(ByVal buffer() As Byte, ByVal offset As Integer, ByVal count As Integer) As Integer
        Read = m_stream.Read(buffer, offset, count)
    End Function
    Public Overrides Function ReadByte() As Integer
        ReadByte = m_stream.ReadByte()
    End Function
    Public Overrides Function Seek(ByVal offset As Long, ByVal origin As System.IO.SeekOrigin) As Long
        Seek = m_stream.Seek(offset, origin)
    End Function
    Public Overrides ReadOnly Property CanRead() As Boolean
        Get
            CanRead = m_stream.CanRead
        End Get
    End Property
    Public Overrides ReadOnly Property CanSeek() As Boolean
        Get
            CanSeek = m_stream.CanSeek
        End Get
    End Property
    Public Overrides ReadOnly Property CanTimeout() As Boolean
        Get
            CanTimeout = m_stream.CanTimeout
        End Get
    End Property
    Public Overrides ReadOnly Property CanWrite() As Boolean
        Get
            CanWrite = m_stream.CanWrite
        End Get
    End Property
    Public Overrides ReadOnly Property Length() As Long
        Get
            Length = m_stream.Length
        End Get
    End Property
    Public Overrides Property Position() As Long
        Get
            Position = m_stream.Position
        End Get
        Set(ByVal value As Long)
            m_stream.Position = value
        End Set
    End Property
    Public Overrides Property ReadTimeout() As Integer
        Get
            ReadTimeout = m_stream.ReadTimeout
        End Get
        Set(ByVal value As Integer)
            m_stream.ReadTimeout = value
        End Set
    End Property
    Public Overrides Property WriteTimeout() As Integer
        Get
            WriteTimeout = m_stream.WriteTimeout
        End Get
        Set(ByVal value As Integer)
            m_stream.WriteTimeout = value
        End Set
    End Property
  
    ' ==========================================================================================
    ' And implement the IStream interface:
  
    Sub Clone(ByRef ppstm As IStream) Implements IStream.Clone
        ppstm = New StreamConnector(m_stream)
    End Sub
  
    Sub Commit(ByVal flags As Integer) Implements IStream.Commit
    End Sub
  
    Overloads Sub CopyTo(ByVal dst As IStream, ByVal cb As Long, ByVal pcbRead As System.IntPtr, ByVal pcbWritten As System.IntPtr) Implements IStream.CopyTo
        Dim bytes(cb) As Byte
        Dim n As Integer = m_stream.Read(bytes, 0, cb)
        System.Runtime.InteropServices.Marshal.WriteInt32(pcbWritten, n)
        dst.Write(bytes, n, pcbWritten)
    End Sub
  
    Sub LockRegion(ByVal libOffset As Long, ByVal cb As Long, ByVal dwLockType As Integer) Implements IStream.LockRegion
    End Sub
  
    Sub IStreamRead(ByVal pv() As Byte, ByVal cb As Integer, ByVal pcbRead As System.IntPtr) Implements IStream.Read
        Marshal.WriteInt32(pcbRead, m_stream.Read(pv, 0, cb))
    End Sub
  
    Sub Revert() Implements IStream.Revert
    End Sub
    Sub IStreamSeek(ByVal dlibMove As Long, ByVal dwOrigin As Integer, ByVal plibNewPosition As System.IntPtr) Implements IStream.Seek
        Dim so As SeekOrigin = SeekOrigin.Begin
        Select Case dwOrigin
            Case 1
                so = SeekOrigin.Current
            Case 2
                so = SeekOrigin.End
        End Select
        Marshal.WriteInt32(plibNewPosition, m_stream.Seek(dlibMove, so))
    End Sub
    Sub SetSize(ByVal libNewSize As Long) Implements IStream.SetSize
        m_stream.SetLength(libNewSize)
    End Sub
    Sub Stat(ByRef pstatstg As System.Runtime.InteropServices.ComTypes.STATSTG, ByVal grfStatFlag As Integer) Implements IStream.Stat
        pstatstg = New ComTypes.STATSTG
    End Sub
    Sub UnlockRegion(ByVal libOffset As Long, ByVal cb As Long, ByVal dwLockType As Integer) Implements IStream.UnlockRegion
    End Sub
    Sub IStreamWrite(ByVal pv() As Byte, ByVal cb As Integer, ByVal pcbWritten As System.IntPtr) Implements IStream.Write
        If cb < pv.Length Then cb = pv.Length
        m_stream.Write(pv, 0, cb)
        Marshal.WriteInt32(pcbWritten, cb)
    End Sub
  
    Private m_stream As Stream
 End Class

See Also

Comments


You are not allowed to post comments.