예제 #1
0
 /// <summary>
 /// Downloads the file.
 /// </summary>
 public void DownloadFile(RelativePath path, long offset, int count, bool readFromEnd,
                          DateTime newerThan, bool throwOnFail, string destFileName,
                          out DateTime lastWriteTime, out FileReadingResult readingResult)
 {
     DownloadFile(path, offset, count, readFromEnd, newerThan, throwOnFail,
                  () => { return(new FileStream(destFileName, FileMode.Create, FileAccess.Write, FileShare.Read)); },
                  out lastWriteTime, out readingResult, out Stream stream);
     stream?.Dispose();
 }
예제 #2
0
        /// <summary>
        /// Downloads the file.
        /// </summary>
        public void DownloadFile(RelativePath path, long offset, int count, bool readFromEnd,
                                 DateTime newerThan, bool throwOnFail, Func <Stream> createStreamFunc,
                                 out DateTime lastWriteTime, out FileReadingResult readingResult, out Stream stream)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (createStreamFunc == null)
            {
                throw new ArgumentNullException(nameof(createStreamFunc));
            }

            RestoreConnection();

            DataPacket request = CreateRequest(FunctionID.DownloadFile);
            int        index   = ArgumentIndex;

            CopyFileName(path.DirectoryID, path.Path, outBuf, ref index);
            CopyInt64(offset, outBuf, ref index);
            CopyInt32(count, outBuf, ref index);
            CopyBool(readFromEnd, outBuf, ref index);
            CopyTime(newerThan, outBuf, ref index);
            request.BufferLength = index;
            SendRequest(request);

            int prevBlockNumber = 0;

            lastWriteTime = DateTime.MinValue;
            readingResult = FileReadingResult.BlockRead;
            stream        = null;

            try
            {
                while (readingResult == FileReadingResult.BlockRead)
                {
                    ReceiveResponse(request);
                    index = ArgumentIndex;
                    int blockNumber = GetInt32(inBuf, ref index);
                    int blockCount  = GetInt32(inBuf, ref index);
                    lastWriteTime = GetTime(inBuf, ref index);
                    readingResult = (FileReadingResult)GetByte(inBuf, ref index);

                    if (blockNumber != prevBlockNumber + 1)
                    {
                        ThrowBlockNumberException();
                    }

                    if (readingResult == FileReadingResult.BlockRead ||
                        readingResult == FileReadingResult.Completed)
                    {
                        if (stream == null)
                        {
                            stream = createStreamFunc();
                        }

                        int bytesToWrite = GetInt32(inBuf, ref index);
                        stream.Write(inBuf, index, bytesToWrite);
                    }

                    prevBlockNumber = blockNumber;
                    OnProgress(blockNumber, blockCount);
                }

                if (throwOnFail && readingResult != FileReadingResult.Completed)
                {
                    throw new ProtocolException(ErrorCode.InternalServerError, string.Format(Locale.IsRussian ?
                                                                                             "Ошибка при чтении файла {0}: {1}" :
                                                                                             "Error reading file {0}: {1}", path, readingResult.ToString(Locale.IsRussian)));
                }
            }
            catch
            {
                stream?.Dispose();
                stream = null;
                throw;
            }
        }
예제 #3
0
        /// <summary>
        /// Downloads the file.
        /// </summary>
        public void DownloadFile(RelativePath relativePath, long offset, int count, bool readFromEnd,
                                 DateTime newerThan, Func <Stream> createStreamFunc,
                                 out DateTime fileAge, out FileReadingResult readingResult, out Stream stream)
        {
            if (createStreamFunc == null)
            {
                throw new ArgumentNullException(nameof(createStreamFunc));
            }

            RestoreConnection();

            DataPacket request = CreateRequest(FunctionID.DownloadFile);
            int        index   = ArgumentIndex;

            CopyFileName(relativePath.GetDirectoryID(), relativePath.Path, outBuf, ref index);
            CopyInt64(offset, outBuf, ref index);
            CopyInt32(count, outBuf, ref index);
            CopyBool(readFromEnd, outBuf, ref index);
            CopyTime(newerThan, outBuf, ref index);
            request.BufferLength = index;
            SendRequest(request);

            int prevBlockNumber = 0;

            fileAge       = DateTime.MinValue;
            readingResult = FileReadingResult.Successful;
            stream        = null;

            try
            {
                while (readingResult == FileReadingResult.Successful)
                {
                    ReceiveResponse(request);
                    index = ArgumentIndex;
                    int blockNumber = GetInt32(inBuf, ref index);
                    int blockCount  = GetInt32(inBuf, ref index);
                    fileAge       = GetTime(inBuf, ref index);
                    readingResult = (FileReadingResult)GetByte(inBuf, ref index);

                    if (blockNumber != prevBlockNumber + 1)
                    {
                        ThrowBlockNumberException();
                    }

                    if (readingResult == FileReadingResult.Successful ||
                        readingResult == FileReadingResult.EndOfFile)
                    {
                        if (stream == null)
                        {
                            stream = createStreamFunc();
                        }

                        int bytesToWrite = GetInt32(inBuf, ref index);
                        stream.Write(inBuf, index, bytesToWrite);
                    }

                    prevBlockNumber = blockNumber;
                    OnProgress(blockNumber, blockCount);
                }
            }
            catch
            {
                stream?.Dispose();
                stream = null;
                throw;
            }
        }