469,282 Members | 1,944 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

How to convert from IStream to System.IO.Stream

MJB
I'm getting an IStream back from function xmlHttp.responsestream. I would
like to convert this to a System.IO.Stream in order to work with it in my
application. Has anyone encountered this and written a conversion?

TIA,
Matt
Nov 15 '05 #1
3 14713
I don't think you can just plain convert it, but you can always read the
contents of one into another.
--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/

"MJB" <mb*@email.com> wrote in message
news:O$**************@tk2msftngp13.phx.gbl...
I'm getting an IStream back from function xmlHttp.responsestream. I would
like to convert this to a System.IO.Stream in order to work with it in my
application. Has anyone encountered this and written a conversion?

TIA,
Matt

Nov 15 '05 #2
You can wrap the IStream in a .NET wrapper which .NET objects can the
access, like this:

using System;

using System.IO;

using System.Runtime.InteropServices;
public class ComStream : Stream {

// the managed stream being wrapped

UCOMIStream originalStream_;

public ComStream( UCOMIStream stream ) {

if( stream != null ) {

originalStream_ = stream;

}

else {

throw new ArgumentNullException( "stream" );

}

}

~ComStream() {

Close();

}

// property to get original stream object

public UCOMIStream UnderlyingStream {

get {

return originalStream_;

}

}

// reads a specified number of bytes from the stream object

// into memory starting at the current seek pointer

public unsafe override int Read( byte[] buffer, int offset, int count ) {

if( originalStream_ == null ) {

throw new ObjectDisposedException( "originalStream_" );

}

if( offset != 0 ) {

throw new NotSupportedException("only 0 offset is supported");

}

int bytesRead;

IntPtr address = new IntPtr( &bytesRead );

originalStream_.Read( buffer, count, address );

return bytesRead;

}
// writes a specified number of bytes into the stream object

// starting at the current seek pointer

public override void Write( byte[] buffer, int offset, int count ) {

if( originalStream_ == null ) {

throw new ObjectDisposedException( "originalStream_" );

}

if( offset != 0 ) {

throw new NotSupportedException("only 0 offset is supported");

}

originalStream_.Write( buffer, count, IntPtr.Zero );

}

// changes the seek pointer to a new location relative to the beginning

// of the stream, the end of the stream, or the current seek position

public unsafe override long Seek( long offset, SeekOrigin origin ){
if( originalStream_ == null ) {

throw new ObjectDisposedException( "originalStream_" );

}

long position = 0;

IntPtr address = new IntPtr( &position );

originalStream_.Seek( offset, (int) origin, address );
return position;

}

public override long Length {

get {

if( originalStream_ == null ) {

throw new ObjectDisposedException( "originalStream_" );

}

STATSTG statstg;

originalStream_.Stat( out statstg, 1 /* STATSFLAG_NONAME*/ );

return statstg.cbSize;

}

}

public override long Position {

get { return Seek( 0, SeekOrigin.Current ) ; }

set { Seek( value, SeekOrigin.Begin ) ; }

}
// changes the size of the stream object

public override void SetLength( long value ) {

if( originalStream_ == null ) {

throw new ObjectDisposedException( "originalStream_" );

}

originalStream_.SetSize( value );

}

// closes (disposes) the stream

public override void Close() {

if( originalStream_ != null ) {

originalStream_.Commit( 0 );

// Marshal.ReleaseComObject( originalStream_ );

originalStream_= null;

GC.SuppressFinalize( this );

}

}

public override void Flush() {

originalStream_.Commit(0);

}

public override bool CanRead {

get { return true ; }

}

public override bool CanWrite {

get { return true ;}

}

public override bool CanSeek {

get { return true ; }

}

}

This comes mostly from this book:
http://www.amazon.com/exec/obidos/tg...lance&n=507846

"Klaus H. Probst" <us*******@vbbox.com> wrote in message
news:up**************@TK2MSFTNGP10.phx.gbl...
I don't think you can just plain convert it, but you can always read the
contents of one into another.
--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/

"MJB" <mb*@email.com> wrote in message
news:O$**************@tk2msftngp13.phx.gbl...
I'm getting an IStream back from function xmlHttp.responsestream. I would like to convert this to a System.IO.Stream in order to work with it in my application. Has anyone encountered this and written a conversion?

