Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 27 additions & 25 deletions mcs/class/corlib/System.IO/FileStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,7 @@ internal FileStream (IntPtr handle, FileAccess access, bool ownsHandle, int buff
#else
this.anonymous = false;
#endif
if (isZeroSize)
bufferSize = 1;

InitBuffer (bufferSize);
InitBuffer (bufferSize, isZeroSize);

if (canseek) {
buf_start = MonoIO.Seek (handle, 0, SeekOrigin.Current, out error);
Expand Down Expand Up @@ -335,7 +332,7 @@ internal FileStream (string path, FileMode mode, FileAccess access, FileShare sh
}
}

InitBuffer (bufferSize);
InitBuffer (bufferSize, false);

if (mode==FileMode.Append) {
this.Seek (0, SeekOrigin.End);
Expand Down Expand Up @@ -1089,30 +1086,35 @@ private int ReadData (IntPtr handle, byte[] buf, int offset,
return(amount);
}

void InitBuffer (int size)
void InitBuffer (int size, bool isZeroSize)
{
if (size <= 0)
throw new ArgumentOutOfRangeException ("bufferSize", "Positive number required.");

size = Math.Max (size, 8);

//
// Instead of allocating a new default buffer use the
// last one if there is any available
//
if (size <= DefaultBufferSize && buf_recycle != null) {
lock (buf_recycle_lock) {
if (buf_recycle != null) {
buf = buf_recycle;
buf_recycle = null;
if (isZeroSize) {
size = 0;
buf = new byte[1];
} else {
if (size <= 0)
throw new ArgumentOutOfRangeException ("bufferSize", "Positive number required.");

size = Math.Max (size, 8);

//
// Instead of allocating a new default buffer use the
// last one if there is any available
//
if (size <= DefaultBufferSize && buf_recycle != null) {
lock (buf_recycle_lock) {
if (buf_recycle != null) {
buf = buf_recycle;
buf_recycle = null;
}
}
}

if (buf == null)
buf = new byte [size];
else
Array.Clear (buf, 0, size);
}

if (buf == null)
buf = new byte [size];
else
Array.Clear (buf, 0, size);

buf_size = size;
// buf_start = 0;
Expand Down