ConstrainedStream.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. namespace MatrixIO.IO
  6. {
  7. public class ConstrainedStream : Stream
  8. {
  9. private Stream _BaseStream;
  10. private Stack<ByteRange> _ConstraintStack = new Stack<ByteRange>();
  11. private ByteRange _CurrentConstraint = ByteRange.MaxValue;
  12. public ByteRange CurrentConstraint
  13. {
  14. get
  15. {
  16. return _CurrentConstraint;
  17. }
  18. }
  19. public ConstrainedStream(Stream baseStream)
  20. {
  21. _BaseStream = baseStream;
  22. }
  23. public static ConstrainedStream WrapStream(Stream baseStream)
  24. {
  25. if (baseStream is ConstrainedStream) return (ConstrainedStream)baseStream;
  26. else return new ConstrainedStream(baseStream);
  27. }
  28. public static Stream UnwrapStream(Stream stream)
  29. {
  30. if (stream is ConstrainedStream) return ((ConstrainedStream)stream)._BaseStream;
  31. else return stream;
  32. }
  33. public void PushConstraint(long length)
  34. {
  35. PushConstraint(Position, length);
  36. }
  37. public void PushConstraint(long offset, long length)
  38. {
  39. if (offset < _CurrentConstraint.Start)
  40. {
  41. Trace.WriteLine(String.Format("Attempted to set a new constraint ({0},{1}) offset outside the bounds of the existing constraint({2},{3}).", offset, length, _CurrentConstraint.Start, _CurrentConstraint.Length), "WARNING");
  42. offset = _CurrentConstraint.Start;
  43. }
  44. if (offset + length > _CurrentConstraint.End)
  45. {
  46. Trace.WriteLine(String.Format("Attempted to set a new constraint ({0},{1}) length outside the bounds of the existing constraint({2},{3}).", offset, length, _CurrentConstraint.Start, _CurrentConstraint.Length), "WARNING");
  47. length = _CurrentConstraint.End - offset;
  48. }
  49. if (_CurrentConstraint != null) _ConstraintStack.Push(_CurrentConstraint);
  50. _CurrentConstraint = new ByteRange(offset, offset + length);
  51. }
  52. public void PopConstraint()
  53. {
  54. if (_ConstraintStack.Count > 0) _CurrentConstraint = _ConstraintStack.Pop();
  55. else
  56. {
  57. Trace.WriteLine("Attempted to pop constraint too many times.", "WARNING");
  58. _CurrentConstraint = ByteRange.MaxValue;
  59. }
  60. }
  61. public override bool CanRead
  62. {
  63. get { return _BaseStream.CanRead; }
  64. }
  65. public override bool CanSeek
  66. {
  67. get { return _BaseStream.CanSeek; }
  68. }
  69. public override bool CanWrite
  70. {
  71. get { return _BaseStream.CanWrite; }
  72. }
  73. public override void Flush()
  74. {
  75. _BaseStream.Flush();
  76. }
  77. public override long Length
  78. {
  79. get
  80. {
  81. return Math.Min(_BaseStream.Length, _CurrentConstraint.End);
  82. }
  83. }
  84. public long _Count;
  85. public override long Position
  86. {
  87. get
  88. {
  89. if (CanSeek) return _BaseStream.Position;
  90. else return _Count;
  91. }
  92. set
  93. {
  94. _BaseStream.Position = value;
  95. }
  96. }
  97. public override int Read(byte[] buffer, int offset, int count)
  98. {
  99. long position = Position;
  100. if (position + count > _CurrentConstraint.End)
  101. {
  102. int availableCount = checked((int)(_CurrentConstraint.End - position));
  103. Trace.WriteLine(String.Format("Attempt to read {0} bytes past end of constrained region.", count - availableCount), "WARNING");
  104. if (availableCount > 0)
  105. {
  106. Trace.WriteLine("Returning partial result.", "WARNING");
  107. int actualCount = _BaseStream.Read(buffer, offset, availableCount);
  108. if (!CanSeek) _Count += actualCount;
  109. return actualCount;
  110. }
  111. else return 0;
  112. }
  113. else
  114. {
  115. int actualCount = _BaseStream.Read(buffer, offset, count);
  116. if (!CanSeek) _Count += actualCount;
  117. return actualCount;
  118. }
  119. }
  120. public override long Seek(long offset, SeekOrigin origin)
  121. {
  122. if ((origin == SeekOrigin.Begin && !_CurrentConstraint.Contains(offset)) ||
  123. (origin == SeekOrigin.Current && !_CurrentConstraint.Contains(Position + offset)) ||
  124. (origin == SeekOrigin.End && !_CurrentConstraint.Contains(Length - 1 - Math.Abs(offset))))
  125. throw new EndOfStreamException("Attempt to seek beyond constrained region.");
  126. return _BaseStream.Seek(offset, origin);
  127. }
  128. public override void SetLength(long value)
  129. {
  130. _BaseStream.SetLength(value);
  131. }
  132. public override void Write(byte[] buffer, int offset, int count)
  133. {
  134. if (Position + count > _CurrentConstraint.End) throw new IOException("Attempt to write past end of constrained region.");
  135. if (!CanSeek) _Count += count;
  136. _BaseStream.Write(buffer, offset, count);
  137. }
  138. }
  139. }