TIA,
Matt


Nov 15 '05 #3
MJB
Great information. Thanks John

"john farrow" <vi*******@xtra.co.nz> wrote in message
news:Ov**************@TK2MSFTNGP11.phx.gbl...
You can wrap the IStream in a .NET wrapper which .NET objects can the
access, like this:

using System;

using System.IO;

using System.Runtime.InteropServices;
public class ComStream : Stream {

// the managed stream being wrapped

UCOMIStream originalStream_;

public ComStream( UCOMIStream stream ) {

if( stream != null ) {

originalStream_ = stream;

}

else {

throw new ArgumentNullException( "stream" );

}

}

~ComStream() {

Close();

}

// property to get original stream object

public UCOMIStream UnderlyingStream {

get {

return originalStream_;

}

}

// reads a specified number of bytes from the stream object

// into memory starting at the current seek pointer

public unsafe override int Read( byte[] buffer, int offset, int count ) {

if( originalStream_ == null ) {

throw new ObjectDisposedException( "originalStream_" );

}

if( offset != 0 ) {

throw new NotSupportedException("only 0 offset is supported");

}

int bytesRead;

IntPtr address = new IntPtr( &bytesRead );

originalStream_.Read( buffer, count, address );

return bytesRead;

}
// writes a specified number of bytes into the stream object

// starting at the current seek pointer

public override void Write( byte[] buffer, int offset, int count ) {

if( originalStream_ == null ) {

throw new ObjectDisposedException( "originalStream_" );

}

if( offset != 0 ) {

throw new NotSupportedException("only 0 offset is supported");

}

originalStream_.Write( buffer, count, IntPtr.Zero );

}

// changes the seek pointer to a new location relative to the beginning

// of the stream, the end of the stream, or the current seek position

public unsafe override long Seek( long offset, SeekOrigin origin ){
if( originalStream_ == null ) {

throw new ObjectDisposedException( "originalStream_" );

}

long position = 0;

IntPtr address = new IntPtr( &position );

originalStream_.Seek( offset, (int) origin, address );
return position;

}

public override long Length {

get {

if( originalStream_ == null ) {

throw new ObjectDisposedException( "originalStream_" );

}

STATSTG statstg;

originalStream_.Stat( out statstg, 1 /* STATSFLAG_NONAME*/ );

return statstg.cbSize;

}

}

public override long Position {

get { return Seek( 0, SeekOrigin.Current ) ; }

set { Seek( value, SeekOrigin.Begin ) ; }

}
// changes the size of the stream object

public override void SetLength( long value ) {

if( originalStream_ == null ) {

throw new ObjectDisposedException( "originalStream_" );

}

originalStream_.SetSize( value );

}

// closes (disposes) the stream

public override void Close() {

if( originalStream_ != null ) {

originalStream_.Commit( 0 );

// Marshal.ReleaseComObject( originalStream_ );

originalStream_= null;

GC.SuppressFinalize( this );

}

}

public override void Flush() {

originalStream_.Commit(0);

}

public override bool CanRead {

get { return true ; }

}

public override bool CanWrite {

get { return true ;}

}

public override bool CanSeek {

get { return true ; }

}

}

This comes mostly from this book:
http://www.amazon.com/exec/obidos/tg...lance&n=507846
"Klaus H. Probst" <us*******@vbbox.com> wrote in message
news:up**************@TK2MSFTNGP10.phx.gbl...
I don't think you can just plain convert it, but you can always read the
contents of one into another.
--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/

"MJB" <mb*@email.com> wrote in message
news:O$**************@tk2msftngp13.phx.gbl...
I'm getting an IStream back from function xmlHttp.responsestream. I would like to convert this to a System.IO.Stream in order to work with it in my application. Has anyone encountered this and written a conversion?

TIA,
Matt



Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Juan Carlos CORUÑA | last post: by
3 posts views Thread by Roger That | last post: by
3 posts views Thread by Charles Law | last post: by
13 posts views Thread by Peteroid | last post: by
13 posts views Thread by Gianni Mariani | last post: by
21 posts views Thread by Peter Larsen [] | last post: by
8 posts views Thread by Cartoper | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.