Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement custom stream

Tags:

c#

stream

I am calling a dll that writes to a stream. The signature of the method in the dll looks like:

public bool SomeMethod(Stream stream);

and that method will basically write binary data to that stream. So if I will call that method as:

var file = System.IO.File.Create("SomeFile.txt");
/* call dll method */ SomeMethod(file); 

then I will be basically writing the output to that file. In this question I am writing the output to a networkStream.

Anyways so back to the question. The reason why I will like to create my own stream is because I will like to know when some events take place. For example if I where to create my own stream class as:

class MyStream : Stream
{
    private long Position;

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

            // implementation goes here

            /* HERE I COULD CALL A CUSTOM EVENT */
    }

    public override long Seek(long offset, SeekOrigin origin)
    {
        // SAME THING I WILL LIKE TO PERFORM AN ACTION IF THIS METHOD IS CALLED!
    }

    // etc implement rest of abstract methods....

I am writing the output of that stream to the network so I might want to slow down if some event occurs. If I where to have control of the dll then I would not be trying to implement this.

I will appreciate if someone could show me a very basic example of how to implement the abstract methods of the abstract Stream Class.

like image 644
Tono Nam Avatar asked Sep 08 '12 06:09

Tono Nam


People also ask

How are streams implemented?

Executing a stream pipeline. When the terminal operation is initiated, the stream implementation picks an execution plan. Intermediate operations are divided into stateless ( filter() , map() , flatMap() ) and stateful ( sorted() , limit() , distinct() ) operations.

Which class implements stream interface in Java?

The JDK's standard implementation of Stream is the internal class java. util. stream.

Which method can collect a stream in a custom collection?

toMap() The toMap collector can be used to collect Stream elements into a Map instance.


2 Answers

The easiest custom stream a stream that "wraps" some other stream (similar to compression streams). Each method would simply redirect its implementation to internal stream.

class MyStream : Stream
{
 Stream inner;
 public MyStream(Stream inner)
 {
  this.inner = inner;
 }

 public override int Read(byte[] buffer, int offset, int count) 
 { 
    var result = inner.Read(buffer, offset, count);

            /* HERE I COULD CALL A CUSTOM EVENT */ 
   return result;
 } 
///
}

Usage sample: functionThatTakesStream(new MyStream(new MemoryStream());.

Real code will need to handle exceptions in operations on inners stream before/after fireing events and deal with IDisposable correctly.

like image 191
Alexei Levenkov Avatar answered Oct 03 '22 08:10

Alexei Levenkov


If all you want to do is fire an event when Read, Seek or similar methods are called, override the base class versions, call them directly and raise the appropriate event before or after. If you want help on writing stream classes, have a look at the .Net code itself available at http://referencesource.microsoft.com/netframework.aspx. However, if you want to parse the stream in to something more readable, consider creating an IEnumerable<MyClass> that reads in and processes the stream, instead.

like image 37
akton Avatar answered Oct 03 '22 09:10

akton