2007-11-14 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mcs / class / corlib / System / Buffer.cs
1 //
2 // System.Buffer.cs
3 //
4 // Authors:
5 //   Paolo Molaro (lupus@ximian.com)
6 //   Dan Lewis (dihlewis@yahoo.co.uk)
7 //
8 // (C) 2001 Ximian, Inc.  http://www.ximian.com
9 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System.Runtime.CompilerServices;
35 using System.Runtime.InteropServices;
36
37 namespace System {
38 #if NET_2_0
39         [ComVisible (true)]
40 #endif
41         public 
42 #if NET_2_0
43                 static
44 #else
45                 sealed
46 #endif
47         class Buffer {
48 #if !NET_2_0
49                 private Buffer ()
50                 {
51                 }
52 #endif
53
54                 public static int ByteLength (Array array)
55                 {
56                         // note: the other methods in this class also use ByteLength to test for
57                         // null and non-primitive arguments as a side-effect.
58
59                         if (array == null)
60                                 throw new ArgumentNullException ("array");
61
62                         int length = ByteLengthInternal (array);
63                         if (length < 0)
64                                 throw new ArgumentException (Locale.GetText ("Object must be an array of primitives."));
65
66                         return length;
67                 }
68
69                 public static byte GetByte (Array array, int index)
70                 {
71                         if (index < 0 || index >= ByteLength (array))
72                                 throw new ArgumentOutOfRangeException ("index", Locale.GetText(
73                                         "Value must be non-negative and less than the size of the collection."));
74
75                         return GetByteInternal (array, index);
76                 }
77
78                 public static void SetByte (Array array, int index, byte value)
79                 {
80                         if (index < 0 || index >= ByteLength (array))
81                                 throw new ArgumentOutOfRangeException ("index", Locale.GetText(
82                                         "Value must be non-negative and less than the size of the collection."));
83
84                         SetByteInternal (array, index, value);
85                 }
86
87                 public static void BlockCopy (Array src, int srcOffset, Array dest, int destOffset, int count)
88                 {
89                         if (src == null)
90                                 throw new ArgumentNullException ("src");
91
92                         if (dest == null)
93                                 throw new ArgumentNullException ("dest");
94
95                         if (srcOffset < 0)
96                                 throw new ArgumentOutOfRangeException ("srcOffset", Locale.GetText(
97                                         "Non-negative number required."));
98
99                         if (destOffset < 0)
100                                 throw new ArgumentOutOfRangeException ("destOffset", Locale.GetText (
101                                         "Non-negative number required."));
102
103                         if (count < 0)
104                                 throw new ArgumentOutOfRangeException ("count", Locale.GetText (
105                                         "Non-negative number required."));
106
107                         // We do the checks in unmanaged code for performance reasons
108                         bool res = BlockCopyInternal (src, srcOffset, dest, destOffset, count);
109                         if (!res) {
110                                 // watch for integer overflow
111                                 if ((srcOffset > ByteLength (src) - count) || (destOffset > ByteLength (dest) - count))
112                                         throw new ArgumentException (Locale.GetText (
113                                                 "Offset and length were out of bounds for the array or count is greater than" + 
114                                                 "the number of elements from index to the end of the source collection."));
115                         }
116                 }
117
118                 // private
119                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
120                 private extern static int ByteLengthInternal (Array array);
121
122                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
123                 private extern static byte GetByteInternal (Array array, int index);
124
125                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
126                 private extern static void SetByteInternal (Array array, int index, int value);
127
128                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
129                 internal extern static bool BlockCopyInternal (Array src, int src_offset, Array dest, int dest_offset, int count);
130         }
131 }