filestream.cs
来自「没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没」· CS 代码 · 共 911 行 · 第 1/2 页
CS
911 行
public override int ReadByte() { // Lock down the file stream while we do this. lock(this) { // Setup the object for reading. SetupRead(); // Read more data into the internal buffer if necessary. if(bufferPosn >= bufferLen) { bufferPosn = 0; bufferLen = FileMethods.Read (handle, buffer, 0, bufferSize); if(bufferLen < 0) { bufferLen = 0; throw new IOException (FileMethods.GetErrno(), _("IO_ReadFailed")); } else if(bufferLen == 0) { // We've reached EOF. return -1; } } // Extract the next byte from the buffer. ++position; return buffer[bufferPosn++]; } } // Seek to a new position within this stream. public override long Seek(long offset, SeekOrigin origin) { long newPosn; // Bail out if this stream is not capable of seeking. if(!canSeek) { throw new NotSupportedException(_("IO_NotSupp_Seek")); } // Lock down the file stream while we do this. lock(this) { // Bail out if the handle is invalid. if(handle == invalidHandle) { throw new ObjectDisposedException (_("IO_StreamClosed")); } // Don't do anything if the position won't be moving. if(origin == SeekOrigin.Begin && offset == position) { return offset; } else if(origin == SeekOrigin.Current && offset == 0) { return position; } // The behaviour depends upon the read/write mode. if(bufferOwnedByWrite) { // Flush the write buffer and then seek. FlushWriteBuffer(); newPosn = FileMethods.Seek(handle, offset, origin); if(newPosn == -1) { throw new EndOfStreamException (_("IO_EndOfStream")); } position = newPosn; } else { // Determine if the seek is to somewhere inside // the current read buffer bounds. if(origin == SeekOrigin.Begin) { newPosn = position - bufferPosn; if(offset >= newPosn && offset < (newPosn + bufferLen)) { bufferPosn = (int)(offset - newPosn); position = offset; return position; } } else if(origin == SeekOrigin.Current) { newPosn = position + offset; if(newPosn >= (position - bufferPosn) && newPosn < (position - bufferPosn + bufferLen)) { bufferPosn = (int)(newPosn - (position - bufferPosn)); position = newPosn; return position; } } // Abandon the read buffer. bufferPosn = 0; bufferLen = 0; // Seek to the new position. newPosn = FileMethods.Seek(handle, offset, origin); if(newPosn == -1) { throw new EndOfStreamException (_("IO_EndOfStream")); } position = newPosn; } return position; } } // Set the length of this stream. public override void SetLength(long value) { // Validate the parameters and setup the object for writing. if(value < 0) { throw new ArgumentOutOfRangeException ("value", _("ArgRange_NonNegative")); } if(!canSeek) { throw new NotSupportedException(_("IO_NotSupp_Seek")); } // Lock down the file stream while we do this. lock(this) { // Setup this object for writing. SetupWrite(); // Call the underlying platform's "SetLength" method. if(!FileMethods.SetLength(handle, value)) { throw new IOException (FileMethods.GetErrno(), _("IO_SetLengthFailed")); } } } // Write a buffer of bytes to this stream. public override void Write(byte[] buffer, int offset, int count) { int tempLen; // Validate the parameters and setup the object for writing. ValidateBuffer(buffer, offset, count); // Lock down the file stream while we do this. lock(this) { // Setup this object for writing. SetupWrite(); // Write data to the file stream. while(count > 0) { // Determine how many bytes we can write to the buffer. tempLen = bufferSize - bufferPosn; if(tempLen <= 0) { // Flush the current buffer contents. if(!FileMethods.Write (handle, this.buffer, 0, bufferPosn)) { throw new IOException (FileMethods.GetErrno(), _("IO_WriteFailed")); } bufferPosn = 0; tempLen = bufferSize; } if(tempLen > count) { tempLen = count; } // Can we short-cut the internal buffer? if(bufferPosn == 0 && tempLen == bufferSize) { // Yes: write the data directly to the file. if(!FileMethods.Write (handle, buffer, offset, tempLen)) { throw new IOException (FileMethods.GetErrno(), _("IO_WriteFailed")); } } else { // No: copy the data to the write buffer first. Array.Copy(buffer, offset, this.buffer, bufferPosn, tempLen); bufferPosn += tempLen; } // Advance the buffer and stream positions. position += tempLen; offset += tempLen; count -= tempLen; } // If the buffer is full, then do a speculative flush now, // rather than waiting for the next call to this method. if(bufferPosn >= bufferSize) { if(!FileMethods.Write (handle, this.buffer, 0, bufferPosn)) { throw new IOException (FileMethods.GetErrno(), _("IO_WriteFailed")); } bufferPosn = 0; } } } // Write a single byte to this stream. public override void WriteByte(byte value) { // Lock down the file stream while we do this. lock(this) { // Setup the object for writing. SetupWrite(); // Flush the current buffer if it is full. if(bufferPosn >= bufferSize) { if(!FileMethods.Write (handle, this.buffer, 0, bufferPosn)) { throw new IOException (FileMethods.GetErrno(), _("IO_WriteFailed")); } bufferPosn = 0; } // Write the byte into the buffer and advance the posn. buffer[bufferPosn++] = value; ++position; } } // Determine if it is possible to read from this stream. public override bool CanRead { get { return ((access & FileAccess.Read) != 0); } } // Determine if it is possible to seek within this stream. public override bool CanSeek { get { return canSeek; } } // Determine if it is possible to write to this stream. public override bool CanWrite { get { return ((access & FileAccess.Write) != 0); } } // Get the length of this stream. public override long Length { get { // Validate that the object can actually do this. if(!canSeek) { throw new NotSupportedException (_("IO_NotSupp_Seek")); } // Lock down the file stream while we do this. lock(this) { if(handle == invalidHandle) { // ECMA says this should be IOException even though // everywhere else uses ObjectDisposedException. throw new IOException (FileMethods.GetErrno(), _("IO_StreamClosed")); } // Flush the write buffer, because it may // affect the length of the stream. if(bufferOwnedByWrite) { FlushWriteBuffer(); } // Seek to the end to get the stream length. long posn = FileMethods.Seek (handle, 0, SeekOrigin.End); // Seek back to where we used to be. if(bufferOwnedByWrite) { FileMethods.Seek (handle, position - bufferPosn, SeekOrigin.Begin); } else { FileMethods.Seek (handle, position - bufferPosn + bufferLen, SeekOrigin.Begin); } // Decode the result. if(posn == -1) { throw new IOException (FileMethods.GetErrno(), _("IO_SeekFailed")); } return posn; } } } // Get the current position within the stream. public override long Position { get { if(!canSeek) { throw new NotSupportedException(_("IO_NotSupp_Seek")); } return position; } set { Seek(value, SeekOrigin.Begin); } } // Determine if this file stream was created in asynchronous mode. public virtual bool IsAsync { get { return isAsync; } }#if !ECMA_COMPAT // Get the name of the file underlying this stream object. public String Name { get { if(path != null) { return path; } else { return _("IO_UnknownFile"); } } } // Lock a region of the file stream. public virtual void Lock(long position, long length) { if(position < 0) { throw new ArgumentOutOfRangeException ("position", _("ArgRange_NonNegative")); } if(length < 0) { throw new ArgumentOutOfRangeException ("position", _("ArgRange_NonNegative")); } lock(this) { if(handle == invalidHandle) { throw new ObjectDisposedException(_("IO_StreamClosed")); } if(!FileMethods.Lock(handle, position, length)) { throw new IOException (FileMethods.GetErrno(), _("IO_LockFailed")); } } } // Unlock a region of the file stream. public virtual void Unlock(long position, long length) { if(position < 0) { throw new ArgumentOutOfRangeException ("position", _("ArgRange_NonNegative")); } if(length < 0) { throw new ArgumentOutOfRangeException ("position", _("ArgRange_NonNegative")); } lock(this) { if(handle == invalidHandle) { throw new ObjectDisposedException(_("IO_StreamClosed")); } if(!FileMethods.Unlock(handle, position, length)) { throw new IOException (FileMethods.GetErrno(), _("IO_UnlockFailed")); } } } // Get the underlying file stream handle. public virtual IntPtr Handle { get { Flush(); return handle; } }#endif // !ECMA_COMPAT}; // class FileStream}; // namespace System.IO
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?