System.IO.BufferedStream.Flush C# (CSharp) Méthode

Flush() public méthode

public Flush ( ) : void
Résultat void
        public override void Flush()
        {
            EnsureNotClosed();

            // Has write data in the buffer:
            if (_writePos > 0)
            {
                FlushWrite();
                Debug.Assert(_writePos == 0 && _readPos == 0 && _readLen == 0);
                return;
            }

            // Has read data in the buffer:
            if (_readPos < _readLen)
            {
                // If the underlying stream is not seekable AND we have something in the read buffer, then FlushRead would throw.
                // We can either throw away the buffer resulting in data loss (!) or ignore the Flush.
                // (We cannot throw because it would be a breaking change.) We opt into ignoring the Flush in that situation.
                if (!_stream.CanSeek)
                    return;

                FlushRead();

                // User streams may have opted to throw from Flush if CanWrite is false (although the abstract Stream does not do so).
                // However, if we do not forward the Flush to the underlying stream, we may have problems when chaining several streams.
                // Let us make a best effort attempt:
                if (_stream.CanWrite)
                    _stream.Flush();

                Debug.Assert(_writePos == 0 && _readPos == 0 && _readLen == 0);
                return;
            }

            // We had no data in the buffer, but we still need to tell the underlying stream to flush.
            if (_stream.CanWrite)
                _stream.Flush();

            _writePos = _readPos = _readLen = 0;
        }

Usage Example

Exemple #1
0
        public void putFile()
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("这个请求是一个普通文件");
            Console.ResetColor();

            outputStream.WriteLine("HTTP/1.0 200 OK");
            outputStream.WriteLine("Content-Type: application/octet-stream");
            outputStream.WriteLine("Content-Disposition: attachment");
            outputStream.WriteLine("Connection: close");
            outputStream.WriteLine("");
            outputStream.Flush();

            FileStream stream = new FileStream(http_url_ondisk, FileMode.Open, FileAccess.Read);

            try
            {
                byte[] buff2 = new byte[1024];
                int    count = 0;
                while ((count = stream.Read(buff2, 0, 1024)) != 0)
                {
                    outputByteStream.Write(buff2, 0, count);
                }
                outputByteStream.Flush();
                outputByteStream.Close();
                stream.Close();
            }
            catch (Exception)
            {
                Console.WriteLine("停止传输文件");
                stream.Close();
                outputByteStream.Close();
            }
        }
All Usage Examples Of System.IO.BufferedStream::Flush