Utils.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. namespace System.Net.ApiService.Compression.Brotli {
  2. /// <summary>A set of utility methods.</summary>
  3. internal sealed class Utils {
  4. private static readonly byte[] ByteZeroes = new byte[1024];
  5. private static readonly int[] IntZeroes = new int[1024];
  6. /// <summary>Fills byte array with zeroes.</summary>
  7. /// <remarks>
  8. /// Fills byte array with zeroes.
  9. /// <p> Current implementation uses
  10. /// <see cref="System.Array.Copy(object, int, object, int, int)"/>
  11. /// , so it should be used for length not
  12. /// less than 16.
  13. /// </remarks>
  14. /// <param name="dest">array to fill with zeroes</param>
  15. /// <param name="offset">the first byte to fill</param>
  16. /// <param name="length">number of bytes to change</param>
  17. internal static void FillWithZeroes(byte[] dest, int offset, int length) {
  18. int cursor = 0;
  19. while (cursor < length) {
  20. int step = System.Math.Min(cursor + 1024, length) - cursor;
  21. System.Array.Copy(ByteZeroes, 0, dest, offset + cursor, step);
  22. cursor += step;
  23. }
  24. }
  25. /// <summary>Fills int array with zeroes.</summary>
  26. /// <remarks>
  27. /// Fills int array with zeroes.
  28. /// <p> Current implementation uses
  29. /// <see cref="System.Array.Copy(object, int, object, int, int)"/>
  30. /// , so it should be used for length not
  31. /// less than 16.
  32. /// </remarks>
  33. /// <param name="dest">array to fill with zeroes</param>
  34. /// <param name="offset">the first item to fill</param>
  35. /// <param name="length">number of item to change</param>
  36. internal static void FillWithZeroes(int[] dest, int offset, int length) {
  37. int cursor = 0;
  38. while (cursor < length) {
  39. int step = System.Math.Min(cursor + 1024, length) - cursor;
  40. System.Array.Copy(IntZeroes, 0, dest, offset + cursor, step);
  41. cursor += step;
  42. }
  43. }
  44. }
  45. }