Home | History | Annotate | Download | only in Common
      1 // StreamUtils.cpp
      2 
      3 #include "StdAfx.h"
      4 
      5 #include "StreamUtils.h"
      6 
      7 static const UInt32 kBlockSize = ((UInt32)1 << 31);
      8 
      9 HRESULT ReadStream(ISequentialInStream *stream, void *data, size_t *processedSize)
     10 {
     11   size_t size = *processedSize;
     12   *processedSize = 0;
     13   while (size != 0)
     14   {
     15     UInt32 curSize = (size < kBlockSize) ? (UInt32)size : kBlockSize;
     16     UInt32 processedSizeLoc;
     17     HRESULT res = stream->Read(data, curSize, &processedSizeLoc);
     18     *processedSize += processedSizeLoc;
     19     data = (void *)((Byte *)data + processedSizeLoc);
     20     size -= processedSizeLoc;
     21     RINOK(res);
     22     if (processedSizeLoc == 0)
     23       return S_OK;
     24   }
     25   return S_OK;
     26 }
     27 
     28 HRESULT ReadStream_FALSE(ISequentialInStream *stream, void *data, size_t size)
     29 {
     30   size_t processedSize = size;
     31   RINOK(ReadStream(stream, data, &processedSize));
     32   return (size == processedSize) ? S_OK : S_FALSE;
     33 }
     34 
     35 HRESULT ReadStream_FAIL(ISequentialInStream *stream, void *data, size_t size)
     36 {
     37   size_t processedSize = size;
     38   RINOK(ReadStream(stream, data, &processedSize));
     39   return (size == processedSize) ? S_OK : E_FAIL;
     40 }
     41 
     42 HRESULT WriteStream(ISequentialOutStream *stream, const void *data, size_t size)
     43 {
     44   while (size != 0)
     45   {
     46     UInt32 curSize = (size < kBlockSize) ? (UInt32)size : kBlockSize;
     47     UInt32 processedSizeLoc;
     48     HRESULT res = stream->Write(data, curSize, &processedSizeLoc);
     49     data = (const void *)((const Byte *)data + processedSizeLoc);
     50     size -= processedSizeLoc;
     51     RINOK(res);
     52     if (processedSizeLoc == 0)
     53       return E_FAIL;
     54   }
     55   return S_OK;
     56 }
     57