CopyCoder.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Compress/CopyCoder.cpp
  2. #include "StdAfx.h"
  3. extern "C"
  4. {
  5. #include "../../../../C/Alloc.h"
  6. }
  7. #include "CopyCoder.h"
  8. #include "../../Common/StreamUtils.h"
  9. namespace NCompress {
  10. static const UInt32 kBufferSize = 1 << 17;
  11. CCopyCoder::~CCopyCoder()
  12. {
  13. ::MidFree(_buffer);
  14. }
  15. STDMETHODIMP CCopyCoder::Code(ISequentialInStream *inStream,
  16. ISequentialOutStream *outStream,
  17. const UInt64 * /* inSize */, const UInt64 *outSize,
  18. ICompressProgressInfo *progress)
  19. {
  20. if (_buffer == 0)
  21. {
  22. _buffer = (Byte *)::MidAlloc(kBufferSize);
  23. if (_buffer == 0)
  24. return E_OUTOFMEMORY;
  25. }
  26. TotalSize = 0;
  27. for (;;)
  28. {
  29. UInt32 realProcessedSize;
  30. UInt32 size = kBufferSize;
  31. if (outSize != 0)
  32. if (size > *outSize - TotalSize)
  33. size = (UInt32)(*outSize - TotalSize);
  34. RINOK(inStream->Read(_buffer, size, &realProcessedSize));
  35. if (realProcessedSize == 0)
  36. break;
  37. RINOK(WriteStream(outStream, _buffer, realProcessedSize, NULL));
  38. TotalSize += realProcessedSize;
  39. if (progress != NULL)
  40. {
  41. RINOK(progress->SetRatioInfo(&TotalSize, &TotalSize));
  42. }
  43. }
  44. return S_OK;
  45. }
  46. STDMETHODIMP CCopyCoder::GetInStreamProcessedSize(UInt64 *value)
  47. {
  48. *value = TotalSize;
  49. return S_OK;
  50. }
  51. }