DiskExtent.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* Copyright (C) 2014 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. using System.Text;
  10. public class DiskExtent
  11. {
  12. private Disk m_disk;
  13. private long m_firstSector;
  14. private long m_size; // In bytes
  15. public DiskExtent(Disk disk, long firstSector, long size)
  16. {
  17. m_disk = disk;
  18. m_firstSector = firstSector;
  19. m_size = size;
  20. }
  21. public byte[] ReadSector(long sectorIndex)
  22. {
  23. return ReadSectors(sectorIndex, 1);
  24. }
  25. public byte[] ReadSectors(long sectorIndex, int sectorCount)
  26. {
  27. CheckBoundaries(sectorIndex, sectorCount);
  28. return m_disk.ReadSectors(m_firstSector + sectorIndex, sectorCount);
  29. }
  30. public void WriteSectors(long sectorIndex, byte[] data)
  31. {
  32. CheckBoundaries(sectorIndex, data.Length / this.BytesPerSector);
  33. m_disk.WriteSectors(m_firstSector + sectorIndex, data);
  34. }
  35. public void CheckBoundaries(long sectorIndex, int sectorCount)
  36. {
  37. if (sectorIndex < 0 || sectorIndex + (sectorCount - 1) >= this.TotalSectors)
  38. {
  39. throw new ArgumentOutOfRangeException("Attempted to access data outside of volume");
  40. }
  41. }
  42. public int BytesPerSector
  43. {
  44. get
  45. {
  46. return m_disk.BytesPerSector;
  47. }
  48. }
  49. public long Size
  50. {
  51. get
  52. {
  53. return m_size;
  54. }
  55. }
  56. public long FirstSector
  57. {
  58. get
  59. {
  60. return m_firstSector;
  61. }
  62. }
  63. public long TotalSectors
  64. {
  65. get
  66. {
  67. return this.Size / this.BytesPerSector;
  68. }
  69. }
  70. public long LastSector
  71. {
  72. get
  73. {
  74. return FirstSector + TotalSectors - 1;
  75. }
  76. }
  77. public Disk Disk
  78. {
  79. get
  80. {
  81. return m_disk;
  82. }
  83. }
  84. }