Esempio n. 1
0
        /// <summary>
        /// Sector refers to physical disk blocks, we can only read complete blocks
        /// </summary>
        public byte[] ReadSectorsUnbuffered(long sectorIndex, int sectorCount)
        {
            bool           releaseHandle;
            SafeFileHandle handle = PhysicalDiskHandlePool.ObtainHandle(m_physicalDiskIndex, FileAccess.Read, ShareMode.ReadWrite, out releaseHandle);

            if (!handle.IsInvalid)
            {
                FileStreamEx stream = new FileStreamEx(handle, FileAccess.Read);
                byte[]       buffer = new byte[m_bytesPerSector * sectorCount];
                try
                {
                    stream.Seek(sectorIndex * m_bytesPerSector, SeekOrigin.Begin);
                    stream.Read(buffer, 0, m_bytesPerSector * sectorCount);
                }
                finally
                {
                    stream.Close(releaseHandle);
                    if (releaseHandle)
                    {
                        PhysicalDiskHandlePool.ReleaseHandle(m_physicalDiskIndex);
                    }
                }
                return(buffer);
            }
            else
            {
                // we always release invalid handle
                PhysicalDiskHandlePool.ReleaseHandle(m_physicalDiskIndex);
                // get error code and throw
                int    errorCode = Marshal.GetLastWin32Error();
                string message   = String.Format("Failed to read sector {0} from disk {1}.", sectorIndex, m_physicalDiskIndex);
                IOExceptionHelper.ThrowIOError(errorCode, message);
                return(null); // this line will not be reached
            }
        }
        /// <summary>
        /// Sector refers to physical disk blocks, we can only read complete blocks
        /// </summary>
        public byte[] ReadSectorsUnbuffered(long sectorIndex, int sectorCount)
        {
            bool           releaseHandle;
            SafeFileHandle handle = VolumeHandlePool.ObtainHandle(m_volumeGuid, FileAccess.Read, ShareMode.ReadWrite, out releaseHandle);

            if (!handle.IsInvalid)
            {
                FileStreamEx stream = new FileStreamEx(handle, FileAccess.Read);
                byte[]       buffer = new byte[m_bytesPerSector * sectorCount];
                try
                {
                    stream.Seek(sectorIndex * m_bytesPerSector, SeekOrigin.Begin);
                    stream.Read(buffer, 0, m_bytesPerSector * sectorCount);
                }
                finally
                {
                    stream.Close(releaseHandle);
                    if (releaseHandle)
                    {
                        VolumeHandlePool.ReleaseHandle(m_volumeGuid);
                    }
                }
                return(buffer);
            }
            else
            {
                // we always release invalid handle
                VolumeHandlePool.ReleaseHandle(m_volumeGuid);
                // get error code and throw
                int    errorCode = Marshal.GetLastWin32Error();
                string message   = String.Format("Can't read sector {0} from volume {1}, Win32 Error: {2}", sectorIndex, m_volumeGuid, errorCode);
                throw new IOException(message);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// New PopularimeterFrame
        /// </summary>
        /// <param name="FrameID">4 Characters tag identifier</param>
        /// <param name="Flags">Frame Flags</param>
        /// <param name="Data">FileStream contain frame data</param>
        internal PopularimeterFrame(string FrameID, FrameFlags Flags, FileStreamEx Data, int Length)
            : base(FrameID, Flags)
        {
            EMail = Data.ReadText(Length, TextEncodings.Ascii, ref Length, true); // Read Email Address

            _Rating = Data.ReadByte();                                            // Read Rating
            Length--;

            if (Length > 8)
            {
                ErrorOccur("Counter value for Popularimeter frame is more than 8 byte." +
                           " this is not supported by this program", true);
                return;
            }

            byte[] LBuf = new byte[8];
            byte[] Buf  = new byte[Length];

            Data.Read(Buf, 0, Length);
            Buf.CopyTo(LBuf, 8 - Buf.Length);
            Array.Reverse(LBuf);

            _Counter = BitConverter.ToInt64(LBuf, 0);
        }