/// <summary> /// This function reads data from the file and sends it to the /// buffer stream to be sent to the burner. This function does not /// return until all of the data from the file is read. /// </summary> /// <returns></returns> public bool ReadData(int sector_size, long lba) { bool shortbuf = false; int NumberRead; int num_sectors; bool first = true; // This is the number of whole sectors that will fit into the buffer. num_sectors = m_buffer_stream.PageSize / sector_size; while (true) { WriteBuffer buf; buf = m_buffer_stream.GetFreeBuffer(); if (!m_reader.ReadData(buf.BufferPtr, num_sectors * sector_size, out NumberRead)) { return(false); } // If the read succeeded, but read zero bytes, we have reached the // end of the file. if (NumberRead == 0) { return(true); } if (shortbuf) { throw new Exception("read two short packets"); } // Mark the buffer with the amount of data that has // been read from the file int sectors = NumberRead / sector_size; if ((NumberRead % sector_size) != 0) { shortbuf = true; sectors++; } if (first) { buf.SourceString = "First block from FileReader"; buf.LogicalBlockAddress = lba; first = false; } else { buf.SourceString = "Not first block from FileReader"; buf.LogicalBlockAddress = long.MaxValue; } buf.SectorSize = sector_size; buf.DataSize = sectors; // Send the buffer to the device m_buffer_stream.SendBufferToDevice(buf); } }
/// <summary> /// /// </summary> /// <param name="buffer"></param> /// <param name="offset"></param> /// <param name="count"></param> public override void Write(byte[] buffer, int offset, int count) { while (count > 0) { if (m_buffer == null) { m_buffer = m_pool.GetFreeBuffer(); m_index = 0; } // // Write up to the end of this buffer // int remaining = m_sector_count * m_sector_size - m_index; if (remaining > count) { remaining = count; } IntPtr dest = new IntPtr(m_buffer.BufferPtr.ToInt32() + m_index); Marshal.Copy(buffer, offset, dest, remaining); m_index += remaining; m_length += remaining; count -= remaining; offset += remaining; if (m_index == m_sector_count * m_sector_size) { m_buffer.DataSize = m_index / m_sector_size; m_buffer.SectorSize = m_sector_size; m_buffer.LogicalBlockAddress = m_lba; m_buffer.SourceString = "WriteBufferStream, Write method"; m_lba = long.MaxValue; m_pool.SendBufferToDevice(m_buffer); m_buffer = null; } } }