NTFSFileStream.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.IO;
  10. using System.Text;
  11. using Utilities;
  12. namespace DiskAccessLibrary.FileSystems.NTFS
  13. {
  14. /// <summary>
  15. /// A Stream wrapper for NTFSFile
  16. /// </summary>
  17. public class NTFSFileStream : Stream
  18. {
  19. private NTFSFile m_file;
  20. private long m_position;
  21. public NTFSFileStream(NTFSFile file)
  22. {
  23. m_file = file;
  24. }
  25. public override long Seek(long offset, SeekOrigin origin)
  26. {
  27. if (origin == SeekOrigin.Begin)
  28. {
  29. m_position = offset;
  30. }
  31. else if (origin == SeekOrigin.Current)
  32. {
  33. m_position += offset;
  34. }
  35. else if (origin == SeekOrigin.End)
  36. {
  37. m_position = (long)m_file.Length + offset;
  38. }
  39. return m_position;
  40. }
  41. public override void SetLength(long value)
  42. {
  43. if (value > (long)m_file.Length)
  44. {
  45. ulong additionalLength = (ulong)value - m_file.Length;
  46. m_file.ExtendFile(additionalLength);
  47. }
  48. else if (value < (long)m_file.Length)
  49. {
  50. throw new NotImplementedException();
  51. }
  52. }
  53. public override int Read(byte[] buffer, int offset, int count)
  54. {
  55. byte[] data = m_file.ReadFromFile((ulong)m_position, count);
  56. Array.Copy(data, 0, buffer, offset, data.Length);
  57. m_position += data.Length;
  58. return data.Length;
  59. }
  60. public override void Write(byte[] buffer, int offset, int count)
  61. {
  62. byte[] data = new byte[count];
  63. Array.Copy(buffer, offset, data, 0, count);
  64. m_file.WriteToFile((ulong)m_position, data);
  65. m_position += count;
  66. }
  67. public override void Flush()
  68. {
  69. // Everything was written directly to disk, no need to flush
  70. }
  71. public override long Length
  72. {
  73. get
  74. {
  75. return (long)m_file.Length;
  76. }
  77. }
  78. public override long Position
  79. {
  80. get
  81. {
  82. return m_position;
  83. }
  84. set
  85. {
  86. m_position = value;
  87. }
  88. }
  89. public override bool CanRead
  90. {
  91. get
  92. {
  93. return true;
  94. }
  95. }
  96. public override bool CanSeek
  97. {
  98. get
  99. {
  100. return true;
  101. }
  102. }
  103. public override bool CanWrite
  104. {
  105. get
  106. {
  107. return true;
  108. }
  109. }
  110. }
  111. }