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