Merge pull request #3142 from henricm/fix-for-win-mono_string_to_utf8
[mono.git] / mcs / class / referencesource / System / sys / system / IO / ports / InternalResources.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 /*============================================================
7 **
8 ** Class:  InternalResources
9 **
10 ** Date:  August 2002
11 **
12 ===========================================================*/
13
14 using System.IO;
15 using System.Security;
16 using System.Text;
17 using Marshal=System.Runtime.InteropServices.Marshal;
18 using Microsoft.Win32;
19
20 namespace System.IO.Ports
21 {
22     internal static class InternalResources
23     {
24         // Beginning of static Error methods
25 #if !FEATURE_NETCORE
26         internal static void EndOfFile() 
27         {
28             throw new EndOfStreamException(SR.GetString(SR.IO_EOF_ReadBeyondEOF));
29         }
30 #endif
31     
32 #if FEATURE_NETCORE
33         [SecuritySafeCritical]
34 #endif
35         internal static String GetMessage(int errorCode) 
36         {
37 #if !MONO
38             StringBuilder sb = new StringBuilder(512);
39             int result = SafeNativeMethods.FormatMessage(NativeMethods.FORMAT_MESSAGE_IGNORE_INSERTS |
40                 NativeMethods.FORMAT_MESSAGE_FROM_SYSTEM | NativeMethods.FORMAT_MESSAGE_ARGUMENT_ARRAY,
41                 IntPtr.Zero, (uint) errorCode, 0, sb, sb.Capacity, null);
42             if (result != 0) 
43             {
44                 // result is the # of characters copied to the StringBuilder on NT,
45                 // but on Win9x, it appears to be the number of MBCS bytes.
46                 // Just give up and return the String as-is...
47                 String s = sb.ToString();
48                 return s;
49             }
50             else 
51 #endif
52             {
53                 return SR.GetString(SR.IO_UnknownError, errorCode);
54             }
55         }
56
57 #if !FEATURE_NETCORE
58         internal static void FileNotOpen() 
59         {
60             throw new ObjectDisposedException(null, SR.GetString(SR.Port_not_open));
61         }
62
63         internal static void WrongAsyncResult() 
64         {
65             throw new ArgumentException(SR.GetString(SR.Arg_WrongAsyncResult));
66         }
67
68         internal static void EndReadCalledTwice() 
69         {
70             // Should ideally be InvalidOperationExc but we can't maintain parity with Stream and SerialStream without some work
71             throw new ArgumentException(SR.GetString(SR.InvalidOperation_EndReadCalledMultiple));
72         }
73
74         internal static void EndWriteCalledTwice() 
75         {
76             // Should ideally be InvalidOperationExc but we can't maintain parity with Stream and SerialStream without some work
77             throw new ArgumentException(SR.GetString(SR.InvalidOperation_EndWriteCalledMultiple));
78         }
79 #endif
80
81 #if FEATURE_NETCORE
82         [SecuritySafeCritical]
83 #endif
84         internal static void WinIOError() 
85         {
86             int errorCode = Marshal.GetLastWin32Error();
87             WinIOError(errorCode, String.Empty);
88         }
89
90 #if FEATURE_NETCORE
91         [SecuritySafeCritical]
92 #endif
93         internal static void WinIOError(string str) 
94         {
95             int errorCode = Marshal.GetLastWin32Error();
96             WinIOError(errorCode, str);
97         }
98         
99         // After calling GetLastWin32Error(), it clears the last error field,
100         // so you must save the HResult and pass it to this method.  This method
101         // will determine the appropriate exception to throw dependent on your 
102         // error, and depending on the error, insert a string into the message 
103         // gotten from the ResourceManager.
104         internal static void WinIOError(int errorCode, String str) 
105         {
106             switch (errorCode) 
107             {
108                 case NativeMethods.ERROR_FILE_NOT_FOUND:
109                 case NativeMethods.ERROR_PATH_NOT_FOUND:
110                     if (str.Length == 0)
111                         throw new IOException(SR.GetString(SR.IO_PortNotFound));
112                     else
113                         throw new IOException(SR.GetString(SR.IO_PortNotFoundFileName, str));
114                 
115                 case NativeMethods.ERROR_ACCESS_DENIED:
116                     if (str.Length == 0)
117                         throw new UnauthorizedAccessException(SR.GetString(SR.UnauthorizedAccess_IODenied_NoPathName));
118                     else
119                         throw new UnauthorizedAccessException(SR.GetString(SR.UnauthorizedAccess_IODenied_Path, str));
120
121                 case NativeMethods.ERROR_FILENAME_EXCED_RANGE:
122                     throw new PathTooLongException(SR.GetString(SR.IO_PathTooLong));
123
124                 case NativeMethods.ERROR_SHARING_VIOLATION:
125                     // error message.
126                     if (str.Length == 0)
127                         throw new IOException(SR.GetString(SR.IO_SharingViolation_NoFileName));
128                     else
129                         throw new IOException(SR.GetString(SR.IO_SharingViolation_File, str));
130
131                 default:
132                     throw new IOException(GetMessage(errorCode), MakeHRFromErrorCode(errorCode));
133             }
134         }
135     
136         // Use this to translate error codes like the above into HRESULTs like
137         // 0x80070006 for ERROR_INVALID_HANDLE
138         internal static int MakeHRFromErrorCode(int errorCode)
139         {
140             return unchecked(((int)0x80070000) | errorCode);
141         }
142
143     }
144 }
145     
146