Halve the stack depth for both the new single step tests
[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             StringBuilder sb = new StringBuilder(512);
38             int result = SafeNativeMethods.FormatMessage(NativeMethods.FORMAT_MESSAGE_IGNORE_INSERTS |
39                 NativeMethods.FORMAT_MESSAGE_FROM_SYSTEM | NativeMethods.FORMAT_MESSAGE_ARGUMENT_ARRAY,
40                 IntPtr.Zero, (uint) errorCode, 0, sb, sb.Capacity, null);
41             if (result != 0) 
42             {
43                 // result is the # of characters copied to the StringBuilder on NT,
44                 // but on Win9x, it appears to be the number of MBCS bytes.
45                 // Just give up and return the String as-is...
46                 String s = sb.ToString();
47                 return s;
48             }
49             else 
50             {
51                 return SR.GetString(SR.IO_UnknownError, errorCode);
52             }
53         }
54
55 #if !FEATURE_NETCORE
56         internal static void FileNotOpen() 
57         {
58             throw new ObjectDisposedException(null, SR.GetString(SR.Port_not_open));
59         }
60
61         internal static void WrongAsyncResult() 
62         {
63             throw new ArgumentException(SR.GetString(SR.Arg_WrongAsyncResult));
64         }
65
66         internal static void EndReadCalledTwice() 
67         {
68             // Should ideally be InvalidOperationExc but we can't maintain parity with Stream and SerialStream without some work
69             throw new ArgumentException(SR.GetString(SR.InvalidOperation_EndReadCalledMultiple));
70         }
71
72         internal static void EndWriteCalledTwice() 
73         {
74             // Should ideally be InvalidOperationExc but we can't maintain parity with Stream and SerialStream without some work
75             throw new ArgumentException(SR.GetString(SR.InvalidOperation_EndWriteCalledMultiple));
76         }
77 #endif
78
79 #if FEATURE_NETCORE
80         [SecuritySafeCritical]
81 #endif
82         internal static void WinIOError() 
83         {
84             int errorCode = Marshal.GetLastWin32Error();
85             WinIOError(errorCode, String.Empty);
86         }
87
88 #if FEATURE_NETCORE
89         [SecuritySafeCritical]
90 #endif
91         internal static void WinIOError(string str) 
92         {
93             int errorCode = Marshal.GetLastWin32Error();
94             WinIOError(errorCode, str);
95         }
96         
97         // After calling GetLastWin32Error(), it clears the last error field,
98         // so you must save the HResult and pass it to this method.  This method
99         // will determine the appropriate exception to throw dependent on your 
100         // error, and depending on the error, insert a string into the message 
101         // gotten from the ResourceManager.
102         internal static void WinIOError(int errorCode, String str) 
103         {
104             switch (errorCode) 
105             {
106                 case NativeMethods.ERROR_FILE_NOT_FOUND:
107                 case NativeMethods.ERROR_PATH_NOT_FOUND:
108                     if (str.Length == 0)
109                         throw new IOException(SR.GetString(SR.IO_PortNotFound));
110                     else
111                         throw new IOException(SR.GetString(SR.IO_PortNotFoundFileName, str));
112                 
113                 case NativeMethods.ERROR_ACCESS_DENIED:
114                     if (str.Length == 0)
115                         throw new UnauthorizedAccessException(SR.GetString(SR.UnauthorizedAccess_IODenied_NoPathName));
116                     else
117                         throw new UnauthorizedAccessException(SR.GetString(SR.UnauthorizedAccess_IODenied_Path, str));
118
119                 case NativeMethods.ERROR_FILENAME_EXCED_RANGE:
120                     throw new PathTooLongException(SR.GetString(SR.IO_PathTooLong));
121
122                 case NativeMethods.ERROR_SHARING_VIOLATION:
123                     // error message.
124                     if (str.Length == 0)
125                         throw new IOException(SR.GetString(SR.IO_SharingViolation_NoFileName));
126                     else
127                         throw new IOException(SR.GetString(SR.IO_SharingViolation_File, str));
128
129                 default:
130                     throw new IOException(GetMessage(errorCode), MakeHRFromErrorCode(errorCode));
131             }
132         }
133     
134         // Use this to translate error codes like the above into HRESULTs like
135         // 0x80070006 for ERROR_INVALID_HANDLE
136         internal static int MakeHRFromErrorCode(int errorCode)
137         {
138             return unchecked(((int)0x80070000) | errorCode);
139         }
140
141     }
142 }
143     
144