Example #1
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (m_mode == Compress)
            {
                int r = 0;
                while (m_queue.Count < count && (r = m_io.Read(m_bytesReadFromStream, 0, m_BufferSize)) != 0)
                {
                    byte[] data = new byte[r];
                    for (int i = 0; i < r; data[i] = m_bytesReadFromStream[i], i++)
                    {
                        ;
                    }
                    byte[] compressed = m_naiveCompressor.compress(data);
                    foreach (byte b in compressed)
                    {
                        m_queue.Enqueue(b);
                    }
                }
                int bytesCount = Math.Min(m_queue.Count, count);
                for (int i = 0; i < bytesCount; i++)
                {
                    buffer[i + offset] = m_queue.Dequeue();
                }
                return(-1);
            }
            else if (m_mode == MyCompressorStream.Decompress)
            {
                // we should read X < Count compressed bytes form the source stream
                // and allow the reader to read COUNT decomprssed bytes from buffer
                // starting from OFFSET index

                int r = 0;
                while (m_queue.Count < count && (r = m_io.Read(m_bytesReadFromStream, 0, m_BufferSize)) != 0)
                {
                    // our source actually contain R bytes and if R<bufferSize then the rest of bytes are leftovers...
                    // let's cut them
                    byte[] data = new byte[r];
                    for (int i = 0; i < r; data[i] = m_bytesReadFromStream[i], i++)
                    {
                        ;
                    }

                    byte[] decompressed = m_naiveCompressor.decompress(data);
                    // now, we'll put the decomprssed data in the queue; it is used as a buffer
                    foreach (byte b in decompressed)
                    {
                        m_queue.Enqueue(b);
                    }
                }
                int bytesCount = Math.Min(m_queue.Count, count);

                for (int i = 0; i < bytesCount; i++)
                {
                    buffer[i + offset] = m_queue.Dequeue();
                }
                return(bytesCount);
            }
            return(0);
        }
Example #2
0
 /// <summary>
 /// compress the data from the 'buffer' and write it to thr current stream.
 /// </summary>
 /// <param name="buffer">array of bytes to read data from</param>
 /// <param name="offset">start to read from the 'buffer' in this position</param>
 /// <param name="count">number of bytes to read from the buffer</param>
 public override void Write(byte[] buffer, int offset, int count)
 {
     byte[] data = new byte[count];
     for (int i = 0; i < count; data[i] = buffer[i + offset], i++)
     {
         ;
     }
     byte[] compressed = m_naiveCompressor.compress(data);
     m_io.Write(compressed, 0, compressed.Length);
 }
Example #3
0
        /// <summary>
        /// read an array of bytes and
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        /// <returns>bytesCountreturns>
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (m_mode != m_compress && m_mode != m_decompress)
            {
                return(0);
            }
            int r = 0;

            while (m_queue.Count < count && (r = m_io.Read(m_bytesReadFromStream, 0, m_BufferSize)) != 0)
            {
                byte[] data = new byte[r];
                for (int i = 0; i < r; data[i] = m_bytesReadFromStream[i], i++)
                {
                    ;
                }
                if (m_mode == MyCompressorStream.m_decompress)
                {
                    byte[] decompressed = mazeCompressor.decompress(data);
                    foreach (byte b in decompressed)
                    {
                        m_queue.Enqueue(b);
                    }
                }
                else if (m_mode == MyCompressorStream.m_compress)
                {
                    byte[] compressed = mazeCompressor.compress(data);
                    foreach (byte b in compressed)
                    {
                        m_queue.Enqueue(b);
                    }
                }
            }
            int bytesCount = Math.Min(m_queue.Count, count);

            for (int i = 0; i < bytesCount; i++)
            {
                buffer[i + offset] = m_queue.Dequeue();
            }
            return(bytesCount);
        }