InOutTempBuffer.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // InOutTempBuffer.cpp
  2. #include "StdAfx.h"
  3. #include "InOutTempBuffer.h"
  4. #include "../../Common/Defs.h"
  5. // #include "Windows/Defs.h"
  6. #include "StreamUtils.h"
  7. using namespace NWindows;
  8. using namespace NFile;
  9. using namespace NDirectory;
  10. static UInt32 kTmpBufferMemorySize = (1 << 20);
  11. static LPCTSTR kTempFilePrefixString = TEXT("iot");
  12. CInOutTempBuffer::CInOutTempBuffer():
  13. _buffer(NULL)
  14. {
  15. }
  16. void CInOutTempBuffer::Create()
  17. {
  18. _buffer = new Byte[kTmpBufferMemorySize];
  19. }
  20. CInOutTempBuffer::~CInOutTempBuffer()
  21. {
  22. delete []_buffer;
  23. }
  24. void CInOutTempBuffer::InitWriting()
  25. {
  26. _bufferPosition = 0;
  27. _tmpFileCreated = false;
  28. _fileSize = 0;
  29. }
  30. bool CInOutTempBuffer::WriteToFile(const void *data, UInt32 size)
  31. {
  32. if (size == 0)
  33. return true;
  34. if(!_tmpFileCreated)
  35. {
  36. CSysString tempDirPath;
  37. if(!MyGetTempPath(tempDirPath))
  38. return false;
  39. if (_tempFile.Create(tempDirPath, kTempFilePrefixString, _tmpFileName) == 0)
  40. return false;
  41. // _outFile.SetOpenCreationDispositionCreateAlways();
  42. if(!_outFile.Create(_tmpFileName, true))
  43. return false;
  44. _tmpFileCreated = true;
  45. }
  46. UInt32 processedSize;
  47. if(!_outFile.Write(data, size, processedSize))
  48. return false;
  49. _fileSize += processedSize;
  50. return (processedSize == size);
  51. }
  52. bool CInOutTempBuffer::FlushWrite()
  53. {
  54. return _outFile.Close();
  55. }
  56. bool CInOutTempBuffer::Write(const void *data, UInt32 size)
  57. {
  58. if(_bufferPosition < kTmpBufferMemorySize)
  59. {
  60. UInt32 curSize = MyMin(kTmpBufferMemorySize - _bufferPosition, size);
  61. memmove(_buffer + _bufferPosition, (const Byte *)data, curSize);
  62. _bufferPosition += curSize;
  63. size -= curSize;
  64. data = ((const Byte *)data) + curSize;
  65. _fileSize += curSize;
  66. }
  67. return WriteToFile(data, size);
  68. }
  69. bool CInOutTempBuffer::InitReading()
  70. {
  71. _currentPositionInBuffer = 0;
  72. if(_tmpFileCreated)
  73. return _inFile.Open(_tmpFileName);
  74. return true;
  75. }
  76. HRESULT CInOutTempBuffer::WriteToStream(ISequentialOutStream *stream)
  77. {
  78. if (_currentPositionInBuffer < _bufferPosition)
  79. {
  80. UInt32 sizeToWrite = _bufferPosition - _currentPositionInBuffer;
  81. RINOK(WriteStream(stream, _buffer + _currentPositionInBuffer, sizeToWrite, NULL));
  82. _currentPositionInBuffer += sizeToWrite;
  83. }
  84. if (!_tmpFileCreated)
  85. return true;
  86. for (;;)
  87. {
  88. UInt32 localProcessedSize;
  89. if (!_inFile.ReadPart(_buffer, kTmpBufferMemorySize, localProcessedSize))
  90. return E_FAIL;
  91. if (localProcessedSize == 0)
  92. return S_OK;
  93. RINOK(WriteStream(stream, _buffer, localProcessedSize, NULL));
  94. }
  95. }
  96. STDMETHODIMP CSequentialOutTempBufferImp::Write(const void *data, UInt32 size, UInt32 *processedSize)
  97. {
  98. if (!_buffer->Write(data, size))
  99. {
  100. if (processedSize != NULL)
  101. *processedSize = 0;
  102. return E_FAIL;
  103. }
  104. if (processedSize != NULL)
  105. *processedSize = size;
  106. return S_OK;
  107. }