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); }
/// <summary> /// Read compressed data from a file, decompress it and writes it into the buffer /// </summary> /// <param name="buffer">buffer for decompressed data</param> /// <param name="offset">start to insert data into the 'buffer' from this position</param> /// <param name="count">number of bytes to read</param> /// <returns>number of bytes that actually readed </returns> public override int Read(byte[] buffer, int offset, int count) { 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); }
/// <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); }