ADCStream.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // ADC.cs
  3. //
  4. // Author:
  5. // Natalia Portillo <claunia@claunia.com>
  6. //
  7. // Copyright (c) 2016 © Claunia.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. using System;
  27. using System.IO;
  28. namespace SharpCompress.Compressors.ADC
  29. {
  30. /// <summary>
  31. /// Provides a forward readable only stream that decompresses ADC data
  32. /// </summary>
  33. public class ADCStream : Stream
  34. {
  35. /// <summary>
  36. /// This stream holds the compressed data
  37. /// </summary>
  38. private readonly Stream _stream;
  39. /// <summary>
  40. /// Is this instance disposed?
  41. /// </summary>
  42. private bool _isDisposed;
  43. /// <summary>
  44. /// Position in decompressed data
  45. /// </summary>
  46. private long _position;
  47. /// <summary>
  48. /// Buffer with currently used chunk of decompressed data
  49. /// </summary>
  50. private byte[] _outBuffer;
  51. /// <summary>
  52. /// Position in buffer of decompressed data
  53. /// </summary>
  54. private int _outPosition;
  55. /// <summary>
  56. /// Initializates a stream that decompresses ADC data on the fly
  57. /// </summary>
  58. /// <param name="stream">Stream that contains the compressed data</param>
  59. /// <param name="compressionMode">Must be set to <see cref="CompressionMode.Decompress"/> because compression is not implemented</param>
  60. public ADCStream(Stream stream, CompressionMode compressionMode = CompressionMode.Decompress)
  61. {
  62. if (compressionMode == CompressionMode.Compress)
  63. {
  64. throw new NotSupportedException();
  65. }
  66. _stream = stream;
  67. }
  68. public override bool CanRead => _stream.CanRead;
  69. public override bool CanSeek => false;
  70. public override bool CanWrite => false;
  71. public override long Length => throw new NotSupportedException();
  72. public override long Position { get => _position; set => throw new NotSupportedException(); }
  73. public override void Flush()
  74. {
  75. }
  76. protected override void Dispose(bool disposing)
  77. {
  78. if (_isDisposed)
  79. {
  80. return;
  81. }
  82. _isDisposed = true;
  83. base.Dispose(disposing);
  84. }
  85. public override int Read(byte[] buffer, int offset, int count)
  86. {
  87. if (count == 0)
  88. {
  89. return 0;
  90. }
  91. if (buffer == null)
  92. {
  93. throw new ArgumentNullException(nameof(buffer));
  94. }
  95. if (count < 0)
  96. {
  97. throw new ArgumentOutOfRangeException(nameof(count));
  98. }
  99. if (offset < buffer.GetLowerBound(0))
  100. {
  101. throw new ArgumentOutOfRangeException(nameof(offset));
  102. }
  103. if ((offset + count) > buffer.GetLength(0))
  104. {
  105. throw new ArgumentOutOfRangeException(nameof(count));
  106. }
  107. int size = -1;
  108. if (_outBuffer == null)
  109. {
  110. size = ADCBase.Decompress(_stream, out _outBuffer);
  111. _outPosition = 0;
  112. }
  113. int inPosition = offset;
  114. int toCopy = count;
  115. int copied = 0;
  116. while (_outPosition + toCopy >= _outBuffer.Length)
  117. {
  118. int piece = _outBuffer.Length - _outPosition;
  119. Array.Copy(_outBuffer, _outPosition, buffer, inPosition, piece);
  120. inPosition += piece;
  121. copied += piece;
  122. _position += piece;
  123. toCopy -= piece;
  124. size = ADCBase.Decompress(_stream, out _outBuffer);
  125. _outPosition = 0;
  126. if (size == 0 || _outBuffer == null || _outBuffer.Length == 0)
  127. {
  128. return copied;
  129. }
  130. }
  131. Array.Copy(_outBuffer, _outPosition, buffer, inPosition, toCopy);
  132. _outPosition += toCopy;
  133. _position += toCopy;
  134. copied += toCopy;
  135. return copied;
  136. }
  137. public override long Seek(long offset, SeekOrigin origin)
  138. {
  139. throw new NotSupportedException();
  140. }
  141. public override void SetLength(long value)
  142. {
  143. throw new NotSupportedException();
  144. }
  145. public override void Write(byte[] buffer, int offset, int count)
  146. {
  147. throw new NotSupportedException();
  148. }
  149. }
  150. }