eGospodarka.pl
eGospodarka.pl poleca

eGospodarka.plGrupypl.comp.programmingStreamin c#Re: Streamin c#
  • Data: 2010-09-01 15:31:09
    Temat: Re: Streamin c#
    Od: Selvin <p...@g...com> szukaj wiadomości tego autora
    [ pokaż wszystkie nagłówki ]

    po co stream :) kolejka nie moze byc?
    jak chcesz to mozesz to odziedziczyc po System.IO.Stream

    public class MyBuffer
    {

    Queue<MemoryChunk> buffers = new Queue<MemoryChunk>();
    object readlock = new object();

    public uint AvailableDataLength { get; private set; }

    public void Write(byte[] data, int offset, int count)
    {
    var mem = AllocateMemoryChunk(count);
    Buffer.BlockCopy(data, offset, mem.Buffer, 0, count);
    lock (readlock)
    {
    buffers.Enqueue(mem);
    AvailableDataLength += count;
    }
    }

    public int Read(byte[] buffer, int offset, int count)
    {
    int toread = 0;
    lock (readlock)
    {

    if (buffers.Count != 0)
    {
    var buf = buffers.Peek();
    toread = buf.Buffer.Length - buf.Offset;
    int off = buf.Offset;
    if (count >= toread)
    buffers.Dequeue();
    else
    {
    toread = count;
    buf.Offset += toread;
    }
    Buffer.BlockCopy(buf.Buffer, off, buffer, offset,
    toread);
    AvailableDataLength -= toread;
    }
    return toread;
    }
    }

    private MemoryChunk AllocateMemoryChunk(int newSize)
    {
    //zawsze mozna zrobic z tego pool zeby nie tworzyc nowych
    MemoryChunk chunk = new MemoryChunk(newSize);
    return chunk;
    }

    private class MemoryChunk
    {
    internal byte[] Buffer;
    internal int Offset = 0;
    internal MemoryChunk(uint bufferSize)
    {
    Buffer = new byte[bufferSize];
    Offset = 0;
    }
    }
    }

Podziel się

Poleć ten post znajomemu poleć

Wydrukuj ten post drukuj


Następne wpisy z tego wątku

Najnowsze wątki z tej grupy


Najnowsze wątki

Szukaj w grupach

Eksperci egospodarka.pl

1 1 1

Wpisz nazwę miasta, dla którego chcesz znaleźć jednostkę ZUS.

Wzory dokumentów

Bezpłatne wzory dokumentów i formularzy.
Wyszukaj i pobierz za darmo: