System.IO.FileStream.Write C# (CSharp) Method

Write() public method

public Write ( byte array, int offset, int count ) : void
array byte
offset int
count int
return void
        public override void Write(byte[] array, int offset, int count)
        {
            ValidateReadWriteArgs(array, offset, count);

            if (_writePos == 0)
            {
                // Ensure we can write to the stream, and ready buffer for writing.
                if (!CanWrite) throw Error.GetWriteNotSupported();
                if (_readPos < _readLength) FlushReadBuffer();
                _readPos = 0;
                _readLength = 0;
            }

            // If our buffer has data in it, copy data from the user's array into
            // the buffer, and if we can fit it all there, return.  Otherwise, write
            // the buffer to disk and copy any remaining data into our buffer.
            // The assumption here is memcpy is cheaper than disk (or net) IO.
            // (10 milliseconds to disk vs. ~20-30 microseconds for a 4K memcpy)
            // So the extra copying will reduce the total number of writes, in 
            // non-pathological cases (i.e. write 1 byte, then write for the buffer 
            // size repeatedly)
            if (_writePos > 0)
            {
                int numBytes = _bufferLength - _writePos;   // space left in buffer
                if (numBytes > 0)
                {
                    if (numBytes > count)
                        numBytes = count;
                    Buffer.BlockCopy(array, offset, GetBuffer(), _writePos, numBytes);
                    _writePos += numBytes;
                    if (count == numBytes) return;
                    offset += numBytes;
                    count -= numBytes;
                }
                // Reset our buffer.  We essentially want to call FlushWrite
                // without calling Flush on the underlying Stream.

                if (_useAsyncIO)
                {
                    WriteInternalCoreAsync(GetBuffer(), 0, _writePos, CancellationToken.None).GetAwaiter().GetResult();
                }
                else
                {
                    WriteCore(GetBuffer(), 0, _writePos);
                }
                _writePos = 0;
            }
            // If the buffer would slow writes down, avoid buffer completely.
            if (count >= _bufferLength)
            {
                Debug.Assert(_writePos == 0, "FileStream cannot have buffered data to write here!  Your stream will be corrupted.");
                WriteCore(array, offset, count);
                return;
            }
            else if (count == 0)
            {
                return;  // Don't allocate a buffer then call memcpy for 0 bytes.
            }

            // Copy remaining bytes into buffer, to write at a later date.
            Buffer.BlockCopy(array, offset, GetBuffer(), _writePos, count);
            _writePos = count;
            return;
        }

Usage Example

        static void EncodeFile(byte[] key, String sourceFile, string destFile)
        {
            // initialize keyed hash object
            HMACSHA1 hms = new HMACSHA1(key);

            // open filestreams to read in original file and write out coded file
            using(FileStream inStream = new FileStream(sourceFile,FileMode.Open))
            using (FileStream outStream = new FileStream(destFile, FileMode.Create))
            {
                // array to hold keyed hash value of original file
                byte[] hashValue = hms.ComputeHash(inStream);

                // reset instream ready to read original file contents from start
                inStream.Position = 0;

                // write keyed hash value to coded file
                outStream.Write(hashValue, 0, hashValue.Length);

                // copy data from original file to output file, 1K at a time
                int bytesRead;
                byte[] buffer = new byte[1024];
                do
                {
                    bytesRead = inStream.Read(buffer, 0, 1024);
                    outStream.Write(buffer, 0, bytesRead);
                } while (bytesRead > 0);
                hms.Clear();

                inStream.Close();
                outStream.Close();
            }
        }
All Usage Examples Of System.IO.FileStream::Write