Click here to Skip to main content
15,893,622 members
Articles / Programming Languages / C#

SCSI Library in C# - Burn CDs and DVDs, Access Hard Disks, etc.

Rate me:
Please Sign up or sign in to vote.
4.77/5 (48 votes)
19 Jun 2017Ms-PL6 min read 146.1K   8.1K   146  
Ever wonder how programs like Nero work? They make their own SCSI libraries... like this!
using System;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;

namespace SCSI.Multimedia
{
	public class SCSIStream : Stream
	{
		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		private long _Position;

		/// <summary>Leaves the device open!</summary>
		public SCSIStream(SCSIDevice device) { this.Device = device; }

		public SCSIDevice Device { get; private set; }

		protected override void Dispose(bool disposing) { if (disposing) { this.Flush(); this.Device = null; } base.Dispose(disposing); }

		public override bool CanRead { get { return true; } }

		public override bool CanSeek { get { return true; } }

		public override bool CanWrite { get { return true; } }

		public override void Flush() { this.Device.SynchronizeCache(new SynchronizeCache10Command()); }

		public override long Length { get { return this.Device.Capacity.LogicalBlockAddress * this.Device.Capacity.BlockLength; } }

		public override long Position { get { return this._Position; } set { this._Position = value; } }

		public override int Read(byte[] buffer, int offset, int count)
		{
			long pos = this.Position;
			if (pos % this.Device.Capacity.BlockLength != 0) { throw new DataMisalignedException(); }
			this.Device.Read10(new Read10Command(false, (uint)(pos / this.Device.Capacity.BlockLength), (ushort)count), buffer, offset);
			return count;
		}

		public override long Seek(long offset, SeekOrigin origin)
		{
			long newPos;
			if (origin == SeekOrigin.Begin)
			{
				if (offset % this.Device.Capacity.BlockLength != 0) { throw new DataMisalignedException(); }
				newPos = offset / this.Device.Capacity.BlockLength;
				this.Device.Seek10(new Seek10Command(checked((uint)newPos)));
			}
			else { throw new NotImplementedException(); }
			this._Position = newPos;
			return this._Position;
		}

		public override void SetLength(long value) { throw new NotImplementedException(); }

		public override void Write(byte[] buffer, int offset, int count)
		{
			long pos = this._Position;
			if (pos % this.Device.Capacity.BlockLength != 0) { throw new DataMisalignedException(); }
			unsafe
			{
				fixed (byte* pBuffer = &buffer[offset])
				{
					var buf = new Helper.BufferWithSize(pBuffer, buffer.Length - offset).ExtractSegment(0, count);
					this.Device.Write10(new Write10Command(false, (uint)(pos / this.Device.Capacity.BlockLength)), buf);
				}
			}
			this._Position = pos + count;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions