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