5 // Paolo Molaro (lupus@ximian.com)
6 // Dan Lewis (dihlewis@yahoo.co.uk)
8 // (C) 2001 Ximian, Inc. http://www.ximian.com
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
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.
34 using System.Runtime.CompilerServices;
35 using System.Runtime.InteropServices;
36 using System.Diagnostics.Contracts;
40 public static class Buffer {
42 public static int ByteLength (Array array)
44 // note: the other methods in this class also use ByteLength to test for
45 // null and non-primitive arguments as a side-effect.
48 throw new ArgumentNullException ("array");
50 int length = ByteLengthInternal (array);
52 throw new ArgumentException (Locale.GetText ("Object must be an array of primitives."));
57 public static byte GetByte (Array array, int index)
59 if (index < 0 || index >= ByteLength (array))
60 throw new ArgumentOutOfRangeException ("index", Locale.GetText(
61 "Value must be non-negative and less than the size of the collection."));
63 return GetByteInternal (array, index);
66 public static void SetByte (Array array, int index, byte value)
68 if (index < 0 || index >= ByteLength (array))
69 throw new ArgumentOutOfRangeException ("index", Locale.GetText(
70 "Value must be non-negative and less than the size of the collection."));
72 SetByteInternal (array, index, value);
75 public static void BlockCopy (Array src, int srcOffset, Array dst, int dstOffset, int count)
78 throw new ArgumentNullException ("src");
81 throw new ArgumentNullException ("dst");
84 throw new ArgumentOutOfRangeException ("srcOffset", Locale.GetText(
85 "Non-negative number required."));
88 throw new ArgumentOutOfRangeException ("dstOffset", Locale.GetText (
89 "Non-negative number required."));
92 throw new ArgumentOutOfRangeException ("count", Locale.GetText (
93 "Non-negative number required."));
95 // We do the checks in unmanaged code for performance reasons
96 bool res = BlockCopyInternal (src, srcOffset, dst, dstOffset, count);
98 // watch for integer overflow
99 if ((srcOffset > ByteLength (src) - count) || (dstOffset > ByteLength (dst) - count))
100 throw new ArgumentException (Locale.GetText (
101 "Offset and length were out of bounds for the array or count is greater than " +
102 "the number of elements from index to the end of the source collection."));
107 [MethodImplAttribute (MethodImplOptions.InternalCall)]
108 private extern static int ByteLengthInternal (Array array);
110 [MethodImplAttribute (MethodImplOptions.InternalCall)]
111 private extern static byte GetByteInternal (Array array, int index);
113 [MethodImplAttribute (MethodImplOptions.InternalCall)]
114 private extern static void SetByteInternal (Array array, int index, int value);
116 [MethodImplAttribute (MethodImplOptions.InternalCall)]
117 internal extern static bool BlockCopyInternal (Array src, int src_offset, Array dest, int dest_offset, int count);
119 internal static bool InternalBlockCopy (Array src, int src_offset, Array dest, int dest_offset, int count)
121 return BlockCopyInternal (src, src_offset, dest, dest_offset, count);
124 internal unsafe static void ZeroMemory (byte* src, long len)
130 internal unsafe static void Memcpy (byte* pDest, int destIndex, byte[] src, int srcIndex, int len)
132 Contract.Assert( (srcIndex >= 0) && (destIndex >= 0) && (len >= 0), "Index and length must be non-negative!");
133 Contract.Assert(src.Length - srcIndex >= len, "not enough bytes in src");
134 // If dest has 0 elements, the fixed statement will throw an
135 // IndexOutOfRangeException. Special-case 0-byte copies.
138 fixed(byte* pSrc = src) {
139 Memcpy(pDest + destIndex, pSrc + srcIndex, len);
143 internal unsafe static void Memcpy(byte[] dest, int destIndex, byte* src, int srcIndex, int len) {
144 Contract.Assert( (srcIndex >= 0) && (destIndex >= 0) && (len >= 0), "Index and length must be non-negative!");
145 Contract.Assert(dest.Length - destIndex >= len, "not enough bytes in dest");
146 // If dest has 0 elements, the fixed statement will throw an
147 // IndexOutOfRangeException. Special-case 0-byte copies.
150 fixed(byte* pDest = dest) {
151 Memcpy(pDest + destIndex, src + srcIndex, len);
155 internal unsafe static void Memcpy (byte* dest, byte* src, int len)
157 string.memcpy (dest, src, len);