diff options
author | Jiyoung Yun <jy910.yun@samsung.com> | 2017-06-13 18:47:36 +0900 |
---|---|---|
committer | Jiyoung Yun <jy910.yun@samsung.com> | 2017-06-13 18:47:36 +0900 |
commit | 61d6a817e39d3bae0f47dbc09838d51db22a5d30 (patch) | |
tree | cb37caa1784bc738b976273335d6ed04a7cc80b0 /src/mscorlib/src/System/IO | |
parent | 5b975f8233e8c8d17b215372f89ca713b45d6a0b (diff) | |
download | coreclr-61d6a817e39d3bae0f47dbc09838d51db22a5d30.tar.gz coreclr-61d6a817e39d3bae0f47dbc09838d51db22a5d30.tar.bz2 coreclr-61d6a817e39d3bae0f47dbc09838d51db22a5d30.zip |
Imported Upstream version 2.0.0.11992upstream/2.0.0.11992
Diffstat (limited to 'src/mscorlib/src/System/IO')
-rw-r--r-- | src/mscorlib/src/System/IO/BinaryWriter.cs | 1 | ||||
-rw-r--r-- | src/mscorlib/src/System/IO/DriveNotFoundException.cs | 2 | ||||
-rw-r--r-- | src/mscorlib/src/System/IO/EncodingCache.cs | 13 | ||||
-rw-r--r-- | src/mscorlib/src/System/IO/File.cs | 27 | ||||
-rw-r--r-- | src/mscorlib/src/System/IO/FileLoadException.CoreCLR.cs | 5 | ||||
-rw-r--r-- | src/mscorlib/src/System/IO/FileSystemEnumerable.cs | 19 | ||||
-rw-r--r-- | src/mscorlib/src/System/IO/IOException.cs | 2 | ||||
-rw-r--r-- | src/mscorlib/src/System/IO/MemoryStream.cs | 1 | ||||
-rw-r--r-- | src/mscorlib/src/System/IO/PinnedBufferMemoryStream.cs | 76 | ||||
-rw-r--r-- | src/mscorlib/src/System/IO/SearchOption.cs | 1 | ||||
-rw-r--r-- | src/mscorlib/src/System/IO/Stream.cs | 3 | ||||
-rw-r--r-- | src/mscorlib/src/System/IO/StreamReader.cs | 1 | ||||
-rw-r--r-- | src/mscorlib/src/System/IO/TextReader.cs | 3 | ||||
-rw-r--r-- | src/mscorlib/src/System/IO/UnmanagedMemoryStream.cs | 731 | ||||
-rw-r--r-- | src/mscorlib/src/System/IO/UnmanagedMemoryStreamWrapper.cs | 220 | ||||
-rw-r--r-- | src/mscorlib/src/System/IO/__Error.cs | 6 |
16 files changed, 41 insertions, 1070 deletions
diff --git a/src/mscorlib/src/System/IO/BinaryWriter.cs b/src/mscorlib/src/System/IO/BinaryWriter.cs index 3d9839f46e..644226fa79 100644 --- a/src/mscorlib/src/System/IO/BinaryWriter.cs +++ b/src/mscorlib/src/System/IO/BinaryWriter.cs @@ -27,7 +27,6 @@ namespace System.IO // primitives to an arbitrary stream. A subclass can override methods to // give unique encodings. // - [Serializable] public class BinaryWriter : IDisposable { public static readonly BinaryWriter Null = new BinaryWriter(); diff --git a/src/mscorlib/src/System/IO/DriveNotFoundException.cs b/src/mscorlib/src/System/IO/DriveNotFoundException.cs index 27b6f9015d..7e9b0c9f36 100644 --- a/src/mscorlib/src/System/IO/DriveNotFoundException.cs +++ b/src/mscorlib/src/System/IO/DriveNotFoundException.cs @@ -17,7 +17,6 @@ using System.Runtime.Serialization; namespace System.IO { //Thrown when trying to access a drive that is not availabe. - [Serializable] internal class DriveNotFoundException : IOException { public DriveNotFoundException() @@ -34,6 +33,7 @@ namespace System.IO protected DriveNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context) { + throw new PlatformNotSupportedException(); } } } diff --git a/src/mscorlib/src/System/IO/EncodingCache.cs b/src/mscorlib/src/System/IO/EncodingCache.cs deleted file mode 100644 index 53379bc77f..0000000000 --- a/src/mscorlib/src/System/IO/EncodingCache.cs +++ /dev/null @@ -1,13 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Text; - -namespace System.IO -{ - internal static class EncodingCache - { - internal static readonly Encoding UTF8NoBOM = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true); - } -} diff --git a/src/mscorlib/src/System/IO/File.cs b/src/mscorlib/src/System/IO/File.cs index 4aba1488ec..da962f6feb 100644 --- a/src/mscorlib/src/System/IO/File.cs +++ b/src/mscorlib/src/System/IO/File.cs @@ -150,13 +150,16 @@ namespace System.IO // Remove trialing slash since this can cause grief to FindFirstFile. You will get an invalid argument error String tempPath = path.TrimEnd(new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }); +#if !PLATFORM_UNIX // For floppy drives, normally the OS will pop up a dialog saying // there is no disk in drive A:, please insert one. We don't want that. - // SetErrorMode will let us disable this, but we should set the error + // SetThreadErrorMode will let us disable this, but we should set the error // mode back, since this may have wide-ranging effects. - int oldMode = Win32Native.SetErrorMode(Win32Native.SEM_FAILCRITICALERRORS); + uint oldMode; + bool errorModeSuccess = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out oldMode); try { +#endif bool error = false; SafeFindHandle handle = Win32Native.FindFirstFile(tempPath, findData); try @@ -197,31 +200,41 @@ namespace System.IO } } } +#if !PLATFORM_UNIX } finally { - Win32Native.SetErrorMode(oldMode); + if (errorModeSuccess) + Interop.Kernel32.SetThreadErrorMode(oldMode, out oldMode); } +#endif // Copy the information to data data.PopulateFrom(findData); } else { + bool success = false; + +#if !PLATFORM_UNIX // For floppy drives, normally the OS will pop up a dialog saying // there is no disk in drive A:, please insert one. We don't want that. - // SetErrorMode will let us disable this, but we should set the error + // SetThreadErrorMode will let us disable this, but we should set the error // mode back, since this may have wide-ranging effects. - bool success = false; - int oldMode = Win32Native.SetErrorMode(Win32Native.SEM_FAILCRITICALERRORS); + uint oldMode; + bool errorModeSuccess = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out oldMode); try { +#endif success = Win32Native.GetFileAttributesEx(path, GetFileExInfoStandard, ref data); +#if !PLATFORM_UNIX } finally { - Win32Native.SetErrorMode(oldMode); + if (errorModeSuccess) + Interop.Kernel32.SetThreadErrorMode(oldMode, out oldMode); } +#endif if (!success) { diff --git a/src/mscorlib/src/System/IO/FileLoadException.CoreCLR.cs b/src/mscorlib/src/System/IO/FileLoadException.CoreCLR.cs index f6415670e3..d4ce9a9ab9 100644 --- a/src/mscorlib/src/System/IO/FileLoadException.CoreCLR.cs +++ b/src/mscorlib/src/System/IO/FileLoadException.CoreCLR.cs @@ -27,7 +27,10 @@ namespace System.IO GetFileLoadExceptionMessage(hResult, JitHelpers.GetStringHandleOnStack(ref format)); string message = null; - GetMessageForHR(hResult, JitHelpers.GetStringHandleOnStack(ref message)); + if (hResult == System.__HResults.COR_E_BADEXEFORMAT) + message = SR.Arg_BadImageFormatException; + else + GetMessageForHR(hResult, JitHelpers.GetStringHandleOnStack(ref message)); return string.Format(CultureInfo.CurrentCulture, format, fileName, message); } diff --git a/src/mscorlib/src/System/IO/FileSystemEnumerable.cs b/src/mscorlib/src/System/IO/FileSystemEnumerable.cs index 0316de0f93..5e19fbed36 100644 --- a/src/mscorlib/src/System/IO/FileSystemEnumerable.cs +++ b/src/mscorlib/src/System/IO/FileSystemEnumerable.cs @@ -146,8 +146,10 @@ namespace System.IO private SearchOption searchOption; private String fullPath; private String normalizedSearchPath; - private int oldMode; - +#if !PLATFORM_UNIX + private int _oldMode; + private bool _setBackOldMode; +#endif internal FileSystemEnumerableIterator(String path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler<TSource> resultHandler, bool checkHost) { Contract.Requires(path != null); @@ -156,7 +158,9 @@ namespace System.IO Contract.Requires(searchOption == SearchOption.AllDirectories || searchOption == SearchOption.TopDirectoryOnly); Contract.Requires(resultHandler != null); - oldMode = Win32Native.SetErrorMode(Win32Native.SEM_FAILCRITICALERRORS); +#if !PLATFORM_UNIX + _setBackOldMode = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out _oldMode); +#endif searchStack = new List<Directory.SearchData>(); @@ -284,7 +288,14 @@ namespace System.IO } finally { - Win32Native.SetErrorMode(oldMode); +#if !PLATFORM_UNIX + if (_setBackOldMode) + { + uint _ignore; + Interop.Kernel32.SetThreadErrorMode(_oldMode, out _ignore); + } +#endif + base.Dispose(disposing); } } diff --git a/src/mscorlib/src/System/IO/IOException.cs b/src/mscorlib/src/System/IO/IOException.cs index 2628f7b652..65504de863 100644 --- a/src/mscorlib/src/System/IO/IOException.cs +++ b/src/mscorlib/src/System/IO/IOException.cs @@ -18,7 +18,6 @@ using System.Runtime.Serialization; namespace System.IO { - [Serializable] public class IOException : SystemException { // For debugging purposes, store the complete path in the IOException @@ -68,6 +67,7 @@ namespace System.IO protected IOException(SerializationInfo info, StreamingContext context) : base(info, context) { + throw new PlatformNotSupportedException(); } } } diff --git a/src/mscorlib/src/System/IO/MemoryStream.cs b/src/mscorlib/src/System/IO/MemoryStream.cs index 3d5668d774..daf09d1274 100644 --- a/src/mscorlib/src/System/IO/MemoryStream.cs +++ b/src/mscorlib/src/System/IO/MemoryStream.cs @@ -34,7 +34,6 @@ namespace System.IO // from an unsigned byte array, or you can create an empty one. Empty // memory streams are resizable, while ones created with a byte array provide // a stream "view" of the data. - [Serializable] public class MemoryStream : Stream { private byte[] _buffer; // Either allocated internally or externally. diff --git a/src/mscorlib/src/System/IO/PinnedBufferMemoryStream.cs b/src/mscorlib/src/System/IO/PinnedBufferMemoryStream.cs deleted file mode 100644 index 284cd927dc..0000000000 --- a/src/mscorlib/src/System/IO/PinnedBufferMemoryStream.cs +++ /dev/null @@ -1,76 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -/*============================================================ -** -** -** -** -** -** Purpose: Pins a byte[], exposing it as an unmanaged memory -** stream. Used in ResourceReader for corner cases. -** -** -===========================================================*/ - -using System; -using System.Runtime.InteropServices; -using System.Diagnostics; -using System.Diagnostics.Contracts; - -namespace System.IO -{ - internal sealed unsafe class PinnedBufferMemoryStream : UnmanagedMemoryStream - { - private byte[] _array; - private GCHandle _pinningHandle; - - // The new inheritance model requires a Critical default ctor since base (UnmanagedMemoryStream) has one - private PinnedBufferMemoryStream() : base() { } - - internal PinnedBufferMemoryStream(byte[] array) - { - Debug.Assert(array != null, "Array can't be null"); - - int len = array.Length; - // Handle 0 length byte arrays specially. - if (len == 0) - { - array = new byte[1]; - len = 0; - } - - _array = array; - _pinningHandle = new GCHandle(array, GCHandleType.Pinned); - // Now the byte[] is pinned for the lifetime of this instance. - // But I also need to get a pointer to that block of memory... - fixed (byte* ptr = &_array[0]) - Initialize(ptr, len, len, FileAccess.Read); - } - - ~PinnedBufferMemoryStream() - { - Dispose(false); - } - - protected override void Dispose(bool disposing) - { - if (_isOpen) - { - _pinningHandle.Free(); - _isOpen = false; - } -#if _DEBUG - // To help track down lifetime issues on checked builds, force - //a full GC here. - if (disposing) - { - GC.Collect(); - GC.WaitForPendingFinalizers(); - } -#endif - base.Dispose(disposing); - } - } -} diff --git a/src/mscorlib/src/System/IO/SearchOption.cs b/src/mscorlib/src/System/IO/SearchOption.cs index 75909d7499..c2f7e0db6e 100644 --- a/src/mscorlib/src/System/IO/SearchOption.cs +++ b/src/mscorlib/src/System/IO/SearchOption.cs @@ -20,7 +20,6 @@ using System; namespace System.IO { - [Serializable] internal enum SearchOption { // Include only the current directory in the search operation diff --git a/src/mscorlib/src/System/IO/Stream.cs b/src/mscorlib/src/System/IO/Stream.cs index 65cc4ddcb9..786dfedef9 100644 --- a/src/mscorlib/src/System/IO/Stream.cs +++ b/src/mscorlib/src/System/IO/Stream.cs @@ -30,7 +30,6 @@ using System.Reflection; namespace System.IO { - [Serializable] public abstract class Stream : MarshalByRefObject, IDisposable { public static readonly Stream Null = new NullStream(); @@ -854,7 +853,6 @@ namespace System.IO SynchronousAsyncResult.EndWrite(asyncResult); } - [Serializable] private sealed class NullStream : Stream { internal NullStream() { } @@ -1093,7 +1091,6 @@ namespace System.IO // SyncStream is a wrapper around a stream that takes // a lock for every operation making it thread safe. - [Serializable] internal sealed class SyncStream : Stream, IDisposable { private Stream _stream; diff --git a/src/mscorlib/src/System/IO/StreamReader.cs b/src/mscorlib/src/System/IO/StreamReader.cs index dfb928c85d..1fc72bffd1 100644 --- a/src/mscorlib/src/System/IO/StreamReader.cs +++ b/src/mscorlib/src/System/IO/StreamReader.cs @@ -16,7 +16,6 @@ namespace System.IO // This is designed for character input in a particular Encoding, // whereas the Stream class is designed for byte input and output. // - [Serializable] internal class StreamReader : TextReader { // StreamReader.Null is threadsafe. diff --git a/src/mscorlib/src/System/IO/TextReader.cs b/src/mscorlib/src/System/IO/TextReader.cs index 3da68591b0..2531778d6d 100644 --- a/src/mscorlib/src/System/IO/TextReader.cs +++ b/src/mscorlib/src/System/IO/TextReader.cs @@ -30,7 +30,6 @@ namespace System.IO { // // This class is intended for character input, not bytes. // There are methods on the Stream class for reading bytes. - [Serializable] internal abstract class TextReader : MarshalByRefObject, IDisposable { public static readonly TextReader Null = new NullTextReader(); @@ -263,7 +262,6 @@ namespace System.IO { return new SyncTextReader(reader); } - [Serializable] private sealed class NullTextReader : TextReader { public NullTextReader(){} @@ -280,7 +278,6 @@ namespace System.IO { } } - [Serializable] internal sealed class SyncTextReader : TextReader { internal TextReader _in; diff --git a/src/mscorlib/src/System/IO/UnmanagedMemoryStream.cs b/src/mscorlib/src/System/IO/UnmanagedMemoryStream.cs deleted file mode 100644 index f21fe47371..0000000000 --- a/src/mscorlib/src/System/IO/UnmanagedMemoryStream.cs +++ /dev/null @@ -1,731 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -/*============================================================ -** -** -** -** -** Purpose: Create a stream over unmanaged memory, mostly -** useful for memory-mapped files. -** -** -===========================================================*/ - -using System; -using System.Runtime; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Security; -using System.Threading; -using System.Diagnostics; -using System.Diagnostics.Contracts; -using System.Threading.Tasks; - - -namespace System.IO -{ - /* - * This class is used to access a contiguous block of memory, likely outside - * the GC heap (or pinned in place in the GC heap, but a MemoryStream may - * make more sense in those cases). It's great if you have a pointer and - * a length for a section of memory mapped in by someone else and you don't - * want to copy this into the GC heap. UnmanagedMemoryStream assumes these - * two things: - * - * 1) All the memory in the specified block is readable or writable, - * depending on the values you pass to the constructor. - * 2) The lifetime of the block of memory is at least as long as the lifetime - * of the UnmanagedMemoryStream. - * 3) You clean up the memory when appropriate. The UnmanagedMemoryStream - * currently will do NOTHING to free this memory. - * 4) All calls to Write and WriteByte may not be threadsafe currently. - * - * It may become necessary to add in some sort of - * DeallocationMode enum, specifying whether we unmap a section of memory, - * call free, run a user-provided delegate to free the memory, etc. - * We'll suggest user write a subclass of UnmanagedMemoryStream that uses - * a SafeHandle subclass to hold onto the memory. - * Check for problems when using this in the negative parts of a - * process's address space. We may need to use unsigned longs internally - * and change the overflow detection logic. - * - * -----SECURITY MODEL AND SILVERLIGHT----- - * A few key notes about exposing UMS in silverlight: - * 1. No ctors are exposed to transparent code. This version of UMS only - * supports byte* (not SafeBuffer). Therefore, framework code can create - * a UMS and hand it to transparent code. Transparent code can use most - * operations on a UMS, but not operations that directly expose a - * pointer. - * - * 2. Scope of "unsafe" and non-CLS compliant operations reduced: The - * Whidbey version of this class has CLSCompliant(false) at the class - * level and unsafe modifiers at the method level. These were reduced to - * only where the unsafe operation is performed -- i.e. immediately - * around the pointer manipulation. Note that this brings UMS in line - * with recent changes in pu/clr to support SafeBuffer. - * - * 3. Currently, the only caller that creates a UMS is ResourceManager, - * which creates read-only UMSs, and therefore operations that can - * change the length will throw because write isn't supported. A - * conservative option would be to formalize the concept that _only_ - * read-only UMSs can be creates, and enforce this by making WriteX and - * SetLength SecurityCritical. However, this is a violation of - * security inheritance rules, so we must keep these safe. The - * following notes make this acceptable for future use. - * a. a race condition in WriteX that could have allowed a thread to - * read from unzeroed memory was fixed - * b. memory region cannot be expanded beyond _capacity; in other - * words, a UMS creator is saying a writable UMS is safe to - * write to anywhere in the memory range up to _capacity, specified - * in the ctor. Even if the caller doesn't specify a capacity, then - * length is used as the capacity. - */ - public class UnmanagedMemoryStream : Stream - { - private const long UnmanagedMemStreamMaxLength = Int64.MaxValue; - - private SafeBuffer _buffer; - private unsafe byte* _mem; - private long _length; - private long _capacity; - private long _position; - private long _offset; - private FileAccess _access; - internal bool _isOpen; - [NonSerialized] - private Task<Int32> _lastReadTask; // The last successful task returned from ReadAsync - - - // Needed for subclasses that need to map a file, etc. - protected UnmanagedMemoryStream() - { - unsafe - { - _mem = null; - } - _isOpen = false; - } - - public UnmanagedMemoryStream(SafeBuffer buffer, long offset, long length) - { - Initialize(buffer, offset, length, FileAccess.Read); - } - - public UnmanagedMemoryStream(SafeBuffer buffer, long offset, long length, FileAccess access) - { - Initialize(buffer, offset, length, access); - } - - protected void Initialize(SafeBuffer buffer, long offset, long length, FileAccess access) - { - if (buffer == null) - { - throw new ArgumentNullException(nameof(buffer)); - } - if (offset < 0) - { - throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_NeedNonNegNum); - } - if (length < 0) - { - throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_NeedNonNegNum); - } - if (buffer.ByteLength < (ulong)(offset + length)) - { - throw new ArgumentException(SR.Argument_InvalidSafeBufferOffLen); - } - if (access < FileAccess.Read || access > FileAccess.ReadWrite) - { - throw new ArgumentOutOfRangeException(nameof(access)); - } - Contract.EndContractBlock(); - - if (_isOpen) - { - throw new InvalidOperationException(SR.InvalidOperation_CalledTwice); - } - - // check for wraparound - unsafe - { - byte* pointer = null; - RuntimeHelpers.PrepareConstrainedRegions(); - try - { - buffer.AcquirePointer(ref pointer); - if ((pointer + offset + length) < pointer) - { - throw new ArgumentException(SR.ArgumentOutOfRange_UnmanagedMemStreamWrapAround); - } - } - finally - { - if (pointer != null) - { - buffer.ReleasePointer(); - } - } - } - - _offset = offset; - _buffer = buffer; - _length = length; - _capacity = length; - _access = access; - _isOpen = true; - } - - [CLSCompliant(false)] - public unsafe UnmanagedMemoryStream(byte* pointer, long length) - { - Initialize(pointer, length, length, FileAccess.Read); - } - - [CLSCompliant(false)] - public unsafe UnmanagedMemoryStream(byte* pointer, long length, long capacity, FileAccess access) - { - Initialize(pointer, length, capacity, access); - } - - [CLSCompliant(false)] - protected unsafe void Initialize(byte* pointer, long length, long capacity, FileAccess access) - { - if (pointer == null) - throw new ArgumentNullException(nameof(pointer)); - if (length < 0 || capacity < 0) - throw new ArgumentOutOfRangeException((length < 0) ? nameof(length) : nameof(capacity), SR.ArgumentOutOfRange_NeedNonNegNum); - if (length > capacity) - throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_LengthGreaterThanCapacity); - Contract.EndContractBlock(); - // Check for wraparound. - if (((byte*)((long)pointer + capacity)) < pointer) - throw new ArgumentOutOfRangeException(nameof(capacity), SR.ArgumentOutOfRange_UnmanagedMemStreamWrapAround); - if (access < FileAccess.Read || access > FileAccess.ReadWrite) - throw new ArgumentOutOfRangeException(nameof(access), SR.ArgumentOutOfRange_Enum); - if (_isOpen) - throw new InvalidOperationException(SR.InvalidOperation_CalledTwice); - - _mem = pointer; - _offset = 0; - _length = length; - _capacity = capacity; - _access = access; - _isOpen = true; - } - - public override bool CanRead - { - [Pure] - get { return _isOpen && (_access & FileAccess.Read) != 0; } - } - - public override bool CanSeek - { - [Pure] - get { return _isOpen; } - } - - public override bool CanWrite - { - [Pure] - get { return _isOpen && (_access & FileAccess.Write) != 0; } - } - - protected override void Dispose(bool disposing) - { - _isOpen = false; - unsafe { _mem = null; } - - // Stream allocates WaitHandles for async calls. So for correctness - // call base.Dispose(disposing) for better perf, avoiding waiting - // for the finalizers to run on those types. - base.Dispose(disposing); - } - - public override void Flush() - { - if (!_isOpen) __Error.StreamIsClosed(); - } - - public override Task FlushAsync(CancellationToken cancellationToken) - { - if (cancellationToken.IsCancellationRequested) - return Task.FromCanceled(cancellationToken); - - try - { - Flush(); - return Task.CompletedTask; - } - catch (Exception ex) - { - return Task.FromException(ex); - } - } - - - public override long Length - { - get - { - if (!_isOpen) __Error.StreamIsClosed(); - return Interlocked.Read(ref _length); - } - } - - public long Capacity - { - get - { - if (!_isOpen) __Error.StreamIsClosed(); - return _capacity; - } - } - - public override long Position - { - get - { - if (!CanSeek) __Error.StreamIsClosed(); - Contract.EndContractBlock(); - return Interlocked.Read(ref _position); - } - set - { - if (value < 0) - throw new ArgumentOutOfRangeException(nameof(value), SR.ArgumentOutOfRange_NeedNonNegNum); - Contract.EndContractBlock(); - if (!CanSeek) __Error.StreamIsClosed(); - -#if !BIT64 - unsafe { - // On 32 bit machines, ensure we don't wrap around. - if (value > (long) Int32.MaxValue || _mem + value < _mem) - throw new ArgumentOutOfRangeException(nameof(value), SR.ArgumentOutOfRange_StreamLength); - } -#endif - Interlocked.Exchange(ref _position, value); - } - } - - [CLSCompliant(false)] - public unsafe byte* PositionPointer - { - get - { - if (_buffer != null) - { - throw new NotSupportedException(SR.NotSupported_UmsSafeBuffer); - } - - // Use a temp to avoid a race - long pos = Interlocked.Read(ref _position); - if (pos > _capacity) - throw new IndexOutOfRangeException(SR.IndexOutOfRange_UMSPosition); - byte* ptr = _mem + pos; - if (!_isOpen) __Error.StreamIsClosed(); - return ptr; - } - set - { - if (_buffer != null) - throw new NotSupportedException(SR.NotSupported_UmsSafeBuffer); - if (!_isOpen) __Error.StreamIsClosed(); - - // Note: subtracting pointers returns an Int64. Working around - // to avoid hitting compiler warning CS0652 on this line. - if (new IntPtr(value - _mem).ToInt64() > UnmanagedMemStreamMaxLength) - throw new ArgumentOutOfRangeException("offset", SR.ArgumentOutOfRange_UnmanagedMemStreamLength); - if (value < _mem) - throw new IOException(SR.IO_SeekBeforeBegin); - - Interlocked.Exchange(ref _position, value - _mem); - } - } - - internal unsafe byte* Pointer - { - get - { - if (_buffer != null) - throw new NotSupportedException(SR.NotSupported_UmsSafeBuffer); - - return _mem; - } - } - - public override int Read([In, Out] byte[] buffer, int offset, int count) - { - if (buffer == null) - throw new ArgumentNullException(nameof(buffer), SR.ArgumentNull_Buffer); - if (offset < 0) - throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_NeedNonNegNum); - if (count < 0) - throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum); - if (buffer.Length - offset < count) - throw new ArgumentException(SR.Argument_InvalidOffLen); - Contract.EndContractBlock(); // Keep this in sync with contract validation in ReadAsync - - if (!_isOpen) __Error.StreamIsClosed(); - if (!CanRead) __Error.ReadNotSupported(); - - // Use a local variable to avoid a race where another thread - // changes our position after we decide we can read some bytes. - long pos = Interlocked.Read(ref _position); - long len = Interlocked.Read(ref _length); - long n = len - pos; - if (n > count) - n = count; - if (n <= 0) - return 0; - - int nInt = (int)n; // Safe because n <= count, which is an Int32 - if (nInt < 0) - return 0; // _position could be beyond EOF - Debug.Assert(pos + nInt >= 0, "_position + n >= 0"); // len is less than 2^63 -1. - - unsafe - { - fixed (byte* pBuffer = buffer) - { - if (_buffer != null) - { - byte* pointer = null; - - RuntimeHelpers.PrepareConstrainedRegions(); - try - { - _buffer.AcquirePointer(ref pointer); - Buffer.Memcpy(pBuffer + offset, pointer + pos + _offset, nInt); - } - finally - { - if (pointer != null) - { - _buffer.ReleasePointer(); - } - } - } - else - { - Buffer.Memcpy(pBuffer + offset, _mem + pos, nInt); - } - } - } - Interlocked.Exchange(ref _position, pos + n); - return nInt; - } - - public override Task<Int32> ReadAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken) - { - if (buffer == null) - throw new ArgumentNullException(nameof(buffer), SR.ArgumentNull_Buffer); - if (offset < 0) - throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_NeedNonNegNum); - if (count < 0) - throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum); - if (buffer.Length - offset < count) - throw new ArgumentException(SR.Argument_InvalidOffLen); - Contract.EndContractBlock(); // contract validation copied from Read(...) - - if (cancellationToken.IsCancellationRequested) - return Task.FromCanceled<Int32>(cancellationToken); - - try - { - Int32 n = Read(buffer, offset, count); - Task<Int32> t = _lastReadTask; - return (t != null && t.Result == n) ? t : (_lastReadTask = Task.FromResult<Int32>(n)); - } - catch (Exception ex) - { - Debug.Assert(!(ex is OperationCanceledException)); - return Task.FromException<Int32>(ex); - } - } - - public override int ReadByte() - { - if (!_isOpen) __Error.StreamIsClosed(); - if (!CanRead) __Error.ReadNotSupported(); - - long pos = Interlocked.Read(ref _position); // Use a local to avoid a race condition - long len = Interlocked.Read(ref _length); - if (pos >= len) - return -1; - Interlocked.Exchange(ref _position, pos + 1); - int result; - if (_buffer != null) - { - unsafe - { - byte* pointer = null; - RuntimeHelpers.PrepareConstrainedRegions(); - try - { - _buffer.AcquirePointer(ref pointer); - result = *(pointer + pos + _offset); - } - finally - { - if (pointer != null) - { - _buffer.ReleasePointer(); - } - } - } - } - else - { - unsafe - { - result = _mem[pos]; - } - } - return result; - } - - public override long Seek(long offset, SeekOrigin loc) - { - if (!_isOpen) __Error.StreamIsClosed(); - if (offset > UnmanagedMemStreamMaxLength) - throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_UnmanagedMemStreamLength); - switch (loc) - { - case SeekOrigin.Begin: - if (offset < 0) - throw new IOException(SR.IO_SeekBeforeBegin); - Interlocked.Exchange(ref _position, offset); - break; - - case SeekOrigin.Current: - long pos = Interlocked.Read(ref _position); - if (offset + pos < 0) - throw new IOException(SR.IO_SeekBeforeBegin); - Interlocked.Exchange(ref _position, offset + pos); - break; - - case SeekOrigin.End: - long len = Interlocked.Read(ref _length); - if (len + offset < 0) - throw new IOException(SR.IO_SeekBeforeBegin); - Interlocked.Exchange(ref _position, len + offset); - break; - - default: - throw new ArgumentException(SR.Argument_InvalidSeekOrigin); - } - - long finalPos = Interlocked.Read(ref _position); - Debug.Assert(finalPos >= 0, "_position >= 0"); - return finalPos; - } - - public override void SetLength(long value) - { - if (value < 0) - throw new ArgumentOutOfRangeException("length", SR.ArgumentOutOfRange_NeedNonNegNum); - Contract.EndContractBlock(); - if (_buffer != null) - throw new NotSupportedException(SR.NotSupported_UmsSafeBuffer); - if (!_isOpen) __Error.StreamIsClosed(); - if (!CanWrite) __Error.WriteNotSupported(); - - if (value > _capacity) - throw new IOException(SR.IO_FixedCapacity); - - long pos = Interlocked.Read(ref _position); - long len = Interlocked.Read(ref _length); - if (value > len) - { - unsafe - { - Buffer.ZeroMemory(_mem + len, value - len); - } - } - Interlocked.Exchange(ref _length, value); - if (pos > value) - { - Interlocked.Exchange(ref _position, value); - } - } - - public override void Write(byte[] buffer, int offset, int count) - { - if (buffer == null) - throw new ArgumentNullException(nameof(buffer), SR.ArgumentNull_Buffer); - if (offset < 0) - throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_NeedNonNegNum); - if (count < 0) - throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum); - if (buffer.Length - offset < count) - throw new ArgumentException(SR.Argument_InvalidOffLen); - Contract.EndContractBlock(); // Keep contract validation in sync with WriteAsync(..) - - if (!_isOpen) __Error.StreamIsClosed(); - if (!CanWrite) __Error.WriteNotSupported(); - - long pos = Interlocked.Read(ref _position); // Use a local to avoid a race condition - long len = Interlocked.Read(ref _length); - long n = pos + count; - // Check for overflow - if (n < 0) - throw new IOException(SR.IO_StreamTooLong); - - if (n > _capacity) - { - throw new NotSupportedException(SR.IO_FixedCapacity); - } - - if (_buffer == null) - { - // Check to see whether we are now expanding the stream and must - // zero any memory in the middle. - if (pos > len) - { - unsafe - { - Buffer.ZeroMemory(_mem + len, pos - len); - } - } - - // set length after zeroing memory to avoid race condition of accessing unzeroed memory - if (n > len) - { - Interlocked.Exchange(ref _length, n); - } - } - - unsafe - { - fixed (byte* pBuffer = buffer) - { - if (_buffer != null) - { - long bytesLeft = _capacity - pos; - if (bytesLeft < count) - { - throw new ArgumentException(SR.Arg_BufferTooSmall); - } - - byte* pointer = null; - RuntimeHelpers.PrepareConstrainedRegions(); - try - { - _buffer.AcquirePointer(ref pointer); - Buffer.Memcpy(pointer + pos + _offset, pBuffer + offset, count); - } - finally - { - if (pointer != null) - { - _buffer.ReleasePointer(); - } - } - } - else - { - Buffer.Memcpy(_mem + pos, pBuffer + offset, count); - } - } - } - Interlocked.Exchange(ref _position, n); - return; - } - - public override Task WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken) - { - if (buffer == null) - throw new ArgumentNullException(nameof(buffer), SR.ArgumentNull_Buffer); - if (offset < 0) - throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_NeedNonNegNum); - if (count < 0) - throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum); - if (buffer.Length - offset < count) - throw new ArgumentException(SR.Argument_InvalidOffLen); - Contract.EndContractBlock(); // contract validation copied from Write(..) - - if (cancellationToken.IsCancellationRequested) - return Task.FromCanceled(cancellationToken); - - try - { - Write(buffer, offset, count); - return Task.CompletedTask; - } - catch (Exception ex) - { - Debug.Assert(!(ex is OperationCanceledException)); - return Task.FromException<Int32>(ex); - } - } - - - public override void WriteByte(byte value) - { - if (!_isOpen) __Error.StreamIsClosed(); - if (!CanWrite) __Error.WriteNotSupported(); - - long pos = Interlocked.Read(ref _position); // Use a local to avoid a race condition - long len = Interlocked.Read(ref _length); - long n = pos + 1; - if (pos >= len) - { - // Check for overflow - if (n < 0) - throw new IOException(SR.IO_StreamTooLong); - - if (n > _capacity) - throw new NotSupportedException(SR.IO_FixedCapacity); - - // Check to see whether we are now expanding the stream and must - // zero any memory in the middle. - // don't do if created from SafeBuffer - if (_buffer == null) - { - if (pos > len) - { - unsafe - { - Buffer.ZeroMemory(_mem + len, pos - len); - } - } - - // set length after zeroing memory to avoid race condition of accessing unzeroed memory - Interlocked.Exchange(ref _length, n); - } - } - - if (_buffer != null) - { - unsafe - { - byte* pointer = null; - RuntimeHelpers.PrepareConstrainedRegions(); - try - { - _buffer.AcquirePointer(ref pointer); - *(pointer + pos + _offset) = value; - } - finally - { - if (pointer != null) - { - _buffer.ReleasePointer(); - } - } - } - } - else - { - unsafe - { - _mem[pos] = value; - } - } - Interlocked.Exchange(ref _position, n); - } - } -} diff --git a/src/mscorlib/src/System/IO/UnmanagedMemoryStreamWrapper.cs b/src/mscorlib/src/System/IO/UnmanagedMemoryStreamWrapper.cs deleted file mode 100644 index 86e4707dfd..0000000000 --- a/src/mscorlib/src/System/IO/UnmanagedMemoryStreamWrapper.cs +++ /dev/null @@ -1,220 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -/*============================================================ -** -** -** -** -** Purpose: Create a Memorystream over an UnmanagedMemoryStream -** -===========================================================*/ - -using System; -using System.Runtime.InteropServices; -using System.Diagnostics.CodeAnalysis; -using System.Diagnostics.Contracts; -using System.Threading; -using System.Threading.Tasks; - -namespace System.IO -{ - // Needed for backwards compatibility with V1.x usages of the - // ResourceManager, where a MemoryStream is now returned as an - // UnmanagedMemoryStream from ResourceReader. - internal sealed class UnmanagedMemoryStreamWrapper : MemoryStream - { - private UnmanagedMemoryStream _unmanagedStream; - - internal UnmanagedMemoryStreamWrapper(UnmanagedMemoryStream stream) - { - _unmanagedStream = stream; - } - - public override bool CanRead - { - [Pure] - get { return _unmanagedStream.CanRead; } - } - - public override bool CanSeek - { - [Pure] - get { return _unmanagedStream.CanSeek; } - } - - public override bool CanWrite - { - [Pure] - get { return _unmanagedStream.CanWrite; } - } - - protected override void Dispose(bool disposing) - { - try - { - if (disposing) - _unmanagedStream.Close(); - } - finally - { - base.Dispose(disposing); - } - } - - public override void Flush() - { - _unmanagedStream.Flush(); - } - - public override byte[] GetBuffer() - { - throw new UnauthorizedAccessException(SR.UnauthorizedAccess_MemStreamBuffer); - } - - public override bool TryGetBuffer(out ArraySegment<byte> buffer) - { - buffer = default(ArraySegment<byte>); - return false; - } - - public override int Capacity - { - get - { - return (int)_unmanagedStream.Capacity; - } - [SuppressMessage("Microsoft.Contracts", "CC1055")] // Skip extra error checking to avoid *potential* AppCompat problems. - set - { - throw new IOException(SR.IO_FixedCapacity); - } - } - - public override long Length - { - get - { - return _unmanagedStream.Length; - } - } - - public override long Position - { - get - { - return _unmanagedStream.Position; - } - set - { - _unmanagedStream.Position = value; - } - } - - public override int Read([In, Out] byte[] buffer, int offset, int count) - { - return _unmanagedStream.Read(buffer, offset, count); - } - - public override int ReadByte() - { - return _unmanagedStream.ReadByte(); - } - - public override long Seek(long offset, SeekOrigin loc) - { - return _unmanagedStream.Seek(offset, loc); - } - - public unsafe override byte[] ToArray() - { - if (!_unmanagedStream._isOpen) __Error.StreamIsClosed(); - if (!_unmanagedStream.CanRead) __Error.ReadNotSupported(); - - byte[] buffer = new byte[_unmanagedStream.Length]; - Buffer.Memcpy(buffer, 0, _unmanagedStream.Pointer, 0, (int)_unmanagedStream.Length); - return buffer; - } - - public override void Write(byte[] buffer, int offset, int count) - { - _unmanagedStream.Write(buffer, offset, count); - } - - public override void WriteByte(byte value) - { - _unmanagedStream.WriteByte(value); - } - - // Writes this MemoryStream to another stream. - public unsafe override void WriteTo(Stream stream) - { - if (stream == null) - throw new ArgumentNullException(nameof(stream), SR.ArgumentNull_Stream); - Contract.EndContractBlock(); - - if (!_unmanagedStream._isOpen) __Error.StreamIsClosed(); - if (!CanRead) __Error.ReadNotSupported(); - - byte[] buffer = ToArray(); - - stream.Write(buffer, 0, buffer.Length); - } - - public override void SetLength(Int64 value) - { - // This was probably meant to call _unmanagedStream.SetLength(value), but it was forgotten in V.4.0. - // Now this results in a call to the base which touches the underlying array which is never actually used. - // We cannot fix it due to compat now, but we should fix this at the next SxS release oportunity. - base.SetLength(value); - } - - - public override Task CopyToAsync(Stream destination, Int32 bufferSize, CancellationToken cancellationToken) - { - // The parameter checks must be in sync with the base version: - if (destination == null) - throw new ArgumentNullException(nameof(destination)); - - if (bufferSize <= 0) - throw new ArgumentOutOfRangeException(nameof(bufferSize), SR.ArgumentOutOfRange_NeedPosNum); - - if (!CanRead && !CanWrite) - throw new ObjectDisposedException(null, SR.ObjectDisposed_StreamClosed); - - if (!destination.CanRead && !destination.CanWrite) - throw new ObjectDisposedException(nameof(destination), SR.ObjectDisposed_StreamClosed); - - if (!CanRead) - throw new NotSupportedException(SR.NotSupported_UnreadableStream); - - if (!destination.CanWrite) - throw new NotSupportedException(SR.NotSupported_UnwritableStream); - - Contract.EndContractBlock(); - - return _unmanagedStream.CopyToAsync(destination, bufferSize, cancellationToken); - } - - - public override Task FlushAsync(CancellationToken cancellationToken) - { - return _unmanagedStream.FlushAsync(cancellationToken); - } - - - public override Task<Int32> ReadAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken) - { - return _unmanagedStream.ReadAsync(buffer, offset, count, cancellationToken); - } - - - public override Task WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken) - { - return _unmanagedStream.WriteAsync(buffer, offset, count, cancellationToken); - } - } // class UnmanagedMemoryStreamWrapper -} // namespace - - diff --git a/src/mscorlib/src/System/IO/__Error.cs b/src/mscorlib/src/System/IO/__Error.cs index 70f83261ed..1af195ef77 100644 --- a/src/mscorlib/src/System/IO/__Error.cs +++ b/src/mscorlib/src/System/IO/__Error.cs @@ -206,11 +206,5 @@ namespace System.IO { throw new NotSupportedException(SR.NotSupported_UnwritableStream); } - - // From WinError.h - internal const int ERROR_FILE_NOT_FOUND = Win32Native.ERROR_FILE_NOT_FOUND; - internal const int ERROR_PATH_NOT_FOUND = Win32Native.ERROR_PATH_NOT_FOUND; - internal const int ERROR_ACCESS_DENIED = Win32Native.ERROR_ACCESS_DENIED; - internal const int ERROR_INVALID_PARAMETER = Win32Native.ERROR_INVALID_PARAMETER; } } |