CStreamSwitch.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.Collections.Generic;
  3. using SharpCompress.Compressors.LZMA;
  4. namespace SharpCompress.Common.SevenZip
  5. {
  6. internal struct CStreamSwitch : IDisposable
  7. {
  8. private ArchiveReader _archive;
  9. private bool _needRemove;
  10. private bool _active;
  11. public void Dispose()
  12. {
  13. if (_active)
  14. {
  15. _active = false;
  16. #if DEBUG
  17. Log.WriteLine("[end of switch]");
  18. #endif
  19. }
  20. if (_needRemove)
  21. {
  22. _needRemove = false;
  23. _archive.DeleteByteStream();
  24. }
  25. }
  26. public void Set(ArchiveReader archive, byte[] dataVector)
  27. {
  28. Dispose();
  29. _archive = archive;
  30. _archive.AddByteStream(dataVector, 0, dataVector.Length);
  31. _needRemove = true;
  32. _active = true;
  33. }
  34. public void Set(ArchiveReader archive, List<byte[]> dataVector)
  35. {
  36. Dispose();
  37. _active = true;
  38. byte external = archive.ReadByte();
  39. if (external != 0)
  40. {
  41. int dataIndex = archive.ReadNum();
  42. if (dataIndex < 0 || dataIndex >= dataVector.Count)
  43. {
  44. throw new InvalidOperationException();
  45. }
  46. #if DEBUG
  47. Log.WriteLine("[switch to stream {0}]", dataIndex);
  48. #endif
  49. _archive = archive;
  50. _archive.AddByteStream(dataVector[dataIndex], 0, dataVector[dataIndex].Length);
  51. _needRemove = true;
  52. _active = true;
  53. }
  54. else
  55. {
  56. #if DEBUG
  57. Log.WriteLine("[inline data]");
  58. #endif
  59. }
  60. }
  61. }
  62. }