DiskExtent.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* Copyright (C) 2014-2018 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. public class DiskExtent
  10. {
  11. private Disk m_disk;
  12. private long m_firstSector;
  13. private long m_size; // In bytes
  14. public DiskExtent(Disk disk, long firstSector, long size)
  15. {
  16. m_disk = disk;
  17. m_firstSector = firstSector;
  18. m_size = size;
  19. }
  20. public byte[] ReadSector(long sectorIndex)
  21. {
  22. return ReadSectors(sectorIndex, 1);
  23. }
  24. public byte[] ReadSectors(long sectorIndex, int sectorCount)
  25. {
  26. CheckBoundaries(sectorIndex, sectorCount);
  27. return m_disk.ReadSectors(m_firstSector + sectorIndex, sectorCount);
  28. }
  29. public void WriteSectors(long sectorIndex, byte[] data)
  30. {
  31. CheckBoundaries(sectorIndex, data.Length / this.BytesPerSector);
  32. m_disk.WriteSectors(m_firstSector + sectorIndex, data);
  33. }
  34. public void CheckBoundaries(long sectorIndex, int sectorCount)
  35. {
  36. if (sectorIndex < 0 || sectorIndex + (sectorCount - 1) >= this.TotalSectors)
  37. {
  38. throw new ArgumentOutOfRangeException("Attempted to access data outside of volume");
  39. }
  40. }
  41. public int BytesPerSector
  42. {
  43. get
  44. {
  45. return m_disk.BytesPerSector;
  46. }
  47. }
  48. public long Size
  49. {
  50. get
  51. {
  52. return m_size;
  53. }
  54. }
  55. public bool IsReadOnly
  56. {
  57. get
  58. {
  59. return m_disk.IsReadOnly;
  60. }
  61. }
  62. public long FirstSector
  63. {
  64. get
  65. {
  66. return m_firstSector;
  67. }
  68. }
  69. public long TotalSectors
  70. {
  71. get
  72. {
  73. return this.Size / this.BytesPerSector;
  74. }
  75. }
  76. public long LastSector
  77. {
  78. get
  79. {
  80. return FirstSector + TotalSectors - 1;
  81. }
  82. }
  83. public Disk Disk
  84. {
  85. get
  86. {
  87. return m_disk;
  88. }
  89. }
  90. }