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