5 #include "InOutTempBuffer.h"
6 #include "../../Common/Defs.h"
7 // #include "Windows/Defs.h"
9 #include "StreamUtils.h"
11 using namespace NWindows;
12 using namespace NFile;
13 using namespace NDirectory;
15 static UInt32 kTmpBufferMemorySize = (1 << 20);
17 static LPCTSTR kTempFilePrefixString = TEXT("iot");
19 CInOutTempBuffer::CInOutTempBuffer():
24 void CInOutTempBuffer::Create()
26 _buffer = new Byte[kTmpBufferMemorySize];
29 CInOutTempBuffer::~CInOutTempBuffer()
33 void CInOutTempBuffer::InitWriting()
36 _tmpFileCreated = false;
40 bool CInOutTempBuffer::WriteToFile(const void *data, UInt32 size)
46 CSysString tempDirPath;
47 if(!MyGetTempPath(tempDirPath))
49 if (_tempFile.Create(tempDirPath, kTempFilePrefixString, _tmpFileName) == 0)
51 // _outFile.SetOpenCreationDispositionCreateAlways();
52 if(!_outFile.Create(_tmpFileName, true))
54 _tmpFileCreated = true;
57 if(!_outFile.Write(data, size, processedSize))
59 _fileSize += processedSize;
60 return (processedSize == size);
63 bool CInOutTempBuffer::FlushWrite()
65 return _outFile.Close();
68 bool CInOutTempBuffer::Write(const void *data, UInt32 size)
70 if(_bufferPosition < kTmpBufferMemorySize)
72 UInt32 curSize = MyMin(kTmpBufferMemorySize - _bufferPosition, size);
73 memmove(_buffer + _bufferPosition, (const Byte *)data, curSize);
74 _bufferPosition += curSize;
76 data = ((const Byte *)data) + curSize;
79 return WriteToFile(data, size);
82 bool CInOutTempBuffer::InitReading()
84 _currentPositionInBuffer = 0;
86 return _inFile.Open(_tmpFileName);
90 HRESULT CInOutTempBuffer::WriteToStream(ISequentialOutStream *stream)
92 if (_currentPositionInBuffer < _bufferPosition)
94 UInt32 sizeToWrite = _bufferPosition - _currentPositionInBuffer;
95 RINOK(WriteStream(stream, _buffer + _currentPositionInBuffer, sizeToWrite, NULL));
96 _currentPositionInBuffer += sizeToWrite;
102 UInt32 localProcessedSize;
103 if (!_inFile.ReadPart(_buffer, kTmpBufferMemorySize, localProcessedSize))
105 if (localProcessedSize == 0)
107 RINOK(WriteStream(stream, _buffer, localProcessedSize, NULL));
111 STDMETHODIMP CSequentialOutTempBufferImp::Write(const void *data, UInt32 size, UInt32 *processedSize)
113 if (!_buffer->Write(data, size))
115 if (processedSize != NULL)
119 if (processedSize != NULL)
120 *processedSize = size;