Esempio n. 1
0
        /// <summary>
        /// When overridden in a derived class, sets the position within the current stream.
        /// </summary>
        /// <param name="offset">A byte offset relative to the <paramref name="origin"/> parameter.</param>
        /// <param name="origin">A value of type <see cref="T:System.IO.SeekOrigin"/> indicating the reference point used to obtain the new position.</param>
        /// <returns>
        /// The new position within the current stream.
        /// </returns>
        /// <exception cref="T:System.IO.IOException">
        /// An I/O error occurs.
        /// </exception>
        /// <exception cref="T:System.NotSupportedException">
        /// The stream does not support seeking, such as if the stream is constructed from a pipe or console output.
        /// </exception>
        /// <exception cref="T:System.ObjectDisposedException">
        /// Methods were called after the stream was closed.
        /// </exception>
        public override long Seek(long offset, System.IO.SeekOrigin origin)
        {
            // FIXME: off-by-one bug when new position modulus 512 = 0

            long newposition = position;

            switch (origin)
            {
            case System.IO.SeekOrigin.Begin: newposition = offset; break;

            case System.IO.SeekOrigin.Current: newposition = position + offset; break;

            case System.IO.SeekOrigin.End: newposition = length + offset; break;
            }

            // find cluster number of new position
            uint newNthCluster     = (uint)(newposition / clusterSize);
            uint currentNthCluster = (uint)(position / clusterSize);
            int  diff = (int)(newNthCluster - currentNthCluster);

            uint newCluster = 0;

            if (newNthCluster == currentNthCluster)
            {
                newCluster = currentCluster;
            }
            else
            if (newNthCluster > currentNthCluster)
            {
                newCluster        = fs.FindNthCluster(currentCluster, (uint)diff);
                currentNthCluster = currentNthCluster + (uint)diff;
            }
            else
            if (newNthCluster < currentNthCluster)
            {
                newCluster        = fs.FindNthCluster(this.startCluster, newNthCluster);
                currentNthCluster = newNthCluster;
            }

            ReadCluster(newCluster);
            position = newposition;
            return(position);
        }