Read() public method

Reads data
Timeout has occurred
public Read ( byte buffer, int millisecondsTimeout ) : int
buffer byte Byte array to store the data
millisecondsTimeout int Timeout in milliseconds
return int
        public void Read_BufferHasOneByte()
        {
            _stream.SetBufferContent(new byte[] { 0xaa });
            byte[] buff       = new byte[100];
            int    readLength = _stream.Read(buff, 1000);

            Assert.AreEqual(1, readLength);
            Assert.AreEqual((byte)0xaa, buff[0]);
        }
        public void Read_BufferHasOneByte()
        {
            _stream.DataBuffer[3] = 0xaa;
            _stream.BufferOffset  = 3;
            _stream.BufferLength  = 1;
            byte[] buff       = new byte[100];
            int    readLength = _stream.Read(buff, 1000);

            Assert.AreEqual(1, readLength);
            Assert.AreEqual((byte)0xaa, buff[0]);
            Assert.AreEqual(4, _stream.BufferOffset);
            Assert.AreEqual(0, _stream.BufferLength);
        }
Example #3
0
        private bool CreateFile(SCPChannelStream stream, string filePath, SCPEntry entry, SCPModTime modTime,
                                Cancellation cancellation,
                                SCPFileTransferProgressDelegate progressDelegate)
        {
            string fileName    = Path.GetFileName(filePath);
            ulong  transmitted = 0;

            if (progressDelegate != null)
            {
                progressDelegate(filePath, fileName, SCPFileTransferStatus.Open, (ulong)entry.FileSize, transmitted);
            }

            FileStream fileStream;

            try {
                fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Read);
            }
            catch (Exception e) {
                SendError(stream, "failed to create a file");
                throw new SCPClientException("Failed to create a file: " + filePath, e);
            }
            stream.Write(ZERO);

            using (fileStream) {
                byte[] buff   = new byte[FILE_TRANSFER_BLOCK_SIZE];
                long   remain = entry.FileSize;
                try {
                    while (remain > 0)
                    {
                        if (cancellation != null && cancellation.IsRequested)
                        {
                            if (progressDelegate != null)
                            {
                                progressDelegate(filePath, fileName, SCPFileTransferStatus.CompletedAbort, (ulong)entry.FileSize, transmitted);
                            }
                            return(false);   // cancel
                        }

                        int maxLength  = (int)Math.Min((long)buff.Length, remain);
                        int readLength = stream.Read(buff, maxLength, _protocolTimeout);
                        fileStream.Write(buff, 0, readLength);
                        remain -= readLength;

                        transmitted += (ulong)readLength;
                        if (progressDelegate != null)
                        {
                            progressDelegate(filePath, fileName, SCPFileTransferStatus.Transmitting, (ulong)entry.FileSize, transmitted);
                        }
                    }
                }
                catch (Exception e) {
                    SendError(stream, "failed to write to a file");
                    throw new SCPClientException("Failed to write to a file: " + filePath, e);
                }
            }

            if (modTime != null)
            {
                try {
                    File.SetLastWriteTimeUtc(filePath, modTime.MTime);
                    File.SetLastAccessTimeUtc(filePath, modTime.ATime);
                }
                catch (Exception e) {
                    SendError(stream, "failed to modify time of a file");
                    throw new SCPClientException("Failed to modify time of a file: " + filePath, e);
                }
            }

            CheckResponse(stream);
            stream.Write(ZERO);

            if (progressDelegate != null)
            {
                progressDelegate(filePath, fileName, SCPFileTransferStatus.CompletedSuccess, (ulong)entry.FileSize, transmitted);
            }

            return(true);
        }
Example #4
0
        private bool CreateFile(SCPChannelStream stream, string filePath, SCPEntry entry, SCPModTime modTime,
                    Cancellation cancellation,
                    SCPFileTransferProgressDelegate progressDelegate)
        {
            string fileName = Path.GetFileName(filePath);
            ulong transmitted = 0;

            if (progressDelegate != null)
                progressDelegate(filePath, fileName, SCPFileTransferStatus.Open, (ulong)entry.FileSize, transmitted);

            FileStream fileStream;
            try {
                fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Read);
            }
            catch (Exception e) {
                SendError(stream, "failed to create a file");
                throw new SCPClientException("Failed to create a file: " + filePath, e);
            }
            stream.Write(ZERO);

            using (fileStream) {
                byte[] buff = new byte[FILE_TRANSFER_BLOCK_SIZE];
                long remain = entry.FileSize;
                try {
                    while (remain > 0) {
                        if (cancellation != null && cancellation.IsRequested) {
                            if (progressDelegate != null)
                                progressDelegate(filePath, fileName, SCPFileTransferStatus.CompletedAbort, (ulong)entry.FileSize, transmitted);
                            return false;   // cancel
                        }

                        int maxLength = (int)Math.Min((long)buff.Length, remain);
                        int readLength = stream.Read(buff, maxLength, _protocolTimeout);
                        fileStream.Write(buff, 0, readLength);
                        remain -= readLength;

                        transmitted += (ulong)readLength;
                        if (progressDelegate != null)
                            progressDelegate(filePath, fileName, SCPFileTransferStatus.Transmitting, (ulong)entry.FileSize, transmitted);
                    }
                }
                catch (Exception e) {
                    SendError(stream, "failed to write to a file");
                    throw new SCPClientException("Failed to write to a file: " + filePath, e);
                }
            }

            if (modTime != null) {
                try {
                    File.SetLastWriteTimeUtc(filePath, modTime.MTime);
                    File.SetLastAccessTimeUtc(filePath, modTime.ATime);
                }
                catch (Exception e) {
                    SendError(stream, "failed to modify time of a file");
                    throw new SCPClientException("Failed to modify time of a file: " + filePath, e);
                }
            }

            CheckResponse(stream);
            stream.Write(ZERO);

            if (progressDelegate != null)
                progressDelegate(filePath, fileName, SCPFileTransferStatus.CompletedSuccess, (ulong)entry.FileSize, transmitted);

            return true;
        }