Misc.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // <copyright file="Misc.cs" company="Google Inc.">
  2. // Copyright (C) 2014 Google Inc.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. // </copyright>
  16. namespace GooglePlayGames.OurUtils
  17. {
  18. using System;
  19. public static class Misc
  20. {
  21. public static bool BuffersAreIdentical(byte[] a, byte[] b)
  22. {
  23. if (a == b)
  24. {
  25. // not only identical but the very same!
  26. return true;
  27. }
  28. if (a == null || b == null)
  29. {
  30. // one of them is null, the other one isn't
  31. return false;
  32. }
  33. if (a.Length != b.Length)
  34. {
  35. return false;
  36. }
  37. for (int i = 0; i < a.Length; i++)
  38. {
  39. if (a[i] != b[i])
  40. {
  41. return false;
  42. }
  43. }
  44. return true;
  45. }
  46. public static byte[] GetSubsetBytes(byte[] array, int offset, int length)
  47. {
  48. if (array == null)
  49. {
  50. throw new ArgumentNullException("array");
  51. }
  52. if (offset < 0 || offset >= array.Length)
  53. {
  54. throw new ArgumentOutOfRangeException("offset");
  55. }
  56. if (length < 0 || (array.Length - offset) < length)
  57. {
  58. throw new ArgumentOutOfRangeException("length");
  59. }
  60. if (offset == 0 && length == array.Length)
  61. {
  62. return array;
  63. }
  64. byte[] piece = new byte[length];
  65. Array.Copy(array, offset, piece, 0, length);
  66. return piece;
  67. }
  68. public static T CheckNotNull<T>(T value)
  69. {
  70. if (value == null)
  71. {
  72. throw new ArgumentNullException();
  73. }
  74. return value;
  75. }
  76. public static T CheckNotNull<T>(T value, string paramName)
  77. {
  78. if (value == null)
  79. {
  80. throw new ArgumentNullException(paramName);
  81. }
  82. return value;
  83. }
  84. }
  85. }