Ejemplo n.º 1
0
        /// <summary>
        /// 结束读取文件
        /// </summary>
        /// <param name="result"></param>
        private void EndRead(IAsyncResult result)
        {
            if (FileStream == null)
            {
                return;
            }
            int            length = FileStream.EndRead(result);
            ReadFileObject state  = (ReadFileObject)result.AsyncState;
            int            index  = state.Index;

            byte[] buffer             = state.Buffer;
            ReadFileBufferEventArgs e = null;

            if (length < _partSize)
            {
                byte[] realBuffer = new byte[length];
                Buffer.BlockCopy(buffer, 0, realBuffer, 0, length);
                e = new ReadFileBufferEventArgs(index, realBuffer);
            }
            else
            {
                e = new ReadFileBufferEventArgs(index, buffer);
            }
            OnReadFileBuffer(e);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="index"></param>
        public void Read(int index)
        {
            int size = _partSize;

            if (Length - _partSize * index < _partSize)
            {
                size = (int)(Length - _partSize * index);
            }
            byte[]         buffer = new byte[size];
            ReadFileObject obj    = new ReadFileObject(index, buffer);

            FileStream.Position = index * _partSize;
            FileStream.BeginRead(
                buffer,
                0,
                size,
                new AsyncCallback(EndRead),
                obj);
        }